Merge "[Settings] NullPointerException when disable eSIM/Fi" into rvc-dev
diff --git a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
index efbbbf8..d3fb437 100644
--- a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
@@ -48,8 +48,6 @@
 
     @VisibleForTesting
     Preference mPreference;
-    private CarrierConfigManager mCarrierConfigManager;
-    private PersistableBundle mCarrierConfig;
     private PhoneCallStateListener mPhoneStateListener;
     @VisibleForTesting
     Integer mCallState;
@@ -63,21 +61,23 @@
 
     public Enhanced4gBasePreferenceController(Context context, String key) {
         super(context, key);
-        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
         m4gLteListeners = new ArrayList<>();
         mPhoneStateListener = new PhoneCallStateListener();
     }
 
     public Enhanced4gBasePreferenceController init(int subId) {
-        if (SubscriptionManager.isValidSubscriptionId(mSubId) && mSubId == subId) {
+        if (mSubId == subId) {
             return this;
         }
         mSubId = subId;
-        mCarrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
+        final PersistableBundle carrierConfig = getCarrierConfigForSubId(subId);
+        if (carrierConfig == null) {
+            return this;
+        }
 
-        final boolean show4GForLTE = mCarrierConfig.getBoolean(
+        final boolean show4GForLTE = carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL);
-        m4gCurrentMode = mCarrierConfig.getInt(
+        m4gCurrentMode = carrierConfig.getInt(
                 CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT);
         if (m4gCurrentMode != MODE_ADVANCED_CALL) {
             m4gCurrentMode = show4GForLTE ? MODE_4G_CALLING : MODE_VOLTE;
@@ -91,10 +91,7 @@
         if (!isModeMatched()) {
             return CONDITIONALLY_UNAVAILABLE;
         }
-        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
-            return CONDITIONALLY_UNAVAILABLE;
-        }
-        final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
+        final PersistableBundle carrierConfig = getCarrierConfigForSubId(subId);
         if ((carrierConfig == null)
                 || carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_ENHANCED_4G_LTE_BOOL)) {
             return CONDITIONALLY_UNAVAILABLE;
@@ -103,7 +100,7 @@
         if (!queryState.isReadyToVoLte()) {
             return CONDITIONALLY_UNAVAILABLE;
         }
-        return (isUserControlAllowed() && queryState.isAllowUserControl())
+        return (isUserControlAllowed(carrierConfig) && queryState.isAllowUserControl())
                 ? AVAILABLE : AVAILABLE_UNSEARCHABLE;
     }
 
@@ -129,7 +126,7 @@
         final SwitchPreference switchPreference = (SwitchPreference) preference;
 
         final VolteQueryImsState queryState = queryImsState(mSubId);
-        switchPreference.setEnabled(isUserControlAllowed()
+        switchPreference.setEnabled(isUserControlAllowed(getCarrierConfigForSubId(mSubId))
                 && queryState.isAllowUserControl());
         switchPreference.setChecked(queryState.isEnabledByUser()
                 && queryState.isAllowUserControl());
@@ -180,9 +177,10 @@
         return new VolteQueryImsState(mContext, subId);
     }
 
-    private boolean isUserControlAllowed() {
+    private boolean isUserControlAllowed(final PersistableBundle carrierConfig) {
         return (mCallState != null) && (mCallState == TelephonyManager.CALL_STATE_IDLE)
-                && mCarrierConfig.getBoolean(
+                && (carrierConfig != null)
+                && carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL);
     }
 
diff --git a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
index e4ff5c4..241dc5d 100644
--- a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
@@ -17,6 +17,8 @@
 package com.android.settings.network.telephony;
 
 import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
 
 import com.android.settings.core.BasePreferenceController;
@@ -37,4 +39,20 @@
     public int getAvailabilityStatus() {
         return MobileNetworkUtils.getAvailability(mContext, mSubId, this::getAvailabilityStatus);
     }
+
+    /**
+     * Get carrier config based on specific subscription id.
+     *
+     * @param subId is the subscription id
+     * @return {@link PersistableBundle} of carrier config, or {@code null} when carrier config
+     * is not available.
+     */
+    public PersistableBundle getCarrierConfigForSubId(int subId) {
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            return null;
+        }
+        final CarrierConfigManager carrierConfigMgr =
+                mContext.getSystemService(CarrierConfigManager.class);
+        return carrierConfigMgr.getConfigForSubId(subId);
+    }
 }
diff --git a/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java b/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
index 71efc57..fc30030 100644
--- a/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
+++ b/src/com/android/settings/network/telephony/TelephonyTogglePreferenceController.java
@@ -17,6 +17,8 @@
 package com.android.settings.network.telephony;
 
 import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
 
 import com.android.settings.core.TogglePreferenceController;
@@ -37,4 +39,20 @@
     public int getAvailabilityStatus() {
         return MobileNetworkUtils.getAvailability(mContext, mSubId, this::getAvailabilityStatus);
     }
+
+    /**
+     * Get carrier config based on specific subscription id.
+     *
+     * @param subId is the subscription id
+     * @return {@link PersistableBundle} of carrier config, or {@code null} when carrier config
+     * is not available.
+     */
+    public PersistableBundle getCarrierConfigForSubId(int subId) {
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            return null;
+        }
+        final CarrierConfigManager carrierConfigMgr =
+                mContext.getSystemService(CarrierConfigManager.class);
+        return carrierConfigMgr.getConfigForSubId(subId);
+    }
 }