Stk: Support for modem re-start and ICC refresh

Uninstall the STK app, stop StkAppService and remove the idle text
when StkAppService receives the CARD_ABSENT/CARD_ERROR status.

IdleModeText and SETUP_MENU need to be cleared when a card refresh
occurs.

Bug: 12161291
Change-Id: I626ff5196270377d02c570703f46c5b272a115fd
(cherry picked from commit f48e483b46cfec93edc70fa9e39c8a7120b864f8)
(cherry picked from commit 8390bbbbfa8e170e81e8a99ab997fb1406d8e81a)
(cherry picked from commit dc0f924d464dbc093f1168f0e9a8bca8b0aea73c)
diff --git a/src/com/android/stk/StkAppService.java b/src/com/android/stk/StkAppService.java
index d98c14b..4699e04 100644
--- a/src/com/android/stk/StkAppService.java
+++ b/src/com/android/stk/StkAppService.java
@@ -53,6 +53,8 @@
 import com.android.internal.telephony.cat.CatLog;
 import com.android.internal.telephony.cat.CatResponseMessage;
 import com.android.internal.telephony.cat.TextMessage;
+import com.android.internal.telephony.uicc.IccRefreshResponse;
+import com.android.internal.telephony.uicc.IccCardStatus.CardState;
 
 import java.util.LinkedList;
 
@@ -105,6 +107,7 @@
     static final int OP_END_SESSION = 4;
     static final int OP_BOOT_COMPLETED = 5;
     private static final int OP_DELAYED_MSG = 6;
+    static final int OP_CARD_STATUS_CHANGED = 7;
 
     // Response ids
     static final int RES_ID_MENU_SELECTION = 11;
@@ -188,6 +191,7 @@
             msg.obj = args.getParcelable(CMD_MSG);
             break;
         case OP_RESPONSE:
+        case OP_CARD_STATUS_CHANGED:
             msg.obj = args;
             /* falls through */
         case OP_LAUNCH_APP:
@@ -318,6 +322,40 @@
             case OP_DELAYED_MSG:
                 handleDelayedCmd();
                 break;
+            case OP_CARD_STATUS_CHANGED:
+                CatLog.d(this, "Card/Icc Status change received");
+                handleCardStatusChangeAndIccRefresh((Bundle) msg.obj);
+                break;
+            }
+        }
+
+        private void handleCardStatusChangeAndIccRefresh(Bundle args) {
+            boolean cardStatus = args.getBoolean(AppInterface.CARD_STATUS);
+
+            CatLog.d(this, "CardStatus: " + cardStatus);
+            if (cardStatus == false) {
+                CatLog.d(this, "CARD is ABSENT");
+                // Uninstall STKAPP, Clear Idle text, Stop StkAppService
+                StkAppInstaller.unInstall(mContext);
+                mNotificationManager.cancel(STK_NOTIFICATION_ID);
+                stopSelf();
+            } else {
+                IccRefreshResponse state = new IccRefreshResponse();
+                state.refreshResult = args.getInt(AppInterface.REFRESH_RESULT);
+
+                CatLog.d(this, "Icc Refresh Result: "+ state.refreshResult);
+                if ((state.refreshResult == IccRefreshResponse.REFRESH_RESULT_INIT) ||
+                    (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET)) {
+                    // Clear Idle Text
+                    mNotificationManager.cancel(STK_NOTIFICATION_ID);
+                }
+
+                if (state.refreshResult == IccRefreshResponse.REFRESH_RESULT_RESET) {
+                    // Uninstall STkmenu
+                    StkAppInstaller.unInstall(mContext);
+                    mCurrentMenu = null;
+                    mMainCmd = null;
+                }
             }
         }
     }