Merge "Disable certain mobile settings during phone call."
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index 233530e..8da980c 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -101,6 +101,7 @@
     private SwitchPreference mButtonAutoRetry;
     private PreferenceScreen mVoicemailSettingsScreen;
     private SwitchPreference mEnableVideoCalling;
+    private Preference mButtonWifiCalling;
 
     /*
      * Click Listeners, handle click based on objects attached to UI.
@@ -200,6 +201,8 @@
                         (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                 mEnableVideoCalling.setEnabled(
                         telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE);
+                mButtonWifiCalling.setEnabled(
+                        telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE);
             }
         }
     };
@@ -239,6 +242,8 @@
         mButtonAutoRetry = (SwitchPreference) findPreference(BUTTON_RETRY_KEY);
 
         mEnableVideoCalling = (SwitchPreference) findPreference(ENABLE_VIDEO_CALLING_KEY);
+        mButtonWifiCalling = findPreference(getResources().getString(
+                R.string.wifi_calling_settings_key));
 
         PersistableBundle carrierConfig =
                 PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
@@ -309,9 +314,6 @@
             /* tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); */
         }
 
-        Preference wifiCallingSettings = findPreference(
-                getResources().getString(R.string.wifi_calling_settings_key));
-
         final PhoneAccountHandle simCallManager = mTelecomManager.getSimCallManager();
         if (simCallManager != null) {
             Intent intent = PhoneAccountSettingsFragment.buildPhoneAccountConfigureIntent(
@@ -320,17 +322,17 @@
                 PackageManager pm = mPhone.getContext().getPackageManager();
                 List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
                 if (!resolutions.isEmpty()) {
-                    wifiCallingSettings.setTitle(resolutions.get(0).loadLabel(pm));
-                    wifiCallingSettings.setSummary(null);
-                    wifiCallingSettings.setIntent(intent);
+                    mButtonWifiCalling.setTitle(resolutions.get(0).loadLabel(pm));
+                    mButtonWifiCalling.setSummary(null);
+                    mButtonWifiCalling.setIntent(intent);
                 } else {
-                    prefSet.removePreference(wifiCallingSettings);
+                    prefSet.removePreference(mButtonWifiCalling);
                 }
             } else {
-                prefSet.removePreference(wifiCallingSettings);
+                prefSet.removePreference(mButtonWifiCalling);
             }
         } else if (!mImsMgr.isWfcEnabledByPlatform() || !mImsMgr.isWfcProvisionedOnDevice()) {
-            prefSet.removePreference(wifiCallingSettings);
+            prefSet.removePreference(mButtonWifiCalling);
         } else {
             int resId = com.android.internal.R.string.wifi_calling_off_summary;
             if (mImsMgr.isWfcEnabledByUser()) {
@@ -350,19 +352,19 @@
                         if (DBG) log("Unexpected WFC mode value: " + wfcMode);
                 }
             }
-            wifiCallingSettings.setSummary(resId);
+            mButtonWifiCalling.setSummary(resId);
         }
 
         try {
             if (mImsMgr.getImsServiceState() != ImsFeature.STATE_READY) {
                 log("Feature state not ready so remove vt and wfc settings for "
                         + " phone =" + mPhone.getPhoneId());
-                prefSet.removePreference(wifiCallingSettings);
+                prefSet.removePreference(mButtonWifiCalling);
                 prefSet.removePreference(mEnableVideoCalling);
             }
         } catch (ImsException ex) {
             log("Exception when trying to get ImsServiceStatus: " + ex);
-            prefSet.removePreference(wifiCallingSettings);
+            prefSet.removePreference(mButtonWifiCalling);
             prefSet.removePreference(mEnableVideoCalling);
         }
     }
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 0accb56..5dc57dd 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -264,12 +264,10 @@
         private boolean mIsGlobalCdma;
         private boolean mUnavailable;
 
-        private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+        private class PhoneCallStateListener extends PhoneStateListener {
             /*
-             * Enable/disable the 'Enhanced 4G LTE Mode' and 'Carrier video calling'
-             * when in/out of a call. 'Enhanced 4G LTE Mode' depends on TTY mode
-             * and TTY support over VoLTE.
-             *
+             * Enable/disable the 'Enhanced 4G LTE Mode' when in/out of a call
+             * and depending on TTY mode and TTY support over VoLTE.
              * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
              * java.lang.String)
              */
@@ -277,17 +275,31 @@
             public void onCallStateChanged(int state, String incomingNumber) {
                 if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
 
-                int subId = mPhone != null
-                        ? mPhone.getSubId() : SubscriptionManager.INVALID_SUBSCRIPTION_ID;
-                PersistableBundle carrierConfig =
-                        PhoneGlobals.getInstance().getCarrierConfigForSubId(subId);
-                boolean enabled = is4gLtePrefEnabled(carrierConfig);
-                Preference pref = getPreferenceScreen().findPreference(BUTTON_4G_LTE_KEY);
-                if (pref != null) pref.setEnabled(enabled && hasActiveSubscriptions());
-
+                updateEnhanced4gLteState();
+                updateWiFiCallState();
                 updateVideoCallState();
             }
-        };
+
+            /*
+             * Listen to different subId if mPhone is updated.
+             */
+            protected void updatePhone() {
+                int newSubId = (mPhone != null
+                        && SubscriptionManager.isValidSubscriptionId(mPhone.getSubId()))
+                        ? mPhone.getSubId()
+                        : SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
+                // Now, listen to new subId if it's valid.
+                mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
+
+                mSubId = newSubId;
+                if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
+                    mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
+                }
+            }
+        }
+
+        private final PhoneCallStateListener mPhoneStateListener = new PhoneCallStateListener();
 
         /**
          * Service connection code for the NetworkQueryService.
@@ -579,7 +591,6 @@
                 int phoneId = SubscriptionManager.getPhoneId(sir.getSubscriptionId());
                 if (SubscriptionManager.isValidPhoneId(phoneId)) {
                     mPhone = PhoneFactory.getPhone(phoneId);
-                    mImsMgr = ImsManager.getInstance(getContext(), phoneId);
                 }
             }
             if (mPhone == null) {
@@ -589,11 +600,14 @@
             Log.i(LOG_TAG, "updatePhone:- slotId=" + slotId + " sir=" + sir);
 
             mImsMgr = ImsManager.getInstance(mPhone.getContext(), mPhone.getPhoneId());
+            mTelephonyManager = new TelephonyManager(mPhone.getContext(), mPhone.getSubId());
             if (mImsMgr == null) {
                 log("updatePhone :: Could not get ImsManager instance!");
             } else if (DBG) {
                 log("updatePhone :: mImsMgr=" + mImsMgr);
             }
+
+            mPhoneStateListener.updatePhone();
         }
 
         private TabHost.TabContentFactory mEmptyTabContent = new TabHost.TabContentFactory() {
@@ -1047,7 +1061,6 @@
              * but you do need to remember that this all needs to work when subscriptions
              * change dynamically such as when hot swapping sims.
              */
-            boolean canChange4glte = is4gLtePrefEnabled(carrierConfig);
             boolean useVariant4glteTitle = carrierConfig.getBoolean(
                     CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_BOOL);
             int enhanced4glteModeTitleId = useVariant4glteTitle ?
@@ -1057,7 +1070,6 @@
             mButtonPreferredNetworkMode.setEnabled(hasActiveSubscriptions);
             mButtonEnabledNetworks.setEnabled(hasActiveSubscriptions);
             mButton4glte.setTitle(enhanced4glteModeTitleId);
-            mButton4glte.setEnabled(hasActiveSubscriptions && canChange4glte);
             mLteDataServicePref.setEnabled(hasActiveSubscriptions);
             Preference ps;
             ps = findPreference(BUTTON_CELL_BROADCAST_SETTINGS);
@@ -1271,7 +1283,8 @@
         }
 
         private boolean is4gLtePrefEnabled(PersistableBundle carrierConfig) {
-            return (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)
+            return (mTelephonyManager.getCallState(mPhone.getSubId())
+                    == TelephonyManager.CALL_STATE_IDLE)
                     && mImsMgr != null
                     && mImsMgr.isNonTtyOrTtyOnVolteEnabled()
                     && carrierConfig.getBoolean(
@@ -1661,6 +1674,8 @@
                 mCallingCategory.removePreference(mWiFiCallingPref);
             } else {
                 mCallingCategory.addPreference(mWiFiCallingPref);
+                mWiFiCallingPref.setEnabled(mTelephonyManager.getCallState(mPhone.getSubId())
+                        == TelephonyManager.CALL_STATE_IDLE && hasActiveSubscriptions());
             }
         }
 
@@ -1681,7 +1696,8 @@
                         CarrierConfigManager.KEY_HIDE_ENHANCED_4G_LTE_BOOL))) {
                     getPreferenceScreen().removePreference(mButton4glte);
                 } else {
-                    // NOTE: Buttons will be enabled/disabled in mPhoneStateListener
+                    mButton4glte.setEnabled(is4gLtePrefEnabled(carrierConfig)
+                            && hasActiveSubscriptions());
                     boolean enh4glteMode = mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()
                             && mImsMgr.isNonTtyOrTtyOnVolteEnabled();
                     mButton4glte.setChecked(enh4glteMode);
@@ -1711,8 +1727,8 @@
                     mVideoCallingPref.setEnabled(false);
                     mVideoCallingPref.setChecked(false);
                 } else {
-                    mVideoCallingPref.setEnabled(
-                            mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE);
+                    mVideoCallingPref.setEnabled(mTelephonyManager.getCallState(mPhone.getSubId())
+                            == TelephonyManager.CALL_STATE_IDLE && hasActiveSubscriptions());
                     mVideoCallingPref.setChecked(mImsMgr.isVtEnabledByUser());
                     mVideoCallingPref.setOnPreferenceChangeListener(this);
                 }