Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch/Bring to the foreground Content App on Target Navigator and Content Launch Command #34223

Merged
merged 11 commits into from
Jul 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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 {

Expand All @@ -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);

Expand All @@ -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);
Expand Down
Loading