Merge d137a17c589914ef498f76511146981f8067b5bb on remote branch

Change-Id: I8e5946ffc5af1ef2d1b3595f398eb7eda85a0894
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index 11bbd32..a13a37c 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -119,7 +119,6 @@
         protected LinkedList<DelayedCmd> mCmdsQ = null;
         protected boolean mCmdInProgress = false;
         protected int mStkServiceState = STATE_UNKNOWN;
-        protected int mSetupMenuState = STATE_NOT_EXIST;
         protected int mMenuState = StkMenuActivity.STATE_INIT;
         protected int mOpCode = -1;
         private Activity mActivityInstance = null;
@@ -631,15 +630,7 @@
                 break;
             case OP_BOOT_COMPLETED:
                 CatLog.d(LOG_TAG, " OP_BOOT_COMPLETED");
-                int i = 0;
-                for (i = 0; i < mSimCount; i++) {
-                    if (mStkContext[i].mMainCmd != null) {
-                        break;
-                    }
-                }
-                if (i == mSimCount) {
-                    StkAppInstaller.uninstall(StkAppService.this);
-                }
+                uninstallIfUnnecessary();
                 break;
             case OP_DELAYED_MSG:
                 handleDelayedCmd(slotId);
@@ -740,12 +731,12 @@
                         == AppInterface.CommandType.PLAY_TONE.value()) {
                     terminateTone(slotId);
                 }
+                if (!uninstallIfUnnecessary()) {
+                    addToMenuSystemOrUpdateLabel();
+                }
                 if (isAllOtherCardsAbsent(slotId)) {
                     CatLog.d(LOG_TAG, "All CARDs are ABSENT");
-                    StkAppInstaller.uninstall(StkAppService.this);
                     stopSelf();
-                } else {
-                    addToMenuSystemOrUpdateLabel();
                 }
             } else {
                 IccRefreshResponse state = new IccRefreshResponse();
@@ -1128,21 +1119,10 @@
             CatLog.d(LOG_TAG, "SET_UP_MENU [" + removeMenu(slotId) + "]");
 
             if (removeMenu(slotId)) {
-                int i = 0;
-                CatLog.d(LOG_TAG, "removeMenu() - Uninstall App");
                 mStkContext[slotId].mCurrentMenu = null;
                 mStkContext[slotId].mMainCmd = null;
                 //Check other setup menu state. If all setup menu are removed, uninstall apk.
-                for (i = 0; i < mSimCount; i++) {
-                    if (i != slotId && mStkContext[i].mSetupMenuState != STATE_NOT_EXIST) {
-                        CatLog.d(LOG_TAG, "Not Uninstall App:" + i + ","
-                                + mStkContext[i].mSetupMenuState);
-                        break;
-                    }
-                }
-                if (i == mSimCount) {
-                    StkAppInstaller.uninstall(this);
-                } else {
+                if (!uninstallIfUnnecessary()) {
                     addToMenuSystemOrUpdateLabel();
                 }
             } else {
@@ -2472,18 +2452,26 @@
         try {
             if (mStkContext[slotId].mCurrentMenu.items.size() == 1 &&
                 mStkContext[slotId].mCurrentMenu.items.get(0) == null) {
-                mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
                 return true;
             }
         } catch (NullPointerException e) {
             CatLog.d(LOG_TAG, "Unable to get Menu's items size");
-            mStkContext[slotId].mSetupMenuState = STATE_NOT_EXIST;
             return true;
         }
-        mStkContext[slotId].mSetupMenuState = STATE_EXIST;
         return false;
     }
 
+    private boolean uninstallIfUnnecessary() {
+        for (int slot = 0; slot < mSimCount; slot++) {
+            if (mStkContext[slot].mMainCmd != null) {
+                return false;
+            }
+        }
+        CatLog.d(LOG_TAG, "Uninstall App");
+        StkAppInstaller.uninstall(this);
+        return true;
+    }
+
     synchronized StkContext getStkContext(int slotId) {
         if (slotId >= 0 && slotId < mSimCount) {
             return mStkContext[slotId];