diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml deleted file mode 100644 index facc1d3294a..00000000000 --- a/res/xml/cdma_options.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - diff --git a/res/xml/gsm_umts_options.xml b/res/xml/gsm_umts_options.xml index f5439a4407e..1d1d4f3aa6f 100644 --- a/res/xml/gsm_umts_options.xml +++ b/res/xml/gsm_umts_options.xml @@ -27,7 +27,7 @@ android:title="@string/select_automatically" android:persistent="false"/> - diff --git a/res/xml/network_setting_fragment.xml b/res/xml/network_setting_fragment.xml index 1a011b07fef..1de3ef7f33d 100644 --- a/res/xml/network_setting_fragment.xml +++ b/res/xml/network_setting_fragment.xml @@ -119,4 +119,10 @@ settings:controller="com.android.settings.network.telephony.cdma.CdmaApnPreferenceController"/> + + + diff --git a/src/com/android/settings/network/telephony/CarrierPreferenceController.java b/src/com/android/settings/network/telephony/CarrierPreferenceController.java new file mode 100644 index 00000000000..957eaaaa01a --- /dev/null +++ b/src/com/android/settings/network/telephony/CarrierPreferenceController.java @@ -0,0 +1,68 @@ +/* + * 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; + +import android.content.Context; +import android.os.PersistableBundle; +import android.telephony.CarrierConfigManager; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; + +import com.android.settings.core.BasePreferenceController; + +/** + * Preference controller for "Carrier Settings" + */ +public class CarrierPreferenceController extends BasePreferenceController { + + @VisibleForTesting + CarrierConfigManager mCarrierConfigManager; + private int mSubId; + + public CarrierPreferenceController(Context context, String key) { + super(context, key); + mCarrierConfigManager = new CarrierConfigManager(context); + } + + public void init(int subId) { + mSubId = subId; + } + + @Override + public int getAvailabilityStatus() { + final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId); + + // Return available if it is in CDMA or GSM mode, and the flag is on + return carrierConfig != null + && carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL) + && (MobileNetworkUtils.isCdmaOptions(mContext, mSubId) + || MobileNetworkUtils.isGsmOptions(mContext, mSubId)) + ? AVAILABLE + : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (getPreferenceKey().equals(preference.getKey())) { + //TODO(b/117651939): start carrier settings activity + return true; + } + + return false; + } +} diff --git a/src/com/android/settings/network/telephony/CdmaOptions.java b/src/com/android/settings/network/telephony/CdmaOptions.java index 501746696c8..c573128c40e 100644 --- a/src/com/android/settings/network/telephony/CdmaOptions.java +++ b/src/com/android/settings/network/telephony/CdmaOptions.java @@ -16,10 +16,6 @@ package com.android.settings.network.telephony; -import android.os.PersistableBundle; -import android.telephony.CarrierConfigManager; -import android.telephony.TelephonyManager; - import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceScreen; @@ -32,50 +28,18 @@ import com.android.settings.R; public class CdmaOptions { private static final String LOG_TAG = "CdmaOptions"; - private CarrierConfigManager mCarrierConfigManager; - 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 PreferenceFragmentCompat mPrefFragment; - private PreferenceScreen mPrefScreen; - private int mSubId; public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen, int subId) { mPrefFragment = prefFragment; - mPrefScreen = prefScreen; - mPrefFragment.addPreferencesFromResource(R.xml.cdma_options); - mCarrierConfigManager = new CarrierConfigManager(prefFragment.getContext()); - - // Initialize preferences. - mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY); - - updateSubscriptionId(subId); - } - - protected void updateSubscriptionId(int subId) { - mSubId = subId; - - PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId); - // Read platform settings for carrier settings - boolean addCarrierSettings = - carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL); - - // 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 (addCarrierSettings) { - mPrefScreen.addPreference(mButtonCarrierSettings); - } else { - mPrefScreen.removePreference(mButtonCarrierSettings); - } } public boolean preferenceTreeClick(Preference preference) { + //TODO(b/114749736): handle it in preferenceController and remove this file if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) { log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true"); return true; diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java index 91b351eb2c5..518e59ba1e4 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkFragment.java +++ b/src/com/android/settings/network/telephony/MobileNetworkFragment.java @@ -401,6 +401,7 @@ public class MobileNetworkFragment extends DashboardFragment implements use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId); use(CdmaApnPreferenceController.class).init(mSubId); + use(CarrierPreferenceController.class).init(mSubId); mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class); mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId); @@ -1798,8 +1799,6 @@ public class MobileNetworkFragment extends DashboardFragment implements // the open dialog gets dismissed or detached after pause / resume. if (mCdmaOptions == null) { mCdmaOptions = new CdmaOptions(prefFragment, prefScreen, subId); - } else { - mCdmaOptions.updateSubscriptionId(subId); } } diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java index 666db1b5d99..8950178dd40 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java +++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java @@ -275,6 +275,20 @@ public class MobileNetworkUtils { return false; } + public static boolean isGsmOptions(Context context, int subId) { + if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + return false; + } + final TelephonyManager telephonyManager = TelephonyManager.from(context) + .createForSubscriptionId(subId); + + if (telephonyManager.getPhoneType() == PhoneConstants.PHONE_TYPE_GSM) { + return true; + } + + return false; + } + /** * Return {@code true} if it is world mode, and we may show advanced options in telephony * settings diff --git a/tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java new file mode 100644 index 00000000000..e119e8990e0 --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/telephony/CarrierPreferenceControllerTest.java @@ -0,0 +1,108 @@ +/* + * 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; + +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 android.content.Context; +import android.os.PersistableBundle; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import com.android.internal.telephony.PhoneConstants; +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.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +@RunWith(SettingsRobolectricTestRunner.class) +public class CarrierPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private TelephonyManager mInvalidTelephonyManager; + @Mock + private SubscriptionManager mSubscriptionManager; + @Mock + private CarrierConfigManager mCarrierConfigManager; + + private CarrierPreferenceController 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 CarrierPreferenceController(mContext, "mobile_data"); + mController.init(SUB_ID); + mController.mCarrierConfigManager = mCarrierConfigManager; + mPreference.setKey(mController.getPreferenceKey()); + } + + @Test + public void getAvailabilityStatus_cdmaWithFlagOff_returnUnavailable() { + doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType(); + final PersistableBundle bundle = new PersistableBundle(); + bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, false); + doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); + } + + @Test + public void getAvailabilityStatus_cdmaWithFlagOnreturnAvailable() { + doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType(); + final PersistableBundle bundle = new PersistableBundle(); + bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, true); + doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void getAvailabilityStatus_gsmWithFlagOnreturnAvailable() { + doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType(); + final PersistableBundle bundle = new PersistableBundle(); + bundle.putBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL, true); + doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } +}