MSIM: Add MSIM support to ECBM implementation

Make use of phoneid details in ECBM intent to display ECBM dialog
properly for appropriate phone object.

Change-Id: I7af9f4821b3590dbb080d5ab5e591c3a38e5a389
diff --git a/src/com/android/phone/EmergencyCallbackModeExitDialog.java b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
index 7f4bd1b..b423e14 100644
--- a/src/com/android/phone/EmergencyCallbackModeExitDialog.java
+++ b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
@@ -78,11 +78,12 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        mPhone = PhoneGlobals.getInstance().getPhoneInEcm();
         // Check if phone is in Emergency Callback Mode. If not, exit.
         final boolean isInEcm = Boolean.parseBoolean(
                 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE));
-        Log.i(TAG, "ECMModeExitDialog launched - isInEcm: " + isInEcm);
-        if (!isInEcm) {
+        Log.i(TAG, "ECMModeExitDialog launched - isInEcm: " + isInEcm + " phone:" + mPhone);
+        if (mPhone == null || !isInEcm) {
             finish();
             return;
         }
@@ -96,7 +97,6 @@
         waitForConnectionCompleteThread.start();
 
         // Register ECM timer reset notfication
-        mPhone = PhoneGlobals.getPhone();
         mPhone.registerForEcmTimerReset(mTimerResetHandler, ECM_TIMER_RESET, null);
 
         // Register receiver for intent closing the dialog
diff --git a/src/com/android/phone/EmergencyCallbackModeService.java b/src/com/android/phone/EmergencyCallbackModeService.java
index f06ae0c..ca25653 100644
--- a/src/com/android/phone/EmergencyCallbackModeService.java
+++ b/src/com/android/phone/EmergencyCallbackModeService.java
@@ -73,12 +73,13 @@
 
     @Override
     public void onCreate() {
+         Phone phoneInEcm = PhoneGlobals.getInstance().getPhoneInEcm();
         // Check if it is CDMA phone
-        if ((PhoneFactory.getDefaultPhone().getPhoneType() != PhoneConstants.PHONE_TYPE_CDMA)
-                && (PhoneFactory.getDefaultPhone().getImsPhone() == null)) {
-            Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " +
-                    PhoneFactory.getDefaultPhone().getPhoneName() + " phones");
+        if (phoneInEcm == null || ((phoneInEcm.getPhoneType() != PhoneConstants.PHONE_TYPE_CDMA)
+                && (phoneInEcm.getImsPhone() == null))) {
+            Log.e(LOG_TAG, "Error! Emergency Callback Mode not supported for " + phoneInEcm);
             stopSelf();
+            return;
         }
 
         // Register receiver for intents
@@ -90,7 +91,7 @@
         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 
         // Register ECM timer reset notfication
-        mPhone = PhoneFactory.getDefaultPhone();
+        mPhone = phoneInEcm;
         mPhone.registerForEcmTimerReset(mHandler, ECM_TIMER_RESET, null);
 
         startTimerNotification();
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index e9f4c25..6823927 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -130,6 +130,7 @@
     CarrierConfigLoader configLoader;
 
     private CallGatewayManager callGatewayManager;
+    private Phone phoneInEcm;
 
     static boolean sVoiceCapable = true;
 
@@ -699,19 +700,28 @@
             } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) {
                 handleServiceStateChanged(intent);
             } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
-                if (TelephonyCapabilities.supportsEcm(mCM.getFgPhone())) {
-                    Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
-                    // Start Emergency Callback Mode service
-                    if (intent.getBooleanExtra("phoneinECMState", false)) {
-                        context.startService(new Intent(context,
-                                EmergencyCallbackModeService.class));
+                int phoneId = intent.getIntExtra(PhoneConstants.PHONE_KEY, 0);
+                phoneInEcm = getPhone(phoneId);
+                Log.d(LOG_TAG, "Emergency Callback Mode. phoneId:" + phoneId);
+                if (phoneInEcm != null) {
+                    if (TelephonyCapabilities.supportsEcm(phoneInEcm)) {
+                        Log.d(LOG_TAG, "Emergency Callback Mode arrived in PhoneApp.");
+                        // Start Emergency Callback Mode service
+                        if (intent.getBooleanExtra("phoneinECMState", false)) {
+                            context.startService(new Intent(context,
+                                    EmergencyCallbackModeService.class));
+                        } else {
+                            phoneInEcm = null;
+                        }
+                    } else {
+                        // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
+                        // on a device that doesn't support ECM in the first place.
+                        Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, but "
+                                + "ECM isn't supported for phone: " + phoneInEcm.getPhoneName());
+                        phoneInEcm = null;
                     }
                 } else {
-                    // It doesn't make sense to get ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
-                    // on a device that doesn't support ECM in the first place.
-                    Log.e(LOG_TAG, "Got ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, "
-                            + "but ECM isn't supported for phone: "
-                            + mCM.getFgPhone().getPhoneName());
+                    Log.w(LOG_TAG, "phoneInEcm is null.");
                 }
             }
         }
@@ -754,6 +764,10 @@
         }
     }
 
+    public Phone getPhoneInEcm() {
+        return phoneInEcm;
+    }
+
     /**
      * Triggers a refresh of the message waiting (voicemail) indicator.
      *