From 89a6f5e7a3b18a5266f989e59a981a6071e43adf Mon Sep 17 00:00:00 2001 From: mthiesc Date: Thu, 11 Jul 2024 20:13:29 +0200 Subject: [PATCH] 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 Co-authored-by: Restyled.io --- .../platform-app/src/main/AndroidManifest.xml | 3 ++ .../ContentAppEndpointManagerImpl.java | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/examples/tv-app/android/App/platform-app/src/main/AndroidManifest.xml b/examples/tv-app/android/App/platform-app/src/main/AndroidManifest.xml index b6e00cd75d0365..476faa2030c7a2 100644 --- a/examples/tv-app/android/App/platform-app/src/main/AndroidManifest.xml +++ b/examples/tv-app/android/App/platform-app/src/main/AndroidManifest.xml @@ -26,6 +26,9 @@ tools:ignore="QueryAllPackagesPermission" /> + + + 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);