Merge "Remove LAST_FORWARDED_NUMBER reporting." into pi-dev
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 799b9bc..691d9ff 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -1929,15 +1929,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);
diff --git a/src/com/android/phone/NetworkOperators.java b/src/com/android/phone/NetworkOperators.java
index 44af267..4467345 100644
--- a/src/com/android/phone/NetworkOperators.java
+++ b/src/com/android/phone/NetworkOperators.java
@@ -66,8 +66,9 @@
     private int mSubId;
     private ProgressDialog mProgressDialog;
 
-    // There's two sets of Auto-Select UI in this class. {@link mNetworkSelect} is used for all
-    // pre-Pixel 3 devices, while {@link mChooseNetwork} is used for all devices after Pixel3.
+    // There's two sets of Auto-Select UI in this class.
+    // If {@code com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI} set as true
+    // {@link mChooseNetwork} will be used, otherwise {@link mNetworkSelect} will be used.
     boolean mEnableNewManualSelectNetworkUI;
 
     public NetworkOperators(Context context, AttributeSet attrs) {
@@ -241,17 +242,17 @@
 
     private void selectNetworkAutomatic(boolean autoSelect) {
         if (DBG) logd("selectNetworkAutomatic: " + String.valueOf(autoSelect));
-        if (mEnableNewManualSelectNetworkUI) {
-            if (mChooseNetwork != null) {
-                mChooseNetwork.setEnabled(!autoSelect);
-            }
-        } else {
-            if (mNetworkSelect != null) {
-                mNetworkSelect.setEnabled(!autoSelect);
-            }
-        }
 
         if (autoSelect) {
+            if (mEnableNewManualSelectNetworkUI) {
+                if (mChooseNetwork != null) {
+                    mChooseNetwork.setEnabled(!autoSelect);
+                }
+            } else {
+                if (mNetworkSelect != null) {
+                    mNetworkSelect.setEnabled(!autoSelect);
+                }
+            }
             if (DBG) logd("select network automatically...");
             showAutoSelectProgressBar();
             mAutoSelect.setEnabled(false);
diff --git a/src/com/android/phone/NetworkSelectListPreference.java b/src/com/android/phone/NetworkSelectListPreference.java
index 00f58b8..199942a 100644
--- a/src/com/android/phone/NetworkSelectListPreference.java
+++ b/src/com/android/phone/NetworkSelectListPreference.java
@@ -274,7 +274,6 @@
     @Override
     protected void onDialogClosed(boolean positiveResult) {
         super.onDialogClosed(positiveResult);
-
         // If dismissed, we query NetworkSelectMode and update states of AutoSelect button.
         if (!positiveResult) {
             mNetworkOperators.getNetworkSelectionMode();
@@ -395,9 +394,7 @@
             // connected after this activity is moved to background.
             loge("Fail to dismiss network load list dialog " + e);
         }
-
-        setEnabled(true);
-
+        mNetworkOperators.getNetworkSelectionMode();
         if (mCellInfoList != null) {
             // create a preference for each item in the list.
             // just use the operator name instead of the mildly
diff --git a/tests/src/com/android/phone/CdmaOptionsTest.java b/tests/src/com/android/phone/CdmaOptionsTest.java
new file mode 100644
index 0000000..446f2c5
--- /dev/null
+++ b/tests/src/com/android/phone/CdmaOptionsTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.phone;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.telephony.CarrierConfigManager;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneConstants;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class CdmaOptionsTest {
+    @Mock
+    private Phone mMockPhone;
+
+    private CdmaOptions mCdmaOptions;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = InstrumentationRegistry.getContext();
+        mCdmaOptions = new CdmaOptions(mMockPhone);
+    }
+
+    @Test
+    public void shouldAddApnExpandPreference_doesNotExpandOnGsm() {
+        when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM);
+        PersistableBundle bundle = new PersistableBundle();
+        bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true);
+        assertFalse(mCdmaOptions.shouldAddApnExpandPreference(bundle));
+
+        when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_CDMA);
+        bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, false);
+        assertFalse(mCdmaOptions.shouldAddApnExpandPreference(bundle));
+
+        when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_CDMA);
+        bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true);
+        assertTrue(mCdmaOptions.shouldAddApnExpandPreference(bundle));
+    }
+}