diff --git a/res/xml/cdma_options.xml b/res/xml/cdma_options.xml index 92265881500..b95e31b1e93 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 b927e2584b9..0e6c6c2b238 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 CdmaSubscriptionListPreference mButtonCdmaSubscription; private RestrictedPreference mButtonAPNExpand; private Preference mCategoryAPNExpand; private Preference mButtonCarrierSettings; @@ -65,8 +64,6 @@ public class CdmaOptions { mCarrierConfigManager = new CarrierConfigManager(prefFragment.getContext()); // Initialize preferences. - mButtonCdmaSubscription = (CdmaSubscriptionListPreference) mPrefScreen - .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY); mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY); mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY); mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY); @@ -82,8 +79,6 @@ public class CdmaOptions { PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId); // Some CDMA carriers want the APN settings. boolean addAPNExpand = shouldAddApnExpandPreference(phoneType, carrierConfig); - boolean addCdmaSubscription = - deviceSupportsNvAndRuim(); // Read platform settings for carrier settings boolean addCarrierSettings = carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL); @@ -120,15 +115,6 @@ public class CdmaOptions { mPrefScreen.removePreference(mCategoryAPNExpand); } - if (addCdmaSubscription) { - log("Both NV and Ruim supported, ENABLE subscription type selection"); - mPrefScreen.addPreference(mButtonCdmaSubscription); - mButtonCdmaSubscription.setEnabled(true); - } else { - log("Both NV and Ruim NOT supported, REMOVE subscription type selection"); - mPrefScreen.removePreference(mButtonCdmaSubscription); - } - if (addCarrierSettings) { mPrefScreen.addPreference(mButtonCarrierSettings); } else { @@ -146,32 +132,6 @@ public class CdmaOptions { && config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL); } - private boolean deviceSupportsNvAndRuim() { - // retrieve the list of subscription types supported by device. - String subscriptionsSupported = SystemProperties.get("ril.subscription.types"); - boolean nvSupported = false; - boolean ruimSupported = false; - - log("deviceSupportsnvAnRum: prop=" + subscriptionsSupported); - if (!TextUtils.isEmpty(subscriptionsSupported)) { - // Searches through the comma-separated list for a match for "NV" - // and "RUIM" to update nvSupported and ruimSupported. - for (String subscriptionType : subscriptionsSupported.split(",")) { - subscriptionType = subscriptionType.trim(); - if (subscriptionType.equalsIgnoreCase("NV")) { - nvSupported = true; - } - if (subscriptionType.equalsIgnoreCase("RUIM")) { - ruimSupported = true; - } - } - } - - log("deviceSupportsnvAnRum: nvSupported=" + nvSupported + - " ruimSupported=" + ruimSupported); - return (nvSupported && ruimSupported); - } - public boolean preferenceTreeClick(Preference preference) { if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) { log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true"); @@ -184,12 +144,6 @@ public class CdmaOptions { return false; } - public void showDialog(Preference preference) { - if (preference.getKey().equals(BUTTON_CDMA_SUBSCRIPTION_KEY)) { - mButtonCdmaSubscription.showDialog(null); - } - } - protected void log(String s) { android.util.Log.d(LOG_TAG, s); } diff --git a/src/com/android/settings/network/telephony/CdmaSubscriptionListPreference.java b/src/com/android/settings/network/telephony/CdmaSubscriptionListPreference.java deleted file mode 100644 index 559ba2d882a..00000000000 --- a/src/com/android/settings/network/telephony/CdmaSubscriptionListPreference.java +++ /dev/null @@ -1,115 +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 androidx.preference.ListPreference; -import android.provider.Settings; -import android.telephony.TelephonyManager; -import android.util.AttributeSet; -import android.util.Log; - -import com.android.internal.telephony.Phone; -import com.android.settingslib.utils.ThreadUtils; - -public class CdmaSubscriptionListPreference extends ListPreference { - - private static final String LOG_TAG = "CdmaSubListPref"; - - // Used for CDMA subscription mode - private static final int CDMA_SUBSCRIPTION_RUIM_SIM = 0; - private static final int CDMA_SUBSCRIPTION_NV = 1; - - //preferredSubscriptionMode 0 - RUIM/SIM, preferred - // 1 - NV - static final int preferredSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION; - - private TelephonyManager mTelephonyManager; - - public CdmaSubscriptionListPreference(Context context, AttributeSet attrs) { - super(context, attrs); - - mTelephonyManager = TelephonyManager.from(context); - setCurrentCdmaSubscriptionModeValue(); - } - - private void setCurrentCdmaSubscriptionModeValue() { - int cdmaSubscriptionMode = Settings.Global.getInt(getContext().getContentResolver(), - Settings.Global.CDMA_SUBSCRIPTION_MODE, preferredSubscriptionMode); - setValue(Integer.toString(cdmaSubscriptionMode)); - } - - public CdmaSubscriptionListPreference(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); - } - - //TODO(b/114749736): move this logic to preference controller - protected void showDialog(Bundle state) { - setCurrentCdmaSubscriptionModeValue(); - } - - //TODO(b/114749736): move this logic to preference controller - protected void onDialogClosed(boolean positiveResult) { - if (!positiveResult) { - //The button was dismissed - no need to set new value - return; - } - - int buttonCdmaSubscriptionMode = Integer.parseInt(getValue()); - Log.d(LOG_TAG, "Setting new value " + buttonCdmaSubscriptionMode); - int statusCdmaSubscriptionMode; - switch(buttonCdmaSubscriptionMode) { - case CDMA_SUBSCRIPTION_NV: - statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_NV; - break; - case CDMA_SUBSCRIPTION_RUIM_SIM: - statusCdmaSubscriptionMode = Phone.CDMA_SUBSCRIPTION_RUIM_SIM; - break; - default: - statusCdmaSubscriptionMode = Phone.PREFERRED_CDMA_SUBSCRIPTION; - } - - // Set the CDMA subscription mode, when mode has been successfully changed, update the - // mode to the global setting. - ThreadUtils.postOnBackgroundThread(() -> { - // The subscription mode selected by user. - int cdmaSubscriptionMode = Integer.parseInt(getValue()); - - boolean isSuccessed = mTelephonyManager.setCdmaSubscriptionMode( - statusCdmaSubscriptionMode); - - // Update the global settings if successed. - if (isSuccessed) { - Settings.Global.putInt(getContext().getContentResolver(), - Settings.Global.CDMA_SUBSCRIPTION_MODE, - cdmaSubscriptionMode); - } else { - Log.e(LOG_TAG, "Setting Cdma subscription source failed"); - } - }); - } -} diff --git a/src/com/android/settings/network/telephony/MobileNetworkFragment.java b/src/com/android/settings/network/telephony/MobileNetworkFragment.java index 3977cdd5f31..2fe79f82961 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.CdmaSubscriptionPreferenceController; import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; @@ -162,6 +163,7 @@ public class MobileNetworkFragment extends DashboardFragment implements private DataUsagePreference mDataUsagePref; private CdmaSystemSelectPreferenceController mCdmaSystemSelectPreferenceController; + private CdmaSubscriptionPreferenceController mCdmaSubscriptionPreferenceController; private static final String iface = "rmnet0"; //TODO: this will go away private List mActiveSubInfos; @@ -400,6 +402,8 @@ public class MobileNetworkFragment extends DashboardFragment implements mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class); mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId); + mCdmaSubscriptionPreferenceController = use(CdmaSubscriptionPreferenceController.class); + mCdmaSubscriptionPreferenceController.init(getPreferenceManager(), mSubId); } @Override @@ -724,15 +728,6 @@ public class MobileNetworkFragment extends DashboardFragment implements } } - /** - * Listen to extra preference changes that need as Metrics events logging. - */ - - if (prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY) != null) { - prefSet.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY) - .setOnPreferenceChangeListener(this); - } - // Get the networkMode from Settings.System and displays it mButtonEnabledNetworks.setValue(Integer.toString(settingsNetworkMode)); mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode)); @@ -1093,9 +1088,6 @@ public class MobileNetworkFragment extends DashboardFragment implements mVideoCallingPref.setEnabled(false); return false; } - } else if (preference == getPreferenceScreen() - .findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)) { - return true; } updateBody(); @@ -1431,11 +1423,13 @@ public class MobileNetworkFragment extends DashboardFragment implements EXTRA_EXIT_ECM_RESULT, false); if (isChoiceYes) { // If the phone exits from ECM mode, show the CDMA Options - if (TextUtils.equals(mClickedPreference.getKey(), + final String key = mClickedPreference.getKey(); + if (TextUtils.equals(key, mCdmaSystemSelectPreferenceController.getPreferenceKey())) { mCdmaSystemSelectPreferenceController.showDialog(); - } else { - mCdmaOptions.showDialog(mClickedPreference); + } else if (TextUtils.equals(key, + mCdmaSubscriptionPreferenceController.getPreferenceKey())) { + mCdmaSubscriptionPreferenceController.showDialog(); } } else { // do nothing diff --git a/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java new file mode 100644 index 00000000000..49185a83949 --- /dev/null +++ b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.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.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.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 related to CDMA category + */ +public abstract class CdmaBasePreferenceController extends BasePreferenceController + implements LifecycleObserver, OnStart, OnStop { + + protected Preference mPreference; + protected TelephonyManager mTelephonyManager; + protected PreferenceManager mPreferenceManager; + protected int mSubId; + private DataContentObserver mDataContentObserver; + + public CdmaBasePreferenceController(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; + } + + public void init(PreferenceManager preferenceManager, int subId) { + mPreferenceManager = preferenceManager; + mSubId = subId; + mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId); + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = screen.findPreference(getPreferenceKey()); + } + + /** + * Listener that listens to 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) { + final 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/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceController.java new file mode 100644 index 00000000000..eb14664f030 --- /dev/null +++ b/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceController.java @@ -0,0 +1,110 @@ +/* + * 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.os.SystemProperties; +import android.provider.Settings; +import android.telephony.TelephonyManager; +import android.text.TextUtils; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.ListPreference; +import androidx.preference.Preference; +import androidx.preference.PreferenceManager; + +import com.android.internal.telephony.Phone; +import com.android.settings.network.telephony.MobileNetworkUtils; + +/** + * Preference controller for "CDMA subscription" + */ +public class CdmaSubscriptionPreferenceController extends CdmaBasePreferenceController + implements ListPreference.OnPreferenceChangeListener { + private static final String TYPE_NV = "NV"; + private static final String TYPE_RUIM = "RUIM"; + + @VisibleForTesting + ListPreference mPreference; + + public CdmaSubscriptionPreferenceController(Context context, String key) { + super(context, key); + } + + @Override + public int getAvailabilityStatus() { + return MobileNetworkUtils.isCdmaOptions(mContext, mSubId) && deviceSupportsNvAndRuim() + ? AVAILABLE + : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public void updateState(Preference preference) { + super.updateState(preference); + final ListPreference listPreference = (ListPreference) preference; + listPreference.setVisible(getAvailabilityStatus() == AVAILABLE); + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, Phone.PREFERRED_CDMA_SUBSCRIPTION); + if (mode != Phone.CDMA_SUBSCRIPTION_UNKNOWN) { + listPreference.setValue(Integer.toString(mode)); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object object) { + final int newMode = Integer.parseInt((String) object); + //TODO(b/117611981): only set it in one place + if (mTelephonyManager.setCdmaSubscriptionMode(newMode)) { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, newMode); + return true; + } + + return false; + } + + public void showDialog() { + final int mode = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, Phone.PREFERRED_CDMA_SUBSCRIPTION); + + mPreference.setValue(Integer.toString(mode)); + mPreferenceManager.showDialog(mPreference); + } + + @VisibleForTesting + boolean deviceSupportsNvAndRuim() { + // retrieve the list of subscription types supported by device. + final String subscriptionsSupported = SystemProperties.get("ril.subscription.types"); + boolean nvSupported = false; + boolean ruimSupported = false; + + if (!TextUtils.isEmpty(subscriptionsSupported)) { + // Searches through the comma-separated list for a match for "NV" + // and "RUIM" to update nvSupported and ruimSupported. + for (String subscriptionType : subscriptionsSupported.split(",")) { + subscriptionType = subscriptionType.trim(); + if (subscriptionType.equalsIgnoreCase(TYPE_NV)) { + nvSupported = true; + } else if (subscriptionType.equalsIgnoreCase(TYPE_RUIM)) { + ruimSupported = true; + } + } + } + + return (nvSupported && ruimSupported); + } +} diff --git a/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java index 3d47feca83a..95e5dae4278 100644 --- a/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java +++ b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java @@ -17,64 +17,20 @@ 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 class CdmaSystemSelectPreferenceController extends CdmaBasePreferenceController + implements ListPreference.OnPreferenceChangeListener { 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 @@ -106,12 +62,6 @@ public class CdmaSystemSelectPreferenceController extends BasePreferenceControll 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); @@ -119,37 +69,13 @@ public class CdmaSystemSelectPreferenceController extends BasePreferenceControll } private void resetCdmaRoamingModeToDefault() { + final ListPreference listPreference = (ListPreference) mPreference; //set the mButtonCdmaRoam - mPreference.setValue(Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_ANY)); + listPreference.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/cdma/CdmaSubscriptionPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java new file mode 100644 index 00000000000..c938948a403 --- /dev/null +++ b/tests/robotests/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java @@ -0,0 +1,121 @@ +/* + * 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.os.SystemProperties; +import android.provider.Settings; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceManager; + +import com.android.internal.telephony.Phone; +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 CdmaSubscriptionPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private PreferenceManager mPreferenceManager; + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private TelephonyManager mInvalidTelephonyManager; + @Mock + private SubscriptionManager mSubscriptionManager; + + private CdmaSubscriptionPreferenceController 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 CdmaSubscriptionPreferenceController(mContext, "mobile_data"); + mController.init(mPreferenceManager, SUB_ID); + mController.mPreference = mPreference; + mPreference.setKey(mController.getPreferenceKey()); + } + + @Test + public void onPreferenceChange_selectNV_returnNVMode() { + doReturn(true).when(mTelephonyManager).setCdmaSubscriptionMode(anyInt()); + + mController.onPreferenceChange(mPreference, Integer.toString(Phone.CDMA_SUBSCRIPTION_NV)); + + assertThat(Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, + Phone.CDMA_SUBSCRIPTION_RUIM_SIM)).isEqualTo(Phone.CDMA_SUBSCRIPTION_NV); + } + + @Test + public void updateState_stateRUIM_displayRUIM() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, Phone.CDMA_SUBSCRIPTION_NV); + + mController.updateState(mPreference); + + assertThat(mPreference.getValue()).isEqualTo(Integer.toString(Phone.CDMA_SUBSCRIPTION_NV)); + } + + @Test + public void updateState_stateUnknown_doNothing() { + mPreference.setValue(Integer.toString(Phone.CDMA_SUBSCRIPTION_NV)); + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, Phone.CDMA_SUBSCRIPTION_UNKNOWN); + + mController.updateState(mPreference); + + // Still NV mode + assertThat(mPreference.getValue()).isEqualTo(Integer.toString(Phone.CDMA_SUBSCRIPTION_NV)); + } + + @Test + public void deviceSupportsNvAndRuim() { + SystemProperties.set("ril.subscription.types", "NV,RUIM"); + assertThat(mController.deviceSupportsNvAndRuim()).isTrue(); + + SystemProperties.set("ril.subscription.types", ""); + + assertThat(mController.deviceSupportsNvAndRuim()).isFalse(); + } +}