Fix apn option display issue for world phone mode

When configs world_phone_bool and show_apn_setting_cdma_bool are enabled,
'Access Point Names' is displayed twice under Network Settings, as this
preference is added from both GsmUmtsOptions and CdmaOptions.
Fix is to add preference from CdmaOptions only if phone type is cdma and
show_apn_setting_cdma_bool config is enabled.

Change-Id: Ia89b0b882604e287a996fec86c9c961df704f1e8
Test: builds, and CdmaOptionsTest.java (no manual test)
Bug: 70309465
diff --git a/src/com/android/phone/CdmaOptions.java b/src/com/android/phone/CdmaOptions.java
index ff37c70..7f7d58c 100644
--- a/src/com/android/phone/CdmaOptions.java
+++ b/src/com/android/phone/CdmaOptions.java
@@ -26,9 +26,11 @@
 import android.telephony.CarrierConfigManager;
 import android.text.TextUtils;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
 import com.android.settingslib.RestrictedLockUtils;
 
 /**
@@ -53,6 +55,12 @@
     private PreferenceScreen mPrefScreen;
     private Phone mPhone;
 
+    // Constructor for CdmaOptionsTest, since PreferenceScreen is final and cannot be mocked
+    @VisibleForTesting
+    public CdmaOptions(Phone phone) {
+        mPhone = phone;
+    }
+
     public CdmaOptions(PreferenceFragment prefFragment, PreferenceScreen prefScreen, Phone phone) {
         mPrefFragment = prefFragment;
         mPrefScreen = prefScreen;
@@ -79,8 +87,7 @@
         PersistableBundle carrierConfig =
                 PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
         // Some CDMA carriers want the APN settings.
-        boolean addAPNExpand =
-                carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
+        boolean addAPNExpand = shouldAddApnExpandPreference(carrierConfig);
         boolean addCdmaSubscription =
                 deviceSupportsNvAndRuim();
         // Read platform settings for carrier settings
@@ -94,6 +101,7 @@
         // Calling add or remove explicitly to make sure they are updated.
 
         if (addAPNExpand) {
+            log("update: addAPNExpand");
             mButtonAPNExpand.setDisabledByAdmin(
                     MobileNetworkSettings.isDpcApnEnforced(mButtonAPNExpand.getContext())
                             ? RestrictedLockUtils.getDeviceOwner(mButtonAPNExpand.getContext())
@@ -136,6 +144,19 @@
         }
     }
 
+    /**
+     * Return whether we should add the APN expandable preference based on the phone type and
+     * carrier config
+     */
+    @VisibleForTesting
+    public boolean shouldAddApnExpandPreference(PersistableBundle config) {
+        if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA
+                && config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL)) {
+            return true;
+        }
+        return false;
+    }
+
     private boolean deviceSupportsNvAndRuim() {
         // retrieve the list of subscription types supported by device.
         String subscriptionsSupported = SystemProperties.get("ril.subscription.types");
diff --git a/src/com/android/phone/GsmUmtsOptions.java b/src/com/android/phone/GsmUmtsOptions.java
index 220cf34..19cd3ef 100644
--- a/src/com/android/phone/GsmUmtsOptions.java
+++ b/src/com/android/phone/GsmUmtsOptions.java
@@ -26,8 +26,8 @@
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
-import com.android.internal.telephony.PhoneFactory;
 import com.android.settingslib.RestrictedLockUtils;
 
 /**
@@ -72,9 +72,11 @@
         boolean addAPNExpand = true;
         boolean addNetworkOperatorsCategory = true;
         boolean addCarrierSettings = true;
-        if (PhoneFactory.getDefaultPhone().getPhoneType() != PhoneConstants.PHONE_TYPE_GSM) {
+        Phone phone = PhoneGlobals.getPhone(subId);
+        if (phone == null) return;
+        if (phone.getPhoneType() != PhoneConstants.PHONE_TYPE_GSM) {
             log("Not a GSM phone");
-            mCategoryAPNExpand.setEnabled(false);
+            addAPNExpand = false;
             mNetworkOperator.setEnabled(false);
         } else {
             log("Not a CDMA phone");
@@ -96,7 +98,7 @@
             }
 
             if (carrierConfig.getBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL)) {
-                if (PhoneFactory.getDefaultPhone().isCspPlmnEnabled()) {
+                if (phone.isCspPlmnEnabled()) {
                     log("[CSP] Enabling Operator Selection menu.");
                     mNetworkOperator.setEnabled(true);
                 } else {
@@ -114,6 +116,7 @@
         // Calling add or remove explicitly to make sure they are updated.
 
         if (addAPNExpand) {
+            log("update: addAPNExpand");
             mButtonAPNExpand.setDisabledByAdmin(
                     MobileNetworkSettings.isDpcApnEnforced(mButtonAPNExpand.getContext())
                             ? RestrictedLockUtils.getDeviceOwner(mButtonAPNExpand.getContext())
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 99950a8..78e647a 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -1855,15 +1855,10 @@
 
             updateGsmUmtsOptions(this, prefSet, mPhone.getSubId(), mNetworkQueryService);
 
-            PreferenceCategory apnExpand =
-                    (PreferenceCategory) prefSet.findPreference(CATEGORY_GSM_APN_EXPAND_KEY);
             PreferenceCategory networkOperatorCategory =
                     (PreferenceCategory) prefSet.findPreference(
                             NetworkOperators.CATEGORY_NETWORK_OPERATORS_KEY);
             Preference carrierSettings = prefSet.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
-            if (apnExpand != null) {
-                apnExpand.setEnabled(isWorldMode() || enable);
-            }
             if (networkOperatorCategory != null) {
                 if (enable) {
                     networkOperatorCategory.setEnabled(true);