|
| 1 | +package com.matter.tv.server.handlers; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import android.util.Log; |
| 6 | +import androidx.lifecycle.LiveData; |
| 7 | +import androidx.lifecycle.Observer; |
| 8 | +import com.matter.tv.server.tvapp.Application; |
| 9 | +import com.matter.tv.server.tvapp.ApplicationLauncherManager; |
| 10 | +import com.matter.tv.server.tvapp.LauncherResponse; |
| 11 | +import com.matter.tv.server.utils.EndpointsDataStore; |
| 12 | +import com.matter.tv.server.utils.InstallationObserver; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +public class ApplicationLauncherManagerImpl implements ApplicationLauncherManager { |
| 18 | + |
| 19 | + private static final String TAG = "ApplicationLauncherService"; |
| 20 | + |
| 21 | + private volatile boolean registered = false; |
| 22 | + private PackageManager packageManager; |
| 23 | + private EndpointsDataStore endpointsDataStore; |
| 24 | + |
| 25 | + /** Hash Map of packageName & Install Status */ |
| 26 | + private Map<String, InstallationObserver.InstallStatus> lastReceivedInstallationStatus = |
| 27 | + new HashMap<>(); |
| 28 | + |
| 29 | + private LiveData<InstallationObserver.InstallState> installStateLiveData; |
| 30 | + |
| 31 | + public ApplicationLauncherManagerImpl(Context context) { |
| 32 | + packageManager = context.getPackageManager(); |
| 33 | + endpointsDataStore = EndpointsDataStore.getInstance(context); |
| 34 | + registerSelf(context); |
| 35 | + } |
| 36 | + |
| 37 | + private final Observer<InstallationObserver.InstallState> installStateObserver = |
| 38 | + state -> { |
| 39 | + lastReceivedInstallationStatus.put(state.getAppPackageName(), state.getStatus()); |
| 40 | + switch (state.getStatus()) { |
| 41 | + case IN_PROGRESS: |
| 42 | + // Installation is in progress |
| 43 | + Log.d(TAG, "Installation of " + state.getAppPackageName() + " in progress"); |
| 44 | + break; |
| 45 | + case SUCCEEDED: |
| 46 | + // Installation succeeded |
| 47 | + Log.d(TAG, "Installation of " + state.getAppPackageName() + " succeeded"); |
| 48 | + break; |
| 49 | + case FAILED: |
| 50 | + // Installation failed |
| 51 | + Log.d(TAG, "Installation of " + state.getAppPackageName() + " failed"); |
| 52 | + break; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + private void stopObservingInstallations() { |
| 57 | + if (installStateLiveData != null) { |
| 58 | + Log.d("InstallationObserver", "Stopped Observing"); |
| 59 | + installStateLiveData.removeObserver(installStateObserver); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void unregister() { |
| 64 | + stopObservingInstallations(); |
| 65 | + } |
| 66 | + |
| 67 | + private void registerSelf(Context context) { |
| 68 | + if (registered) { |
| 69 | + Log.i(TAG, "Package update receiver for matter already registered"); |
| 70 | + return; |
| 71 | + } else { |
| 72 | + registered = true; |
| 73 | + } |
| 74 | + Log.i(TAG, "Registered the matter package updates receiver"); |
| 75 | + |
| 76 | + installStateLiveData = InstallationObserver.installationStates(context); |
| 77 | + installStateLiveData.observeForever(installStateObserver); |
| 78 | + Log.d(TAG, "Started Observing package installations"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int[] getCatalogList() { |
| 83 | + Log.i(TAG, "Get Catalog List"); |
| 84 | + return new int[] {123, 456, 89010}; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public LauncherResponse launchApp(Application app, String data) { |
| 89 | + Log.i( |
| 90 | + TAG, |
| 91 | + "Launch app id:" + app.applicationId + " cid:" + app.catalogVendorId + " data:" + data); |
| 92 | + |
| 93 | + int status = 0; |
| 94 | + String responseData = ""; |
| 95 | + |
| 96 | + // Installed Apps that have declared CSA product id & vendor id in their manifes |
| 97 | + boolean matterEnabledAppdIsInstalled = |
| 98 | + endpointsDataStore.getAllPersistedContentApps().containsKey(app.applicationId); |
| 99 | + // Installed App |
| 100 | + boolean appIsInstalled = |
| 101 | + InstallationObserver.getInstalledPackages(packageManager).contains(app.applicationId); |
| 102 | + boolean isAppInstalling = |
| 103 | + Objects.equals( |
| 104 | + lastReceivedInstallationStatus.get(app.applicationId), |
| 105 | + InstallationObserver.InstallStatus.IN_PROGRESS); |
| 106 | + boolean appInstallFailed = |
| 107 | + Objects.equals( |
| 108 | + lastReceivedInstallationStatus.get(app.applicationId), |
| 109 | + InstallationObserver.InstallStatus.FAILED); |
| 110 | + |
| 111 | + // This use-case can happen if app is installed |
| 112 | + // but it does not support Matter |
| 113 | + if (!matterEnabledAppdIsInstalled && appIsInstalled) { |
| 114 | + Log.i( |
| 115 | + TAG, |
| 116 | + "Matter enabled app is not installed, but app is installed. Launching app's install page"); |
| 117 | + status = LauncherResponse.STATUS_PENDING_USER_APPROVAL; |
| 118 | + responseData = "App is installed, try updating"; |
| 119 | + |
| 120 | + // |
| 121 | + // Add code to launch App Install Page |
| 122 | + // |
| 123 | + |
| 124 | + } else if (!matterEnabledAppdIsInstalled && !appIsInstalled) { |
| 125 | + Log.i( |
| 126 | + TAG, |
| 127 | + "Matter enabled app is not installed and app is not installed. Launching app's install page"); |
| 128 | + if (isAppInstalling) { |
| 129 | + Log.i(TAG, "App is installing"); |
| 130 | + status = LauncherResponse.STATUS_INSTALLING; |
| 131 | + } else { |
| 132 | + status = LauncherResponse.STATUS_PENDING_USER_APPROVAL; |
| 133 | + if (appInstallFailed) { |
| 134 | + responseData = "App install failed. Try again"; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // |
| 139 | + // Add code to launch App Install Page |
| 140 | + // |
| 141 | + |
| 142 | + } else if (matterEnabledAppdIsInstalled && appIsInstalled) { |
| 143 | + Log.i(TAG, "Launching the app"); |
| 144 | + status = LauncherResponse.STATUS_SUCCESS; |
| 145 | + |
| 146 | + // |
| 147 | + // Add code to launch an app |
| 148 | + // |
| 149 | + } |
| 150 | + |
| 151 | + return new LauncherResponse(status, responseData); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public LauncherResponse stopApp(Application app) { |
| 156 | + Log.i(TAG, "Stop app id:" + app.applicationId + " cid:" + app.catalogVendorId); |
| 157 | + return new LauncherResponse(LauncherResponse.STATUS_SUCCESS, ""); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public LauncherResponse hideApp(Application app) { |
| 162 | + Log.i(TAG, "Hide app id:" + app.applicationId + " cid:" + app.catalogVendorId); |
| 163 | + return new LauncherResponse(LauncherResponse.STATUS_SUCCESS, ""); |
| 164 | + } |
| 165 | +} |
0 commit comments