Merge "Fixed an issue were switching data roaming would disable other settings." into lmp-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0282c76..b78876f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1218,4 +1218,8 @@
     <!-- Label for close button in dialog, for video calling setting. -->
     <string name="enable_video_calling_dialog_close">Close</string>
 
+    <!-- Strings used in Settings->Sim cards for each installed Sim. -->
+    <string name="sim_label_emergency_calls">Emergency calls</string>
+    <string name="sim_description_emergency_calls">Emergency calling only</string>
+    <string name="sim_description_default">SIM card in slot <xliff:g id="slot_id">%d</xliff:g></string>
 </resources>
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 99cb206..5af8fcb 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -714,17 +714,17 @@
     private Phone getPhone(long subId) {
         // FIXME: hack for the moment
         return mPhone;
-        // return PhoneUtils.getPhoneUsingSubId(subId);
+        // return PhoneUtils.getPhoneForSubscriber(subId);
     }
     //
     // Implementation of the ITelephony interface.
     //
 
     public void dial(String number) {
-        dialUsingSubId(getPreferredVoiceSubscription(), number);
+        dialForSubscriber(getPreferredVoiceSubscription(), number);
     }
 
-    public void dialUsingSubId(long subId, String number) {
+    public void dialForSubscriber(long subId, String number) {
         if (DBG) log("dial: " + number);
         // No permission check needed here: This is just a wrapper around the
         // ACTION_DIAL intent, which is available to any app since it puts up
@@ -746,10 +746,10 @@
     }
 
     public void call(String callingPackage, String number) {
-        callUsingSubId(getPreferredVoiceSubscription(), callingPackage, number);
+        callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
     }
 
-    public void callUsingSubId(long subId, String callingPackage, String number) {
+    public void callForSubscriber(long subId, String callingPackage, String number) {
         if (DBG) log("call: " + number);
 
         // This is just a wrapper around the ACTION_CALL intent, but we still
@@ -778,23 +778,23 @@
      * @return true is a call was ended
      */
     public boolean endCall() {
-        return endCallUsingSubId(getDefaultSubscription());
+        return endCallForSubscriber(getDefaultSubscription());
     }
 
     /**
      * End a call based on the call state of the subId
      * @return true is a call was ended
      */
-    public boolean endCallUsingSubId(long subId) {
+    public boolean endCallForSubscriber(long subId) {
         enforceCallPermission();
         return (Boolean) sendRequest(CMD_END_CALL, subId, null);
     }
 
     public void answerRingingCall() {
-        answerRingingCallUsingSubId(getDefaultSubscription());
+        answerRingingCallForSubscriber(getDefaultSubscription());
     }
 
-    public void answerRingingCallUsingSubId(long subId) {
+    public void answerRingingCallForSubscriber(long subId) {
         if (DBG) log("answerRingingCall...");
         // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
         // but that can probably wait till the big TelephonyManager API overhaul.
@@ -865,26 +865,26 @@
     }
 
     public boolean isOffhook() {
-        return isOffhookUsingSubId(getDefaultSubscription());
+        return isOffhookForSubscriber(getDefaultSubscription());
     }
 
-    public boolean isOffhookUsingSubId(long subId) {
+    public boolean isOffhookForSubscriber(long subId) {
         return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
     }
 
     public boolean isRinging() {
-        return (isRingingUsingSubId(getDefaultSubscription()));
+        return (isRingingForSubscriber(getDefaultSubscription()));
     }
 
-    public boolean isRingingUsingSubId(long subId) {
+    public boolean isRingingForSubscriber(long subId) {
         return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
     }
 
     public boolean isIdle() {
-        return isIdleUsingSubId(getDefaultSubscription());
+        return isIdleForSubscriber(getDefaultSubscription());
     }
 
-    public boolean isIdleUsingSubId(long subId) {
+    public boolean isIdleForSubscriber(long subId) {
         return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
     }
 
@@ -894,29 +894,29 @@
     }
 
     public boolean supplyPin(String pin) {
-        return supplyPinUsingSubId(getDefaultSubscription(), pin);
+        return supplyPinForSubscriber(getDefaultSubscription(), pin);
     }
 
-    public boolean supplyPinUsingSubId(long subId, String pin) {
-        int [] resultArray = supplyPinReportResultUsingSubId(subId, pin);
+    public boolean supplyPinForSubscriber(long subId, String pin) {
+        int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
         return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
     }
 
     public boolean supplyPuk(String puk, String pin) {
-        return supplyPukUsingSubId(getDefaultSubscription(), puk, pin);
+        return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
     }
 
-    public boolean supplyPukUsingSubId(long subId, String puk, String pin) {
-        int [] resultArray = supplyPukReportResultUsingSubId(subId, puk, pin);
+    public boolean supplyPukForSubscriber(long subId, String puk, String pin) {
+        int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
         return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
     }
 
     /** {@hide} */
     public int[] supplyPinReportResult(String pin) {
-        return supplyPinReportResultUsingSubId(getDefaultSubscription(), pin);
+        return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
     }
 
-    public int[] supplyPinReportResultUsingSubId(long subId, String pin) {
+    public int[] supplyPinReportResultForSubscriber(long subId, String pin) {
         enforceModifyPermission();
         final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
         checkSimPin.start();
@@ -925,10 +925,10 @@
 
     /** {@hide} */
     public int[] supplyPukReportResult(String puk, String pin) {
-        return supplyPukReportResultUsingSubId(getDefaultSubscription(), puk, pin);
+        return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
     }
 
-    public int[] supplyPukReportResultUsingSubId(long subId, String puk, String pin) {
+    public int[] supplyPukReportResultForSubscriber(long subId, String puk, String pin) {
         enforceModifyPermission();
         final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
         checkSimPuk.start();
@@ -1035,11 +1035,11 @@
     }
 
     public void updateServiceLocation() {
-        updateServiceLocationUsingSubId(getDefaultSubscription());
+        updateServiceLocationForSubscriber(getDefaultSubscription());
 
     }
 
-    public void updateServiceLocationUsingSubId(long subId) {
+    public void updateServiceLocationForSubscriber(long subId) {
         // No permission check needed here: this call is harmless, and it's
         // needed for the ServiceState.requestStateUpdate() call (which is
         // already intentionally exposed to 3rd parties.)
@@ -1047,32 +1047,32 @@
     }
 
     public boolean isRadioOn() {
-        return isRadioOnUsingSubId(getDefaultSubscription());
+        return isRadioOnForSubscriber(getDefaultSubscription());
     }
 
-    public boolean isRadioOnUsingSubId(long subId) {
+    public boolean isRadioOnForSubscriber(long subId) {
         return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
     }
 
     public void toggleRadioOnOff() {
-        toggleRadioOnOffUsingSubId(getDefaultSubscription());
+        toggleRadioOnOffForSubscriber(getDefaultSubscription());
 
     }
 
-    public void toggleRadioOnOffUsingSubId(long subId) {
+    public void toggleRadioOnOffForSubscriber(long subId) {
         enforceModifyPermission();
-        getPhone(subId).setRadioPower(!isRadioOnUsingSubId(subId));
+        getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
     }
 
     public boolean setRadio(boolean turnOn) {
-        return setRadioUsingSubId(getDefaultSubscription(), turnOn);
+        return setRadioForSubscriber(getDefaultSubscription(), turnOn);
     }
 
-    public boolean setRadioUsingSubId(long subId, boolean turnOn) {
+    public boolean setRadioForSubscriber(long subId, boolean turnOn) {
         enforceModifyPermission();
         if ((getPhone(subId).getServiceState().getState() !=
                 ServiceState.STATE_POWER_OFF) != turnOn) {
-            toggleRadioOnOffUsingSubId(subId);
+            toggleRadioOnOffForSubscriber(subId);
         }
         return true;
     }
@@ -1106,10 +1106,10 @@
     }
 
     public boolean setRadioPower(boolean turnOn) {
-        return setRadioPowerUsingSubId(getDefaultSubscription(), turnOn);
+        return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
     }
 
-    public boolean setRadioPowerUsingSubId(long subId, boolean turnOn) {
+    public boolean setRadioPowerForSubscriber(long subId, boolean turnOn) {
         enforceModifyPermission();
         getPhone(subId).setRadioPower(turnOn);
         return true;
@@ -1138,19 +1138,19 @@
     }
 
     public boolean handlePinMmi(String dialString) {
-        return handlePinMmiUsingSubId(getDefaultSubscription(), dialString);
+        return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
     }
 
-    public boolean handlePinMmiUsingSubId(long subId, String dialString) {
+    public boolean handlePinMmiForSubscriber(long subId, String dialString) {
         enforceModifyPermission();
         return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
     }
 
     public int getCallState() {
-        return getCallStateUsingSubId(getDefaultSubscription());
+        return getCallStateForSubscriber(getDefaultSubscription());
     }
 
-    public int getCallStateUsingSubId(long subId) {
+    public int getCallStateForSubscriber(long subId) {
         return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
     }
 
@@ -1190,10 +1190,10 @@
 
     @Override
     public void enableLocationUpdates() {
-        enableLocationUpdatesUsingSubId(getDefaultSubscription());
+        enableLocationUpdatesForSubscriber(getDefaultSubscription());
     }
 
-    public void enableLocationUpdatesUsingSubId(long subId) {
+    public void enableLocationUpdatesForSubscriber(long subId) {
         mApp.enforceCallingOrSelfPermission(
                 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
         getPhone(subId).enableLocationUpdates();
@@ -1201,10 +1201,10 @@
 
     @Override
     public void disableLocationUpdates() {
-        disableLocationUpdatesUsingSubId(getDefaultSubscription());
+        disableLocationUpdatesForSubscriber(getDefaultSubscription());
     }
 
-    public void disableLocationUpdatesUsingSubId(long subId) {
+    public void disableLocationUpdatesForSubscriber(long subId) {
         mApp.enforceCallingOrSelfPermission(
                 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
         getPhone(subId).disableLocationUpdates();
@@ -1400,10 +1400,10 @@
     }
 
     public int getActivePhoneType() {
-        return getActivePhoneTypeUsingSubId(getDefaultSubscription());
+        return getActivePhoneTypeForSubscriber(getDefaultSubscription());
     }
 
-    public int getActivePhoneTypeUsingSubId(long subId) {
+    public int getActivePhoneTypeForSubscriber(long subId) {
         return getPhone(subId).getPhoneType();
     }
 
@@ -1411,11 +1411,11 @@
      * Returns the CDMA ERI icon index to display
      */
     public int getCdmaEriIconIndex() {
-        return getCdmaEriIconIndexUsingSubId(getDefaultSubscription());
+        return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
 
     }
 
-    public int getCdmaEriIconIndexUsingSubId(long subId) {
+    public int getCdmaEriIconIndexForSubscriber(long subId) {
         return getPhone(subId).getCdmaEriIconIndex();
     }
 
@@ -1425,10 +1425,10 @@
      * 1 - FLASHING
      */
     public int getCdmaEriIconMode() {
-        return getCdmaEriIconModeUsingSubId(getDefaultSubscription());
+        return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
     }
 
-    public int getCdmaEriIconModeUsingSubId(long subId) {
+    public int getCdmaEriIconModeForSubscriber(long subId) {
         return getPhone(subId).getCdmaEriIconMode();
     }
 
@@ -1436,10 +1436,10 @@
      * Returns the CDMA ERI text,
      */
     public String getCdmaEriText() {
-        return getCdmaEriTextUsingSubId(getDefaultSubscription());
+        return getCdmaEriTextForSubscriber(getDefaultSubscription());
     }
 
-    public String getCdmaEriTextUsingSubId(long subId) {
+    public String getCdmaEriTextForSubscriber(long subId) {
         return getPhone(subId).getCdmaEriText();
     }
 
@@ -1478,13 +1478,13 @@
      * Returns the unread count of voicemails
      */
     public int getVoiceMessageCount() {
-        return getVoiceMessageCountUsingSubId(getDefaultSubscription());
+        return getVoiceMessageCountForSubscriber(getDefaultSubscription());
     }
 
     /**
      * Returns the unread count of voicemails for a subId
      */
-    public int getVoiceMessageCountUsingSubId( long subId) {
+    public int getVoiceMessageCountForSubscriber( long subId) {
         return getPhone(subId).getVoiceMessageCount();
     }
 
@@ -1495,14 +1495,14 @@
      */
     @Override
     public int getNetworkType() {
-        return getNetworkTypeUsingSubId(getDefaultSubscription());
+        return getNetworkTypeForSubscriber(getDefaultSubscription());
     }
 
     /**
      * Returns the network type for a subId
      */
     @Override
-    public int getNetworkTypeUsingSubId(long subId) {
+    public int getNetworkTypeForSubscriber(long subId) {
         return getPhone(subId).getServiceState().getDataNetworkType();
     }
 
@@ -1511,14 +1511,14 @@
      */
     @Override
     public int getDataNetworkType() {
-        return getDataNetworkTypeUsingSubId(getDefaultSubscription());
+        return getDataNetworkTypeForSubscriber(getDefaultSubscription());
     }
 
     /**
      * Returns the data network type for a subId
      */
     @Override
-    public int getDataNetworkTypeUsingSubId(long subId) {
+    public int getDataNetworkTypeForSubscriber(long subId) {
         return getPhone(subId).getServiceState().getDataNetworkType();
     }
 
@@ -1527,14 +1527,14 @@
      */
     @Override
     public int getVoiceNetworkType() {
-        return getVoiceNetworkTypeUsingSubId(getDefaultSubscription());
+        return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
     }
 
     /**
      * Returns the Voice network type for a subId
      */
     @Override
-    public int getVoiceNetworkTypeUsingSubId(long subId) {
+    public int getVoiceNetworkTypeForSubscriber(long subId) {
         return getPhone(subId).getServiceState().getVoiceNetworkType();
     }
 
@@ -1562,10 +1562,10 @@
      * or {@link Phone#LTE_ON_CDMA_TRUE}
      */
     public int getLteOnCdmaMode() {
-        return getLteOnCdmaModeUsingSubId(getDefaultSubscription());
+        return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
     }
 
-    public int getLteOnCdmaModeUsingSubId(long subId) {
+    public int getLteOnCdmaModeForSubscriber(long subId) {
         return getPhone(subId).getLteOnCdmaMode();
     }
 
diff --git a/src/com/android/services/telephony/TelecommAccountRegistry.java b/src/com/android/services/telephony/TelecommAccountRegistry.java
index 0aba6b4..a8a7d25 100644
--- a/src/com/android/services/telephony/TelecommAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecommAccountRegistry.java
@@ -25,13 +25,16 @@
 import android.telecomm.PhoneAccount;
 import android.telecomm.PhoneAccountHandle;
 import android.telecomm.TelecommManager;
+import android.telephony.SubInfoRecord;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.text.TextUtils;
 
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.PhoneProxy;
 import com.android.internal.telephony.TelephonyIntents;
+import com.android.phone.R;
 
 import java.util.Arrays;
 import java.util.LinkedList;
@@ -43,11 +46,11 @@
  */
 final class TelecommAccountRegistry {
     private final static int[] phoneAccountIcons = {
-        com.android.phone.R.drawable.ic_multi_sim,
-        com.android.phone.R.drawable.ic_multi_sim1,
-        com.android.phone.R.drawable.ic_multi_sim2,
-        com.android.phone.R.drawable.ic_multi_sim3,
-        com.android.phone.R.drawable.ic_multi_sim4
+            R.drawable.ic_multi_sim,
+            R.drawable.ic_multi_sim1,
+            R.drawable.ic_multi_sim2,
+            R.drawable.ic_multi_sim3,
+            R.drawable.ic_multi_sim4
     };
 
     private final class AccountEntry {
@@ -90,12 +93,30 @@
             if (subNumber == null) {
                 subNumber = "";
             }
-            String label = isEmergency
-                    ? "Emergency calls"
-                    : dummyPrefix + "SIM " + slotId;
-            String description = isEmergency
-                    ? "Emergency calling only"
-                    : dummyPrefix + "SIM card in slot " + slotId;
+
+            String subDisplayName = null;
+            SubInfoRecord record = SubscriptionManager.getSubInfoForSubscriber(subId);
+            if (record != null) {
+                subDisplayName = record.displayName;
+            }
+
+            if (TextUtils.isEmpty(subDisplayName)) {
+                // Either the sub record is not there or it has an empty display name.
+                Log.w(this, "Could not get a display name for subid: %d", subId);
+                subDisplayName = mContext.getResources().getString(
+                        R.string.sim_description_default, slotId);
+            }
+
+            // The label is user-visible so let's use the display name that the user may
+            // have set in Settings->Sim cards.
+            String label = isEmergency ?
+                    mContext.getResources().getString(R.string.sim_label_emergency_calls) :
+                    dummyPrefix + subDisplayName;
+            String description = isEmergency ?
+                    mContext.getResources().getString(R.string.sim_description_emergency_calls) :
+                    dummyPrefix + mContext.getResources().getString(
+                            R.string.sim_description_default, slotId);
+
             PhoneAccount account = PhoneAccount.builder(phoneAccountHandle, label)
                     .setAddress(Uri.fromParts(PhoneAccount.SCHEME_TEL, line1Number, null))
                     .setSubscriptionAddress(
@@ -117,6 +138,7 @@
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
+            boolean rebuildAccounts = false;
             String action = intent.getAction();
             if (TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED.equals(action)) {
                 int status = intent.getIntExtra(
@@ -126,6 +148,15 @@
                 // Anytime the SIM state changes...rerun the setup
                 // We rely on this notification even when the status is EXTRA_VALUE_NOCHANGE,
                 // so we explicitly do not check for that here.
+                rebuildAccounts = true;
+            } else if (TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE.equals(action)) {
+                String columnName = intent.getStringExtra(TelephonyIntents.EXTRA_COLUMN_NAME);
+                String stringContent = intent.getStringExtra(TelephonyIntents.EXTRA_STRING_CONTENT);
+                Log.v(this, "SUBINFO_CONTENT_CHANGE: Column: %s Content: %s",
+                        columnName, stringContent);
+                rebuildAccounts = true;
+            }
+            if (rebuildAccounts) {
                 tearDownAccounts();
                 setupAccounts();
             }
@@ -153,10 +184,12 @@
      * Sets up all the phone accounts for SIMs on first boot.
      */
     void setupOnBoot() {
-        IntentFilter intentFilter =
-            new IntentFilter(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);
+        // We need to register for both types of intents if we want to see added/removed Subs
+        // along with changes to a given Sub.
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);
+        intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE);
         mContext.registerReceiver(mReceiver, intentFilter);
-
         setupAccounts();
     }
 
@@ -212,8 +245,6 @@
         if (phones.length > 0 && "TRUE".equals(System.getProperty("dummy_sim"))) {
             mAccounts.add(new AccountEntry(phones[0], false /* emergency */, true /* isDummy */));
         }
-
-        // TODO: Add SIP accounts.
     }
 
     private int getPhoneAccountIcon(int index) {