Merge "Fix for call timer resetting when starting IMS conference call. 3/4" into lmp-mr1-dev
diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml
index d4eaeba..78e3630 100644
--- a/res/xml/cdma_options.xml
+++ b/res/xml/cdma_options.xml
@@ -34,7 +34,7 @@
         android:dialogTitle="@string/cdma_subscription_dialogtitle" />
 
     <PreferenceScreen
-        android:key="button_apn_key"
+        android:key="button_apn_key_cdma"
         android:title="@string/apn_settings"
         android:persistent="false">
 
diff --git a/src/com/android/phone/CallNotifier.java b/src/com/android/phone/CallNotifier.java
index 3669544..bc0e584 100644
--- a/src/com/android/phone/CallNotifier.java
+++ b/src/com/android/phone/CallNotifier.java
@@ -163,7 +163,7 @@
                     BluetoothProfile.HEADSET);
         }
 
-        mSubscriptionManager.registerOnSubscriptionsChangedListener(
+        mSubscriptionManager.addOnSubscriptionsChangedListener(
                 new OnSubscriptionsChangedListener() {
                     @Override
                     public void onSubscriptionsChanged() {
diff --git a/src/com/android/phone/CdmaOptions.java b/src/com/android/phone/CdmaOptions.java
index 897c797..a04427e 100644
--- a/src/com/android/phone/CdmaOptions.java
+++ b/src/com/android/phone/CdmaOptions.java
@@ -46,7 +46,7 @@
     private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key";
     private static final String BUTTON_CDMA_ACTIVATE_DEVICE_KEY = "cdma_activate_device_key";
     private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
-    private static final String BUTTON_APN_EXPAND_KEY = "button_apn_key";
+    private static final String BUTTON_APN_EXPAND_KEY = "button_apn_key_cdma";
 
     private PreferenceActivity mPrefActivity;
     private PreferenceScreen mPrefScreen;
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index b60c963..d00ff9f 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -498,7 +498,8 @@
                     MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
         }
 
-        if (ImsManager.isVolteEnabledByPlatform(this)) {
+        if (ImsManager.isVolteEnabledByPlatform(this)
+                && ImsManager.isVolteProvisionedOnDevice(this)) {
             TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
             tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
         }
@@ -507,7 +508,7 @@
                 && ImsManager.isNonTtyOrTtyOnVolteEnabled(this));
         // NOTE: The button will be enabled/disabled in mPhoneStateListener
 
-        mSubscriptionManager.registerOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+        mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
 
         if (DBG) log("onResume:-");
 
@@ -656,7 +657,8 @@
         }
 
         // Enable enhanced 4G LTE mode settings depending on whether exists on platform
-        if (!ImsManager.isVolteEnabledByPlatform(this)) {
+        if (!(ImsManager.isVolteEnabledByPlatform(this)
+                && ImsManager.isVolteProvisionedOnDevice(this))) {
             Preference pref = prefSet.findPreference(BUTTON_4G_LTE_KEY);
             if (pref != null) {
                 prefSet.removePreference(pref);
@@ -733,13 +735,14 @@
         super.onPause();
         if (DBG) log("onPause:+");
 
-        if (ImsManager.isVolteEnabledByPlatform(this)) {
+        if (ImsManager.isVolteEnabledByPlatform(this)
+                && ImsManager.isVolteProvisionedOnDevice(this)) {
             TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
             tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
         }
 
         mSubscriptionManager
-            .unregisterOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+            .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
         if (DBG) log("onPause:-");
     }
 
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 1a5de74..24abf23 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -1574,14 +1574,19 @@
      */
     public boolean hasIccCard() {
         // FIXME Make changes to pass defaultSimId of type int
-        return hasIccCardUsingSlotId(getDefaultSubscription());
+        return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
     }
 
     /**
      * @return true if a ICC card is present for a slotId
      */
     public boolean hasIccCardUsingSlotId(int slotId) {
-        return getPhone(slotId).getIccCard().hasIccCard();
+        int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
+        if (subId != null) {
+            return getPhone(subId[0]).getIccCard().hasIccCard();
+        } else {
+            return false;
+        }
     }
 
     /**
diff --git a/src/com/android/phone/settings/fdn/EditFdnContactScreen.java b/src/com/android/phone/settings/fdn/EditFdnContactScreen.java
index 00bd9fc..a047b0b 100644
--- a/src/com/android/phone/settings/fdn/EditFdnContactScreen.java
+++ b/src/com/android/phone/settings/fdn/EditFdnContactScreen.java
@@ -208,6 +208,10 @@
             case MENU_DELETE:
                 deleteSelected();
                 return true;
+
+            case android.R.id.home:
+                onBackPressed();
+                return true;
         }
 
         return super.onOptionsItemSelected(item);
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 37e8147..6a533ef 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -229,7 +229,7 @@
 
         // Register for SubscriptionInfo list changes which is guaranteed
         // to invoke onSubscriptionsChanged the first time.
-        SubscriptionManager.from(mContext).registerOnSubscriptionsChangedListener(
+        SubscriptionManager.from(mContext).addOnSubscriptionsChangedListener(
                 mOnSubscriptionsChangedListener);
 
         // We also need to listen for changes to the service state (e.g. emergency -> in service)
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 946ee3e..1a2ef53 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -393,10 +393,6 @@
     }
 
     private Phone getPhoneForAccount(PhoneAccountHandle accountHandle, boolean isEmergency) {
-        if (isEmergency) {
-            return PhoneFactory.getDefaultPhone();
-        }
-
         if (Objects.equals(mExpectedComponentName, accountHandle.getComponentName())) {
             if (accountHandle.getId() != null) {
                 try {
@@ -408,9 +404,53 @@
                 }
             }
         }
+
+        if (isEmergency) {
+            // If this is an emergency number and we've been asked to dial it using a PhoneAccount
+            // which does not exist, then default to whatever subscription is available currently.
+            return getFirstPhoneForEmergencyCall();
+        }
+
         return null;
     }
 
+    private Phone getFirstPhoneForEmergencyCall() {
+        Phone selectPhone = null;
+        for (int i = 0; i < TelephonyManager.getDefault().getSimCount(); i++) {
+            int[] subIds = SubscriptionController.getInstance().getSubIdUsingSlotId(i);
+            if (subIds.length == 0)
+                continue;
+
+            int phoneId = SubscriptionController.getInstance().getPhoneId(subIds[0]);
+            Phone phone = PhoneFactory.getPhone(phoneId);
+            if (phone == null)
+                continue;
+
+            if (ServiceState.STATE_IN_SERVICE == phone.getServiceState().getState()) {
+                // the slot is radio on & state is in service
+                Log.d(this, "pickBestPhoneForEmergencyCall, radio on & in service, slotId:" + i);
+                return phone;
+            } else if (ServiceState.STATE_POWER_OFF != phone.getServiceState().getState()) {
+                // the slot is radio on & with SIM card inserted.
+                if (TelephonyManager.getDefault().hasIccCard(i)) {
+                    Log.d(this, "pickBestPhoneForEmergencyCall," +
+                            "radio on and SIM card inserted, slotId:" + i);
+                    selectPhone = phone;
+                } else if (selectPhone == null) {
+                    Log.d(this, "pickBestPhoneForEmergencyCall, radio on, slotId:" + i);
+                    selectPhone = phone;
+                }
+            }
+        }
+
+        if (selectPhone == null) {
+            Log.d(this, "pickBestPhoneForEmergencyCall, return default phone");
+            selectPhone = PhoneFactory.getDefaultPhone();
+        }
+
+        return selectPhone;
+    }
+
     /**
      * Determines if the connection should allow mute.
      *