Merge "Override the label of the SIM Toolkit application dynamically" am: 22d92add67

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Stk/+/743041

Change-Id: I5df8dcac536329b20ea55a78e85c06a1ffe84622
diff --git a/src/com/android/stk/StkAppInstaller.java b/src/com/android/stk/StkAppInstaller.java
index 4e386ee..fe25af6 100644
--- a/src/com/android/stk/StkAppInstaller.java
+++ b/src/com/android/stk/StkAppInstaller.java
@@ -16,66 +16,75 @@
 
 package com.android.stk;
 
-import com.android.internal.telephony.cat.CatLog;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.cat.CatLog;
+import com.android.internal.telephony.util.TelephonyUtils;
 
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
-import android.telephony.TelephonyManager;
-import android.os.SystemProperties;
+import android.os.Build;
+import android.os.RemoteException;
+import android.os.ServiceManager;
 
 /**
  * Application installer for SIM Toolkit.
  *
  */
-abstract class StkAppInstaller {
-    private static final String STK_MAIN_ACTIVITY = "com.android.stk.StkMain";
+final class StkAppInstaller {
+    private static final boolean DBG = TelephonyUtils.IS_DEBUGGABLE;
     private static final String LOG_TAG =
             new Object(){}.getClass().getEnclosingClass().getSimpleName();
 
     private StkAppInstaller() {
-        CatLog.d(LOG_TAG, "init");
     }
 
-    public static void install(Context context) {
-        setAppState(context, true);
-    }
-
-    public static void unInstall(Context context) {
-        setAppState(context, false);
-    }
-
-    private static void setAppState(Context context, boolean install) {
-        CatLog.d(LOG_TAG, "[setAppState]+");
-        if (context == null) {
-            CatLog.d(LOG_TAG, "[setAppState]- no context, just return.");
-            return;
-        }
-        PackageManager pm = context.getPackageManager();
-        if (pm == null) {
-            CatLog.d(LOG_TAG, "[setAppState]- no package manager, just return.");
-            return;
-        }
-        ComponentName cName = new ComponentName("com.android.stk", STK_MAIN_ACTIVITY);
-        int state = install ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
-                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
-
-        if (((PackageManager.COMPONENT_ENABLED_STATE_ENABLED == state) &&
-                (PackageManager.COMPONENT_ENABLED_STATE_ENABLED ==
-                pm.getComponentEnabledSetting(cName))) ||
-                ((PackageManager.COMPONENT_ENABLED_STATE_DISABLED == state) &&
-                (PackageManager.COMPONENT_ENABLED_STATE_DISABLED ==
-                pm.getComponentEnabledSetting(cName)))) {
-            CatLog.d(LOG_TAG, "Need not change app state!!");
-        } else {
-            CatLog.d(LOG_TAG, "Change app state[" + install + "]");
+    static void installOrUpdate(Context context, String label) {
+        IPackageManager pm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
+        if (pm != null) {
+            ComponentName component = new ComponentName(context, StkMain.class);
+            int userId = context.getUserId();
+            int icon = R.drawable.ic_launcher_sim_toolkit;
             try {
-                pm.setComponentEnabledSetting(cName, state, PackageManager.DONT_KILL_APP);
-            } catch (Exception e) {
-                CatLog.d(LOG_TAG, "Could not change STK app state");
+                try {
+                    if (label != null) {
+                        pm.overrideLabelAndIcon(component, label, icon, userId);
+                    } else {
+                        pm.restoreLabelAndIcon(component, userId);
+                    }
+                    if (DBG) CatLog.d(LOG_TAG, "Set the label to " + label);
+                } catch (SecurityException e) {
+                    CatLog.e(LOG_TAG, "Failed to set the label to " + label);
+                }
+                setAppState(pm, component, userId, true);
+            } catch (RemoteException e) {
+                CatLog.e(LOG_TAG, "Failed to enable SIM Toolkit");
             }
         }
-        CatLog.d(LOG_TAG, "[setAppState]-");
+    }
+
+    static void uninstall(Context context) {
+        IPackageManager pm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
+        if (pm != null) {
+            ComponentName component = new ComponentName(context, StkMain.class);
+            try {
+                setAppState(pm, component, context.getUserId(), false);
+            } catch (RemoteException e) {
+                CatLog.e(LOG_TAG, "Failed to disable SIM Toolkit");
+            }
+        }
+    }
+
+    static void setAppState(IPackageManager pm, ComponentName component, int userId, boolean enable)
+            throws RemoteException {
+        int current = pm.getComponentEnabledSetting(component, userId);
+        int expected = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
+                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
+        if (current != expected) {
+            pm.setComponentEnabledSetting(component, expected, PackageManager.DONT_KILL_APP,
+                    userId);
+            if (DBG) CatLog.d(LOG_TAG, "SIM Toolkit is " + (enable ? "enabled" : "disabled"));
+        }
     }
 }
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 0f9f0e8..e3ceabd 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -369,7 +369,7 @@
             }
             if (i == mSimCount) {
                 stopSelf();
-                StkAppInstaller.unInstall(mContext);
+                StkAppInstaller.uninstall(this);
                 return;
             }
         }
@@ -498,7 +498,7 @@
         CatLog.d(LOG_TAG, "StkAppService, getMainMenu, sim id: " + slotId);
         if (slotId >=0 && slotId < mSimCount && (mStkContext[slotId].mMainCmd != null)) {
             Menu menu = mStkContext[slotId].mMainCmd.getMenu();
-            if (menu != null && mSimCount > PhoneConstants.MAX_PHONE_COUNT_SINGLE_SIM) {
+            if (menu != null) {
                 // If alpha identifier or icon identifier with the self-explanatory qualifier is
                 // specified in SET-UP MENU command, it should be more prioritized than preset ones.
                 if (menu.title == null
@@ -634,7 +634,7 @@
                     }
                 }
                 if (i == mSimCount) {
-                    StkAppInstaller.unInstall(mContext);
+                    StkAppInstaller.uninstall(StkAppService.this);
                 }
                 break;
             case OP_DELAYED_MSG:
@@ -738,8 +738,10 @@
                 }
                 if (isAllOtherCardsAbsent(slotId)) {
                     CatLog.d(LOG_TAG, "All CARDs are ABSENT");
-                    StkAppInstaller.unInstall(mContext);
+                    StkAppInstaller.uninstall(StkAppService.this);
                     stopSelf();
+                } else {
+                    addToMenuSystemOrUpdateLabel();
                 }
             } else {
                 IccRefreshResponse state = new IccRefreshResponse();
@@ -1133,11 +1135,12 @@
                     }
                 }
                 if (i == mSimCount) {
-                    StkAppInstaller.unInstall(mContext);
+                    StkAppInstaller.uninstall(this);
+                } else {
+                    addToMenuSystemOrUpdateLabel();
                 }
             } else {
-                CatLog.d(LOG_TAG, "install App");
-                StkAppInstaller.install(mContext);
+                addToMenuSystemOrUpdateLabel();
             }
             if (mStkContext[slotId].mMenuIsVisible) {
                 launchMenuActivity(null, slotId);
@@ -1265,6 +1268,32 @@
         }
     }
 
+    private void addToMenuSystemOrUpdateLabel() {
+        String candidateLabel = null;
+
+        for (int slotId = 0; slotId < mSimCount; slotId++) {
+            Menu menu = getMainMenu(slotId);
+            if (menu != null) {
+                if (!TextUtils.isEmpty(candidateLabel)) {
+                    if (!TextUtils.equals(menu.title, candidateLabel)) {
+                        // We should not display the alpha identifier of SET-UP MENU command
+                        // as the application label on the application launcher
+                        // if different alpha identifiers are provided by multiple SIMs.
+                        candidateLabel = null;
+                        break;
+                    }
+                } else {
+                    if (TextUtils.isEmpty(menu.title)) {
+                        break;
+                    }
+                    candidateLabel = menu.title;
+                }
+            }
+        }
+
+        StkAppInstaller.installOrUpdate(this, candidateLabel);
+    }
+
     @SuppressWarnings("FallThrough")
     private void handleCmdResponse(Bundle args, int slotId) {
         CatLog.d(LOG_TAG, "handleCmdResponse, sim id: " + slotId);