Differentiate home screen from others implemented in the same activity am: b1cee23060
am: cbabbb0adf

Change-Id: I6a43d163380b577ee375a7672f891f630252683a
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index dea5820..4732c89 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -230,6 +230,7 @@
     static final int OP_ALPHA_NOTIFY = 11;
     static final int OP_IDLE_SCREEN = 12;
     static final int OP_SET_IMMED_DAL_INST = 13;
+    static final int OP_HOME_KEY_PRESSED = 14;
 
     //Invalid SetupEvent
     static final int INVALID_SETUP_EVENT = 0xFF;
@@ -304,6 +305,11 @@
     private static final long[] VIBRATION_PATTERN = new long[] { 0, 350, 250, 350 };
     private BroadcastReceiver mUserPresentReceiver = null;
 
+    // The reason based on Intent.ACTION_CLOSE_SYSTEM_DIALOGS.
+    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
+    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
+    private BroadcastReceiver mHomeKeyEventReceiver = null;
+
     @Override
     public void onCreate() {
         CatLog.d(LOG_TAG, "onCreate()+");
@@ -409,6 +415,7 @@
         unregisterUserActivityReceiver();
         unregisterProcessObserver();
         unregisterLocaleChangeReceiver();
+        unregisterHomeKeyEventReceiver();
         sInstance = null;
         waitForLooper();
         mServiceLooper.quit();
@@ -702,6 +709,14 @@
             case EVENT_MULTI_SIM_CONFIG_CHANGED:
                 handleMultiSimConfigChanged();
                 break;
+            case OP_HOME_KEY_PRESSED:
+                CatLog.d(LOG_TAG, "Process the home key pressed event");
+                for (int slot = 0; slot < mSimCount; slot++) {
+                    if (mStkContext[slot] != null) {
+                        handleHomeKeyPressed(slot);
+                    }
+                }
+                break;
             }
         }
 
@@ -809,36 +824,54 @@
         return false;
     }
 
-    private void startToObserveIdleScreen(int slotId) {
-        if (!mStkContext[slotId].mIsSessionFromUser) {
-            if (!isScreenIdle()) {
-                synchronized (this) {
-                    if (mProcessObserver == null && !mServiceHandler.hasMessages(OP_IDLE_SCREEN)) {
-                        registerProcessObserver();
-                    }
+    private synchronized void startToObserveHomeKeyEvent(int slotId) {
+        if (mStkContext[slotId].mIsSessionFromUser || mHomeKeyEventReceiver != null) {
+            return;
+        }
+        mHomeKeyEventReceiver = new BroadcastReceiver() {
+            @Override public void onReceive(Context context, Intent intent) {
+                if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(
+                        intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY))) {
+                    Message message = mServiceHandler.obtainMessage();
+                    message.arg1 = OP_HOME_KEY_PRESSED;
+                    mServiceHandler.sendMessage(message);
                 }
-            } else {
-                handleIdleScreen(slotId);
+            }
+        };
+        CatLog.d(LOG_TAG, "Started to observe home key event");
+        registerReceiver(mHomeKeyEventReceiver,
+                new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
+    }
+
+    private synchronized void unregisterHomeKeyEventReceiver() {
+        if (mHomeKeyEventReceiver != null) {
+            CatLog.d(LOG_TAG, "Stopped to observe home key event");
+            unregisterReceiver(mHomeKeyEventReceiver);
+            mHomeKeyEventReceiver = null;
+        }
+        if (mServiceHandler != null) {
+            mServiceHandler.removeMessages(OP_HOME_KEY_PRESSED);
+        }
+    }
+
+    private void handleHomeKeyPressed(int slotId) {
+        // It might be hard for user to recognize that the dialog or screens belong to SIM Toolkit
+        // application if the current session was not initiated by user but by the SIM card,
+        // so it is recommended to send TERMINAL RESPONSE if user press the home key.
+        if (!mStkContext[slotId].mIsSessionFromUser) {
+            Activity dialog = mStkContext[slotId].getPendingDialogInstance();
+            Activity activity = mStkContext[slotId].getPendingActivityInstance();
+            if (dialog != null) {
+                dialog.finish();
+                mStkContext[slotId].mDialogInstance = null;
+            } else if (activity != null) {
+                activity.finish();
+                mStkContext[slotId].mActivityInstance = null;
             }
         }
     }
 
     private void handleIdleScreen(int slotId) {
-        // It might be hard for user to recognize that the dialog or screens belong to SIM Toolkit
-        // application if the current session was not initiated by user but by the SIM card,
-        // so it is recommended to send TERMINAL RESPONSE if user goes to the idle screen.
-        if (!mStkContext[slotId].mIsSessionFromUser) {
-            Activity dialog = mStkContext[slotId].getPendingDialogInstance();
-            if (dialog != null) {
-                dialog.finish();
-                mStkContext[slotId].mDialogInstance = null;
-            }
-            Activity activity = mStkContext[slotId].getPendingActivityInstance();
-            if (activity != null) {
-                activity.finish();
-                mStkContext[slotId].mActivityInstance = null;
-            }
-        }
         // If the idle screen event is present in the list need to send the
         // response to SIM.
         CatLog.d(this, "Need to send IDLE SCREEN Available event to SIM");
@@ -1241,7 +1274,7 @@
     @SuppressWarnings("FallThrough")
     private void handleCmdResponse(Bundle args, int slotId) {
         CatLog.d(LOG_TAG, "handleCmdResponse, sim id: " + slotId);
-        unregisterProcessObserver();
+        unregisterHomeKeyEventReceiver();
         if (mStkContext[slotId].mCurrentCmd == null) {
             return;
         }
@@ -1550,7 +1583,7 @@
             mStkContext[slotId].mMenuState = StkMenuActivity.STATE_SECONDARY;
         }
         if (mStkContext[slotId].mMenuState == StkMenuActivity.STATE_SECONDARY) {
-            startToObserveIdleScreen(slotId);
+            startToObserveHomeKeyEvent(slotId);
         }
         newIntent.putExtra(SLOT_ID, slotId);
         newIntent.setData(uriData);
@@ -1578,7 +1611,7 @@
             notifyUserIfNecessary(slotId, input.text);
         }
         startActivity(newIntent);
-        startToObserveIdleScreen(slotId);
+        startToObserveHomeKeyEvent(slotId);
     }
 
     private void launchTextDialog(int slotId) {
@@ -1608,7 +1641,7 @@
         if (!mStkContext[slotId].mCurrentCmd.geTextMessage().responseNeeded) {
             sendResponse(RES_ID_CONFIRM, slotId, true);
         } else {
-            startToObserveIdleScreen(slotId);
+            startToObserveHomeKeyEvent(slotId);
         }
     }