From 2de3d28138c3c396380c320c974aec21ef877e70 Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Thu, 11 Oct 2018 16:54:14 -0700 Subject: [PATCH] Refactor CdmaApnPreference Bug: 114749736 Test: RunSettingsRoboTests Change-Id: I3d0203b7b5fb732e2814fb741761af4371924e9a --- res/xml/cdma_options.xml | 11 -- res/xml/network_setting_fragment.xml | 10 ++ .../network/telephony/CdmaOptions.java | 61 +-------- .../telephony/MobileNetworkFragment.java | 2 + .../cdma/CdmaApnPreferenceController.java | 87 +++++++++++++ .../cdma/CdmaBasePreferenceController.java | 4 + .../cdma/CdmaApnPreferenceControllerTest.java | 116 ++++++++++++++++++ 7 files changed, 221 insertions(+), 70 deletions(-) create mode 100644 src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceControllerTest.java diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml index b95e31b1e93..facc1d3294a 100644 --- a/res/xml/cdma_options.xml +++ b/res/xml/cdma_options.xml @@ -17,17 +17,6 @@ - - - - - - - diff --git a/res/xml/network_setting_fragment.xml b/res/xml/network_setting_fragment.xml index 40d28dffcca..1a011b07fef 100644 --- a/res/xml/network_setting_fragment.xml +++ b/res/xml/network_setting_fragment.xml @@ -109,4 +109,14 @@ android:dialogTitle="@string/cdma_subscription_dialogtitle" settings:controller="com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController"/> + + + + + diff --git a/src/com/android/settings/network/telephony/CdmaOptions.java b/src/com/android/settings/network/telephony/CdmaOptions.java index 0e6c6c2b238..501746696c8 100644 --- a/src/com/android/settings/network/telephony/CdmaOptions.java +++ b/src/com/android/settings/network/telephony/CdmaOptions.java @@ -16,25 +16,15 @@ package com.android.settings.network.telephony; -import android.content.Intent; import android.os.PersistableBundle; -import android.os.SystemProperties; -import android.provider.Settings; import android.telephony.CarrierConfigManager; import android.telephony.TelephonyManager; -import android.text.TextUtils; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceScreen; -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.PhoneConstants; import com.android.settings.R; -import com.android.settingslib.RestrictedLockUtilsInternal; -import com.android.settingslib.RestrictedPreference; /** * List of Phone-specific settings screens. @@ -43,21 +33,18 @@ public class CdmaOptions { private static final String LOG_TAG = "CdmaOptions"; private CarrierConfigManager mCarrierConfigManager; - private RestrictedPreference mButtonAPNExpand; - private Preference mCategoryAPNExpand; private Preference mButtonCarrierSettings; private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key"; private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key"; private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key"; - private static final String BUTTON_APN_EXPAND_KEY = "button_cdma_apn_key"; - private static final String CATEGORY_APN_EXPAND_KEY = "category_cdma_apn_key"; private PreferenceFragmentCompat mPrefFragment; private PreferenceScreen mPrefScreen; private int mSubId; - public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen, int subId) { + public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen, + int subId) { mPrefFragment = prefFragment; mPrefScreen = prefScreen; mPrefFragment.addPreferencesFromResource(R.xml.cdma_options); @@ -65,20 +52,14 @@ public class CdmaOptions { // Initialize preferences. mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY); - mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY); - mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY); updateSubscriptionId(subId); } protected void updateSubscriptionId(int subId) { mSubId = subId; - int phoneType = TelephonyManager.from(mPrefFragment.getContext()) - .createForSubscriptionId(mSubId).getPhoneType(); PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId); - // Some CDMA carriers want the APN settings. - boolean addAPNExpand = shouldAddApnExpandPreference(phoneType, carrierConfig); // Read platform settings for carrier settings boolean addCarrierSettings = carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL); @@ -86,34 +67,6 @@ public class CdmaOptions { // Making no assumptions of whether they are added or removed at this point. // Calling add or remove explicitly to make sure they are updated. - if (addAPNExpand) { - log("update: addAPNExpand"); - mButtonAPNExpand.setDisabledByAdmin( - MobileNetworkUtils.isDpcApnEnforced(mButtonAPNExpand.getContext()) - ? RestrictedLockUtilsInternal.getDeviceOwner( - mButtonAPNExpand.getContext()) - : null); - mButtonAPNExpand.setOnPreferenceClickListener( - new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - MetricsLogger.action(mButtonAPNExpand.getContext(), - MetricsEvent.ACTION_MOBILE_NETWORK_APN_SETTINGS); - // We need to build the Intent by hand as the Preference Framework - // does not allow to add an Intent with some extras into a Preference - // XML file - final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS); - // This will setup the Home and Search affordance - intent.putExtra(":settings:show_fragment_as_subsetting", true); - intent.putExtra("sub_id", mSubId); - mPrefFragment.startActivity(intent); - return true; - } - }); - mPrefScreen.addPreference(mCategoryAPNExpand); - } else { - mPrefScreen.removePreference(mCategoryAPNExpand); - } if (addCarrierSettings) { mPrefScreen.addPreference(mButtonCarrierSettings); @@ -122,16 +75,6 @@ public class CdmaOptions { } } - /** - * Return whether we should add the APN expandable preference based on the phone type and - * carrier config - */ - @VisibleForTesting - public static boolean shouldAddApnExpandPreference(int phoneType, PersistableBundle config) { - return phoneType == PhoneConstants.PHONE_TYPE_CDMA - && config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL); - } - public boolean preferenceTreeClick(Preference preference) { if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) { log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true"); diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java index 2fe79f82961..91b351eb2c5 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkFragment.java +++ b/src/com/android/settings/network/telephony/MobileNetworkFragment.java @@ -66,6 +66,7 @@ import com.android.internal.telephony.PhoneConstants; import com.android.internal.telephony.TelephonyIntents; import com.android.settings.R; import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.network.telephony.cdma.CdmaApnPreferenceController; import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController; import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; @@ -399,6 +400,7 @@ public class MobileNetworkFragment extends DashboardFragment implements SubscriptionManager.INVALID_SUBSCRIPTION_ID); use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId); + use(CdmaApnPreferenceController.class).init(mSubId); mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class); mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId); diff --git a/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceController.java new file mode 100644 index 00000000000..eaacdb2f0ec --- /dev/null +++ b/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceController.java @@ -0,0 +1,87 @@ +/* + * 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.settings.network.telephony.cdma; + +import android.content.Context; +import android.content.Intent; +import android.os.PersistableBundle; +import android.provider.Settings; +import android.telephony.CarrierConfigManager; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + +import com.android.settings.SettingsActivity; +import com.android.settings.network.ApnSettings; +import com.android.settings.network.telephony.MobileNetworkUtils; +import com.android.settingslib.RestrictedLockUtilsInternal; +import com.android.settingslib.RestrictedPreference; + +/** + * Preference controller for "CDMA Apn" + */ +public class CdmaApnPreferenceController extends CdmaBasePreferenceController { + + private static final String CATEGORY_KEY = "category_cdma_apn_key"; + @VisibleForTesting + CarrierConfigManager mCarrierConfigManager; + + public CdmaApnPreferenceController(Context context, String key) { + super(context, key); + mCarrierConfigManager = new CarrierConfigManager(context); + } + + @Override + public int getAvailabilityStatus() { + final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId); + + return carrierConfig != null + && carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL) + && MobileNetworkUtils.isCdmaOptions(mContext, mSubId) + ? AVAILABLE + : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + if (isAvailable()) { + ((RestrictedPreference) mPreference).setDisabledByAdmin( + MobileNetworkUtils.isDpcApnEnforced(mContext) + ? RestrictedLockUtilsInternal.getDeviceOwner(mContext) + : null); + } else { + screen.findPreference(CATEGORY_KEY).setVisible(false); + } + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (getPreferenceKey().equals(preference.getKey())) { + // This activity runs in phone process, we must use intent to start + final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS); + // This will setup the Home and Search affordance + intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true); + intent.putExtra(ApnSettings.SUB_ID, mSubId); + mContext.startActivity(intent); + return true; + } + + return false; + } +} diff --git a/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java index 49185a83949..3e9ebc66fc8 100644 --- a/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java +++ b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java @@ -74,6 +74,10 @@ public abstract class CdmaBasePreferenceController extends BasePreferenceControl mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId); } + public void init(int subId) { + init(null, subId); + } + @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); diff --git a/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceControllerTest.java new file mode 100644 index 00000000000..2aceda7286b --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaApnPreferenceControllerTest.java @@ -0,0 +1,116 @@ +/* + * 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.settings.network.telephony.cdma; + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.content.Intent; +import android.os.PersistableBundle; +import android.provider.Settings; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import com.android.internal.telephony.PhoneConstants; +import com.android.settings.network.ApnSettings; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settingslib.RestrictedPreference; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +@RunWith(SettingsRobolectricTestRunner.class) +public class CdmaApnPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private TelephonyManager mInvalidTelephonyManager; + @Mock + private SubscriptionManager mSubscriptionManager; + @Mock + private CarrierConfigManager mCarrierConfigManager; + + private CdmaApnPreferenceController mController; + private RestrictedPreference mPreference; + private Context mContext; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mContext = spy(RuntimeEnvironment.application); + doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE); + doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class); + doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID); + doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId( + SubscriptionManager.INVALID_SUBSCRIPTION_ID); + + mPreference = new RestrictedPreference(mContext); + mController = new CdmaApnPreferenceController(mContext, "mobile_data"); + mController.init(SUB_ID); + mController.mPreference = mPreference; + mController.mCarrierConfigManager = mCarrierConfigManager; + mPreference.setKey(mController.getPreferenceKey()); + } + + @Test + public void getAvailabilityStatus_apnSettingsNotSupported_returnUnavailable() { + doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType(); + final PersistableBundle bundle = new PersistableBundle(); + bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, false); + doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); + } + + @Test + public void getAvailabilityStatus_apnSettingsSupportedWithCDMA_returnAvailable() { + doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType(); + final PersistableBundle bundle = new PersistableBundle(); + bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true); + doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void handPreferenceTreeClick_fireIntent() { + ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); + + mController.handlePreferenceTreeClick(mPreference); + + verify(mContext).startActivity(captor.capture()); + final Intent intent = captor.getValue(); + assertThat(intent.getAction()).isEqualTo(Settings.ACTION_APN_SETTINGS); + assertThat(intent.getIntExtra(ApnSettings.SUB_ID, 0)).isEqualTo(SUB_ID); + } +}