Merge "Fix NPE in Call Forwarding Menu"
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 78e647a..bd0ae0f 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -720,13 +720,6 @@
             int max = mSubscriptionManager.getActiveSubscriptionInfoCountMax();
             mActiveSubInfos = new ArrayList<SubscriptionInfo>(max);
 
-            IntentFilter intentFilter = new IntentFilter(
-                    TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
-            activity.registerReceiver(mPhoneChangeReceiver, intentFilter);
-
-            activity.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
-                    mDpcEnforcedContentObserver);
-
             Log.i(LOG_TAG, "onCreate:-");
         }
 
@@ -753,6 +746,10 @@
             @Override
             public void onReceive(Context context, Intent intent) {
                 Log.i(LOG_TAG, "onReceive:");
+                if (getActivity() == null || getContext() == null) {
+                    // Received broadcast and activity is in the process of being torn down.
+                    return;
+                }
                 // When the radio changes (ex: CDMA->GSM), refresh all options.
                 updateBody();
             }
@@ -766,6 +763,10 @@
             @Override
             public void onChange(boolean selfChange) {
                 Log.i(LOG_TAG, "DPC enforced onChange:");
+                if (getActivity() == null || getContext() == null) {
+                    // Received content change and activity is in the process of being torn down.
+                    return;
+                }
                 updateBody();
             }
         }
@@ -774,11 +775,6 @@
         public void onDestroy() {
             unbindNetworkQueryService();
             super.onDestroy();
-            if (getActivity() != null) {
-                getActivity().unregisterReceiver(mPhoneChangeReceiver);
-                getActivity().getContentResolver().unregisterContentObserver(
-                        mDpcEnforcedContentObserver);
-            }
         }
 
         @Override
@@ -815,6 +811,13 @@
 
             mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
 
+            final Context context = getActivity();
+            IntentFilter intentFilter = new IntentFilter(
+                    TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
+            context.registerReceiver(mPhoneChangeReceiver, intentFilter);
+            context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
+                    mDpcEnforcedContentObserver);
+
             Log.i(LOG_TAG, "onResume:-");
 
         }
@@ -1151,6 +1154,10 @@
 
             mSubscriptionManager
                     .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+
+            final Context context = getActivity();
+            context.unregisterReceiver(mPhoneChangeReceiver);
+            context.getContentResolver().unregisterContentObserver(mDpcEnforcedContentObserver);
             if (DBG) log("onPause:-");
         }
 
diff --git a/src/com/android/phone/NetworkSelectListPreference.java b/src/com/android/phone/NetworkSelectListPreference.java
index 3ba1512..2a55839 100644
--- a/src/com/android/phone/NetworkSelectListPreference.java
+++ b/src/com/android/phone/NetworkSelectListPreference.java
@@ -227,7 +227,7 @@
         TelephonyManager telephonyManager = (TelephonyManager)
                 getContext().getSystemService(Context.TELEPHONY_SERVICE);
 
-        setSummary(telephonyManager.getNetworkOperatorName());
+        setSummary(telephonyManager.getNetworkOperatorName(mSubId));
 
         setOnPreferenceChangeListener(this);
     }
diff --git a/src/com/android/phone/TelephonyShellCommand.java b/src/com/android/phone/TelephonyShellCommand.java
index fce1086..4acb46b 100644
--- a/src/com/android/phone/TelephonyShellCommand.java
+++ b/src/com/android/phone/TelephonyShellCommand.java
@@ -36,6 +36,7 @@
     private static final String LOG_TAG = "TelephonyShellCommand";
     // Don't commit with this true.
     private static final boolean VDBG = true;
+    private static final int DEFAULT_PHONE_ID = 0;
 
     private static final String IMS_SUBCOMMAND = "ims";
     private static final String IMS_SET_CARRIER_SERVICE = "set-ims-service";
@@ -130,7 +131,7 @@
     // ims set-ims-service
     private int handleImsSetServiceCommand() {
         PrintWriter errPw = getErrPrintWriter();
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         Boolean isCarrierService = null;
 
         String opt;
@@ -186,7 +187,7 @@
     // ims get-ims-service
     private int handleImsGetServiceCommand() {
         PrintWriter errPw = getErrPrintWriter();
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         Boolean isCarrierService = null;
 
         String opt;
@@ -232,7 +233,7 @@
     }
 
     private int handleEnableIms() {
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         String opt;
         while ((opt = getNextOption()) != null) {
             switch (opt) {
@@ -259,7 +260,7 @@
     }
 
     private int handleDisableIms() {
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         String opt;
         while ((opt = getNextOption()) != null) {
             switch (opt) {
@@ -285,4 +286,14 @@
         }
         return 0;
     }
+
+    private int getDefaultSlot() {
+        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX
+                || slotId == SubscriptionManager.DEFAULT_PHONE_INDEX) {
+            // If there is no default, default to slot 0.
+            slotId = DEFAULT_PHONE_ID;
+        }
+        return slotId;
+    }
 }
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 55a13bb..b81eeab 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -538,6 +538,7 @@
         @Override
         public void onRttModifyResponseReceived(int status) {
             updateConnectionProperties();
+            refreshConferenceSupported();
             if (status == RttModifyStatus.SESSION_MODIFY_REQUEST_SUCCESS) {
                 sendRttInitiationSuccess();
             } else {
@@ -554,7 +555,12 @@
 
         @Override
         public void onRttInitiated() {
-            updateConnectionProperties();
+            if (mOriginalConnection != null) {
+                // if mOriginalConnection is null, the properties will get set when
+                // mOriginalConnection gets set.
+                updateConnectionProperties();
+                refreshConferenceSupported();
+            }
             sendRttInitiationSuccess();
         }
 
@@ -831,7 +837,11 @@
     public void onStartRtt(RttTextStream textStream) {
         if (isImsConnection()) {
             ImsPhoneConnection originalConnection = (ImsPhoneConnection) mOriginalConnection;
-            originalConnection.sendRttModifyRequest(textStream);
+            if (originalConnection.isRttEnabledForCall()) {
+                originalConnection.setCurrentRttTextStream(textStream);
+            } else {
+                originalConnection.sendRttModifyRequest(textStream);
+            }
         } else {
             Log.w(this, "onStartRtt - not in IMS, so RTT cannot be enabled.");
         }
@@ -2090,6 +2100,9 @@
         if (mTreatAsEmergencyCall) {
             isConferenceSupported = false;
             Log.d(this, "refreshConferenceSupported = false; emergency call");
+        } else if (isRtt()) {
+            isConferenceSupported = false;
+            Log.d(this, "refreshConferenceSupported = false; rtt call");
         } else if (!isConferencingSupported || isIms && !isImsConferencingSupported) {
             isConferenceSupported = false;
             Log.d(this, "refreshConferenceSupported = false; carrier doesn't support conf.");