Skip to content

Commit

Permalink
Launch/Bring to the foreground Content App on Target Navigator and Co…
Browse files Browse the repository at this point in the history
…ntent Launch Command (#34223)

* Launch content app on command

[Problem]
3P content app does not have permissions to launch itself and put main activity in foreground. This is necessary in response to ContentLauncher and TargetNavigator cluster commands.

[Solution]
Intercept the LaunchContent and NavigateTarget commands in
ContentAppEndpointManagerImpl. Then start content app main/launch activity if it's not
already in the foreground before sending command intent.

[Testing]
WIP

* Add logic to detect foreground apps

* Restyled by whitespace

* Restyled by google-java-format

* Update code

* Restyled by google-java-format

---------

Co-authored-by: Lazar Kovacic <lkovacic@amazon.com>
Co-authored-by: Restyled.io <commits@restyled.io>
3 people authored Jul 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a0d6b70 commit 89a6f5e
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -26,6 +26,9 @@
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-permission android:name="android.permission.GET_TASKS" />


<application
android:allowBackup="true"
android:extractNativeLibs="true"
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.matter.tv.server.handlers;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.matter.tv.app.api.Clusters;
import com.matter.tv.server.model.ContentApp;
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
import com.matter.tv.server.service.ContentAppAgentService;
import com.matter.tv.server.tvapp.ContentAppEndpointManager;
import com.matter.tv.server.utils.EndpointsDataStore;
import java.util.Collection;
import java.util.List;

public class ContentAppEndpointManagerImpl implements ContentAppEndpointManager {

@@ -18,6 +22,31 @@ public ContentAppEndpointManagerImpl(Context context) {
this.context = context;
}

private boolean isForegroundCommand(long clusterId, long commandId) {
switch ((int) clusterId) {
case Clusters.ContentLauncher.Id:
return commandId == Clusters.ContentLauncher.Commands.LaunchContent.ID
|| commandId == Clusters.ContentLauncher.Commands.LaunchURL.ID;
case Clusters.TargetNavigator.Id:
return commandId == Clusters.TargetNavigator.Commands.NavigateTarget.ID;
default:
return false;
}
}

private boolean isAppInForeground(String contentAppPackageName) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
if (tasks != null && !tasks.isEmpty()) {
ActivityManager.RunningTaskInfo taskInfo = tasks.get(0);
String packageName =
taskInfo.topActivity != null ? taskInfo.topActivity.getPackageName() : "";
return packageName.equals(contentAppPackageName);
}
return false;
}

public String sendCommand(int endpointId, long clusterId, long commandId, String commandPayload) {
Log.d(TAG, "Received a command for endpointId " + endpointId + ". Message " + commandPayload);

@@ -26,6 +55,17 @@ public String sendCommand(int endpointId, long clusterId, long commandId, String
ContentAppDiscoveryService.getReceiverInstance().getDiscoveredContentApps().values(),
endpointId);
if (discoveredApp != null) {
// Intercept NavigateTarget and LaunchContent commands and launch content app if necessary
if (isForegroundCommand(clusterId, commandId)) {
// Check if contentapp main/launch activity is already in foreground before launching.
if (!isAppInForeground(discoveredApp.getAppName())) {
Intent launchIntent =
context.getPackageManager().getLaunchIntentForPackage(discoveredApp.getAppName());
if (launchIntent != null) {
context.startActivity(launchIntent);
}
}
}
Log.d(TAG, "Sending a command for endpointId " + endpointId + ". Message " + commandPayload);
return ContentAppAgentService.sendCommand(
context, discoveredApp.getAppName(), clusterId, commandId, commandPayload);

0 comments on commit 89a6f5e

Please sign in to comment.