Merge "Support ENVELOPE (EVENT DOWNLOAD - User activity) command" into pi-dev
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index a658763..c7f897e 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -56,6 +56,7 @@
 import android.provider.Settings;
 import android.support.v4.content.LocalBroadcastManager;
 import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.view.Gravity;
@@ -129,6 +130,7 @@
         protected int mOpCode = -1;
         private Activity mActivityInstance = null;
         private Activity mDialogInstance = null;
+        private Activity mImmediateDialogInstance = null;
         private int mSlotId = 0;
         private SetupEventListSettings mSetupEventListSettings = null;
         private boolean mClearSelectItem = false;
@@ -154,6 +156,15 @@
                     mDialogInstance);
             return mDialogInstance;
         }
+        final synchronized void setImmediateDialogInstance(Activity act) {
+            CatLog.d(this, "setImmediateDialogInstance act : " + mSlotId + ", " + act);
+            callSetActivityInstMsg(OP_SET_IMMED_DAL_INST, mSlotId, act);
+        }
+        final synchronized Activity getImmediateDialogInstance() {
+            CatLog.d(this, "getImmediateDialogInstance act : " + mSlotId + ", " +
+                    mImmediateDialogInstance);
+            return mImmediateDialogInstance;
+        }
     }
 
     private volatile Looper mServiceLooper;
@@ -211,6 +222,7 @@
     static final int OP_LOCALE_CHANGED = 10;
     static final int OP_ALPHA_NOTIFY = 11;
     static final int OP_IDLE_SCREEN = 12;
+    static final int OP_SET_IMMED_DAL_INST = 13;
 
     //Invalid SetupEvent
     static final int INVALID_SETUP_EVENT = 0xFF;
@@ -631,11 +643,15 @@
                 }
                 break;
             case OP_SET_DAL_INST:
-                Activity dal = new Activity();
+                Activity dal = (Activity) msg.obj;
                 CatLog.d(LOG_TAG, "Set dialog instance. " + dal);
-                dal = (Activity) msg.obj;
                 mStkContext[slotId].mDialogInstance = dal;
                 break;
+            case OP_SET_IMMED_DAL_INST:
+                Activity immedDal = (Activity) msg.obj;
+                CatLog.d(LOG_TAG, "Set dialog instance for immediate response. " + immedDal);
+                mStkContext[slotId].mImmediateDialogInstance = immedDal;
+                break;
             case OP_LOCALE_CHANGED:
                 CatLog.d(this, "Locale Changed");
                 for (int slot = PhoneConstants.SIM_ID_1; slot < mSimCount; slot++) {
@@ -1392,6 +1408,21 @@
         }
     }
 
+    @Override
+    public void startActivity(Intent intent) {
+        int slotId = intent.getIntExtra(SLOT_ID, SubscriptionManager.INVALID_SIM_SLOT_INDEX);
+        // Close the dialog displayed for DISPLAY TEXT command with an immediate response object
+        // before new dialog is displayed.
+        if (SubscriptionManager.isValidSlotIndex(slotId)) {
+            Activity dialog = mStkContext[slotId].getImmediateDialogInstance();
+            if (dialog != null) {
+                CatLog.d(LOG_TAG, "finish dialog for immediate response.");
+                dialog.finish();
+            }
+        }
+        super.startActivity(intent);
+    }
+
     private void launchMenuActivity(Menu menu, int slotId) {
         Intent newIntent = new Intent(Intent.ACTION_VIEW);
         String targetActivity = STK_MENU_ACTIVITY_NAME;
@@ -1426,7 +1457,7 @@
         newIntent.putExtra(SLOT_ID, slotId);
         newIntent.setData(uriData);
         newIntent.setFlags(intentFlags);
-        mContext.startActivity(newIntent);
+        startActivity(newIntent);
     }
 
     private void launchInputActivity(int slotId) {
@@ -1448,7 +1479,7 @@
         if (input != null) {
             notifyUserIfNecessary(slotId, input.text);
         }
-        mContext.startActivity(newIntent);
+        startActivity(newIntent);
     }
 
     private void launchTextDialog(int slotId) {
diff --git a/src/com/android/stk/StkDialogActivity.java b/src/com/android/stk/StkDialogActivity.java
index cdc3c17..be43747 100644
--- a/src/com/android/stk/StkDialogActivity.java
+++ b/src/com/android/stk/StkDialogActivity.java
@@ -32,6 +32,7 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.SystemClock;
+import android.telephony.SubscriptionManager;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -125,6 +126,10 @@
 
         if (!mTextMsg.responseNeeded) {
             alertDialogBuilder.setNegativeButton(null, null);
+            // Register the instance of this activity because the dialog displayed for DISPLAY TEXT
+            // command with an immediate response object should disappear when the terminal receives
+            // a subsequent proactive command containing display data.
+            appService.getStkContext(mSlotId).setImmediateDialogInstance(this);
         }
 
         alertDialogBuilder.setTitle(mTextMsg.title);
@@ -301,6 +306,18 @@
         setIntent(intent);
     }
 
+    @Override
+    public void finish() {
+        super.finish();
+        // Unregister the instance for DISPLAY TEXT command with an immediate response object
+        // as it is unnecessary to ask the service to finish this anymore.
+        if ((appService != null) && (mTextMsg != null) && !mTextMsg.responseNeeded) {
+            if (SubscriptionManager.isValidSlotIndex(mSlotId)) {
+                appService.getStkContext(mSlotId).setImmediateDialogInstance(null);
+            }
+        }
+    }
+
     private void sendResponse(int resId, boolean confirmed) {
         if (mSlotId == -1) {
             CatLog.d(LOG_TAG, "sim id is invalid");
diff --git a/src/com/android/stk/StkMenuActivity.java b/src/com/android/stk/StkMenuActivity.java
index 9f268d1..bc12eee 100644
--- a/src/com/android/stk/StkMenuActivity.java
+++ b/src/com/android/stk/StkMenuActivity.java
@@ -68,6 +68,12 @@
 
     private StkAppService appService = StkAppService.getInstance();
 
+    // Keys for saving the state of the dialog in the bundle
+    private static final String STATE_KEY = "state";
+    private static final String MENU_KEY = "menu";
+    private static final String ACCEPT_USERS_INPUT_KEY = "accept_users_input";
+    private static final String RESPONSE_SENT_KEY = "response_sent";
+
     // Internal state values
     static final int STATE_INIT = 0;
     static final int STATE_MAIN = 1;
@@ -99,8 +105,8 @@
     };
 
     @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
 
         CatLog.d(LOG_TAG, "onCreate");
 
@@ -242,6 +248,12 @@
     public void onStop() {
         super.onStop();
         CatLog.d(LOG_TAG, "onStop, slot id: " + mSlotId + "," + mIsResponseSent + "," + mState);
+
+        // Nothing should be done here if this activity is being restarted now.
+        if (isChangingConfigurations()) {
+            return;
+        }
+
         //The menu should stay in background, if
         //1. the dialog is pop up in the screen, but the user does not response to the dialog.
         //2. the menu activity enters Stop state (e.g pressing HOME key) but mIsResponseSent is false.
@@ -280,8 +292,12 @@
         //isMenuPending: if input act is finish by stkappservice when OP_LAUNCH_APP again,
         //we can not send TR here, since the input cmd is waiting user to process.
         if (mState == STATE_SECONDARY && !mIsResponseSent && !appService.isMenuPending(mSlotId)) {
-            CatLog.d(LOG_TAG, "handleDestroy - Send End Session");
-            sendResponse(StkAppService.RES_ID_END_SESSION);
+            // Avoid sending the terminal response while the activty is being restarted
+            // due to some kind of configuration change.
+            if (!isChangingConfigurations()) {
+                CatLog.d(LOG_TAG, "handleDestroy - Send End Session");
+                sendResponse(StkAppService.RES_ID_END_SESSION);
+            }
         }
         LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcastReceiver);
     }
@@ -368,17 +384,18 @@
     @Override
     protected void onSaveInstanceState(Bundle outState) {
         CatLog.d(LOG_TAG, "onSaveInstanceState: " + mSlotId);
-        outState.putInt("STATE", mState);
-        outState.putParcelable("MENU", mStkMenu);
-        outState.putBoolean("ACCEPT_USERS_INPUT", mAcceptUsersInput);
+        outState.putInt(STATE_KEY, mState);
+        outState.putParcelable(MENU_KEY, mStkMenu);
+        outState.putBoolean(ACCEPT_USERS_INPUT_KEY, mAcceptUsersInput);
+        outState.putBoolean(RESPONSE_SENT_KEY, mIsResponseSent);
     }
 
     @Override
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
         CatLog.d(LOG_TAG, "onRestoreInstanceState: " + mSlotId);
-        mState = savedInstanceState.getInt("STATE");
-        mStkMenu = savedInstanceState.getParcelable("MENU");
-        mAcceptUsersInput = savedInstanceState.getBoolean("ACCEPT_USERS_INPUT");
+        mState = savedInstanceState.getInt(STATE_KEY);
+        mStkMenu = savedInstanceState.getParcelable(MENU_KEY);
+        mAcceptUsersInput = savedInstanceState.getBoolean(ACCEPT_USERS_INPUT_KEY);
         if (!mAcceptUsersInput) {
             // Check the latest information as the saved instance state can be outdated.
             if ((mState == STATE_MAIN) && appService.isMainMenuAvailable(mSlotId)) {
@@ -387,6 +404,7 @@
                 showProgressBar(true);
             }
         }
+        mIsResponseSent = savedInstanceState.getBoolean(RESPONSE_SENT_KEY);
     }
 
     private void cancelTimeOut() {