From b820483fad7ecd306cf2097c143fca30dafa610d Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Tue, 9 Oct 2018 16:08:44 -0700 Subject: [PATCH] Refactor CdmaSystemSelectListPreference Most logic is copied from previous ListPreference however 1. Create PreferenceController for it 2. Extract API to check whether to display CMDA options 3. In controller, provide API to force display dialog Bug: 114749736 Test: RunSettingsRoboTests Change-Id: Ic40d2ac1dc1854df9c90d912ea9fc822531b413d --- res/xml/cdma_options.xml | 8 - res/xml/network_setting_fragment.xml | 9 + .../network/telephony/CdmaOptions.java | 10 +- .../CdmaSystemSelectListPreference.java | 186 ------------------ .../telephony/MobileNetworkFragment.java | 48 ++--- .../network/telephony/MobileNetworkUtils.java | 85 ++++++++ .../CdmaSystemSelectPreferenceController.java | 155 +++++++++++++++ .../telephony/MobileNetworkUtilsTest.java | 22 ++- ...aSystemSelectPreferenceControllerTest.java | 122 ++++++++++++ 9 files changed, 414 insertions(+), 231 deletions(-) delete mode 100644 src/com/android/settings/network/telephony/CdmaSystemSelectListPreference.java create mode 100644 src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml index 202187cbaa4..92265881500 100644 --- a/res/xml/cdma_options.xml +++ b/res/xml/cdma_options.xml @@ -17,14 +17,6 @@ - - + + diff --git a/src/com/android/settings/network/telephony/CdmaOptions.java b/src/com/android/settings/network/telephony/CdmaOptions.java index 2a2d8ba3508..b927e2584b9 100644 --- a/src/com/android/settings/network/telephony/CdmaOptions.java +++ b/src/com/android/settings/network/telephony/CdmaOptions.java @@ -43,7 +43,6 @@ public class CdmaOptions { private static final String LOG_TAG = "CdmaOptions"; private CarrierConfigManager mCarrierConfigManager; - private CdmaSystemSelectListPreference mButtonCdmaSystemSelect; private CdmaSubscriptionListPreference mButtonCdmaSubscription; private RestrictedPreference mButtonAPNExpand; private Preference mCategoryAPNExpand; @@ -66,8 +65,6 @@ public class CdmaOptions { mCarrierConfigManager = new CarrierConfigManager(prefFragment.getContext()); // Initialize preferences. - mButtonCdmaSystemSelect = (CdmaSystemSelectListPreference) mPrefScreen - .findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY); mButtonCdmaSubscription = (CdmaSubscriptionListPreference) mPrefScreen .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY); mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY); @@ -91,9 +88,6 @@ public class CdmaOptions { boolean addCarrierSettings = carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL); - mPrefScreen.addPreference(mButtonCdmaSystemSelect); - mButtonCdmaSystemSelect.setEnabled(true); - // Making no assumptions of whether they are added or removed at this point. // Calling add or remove explicitly to make sure they are updated. @@ -191,9 +185,7 @@ public class CdmaOptions { } public void showDialog(Preference preference) { - if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) { - mButtonCdmaSystemSelect.showDialog(null); - } else if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) { + if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) { mButtonCdmaSubscription.showDialog(null); } } diff --git a/src/com/android/settings/network/telephony/CdmaSystemSelectListPreference.java b/src/com/android/settings/network/telephony/CdmaSystemSelectListPreference.java deleted file mode 100644 index 5727a9a971f..00000000000 --- a/src/com/android/settings/network/telephony/CdmaSystemSelectListPreference.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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.Bundle; -import android.os.Handler; -import android.os.Message; -import android.provider.Settings; -import android.telephony.TelephonyManager; -import android.util.AttributeSet; -import android.util.Log; - -import com.android.settingslib.utils.ThreadUtils; - -import androidx.preference.ListPreference; - -public class CdmaSystemSelectListPreference extends ListPreference { - - private static final String LOG_TAG = "CdmaRoamingListPref"; - private static final boolean DBG = false; - - private TelephonyManager mTelephonyManager; - private MyHandler mHandler = new MyHandler(); - - public CdmaSystemSelectListPreference(Context context, AttributeSet attrs) { - super(context, attrs); - - mHandler = new MyHandler(); - mTelephonyManager = TelephonyManager.from(context); - } - - public CdmaSystemSelectListPreference(Context context) { - this(context, null); - } - - /** - * Sets the subscription id associated with this preference. - * - * @param subId the subscription id. - */ - public void setSubscriptionId(int subId) { - mTelephonyManager = TelephonyManager.from(getContext()).createForSubscriptionId(subId); - queryCdmaRoamingMode(); - } - - //TODO(b/114749736): Move this to preference controller - protected void showDialog(Bundle state) { - if (!mTelephonyManager.getEmergencyCallbackMode()) { - // show Dialog - } - } - - //TODO(b/114749736): Move this to preference controller - protected void onDialogClosed(boolean positiveResult) { - if (positiveResult && (getValue() != null)) { - int buttonCdmaRoamingMode = Integer.parseInt(getValue()); - int settingsCdmaRoamingMode = Settings.Global.getInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - TelephonyManager.CDMA_ROAMING_MODE_HOME); - if (buttonCdmaRoamingMode != settingsCdmaRoamingMode) { - int cdmaRoamingMode = TelephonyManager.CDMA_ROAMING_MODE_ANY; - if (buttonCdmaRoamingMode != TelephonyManager.CDMA_ROAMING_MODE_ANY) { - cdmaRoamingMode = TelephonyManager.CDMA_ROAMING_MODE_HOME; - } - //Set the Settings.Secure network mode - Settings.Global.putInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - buttonCdmaRoamingMode); - //Set the roaming preference mode - setCdmaRoamingMode(cdmaRoamingMode); - } - } else { - Log.d(LOG_TAG, String.format("onDialogClosed: positiveResult=%b value=%s -- do nothing", - positiveResult, getValue())); - } - } - - private class MyHandler extends Handler { - - static final int MESSAGE_GET_ROAMING_PREFERENCE = 0; - static final int MESSAGE_SET_ROAMING_PREFERENCE = 1; - - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case MESSAGE_GET_ROAMING_PREFERENCE: - handleQueryCdmaRoamingPreference(msg); - break; - - case MESSAGE_SET_ROAMING_PREFERENCE: - handleSetCdmaRoamingPreference(msg); - break; - } - } - - private void handleQueryCdmaRoamingPreference(Message msg) { - int cdmaRoamingMode = msg.arg1; - - if (cdmaRoamingMode != TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT) { - int settingsRoamingMode = Settings.Global.getInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - TelephonyManager.CDMA_ROAMING_MODE_HOME); - - //check that statusCdmaRoamingMode is from an accepted value - if (cdmaRoamingMode == TelephonyManager.CDMA_ROAMING_MODE_HOME - || cdmaRoamingMode == TelephonyManager.CDMA_ROAMING_MODE_ANY) { - //check changes in statusCdmaRoamingMode and updates settingsRoamingMode - if (cdmaRoamingMode != settingsRoamingMode) { - settingsRoamingMode = cdmaRoamingMode; - //changes the Settings.Secure accordingly to statusCdmaRoamingMode - Settings.Global.putInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - settingsRoamingMode); - } - //changes the mButtonPreferredNetworkMode accordingly to modemNetworkMode - setValue(Integer.toString(cdmaRoamingMode)); - } - else { - if(DBG) Log.i(LOG_TAG, "reset cdma roaming mode to default" ); - resetCdmaRoamingModeToDefault(); - } - } - } - - private void handleSetCdmaRoamingPreference(Message msg) { - boolean isSuccessed = (boolean) msg.obj; - - if (isSuccessed && (getValue() != null)) { - int cdmaRoamingMode = Integer.parseInt(getValue()); - Settings.Global.putInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - cdmaRoamingMode ); - } else { - queryCdmaRoamingMode(); - } - } - - private void resetCdmaRoamingModeToDefault() { - //set the mButtonCdmaRoam - setValue(Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_ANY)); - //set the Settings.System - Settings.Global.putInt( - getContext().getContentResolver(), - Settings.Global.CDMA_ROAMING_MODE, - TelephonyManager.CDMA_ROAMING_MODE_ANY); - //Set the Status - setCdmaRoamingMode(TelephonyManager.CDMA_ROAMING_MODE_ANY); - } - } - - private void queryCdmaRoamingMode() { - ThreadUtils.postOnBackgroundThread(() -> { - Message msg = mHandler.obtainMessage(MyHandler.MESSAGE_GET_ROAMING_PREFERENCE); - msg.arg1 = mTelephonyManager.getCdmaRoamingMode(); - msg.sendToTarget(); - }); - } - - private void setCdmaRoamingMode(int mode) { - ThreadUtils.postOnBackgroundThread(() -> { - Message msg = mHandler.obtainMessage(MyHandler.MESSAGE_SET_ROAMING_PREFERENCE); - msg.obj = mTelephonyManager.setCdmaRoamingMode(mode); - msg.sendToTarget(); - }); - } -} diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java index 106d9408a99..3977cdd5f31 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkFragment.java +++ b/src/com/android/settings/network/telephony/MobileNetworkFragment.java @@ -49,6 +49,14 @@ import android.text.TextUtils; import android.util.Log; import android.view.MenuItem; +import androidx.fragment.app.FragmentActivity; +import androidx.preference.ListPreference; +import androidx.preference.Preference; +import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; + import com.android.ims.ImsConfig; import com.android.ims.ImsManager; import com.android.internal.logging.MetricsLogger; @@ -58,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.CdmaSystemSelectPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; import com.android.settingslib.RestrictedLockUtilsInternal; @@ -68,14 +77,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import androidx.fragment.app.FragmentActivity; -import androidx.preference.ListPreference; -import androidx.preference.Preference; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceScreen; -import androidx.preference.SwitchPreference; - @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) public class MobileNetworkFragment extends DashboardFragment implements Preference.OnPreferenceChangeListener, RoamingDialogFragment.RoamingDialogListener { @@ -160,6 +161,8 @@ public class MobileNetworkFragment extends DashboardFragment implements private NetworkSelectListPreference mButtonNetworkSelect; private DataUsagePreference mDataUsagePref; + private CdmaSystemSelectPreferenceController mCdmaSystemSelectPreferenceController; + private static final String iface = "rmnet0"; //TODO: this will go away private List mActiveSubInfos; @@ -394,6 +397,9 @@ public class MobileNetworkFragment extends DashboardFragment implements SubscriptionManager.INVALID_SUBSCRIPTION_ID); use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId); + + mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class); + mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId); } @Override @@ -721,10 +727,6 @@ public class MobileNetworkFragment extends DashboardFragment implements /** * Listen to extra preference changes that need as Metrics events logging. */ - if (prefSet.findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY) != null) { - prefSet.findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY) - .setOnPreferenceChangeListener(this); - } if (prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY) != null) { prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY) @@ -784,10 +786,6 @@ public class MobileNetworkFragment extends DashboardFragment implements if (ps != null) { ps.setEnabled(hasActiveSubscriptions); } - ps = findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY); - if (ps != null) { - ps.setEnabled(hasActiveSubscriptions); - } ps = findPreference(CATEGORY_CALLING_KEY); if (ps != null) { ps.setEnabled(hasActiveSubscriptions); @@ -1096,8 +1094,6 @@ public class MobileNetworkFragment extends DashboardFragment implements return false; } } else if (preference == getPreferenceScreen() - .findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY) - || preference == getPreferenceScreen() .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)) { return true; } @@ -1429,13 +1425,18 @@ public class MobileNetworkFragment extends DashboardFragment implements @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - switch(requestCode) { + switch (requestCode) { case REQUEST_CODE_EXIT_ECM: Boolean isChoiceYes = data.getBooleanExtra( EXTRA_EXIT_ECM_RESULT, false); if (isChoiceYes) { // If the phone exits from ECM mode, show the CDMA Options - mCdmaOptions.showDialog(mClickedPreference); + if (TextUtils.equals(mClickedPreference.getKey(), + mCdmaSystemSelectPreferenceController.getPreferenceKey())) { + mCdmaSystemSelectPreferenceController.showDialog(); + } else { + mCdmaOptions.showDialog(mClickedPreference); + } } else { // do nothing } @@ -1665,13 +1666,6 @@ public class MobileNetworkFragment extends DashboardFragment implements return; } updateCdmaOptions(this, prefSet, mSubId); - CdmaSystemSelectListPreference systemSelect = - (CdmaSystemSelectListPreference)prefSet.findPreference - (BUTTON_CDMA_SYSTEM_SELECT_KEY); - systemSelect.setSubscriptionId(mSubId); - if (systemSelect != null) { - systemSelect.setEnabled(enable); - } } private boolean isSupportTdscdma() { diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java index 5e78e0781a3..666db1b5d99 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java +++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java @@ -41,6 +41,9 @@ import androidx.annotation.VisibleForTesting; import com.android.ims.ImsException; import com.android.ims.ImsManager; +import com.android.internal.telephony.Phone; +import com.android.internal.telephony.PhoneConstants; +import com.android.settings.R; import java.util.Arrays; import java.util.List; @@ -238,4 +241,86 @@ public class MobileNetworkUtils { } } } + + /** + * Return {@code true} if show CDMA category + */ + public static boolean isCdmaOptions(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_CDMA) { + return true; + } + + if (isWorldMode(context, subId)) { + final int settingsNetworkMode = android.provider.Settings.Global.getInt( + context.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + subId, + Phone.PREFERRED_NT_MODE); + if (settingsNetworkMode == TelephonyManager.NETWORK_MODE_LTE_GSM_WCDMA + || settingsNetworkMode == TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO) { + return true; + } + + if (settingsNetworkMode == TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA + && !isTdscdmaSupported(context, telephonyManager)) { + return true; + } + } + + return false; + } + + /** + * Return {@code true} if it is world mode, and we may show advanced options in telephony + * settings + */ + public static boolean isWorldMode(Context context, int subId) { + final TelephonyManager telephonyManager = TelephonyManager.from(context) + .createForSubscriptionId(subId); + boolean worldModeOn = false; + final String configString = context.getString(R.string.config_world_mode); + + if (!TextUtils.isEmpty(configString)) { + String[] configArray = configString.split(";"); + // Check if we have World mode configuration set to True only or config is set to True + // and SIM GID value is also set and matches to the current SIM GID. + if (configArray != null && + ((configArray.length == 1 && configArray[0].equalsIgnoreCase("true")) + || (configArray.length == 2 && !TextUtils.isEmpty(configArray[1]) + && telephonyManager != null + && configArray[1].equalsIgnoreCase( + telephonyManager.getGroupIdLevel1())))) { + worldModeOn = true; + } + } + + Log.d(TAG, "isWorldMode=" + worldModeOn); + + return worldModeOn; + } + + //TODO(b/117651939): move it to telephony + private static boolean isTdscdmaSupported(Context context, TelephonyManager telephonyManager) { + if (context.getResources().getBoolean(R.bool.config_support_tdscdma)) { + return true; + } + + String operatorNumeric = telephonyManager.getServiceState().getOperatorNumeric(); + String[] numericArray = context.getResources().getStringArray( + R.array.config_support_tdscdma_roaming_on_networks); + if (numericArray.length == 0 || operatorNumeric == null) { + return false; + } + for (String numeric : numericArray) { + if (operatorNumeric.equals(numeric)) { + return true; + } + } + return false; + } } \ No newline at end of file diff --git a/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java new file mode 100644 index 00000000000..3d47feca83a --- /dev/null +++ b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java @@ -0,0 +1,155 @@ +/* + * 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.database.ContentObserver; +import android.net.Uri; +import android.os.Handler; +import android.os.Looper; +import android.provider.Settings; +import android.telephony.TelephonyManager; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.ListPreference; +import androidx.preference.Preference; +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.network.telephony.MobileNetworkUtils; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; + +/** + * Preference controller for "System Select" + */ +public class CdmaSystemSelectPreferenceController extends BasePreferenceController + implements LifecycleObserver, OnStart, OnStop, ListPreference.OnPreferenceChangeListener { + + @VisibleForTesting + ListPreference mPreference; + private TelephonyManager mTelephonyManager; + private PreferenceManager mPreferenceManager; + private DataContentObserver mDataContentObserver; + private int mSubId; + + public CdmaSystemSelectPreferenceController(Context context, String key) { + super(context, key); + mDataContentObserver = new DataContentObserver(new Handler(Looper.getMainLooper())); + } + + @Override + public void onStart() { + mDataContentObserver.register(mContext, mSubId); + } + + @Override + public void onStop() { + mDataContentObserver.unRegister(mContext); + } + + @Override + public int getAvailabilityStatus() { + return MobileNetworkUtils.isCdmaOptions(mContext, mSubId) + ? AVAILABLE + : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = (ListPreference) screen.findPreference(getPreferenceKey()); + } + + @Override + public void updateState(Preference preference) { + super.updateState(preference); + final ListPreference listPreference = (ListPreference) preference; + listPreference.setVisible(getAvailabilityStatus() == AVAILABLE); + final int mode = mTelephonyManager.getCdmaRoamingMode(); + if (mode != TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT) { + if (mode == TelephonyManager.CDMA_ROAMING_MODE_HOME + || mode == TelephonyManager.CDMA_ROAMING_MODE_ANY) { + listPreference.setValue(Integer.toString(mode)); + } else { + resetCdmaRoamingModeToDefault(); + } + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object object) { + int newMode = Integer.parseInt((String) object); + //TODO(b/117611981): only set it in one place + if (mTelephonyManager.setCdmaRoamingMode(newMode)) { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, newMode); + return true; + } + + return false; + } + + public void init(PreferenceManager preferenceManager, int subId) { + mPreferenceManager = preferenceManager; + mSubId = subId; + mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId); + } + + public void showDialog() { + if (!mTelephonyManager.getEmergencyCallbackMode()) { + mPreferenceManager.showDialog(mPreference); + } + } + + private void resetCdmaRoamingModeToDefault() { + //set the mButtonCdmaRoam + mPreference.setValue(Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_ANY)); + //set the Settings.System + Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_ANY); + //Set the Status + mTelephonyManager.setCdmaRoamingMode(TelephonyManager.CDMA_ROAMING_MODE_ANY); + } + + /** + * Listener that listens mobile data state change. + */ + public class DataContentObserver extends ContentObserver { + + public DataContentObserver(Handler handler) { + super(handler); + } + + @Override + public void onChange(boolean selfChange) { + super.onChange(selfChange); + updateState(mPreference); + } + + public void register(Context context, int subId) { + Uri uri = Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + subId); + context.getContentResolver().registerContentObserver(uri, false, this); + } + + public void unRegister(Context context) { + context.getContentResolver().unregisterContentObserver(this); + } + } +} diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java index d5c86485c66..c03fb71d466 100644 --- a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java +++ b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java @@ -18,7 +18,6 @@ package com.android.settings.network.telephony; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.nullable; @@ -32,11 +31,14 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.provider.Settings; import android.telecom.PhoneAccountHandle; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; +import com.android.internal.telephony.PhoneConstants; +import com.android.settings.R; import com.android.settings.testutils.SettingsRobolectricTestRunner; import org.junit.Before; @@ -142,4 +144,22 @@ public class MobileNetworkUtilsTest { assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, mPhoneAccountHandle)).isNotNull(); } + + @Test + public void isCdmaOptions_phoneTypeCdma_returnTrue() { + doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType(); + + assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void isCdmaOptions_worldModeWithGsmWcdma_returnTrue() { + doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType(); + doReturn("true").when(mContext).getString(R.string.config_world_mode); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManager.NETWORK_MODE_LTE_GSM_WCDMA); + + assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue(); + } } diff --git a/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java new file mode 100644 index 00000000000..204623728ca --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java @@ -0,0 +1,122 @@ +/* + * 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.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import android.content.Context; +import android.provider.Settings; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceManager; + +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +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 CdmaSystemSelectPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private PreferenceManager mPreferenceManager; + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private TelephonyManager mInvalidTelephonyManager; + @Mock + private SubscriptionManager mSubscriptionManager; + + private CdmaSystemSelectPreferenceController mController; + private ListPreference 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 ListPreference(mContext); + mController = new CdmaSystemSelectPreferenceController(mContext, "mobile_data"); + mController.init(mPreferenceManager, SUB_ID); + mController.mPreference = mPreference; + mPreference.setKey(mController.getPreferenceKey()); + } + + @Test + public void onPreferenceChange_selectHome_returnHomeMode() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_ANY); + doReturn(true).when(mTelephonyManager).setCdmaRoamingMode(anyInt()); + + mController.onPreferenceChange(mPreference, + Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_HOME)); + + assertThat(Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_ANY)).isEqualTo( + TelephonyManager.CDMA_ROAMING_MODE_HOME); + } + + @Test + public void updateState_stateHome_displayHome() { + doReturn(TelephonyManager.CDMA_ROAMING_MODE_HOME).when( + mTelephonyManager).getCdmaRoamingMode(); + + mController.updateState(mPreference); + + assertThat(mPreference.getValue()).isEqualTo( + Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_HOME)); + } + + @Test + public void updateState_stateOther_resetToDefault() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_HOME); + doReturn(TelephonyManager.CDMA_ROAMING_MODE_AFFILIATED).when( + mTelephonyManager).getCdmaRoamingMode(); + + mController.updateState(mPreference); + + assertThat(mPreference.getValue()).isEqualTo( + Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_ANY)); + assertThat(Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_HOME)).isEqualTo( + TelephonyManager.CDMA_ROAMING_MODE_ANY); + } + +}