Skip to content

Commit 89a6f5e

Browse files
mthiesclazarkovrestyled-commits
authored
Launch/Bring to the foreground Content App on Target Navigator and Content 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>
1 parent a0d6b70 commit 89a6f5e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

examples/tv-app/android/App/platform-app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
tools:ignore="QueryAllPackagesPermission" />
2727
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2828

29+
<uses-permission android:name="android.permission.GET_TASKS" />
30+
31+
2932
<application
3033
android:allowBackup="true"
3134
android:extractNativeLibs="true"

examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/handlers/ContentAppEndpointManagerImpl.java

+40
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.matter.tv.server.handlers;
22

3+
import android.app.ActivityManager;
34
import android.content.Context;
5+
import android.content.Intent;
46
import android.util.Log;
7+
import com.matter.tv.app.api.Clusters;
58
import com.matter.tv.server.model.ContentApp;
69
import com.matter.tv.server.receivers.ContentAppDiscoveryService;
710
import com.matter.tv.server.service.ContentAppAgentService;
811
import com.matter.tv.server.tvapp.ContentAppEndpointManager;
912
import com.matter.tv.server.utils.EndpointsDataStore;
1013
import java.util.Collection;
14+
import java.util.List;
1115

1216
public class ContentAppEndpointManagerImpl implements ContentAppEndpointManager {
1317

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

25+
private boolean isForegroundCommand(long clusterId, long commandId) {
26+
switch ((int) clusterId) {
27+
case Clusters.ContentLauncher.Id:
28+
return commandId == Clusters.ContentLauncher.Commands.LaunchContent.ID
29+
|| commandId == Clusters.ContentLauncher.Commands.LaunchURL.ID;
30+
case Clusters.TargetNavigator.Id:
31+
return commandId == Clusters.TargetNavigator.Commands.NavigateTarget.ID;
32+
default:
33+
return false;
34+
}
35+
}
36+
37+
private boolean isAppInForeground(String contentAppPackageName) {
38+
ActivityManager activityManager =
39+
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
40+
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
41+
if (tasks != null && !tasks.isEmpty()) {
42+
ActivityManager.RunningTaskInfo taskInfo = tasks.get(0);
43+
String packageName =
44+
taskInfo.topActivity != null ? taskInfo.topActivity.getPackageName() : "";
45+
return packageName.equals(contentAppPackageName);
46+
}
47+
return false;
48+
}
49+
2150
public String sendCommand(int endpointId, long clusterId, long commandId, String commandPayload) {
2251
Log.d(TAG, "Received a command for endpointId " + endpointId + ". Message " + commandPayload);
2352

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

0 commit comments

Comments
 (0)