Stk App bug fix and code refine.

Major changes:

1. StkAppService.java :
line272~279: use getInstance(slotId)@CatService to get instance of cat service instead.
line303~307: remove redundant codes
line457~485: remove redundant codes
line693~695: fix the NPE if the first proactive command is not SET_UP_MENU.
line1109~1116: remove redundant codes.

2. StkMain.java (new file, is the launcher of Stk without UI(No_Display theme) :
To fix a short flash problem when entering Stk app for one SIM inserted case.
If inserted one SIM, it starts StkMenuActivity for showing main menu of the SIM.
If inserted two SIMs, it starts StkLauncherActivity which shows a list for inserted SIMs.

Bug: 18705342
Change-Id: If777f1cc0a16df38d1b534cd60ed7850b6072b22
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
old mode 100755
new mode 100644
index 3b7f0a5..7410f81
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -75,6 +75,7 @@
 import com.android.internal.telephony.IccCardConstants;
 import com.android.internal.telephony.uicc.UiccController;
 import com.android.internal.telephony.GsmAlphabet;
+import com.android.internal.telephony.cat.CatService;
 
 import java.util.LinkedList;
 import java.lang.System;
@@ -115,7 +116,6 @@
         private Activity mActivityInstance = null;
         private Activity mDialogInstance = null;
         private Activity mMainActivityInstance = null;
-        private boolean mBackGroundTRSent = false;
         private int mSlotId = 0;
         private SetupEventListSettings mSetupEventListSettings = null;
         private boolean mClearSelectItem = false;
@@ -264,13 +264,7 @@
         registerReceiver(mStkCmdReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
         for (i = 0; i < mSimCount; i++) {
             CatLog.d(LOG_TAG, "slotId: " + i);
-            if (null != UiccController.getInstance() && null != UiccController.getInstance()
-                    .getUiccCard(i)) {
-                mStkService[i] = UiccController.getInstance().getUiccCard(i).getCatService();
-            } else {
-                CatLog.d(LOG_TAG, "Null instance: [" + UiccController.getInstance() + "],[" +
-                        UiccController.getInstance().getUiccCard(i) + "]");
-            }
+            mStkService[i] = CatService.getInstance(i);
             mStkContext[i] = new StkContext();
             mStkContext[i].mSlotId = i;
             mStkContext[i].mCmdsQ = new LinkedList<DelayedCmd>();
@@ -304,14 +298,7 @@
         }
         CatLog.d(LOG_TAG, "onStart sim id: " + slotId + ", op: " + op + ", " + args);
         if ((slotId >= 0 && slotId < mSimCount) && mStkService[slotId] == null) {
-            if (null != UiccController.getInstance() && null != UiccController.getInstance()
-                    .getUiccCard(slotId)) {
-                mStkService[slotId] = UiccController.getInstance().getUiccCard(slotId)
-                        .getCatService();
-            } else {
-                CatLog.d(LOG_TAG, "Null instance: [" + UiccController.getInstance() + "],[" +
-                        UiccController.getInstance().getUiccCard(slotId)+"]");
-            }
+            mStkService[slotId] = CatService.getInstance(slotId);
             if (mStkService[slotId] == null) {
                 CatLog.d(LOG_TAG, "mStkService is: " + mStkContext[slotId].mStkServiceState);
                 mStkContext[slotId].mStkServiceState = STATE_NOT_EXIST;
@@ -335,11 +322,6 @@
         }
 
         waitForLooper();
-        // onStart() method can be passed a null intent
-        // TODO: replace onStart() with onStartCommand()
-        if (intent == null) {
-            return;
-        }
 
         Message msg = mServiceHandler.obtainMessage();
         msg.arg1 = op;
@@ -506,35 +488,6 @@
                 //just finish it and create a new one to handle the pending command.
                 cleanUpInstanceStackBySlot(slotId);
 
-                //Clean up all other activities in stack.
-                for (int i = 0; i < mSimCount; i++) {
-                    if (i != slotId && mStkContext[i].mCurrentCmd != null) {
-                        Activity otherAct = mStkContext[i].getPendingActivityInstance();
-                        Activity otherDal = mStkContext[i].getPendingDialogInstance();
-                        Activity otherMainMenu = mStkContext[i].getMainActivityInstance();
-                        if (otherAct != null) {
-                            CatLog.d(LOG_TAG, "finish pending otherAct and send SE. slot: " + i);
-                            // Send end session for the pending proactive command of slot i in
-                            // onDestroy of the activity.
-                            // Set mBackGroundTRSent to true for ignoring to show the main menu
-                            // for the following end session event.
-                            mStkContext[i].mBackGroundTRSent = true;
-                            otherAct.finish();
-                            mStkContext[i].mActivityInstance = null;
-                        }
-                        if (otherDal != null) {
-                            CatLog.d(LOG_TAG, "finish pending otherDal and send TR for the dialog");
-                            mStkContext[i].mBackGroundTRSent = true;
-                            otherDal.finish();
-                            mStkContext[i].mDialogInstance = null;
-                        }
-                        if (otherMainMenu != null) {
-                            CatLog.d(LOG_TAG, "finish pending otherMainMenu.");
-                            otherMainMenu.finish();
-                            mStkContext[i].mMainActivityInstance = null;
-                        }
-                    }
-                }
                 CatLog.d(LOG_TAG, "Current cmd type: " +
                         mStkContext[slotId].mCurrentCmd.getCmdType());
                 //Restore the last command from stack by slot id.
@@ -791,6 +744,9 @@
     }
 
     private void handleSessionEnd(int slotId) {
+        // We should finish all pending activity if receiving END SESSION command.
+        cleanUpInstanceStackBySlot(slotId);
+
         mStkContext[slotId].mCurrentCmd = mStkContext[slotId].mMainCmd;
         CatLog.d(LOG_TAG, "[handleSessionEnd] - mCurrentCmd changed to mMainCmd!.");
         mStkContext[slotId].mCurrentMenuCmd = mStkContext[slotId].mMainCmd;
@@ -801,9 +757,6 @@
         mStkContext[slotId].mIsMenuPending = false;
         mStkContext[slotId].mIsDialogPending = false;
 
-        // We should finish all pending activity if receiving END SESSION command.
-        cleanUpInstanceStackBySlot(slotId);
-
         if (mStkContext[slotId].mMainCmd == null) {
             CatLog.d(LOG_TAG, "[handleSessionEnd][mMainCmd is null!]");
         }
@@ -1023,14 +976,7 @@
         }
 
         if (mStkService[slotId] == null) {
-            if(null != UiccController.getInstance() &&
-                    null != UiccController.getInstance().getUiccCard(slotId)) {
-                mStkService[slotId] = UiccController.getInstance().getUiccCard(slotId)
-                        .getCatService();
-            } else {
-                CatLog.d(LOG_TAG, "Null instance: [" + UiccController.getInstance() +
-                        "],["+UiccController.getInstance().getUiccCard(slotId)+"]");
-            }
+            mStkService[slotId] = CatService.getInstance(slotId);
             if (mStkService[slotId] == null) {
                 // This should never happen (we should be responding only to a message
                 // that arrived from StkService). It has to exist by this time
@@ -1188,6 +1134,10 @@
         Activity activity = mStkContext[slotId].getPendingActivityInstance();
         Activity dialog = mStkContext[slotId].getPendingDialogInstance();
         CatLog.d(LOG_TAG, "cleanUpInstanceStackBySlot slotId: " + slotId);
+        if (mStkContext[slotId].mCurrentCmd == null) {
+            CatLog.d(LOG_TAG, "current cmd is null.");
+            return;
+        }
         if (activity != null) {
             CatLog.d(LOG_TAG, "current cmd type: " +
                     mStkContext[slotId].mCurrentCmd.getCmdType());
@@ -1275,14 +1225,6 @@
                     CatLog.d(LOG_TAG, "launchMenuActivity, mMainActivityInstance is not null");
                     return;
                 }
-                // If END SESSION is sent that results from the activity is finished by
-                // stkappservice (line 457), we should igonore to display the stk main menu
-                // of slot id.
-                if (mStkContext[slotId].mBackGroundTRSent) {
-                    CatLog.d(LOG_TAG, "launchMenuActivity, ES is triggered by BG.");
-                    mStkContext[slotId].mBackGroundTRSent = false;
-                    return;
-                }
             }
 
             //If the last pending menu is secondary menu, "STATE" should be "STATE_SECONDARY".
diff --git a/src/com/android/stk/StkDialogActivity.java b/src/com/android/stk/StkDialogActivity.java
old mode 100755
new mode 100644
index da92b24..59bdad8
--- a/src/com/android/stk/StkDialogActivity.java
+++ b/src/com/android/stk/StkDialogActivity.java
@@ -195,7 +195,6 @@
             appService.getStkContext(mSlotId).setPendingDialogInstance(null);
             cancelTimeOut();
             finish();
-            CatLog.d(LOG_TAG, "finish.");
         }
     }
 
diff --git a/src/com/android/stk/StkInputActivity.java b/src/com/android/stk/StkInputActivity.java
old mode 100755
new mode 100644
diff --git a/src/com/android/stk/StkLauncherActivity.java b/src/com/android/stk/StkLauncherActivity.java
old mode 100755
new mode 100644
index 9066c3e..73529e9
--- a/src/com/android/stk/StkLauncherActivity.java
+++ b/src/com/android/stk/StkLauncherActivity.java
@@ -62,21 +62,13 @@
         mContext = getBaseContext();
         mTm = (TelephonyManager) mContext.getSystemService(
                 Context.TELEPHONY_SERVICE);
-        //Check if needs to show the menu list.
-        if (isShowSTKListMenu()) {
-            requestWindowFeature(Window.FEATURE_NO_TITLE);
-            setContentView(R.layout.stk_menu_list);
-            mTitleTextView = (TextView) findViewById(R.id.title_text);
-            mTitleIconView = (ImageView) findViewById(R.id.title_icon);
-            mTitleTextView.setText(R.string.app_name);
-            mBitMap = BitmapFactory.decodeResource(getResources(),
-                    R.drawable.ic_launcher_sim_toolkit);
-        } else {
-            //launch stk menu activity for the SIM.
-            if (mSingleSimId < 0) {
-                finish();
-            }
-        }
+        requestWindowFeature(Window.FEATURE_NO_TITLE);
+        setContentView(R.layout.stk_menu_list);
+        mTitleTextView = (TextView) findViewById(R.id.title_text);
+        mTitleIconView = (ImageView) findViewById(R.id.title_icon);
+        mTitleTextView.setText(R.string.app_name);
+        mBitMap = BitmapFactory.decodeResource(getResources(),
+                R.drawable.ic_launcher_sim_toolkit);
     }
 
     @Override
@@ -201,30 +193,6 @@
             return 0;
         }
     }
-    private boolean isShowSTKListMenu() {
-        int simCount = TelephonyManager.from(mContext).getSimCount();
-        int simInsertedCount = 0;
-        int insertedSlotId = -1;
-
-        CatLog.d(LOG_TAG, "simCount: " + simCount);
-        for (int i = 0; i < simCount; i++) {
-            //Check if the card is inserted.
-            if (mTm.hasIccCard(i)) {
-                CatLog.d(LOG_TAG, "SIM " + i + " is inserted.");
-                mSingleSimId = i;
-                simInsertedCount++;
-            } else {
-                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
-            }
-        }
-        if (simInsertedCount > 1) {
-            return true;
-        } else {
-            //No card or only one card.
-            CatLog.d(LOG_TAG, "do not show stk list menu.");
-            return false;
-        }
-    }
     private void launchSTKMainMenu(int slodId) {
         Bundle args = new Bundle();
         CatLog.d(LOG_TAG, "launchSTKMainMenu.");
diff --git a/src/com/android/stk/StkMain.java b/src/com/android/stk/StkMain.java
new file mode 100644
index 0000000..2be33ab
--- /dev/null
+++ b/src/com/android/stk/StkMain.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.stk;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+
+import com.android.internal.telephony.cat.CatLog;
+import com.android.internal.telephony.PhoneConstants;
+
+import android.telephony.TelephonyManager;
+
+import android.view.Gravity;
+import android.widget.Toast;
+
+/**
+ * Launcher class. Serve as the app's MAIN activity, send an intent to the
+ * StkAppService and finish.
+ *
+ */
+ public class StkMain extends Activity {
+    private static final String className = new Object(){}.getClass().getEnclosingClass().getName();
+    private static final String LOG_TAG = className.substring(className.lastIndexOf('.') + 1);
+    private int mSingleSimId = -1;
+    private Context mContext = null;
+    private TelephonyManager mTm = null;
+    private static final String PACKAGE_NAME = "com.android.stk";
+    private static final String STK_LAUNCHER_ACTIVITY_NAME = PACKAGE_NAME + ".StkLauncherActivity";
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+        CatLog.d(LOG_TAG, "onCreate+");
+        mContext = getBaseContext();
+        mTm = (TelephonyManager) mContext.getSystemService(
+                Context.TELEPHONY_SERVICE);
+        //Check if needs to show the meun list.
+        if (isShowSTKListMenu()) {
+            Intent newIntent = new Intent(Intent.ACTION_VIEW);
+            newIntent.setClassName(PACKAGE_NAME, STK_LAUNCHER_ACTIVITY_NAME);
+            startActivity(newIntent);
+        } else {
+            //launch stk menu activity for the SIM.
+            if (mSingleSimId < 0) {
+                showTextToast(mContext, R.string.no_sim_card_inserted);
+            } else {
+                launchSTKMainMenu(mSingleSimId);
+            }
+        }
+        finish();
+    }
+
+    private boolean isShowSTKListMenu() {
+        int simCount = TelephonyManager.from(mContext).getSimCount();
+        int simInsertedCount = 0;
+        int insertedSlotId = -1;
+
+        CatLog.d(LOG_TAG, "simCount: " + simCount);
+        for (int i = 0; i < simCount; i++) {
+            //Check if the card is inserted.
+            if (mTm.hasIccCard(i)) {
+                CatLog.d(LOG_TAG, "SIM " + i + " is inserted.");
+                mSingleSimId = i;
+                simInsertedCount++;
+            } else {
+                CatLog.d(LOG_TAG, "SIM " + i + " is not inserted.");
+            }
+        }
+        if (simInsertedCount > 1) {
+            return true;
+        } else {
+            //No card or only one card.
+            CatLog.d(LOG_TAG, "do not show stk list menu.");
+            return false;
+        }
+    }
+
+    private void launchSTKMainMenu(int slotId) {
+        Bundle args = new Bundle();
+        CatLog.d(LOG_TAG, "launchSTKMainMenu.");
+        args.putInt(StkAppService.OPCODE, StkAppService.OP_LAUNCH_APP);
+        args.putInt(StkAppService.SLOT_ID
+                , PhoneConstants.SIM_ID_1 + slotId);
+        startService(new Intent(this, StkAppService.class)
+                .putExtras(args));
+    }
+
+    private void showTextToast(Context context, int resId) {
+        Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
+        toast.setGravity(Gravity.BOTTOM, 0, 0);
+        toast.show();
+    }
+}
diff --git a/src/com/android/stk/StkMenuActivity.java b/src/com/android/stk/StkMenuActivity.java
old mode 100755
new mode 100644