Merge "Use getPrivateDnsMode() to get the value of private DNS"
diff --git a/src/com/android/settings/applications/assist/OWNERS b/src/com/android/settings/applications/assist/OWNERS
new file mode 100644
index 0000000..22e486d
--- /dev/null
+++ b/src/com/android/settings/applications/assist/OWNERS
@@ -0,0 +1,9 @@
+# Default reviewers for this and subdirectories.
+adamhe@google.com
+augale@google.com
+joannechung@google.com
+lpeter@google.com
+svetoslavganov@google.com
+tymtsai@google.com
+
+# Emergency approvers in case the above are not available
diff --git a/src/com/android/settings/applications/autofill/OWNERS b/src/com/android/settings/applications/autofill/OWNERS
new file mode 100644
index 0000000..22e486d
--- /dev/null
+++ b/src/com/android/settings/applications/autofill/OWNERS
@@ -0,0 +1,9 @@
+# Default reviewers for this and subdirectories.
+adamhe@google.com
+augale@google.com
+joannechung@google.com
+lpeter@google.com
+svetoslavganov@google.com
+tymtsai@google.com
+
+# Emergency approvers in case the above are not available
diff --git a/src/com/android/settings/datausage/OWNERS b/src/com/android/settings/datausage/OWNERS
index 1ec66b3..d71b165 100644
--- a/src/com/android/settings/datausage/OWNERS
+++ b/src/com/android/settings/datausage/OWNERS
@@ -1,6 +1,7 @@
# Default reviewers for this and subdirectories.
andychou@google.com
bonianchen@google.com
+changbetty@google.com
goldmanj@google.com
songferngwang@google.com
wengsu@google.com
diff --git a/src/com/android/settings/development/AdbIpAddressPreferenceController.java b/src/com/android/settings/development/AdbIpAddressPreferenceController.java
index dbc329e..45fe51b 100644
--- a/src/com/android/settings/development/AdbIpAddressPreferenceController.java
+++ b/src/com/android/settings/development/AdbIpAddressPreferenceController.java
@@ -126,7 +126,7 @@
* @return the formatted and newline-separated IP addresses, or null if none.
*/
private static String getDefaultIpAddresses(ConnectivityManager cm) {
- LinkProperties prop = cm.getActiveLinkProperties();
+ LinkProperties prop = cm.getLinkProperties(cm.getActiveNetwork());
return formatIpAddresses(prop);
}
diff --git a/src/com/android/settings/inputmethod/OWNERS b/src/com/android/settings/inputmethod/OWNERS
index 9f33394..556de5f 100644
--- a/src/com/android/settings/inputmethod/OWNERS
+++ b/src/com/android/settings/inputmethod/OWNERS
@@ -1,5 +1,7 @@
-# Default reviewers for this and subdirectories.
-yukawa@google.com
-michaelwr@google.com
+# Bug component: 34867
+include platform/frameworks/base:/services/core/java/com/android/server/inputmethod/OWNERS
-# Emergency approvers in case the above are not available
\ No newline at end of file
+# Settings for physical keyboard and game pad are better to be reviewed by the input team
+per-file GameControllerPreferenceController.java = file: platform/frameworks/base:/services/core/java/com/android/server/input/OWNERS
+per-file KeyboardLayoutPicker*.java = file: platform/frameworks/base:/services/core/java/com/android/server/input/OWNERS
+per-file PhysicalKeyboard*.java = file: platform/frameworks/base:/services/core/java/com/android/server/input/OWNERS
diff --git a/src/com/android/settings/network/AllowedNetworkTypesListener.java b/src/com/android/settings/network/AllowedNetworkTypesListener.java
new file mode 100644
index 0000000..a9ed8be
--- /dev/null
+++ b/src/com/android/settings/network/AllowedNetworkTypesListener.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2020 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;
+
+import android.content.Context;
+import android.telephony.PhoneStateListener;
+import android.telephony.TelephonyCallback;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+
+import androidx.annotation.VisibleForTesting;
+
+import java.util.Map;
+import java.util.concurrent.Executor;
+
+
+/**
+ * {@link TelephonyCallback} to listen to Allowed Network Types changed
+ */
+public class AllowedNetworkTypesListener extends TelephonyCallback implements
+ TelephonyCallback.AllowedNetworkTypesListener {
+ private static final String LOG_TAG = "NetworkModeListener";
+
+ @VisibleForTesting
+ OnAllowedNetworkTypesListener mListener;
+ private long mAllowedNetworkType = -1;
+ private Executor mExecutor;
+
+ public AllowedNetworkTypesListener(Executor executor) {
+ super();
+ mExecutor = executor;
+ }
+
+ public void setAllowedNetworkTypesListener(OnAllowedNetworkTypesListener lsn) {
+ mListener = lsn;
+ }
+
+ /**
+ * Register a PhoneStateListener for Allowed Network Types changed.
+ * @param context the Context
+ * @param subId the subscription id.
+ */
+ public void register(Context context, int subId) {
+ TelephonyManager telephonyManager = context.getSystemService(
+ TelephonyManager.class).createForSubscriptionId(subId);
+ telephonyManager.registerTelephonyCallback(mExecutor, this);
+ }
+
+ /**
+ * Unregister a PhoneStateListener for Allowed Network Types changed.
+ * @param context the Context
+ * @param subId the subscription id.
+ */
+ public void unregister(Context context, int subId) {
+ TelephonyManager telephonyManager = context.getSystemService(
+ TelephonyManager.class).createForSubscriptionId(subId);
+ telephonyManager.unregisterTelephonyCallback(this);
+ }
+
+ @Override
+ public void onAllowedNetworkTypesChanged(Map<Integer, Long> allowedNetworkTypesList) {
+ long newAllowedNetworkType = allowedNetworkTypesList.get(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER);
+ if (mListener != null && mAllowedNetworkType != newAllowedNetworkType) {
+ mListener.onAllowedNetworkTypesChanged();
+ Log.d(LOG_TAG, "onAllowedNetworkChanged: " + mAllowedNetworkType);
+ }
+ mAllowedNetworkType = newAllowedNetworkType;
+ }
+
+ /**
+ * Listener for update of Preferred Network Mode change
+ */
+ public interface OnAllowedNetworkTypesListener {
+ /**
+ * Notify the allowed network type changed.
+ */
+ void onAllowedNetworkTypesChanged();
+ }
+}
diff --git a/src/com/android/settings/network/OWNERS b/src/com/android/settings/network/OWNERS
index 8b16bc9..ee5b494 100644
--- a/src/com/android/settings/network/OWNERS
+++ b/src/com/android/settings/network/OWNERS
@@ -2,6 +2,7 @@
allenwtsu@google.com
andychou@google.com
bonianchen@google.com
+changbetty@google.com
goldmanj@google.com
leechou@google.com
songferngwang@google.com
diff --git a/src/com/android/settings/network/PreferredNetworkModeContentObserver.java b/src/com/android/settings/network/PreferredNetworkModeContentObserver.java
deleted file mode 100644
index 9d07eb4..0000000
--- a/src/com/android/settings/network/PreferredNetworkModeContentObserver.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2020 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;
-
-import android.content.Context;
-import android.database.ContentObserver;
-import android.net.Uri;
-import android.os.Handler;
-import android.provider.Settings;
-
-import androidx.annotation.VisibleForTesting;
-
-/**
- * {@link ContentObserver} to listen to Preferred Network Mode change
- */
-public class PreferredNetworkModeContentObserver extends ContentObserver {
- @VisibleForTesting
- OnPreferredNetworkModeChangedListener mListener;
-
- public PreferredNetworkModeContentObserver(Handler handler) {
- super(handler);
- }
-
- public void setPreferredNetworkModeChangedListener(OnPreferredNetworkModeChangedListener lsn) {
- mListener = lsn;
- }
-
- @Override
- public void onChange(boolean selfChange) {
- super.onChange(selfChange);
- if (mListener != null) {
- mListener.onPreferredNetworkModeChanged();
- }
- }
-
- 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);
- }
-
- /**
- * Listener for update of Preferred Network Mode change
- */
- public interface OnPreferredNetworkModeChangedListener {
- void onPreferredNetworkModeChanged();
- }
-}
diff --git a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
index fd585a1..c65a972 100644
--- a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
@@ -20,10 +20,7 @@
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import android.content.Context;
-import android.os.Handler;
-import android.os.Looper;
import android.os.PersistableBundle;
-import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -37,7 +34,7 @@
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
-import com.android.settings.network.PreferredNetworkModeContentObserver;
+import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
import java.util.ArrayList;
@@ -53,7 +50,7 @@
ListPreference.OnPreferenceChangeListener, LifecycleObserver {
private static final String LOG_TAG = "EnabledNetworkMode";
- private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
private Preference mPreference;
private PreferenceScreen mPreferenceScreen;
private TelephonyManager mTelephonyManager;
@@ -88,18 +85,18 @@
@OnLifecycleEvent(ON_START)
public void onStart() {
- if (mPreferredNetworkModeObserver == null) {
+ if (mAllowedNetworkTypesListener == null) {
return;
}
- mPreferredNetworkModeObserver.register(mContext, mSubId);
+ mAllowedNetworkTypesListener.register(mContext, mSubId);
}
@OnLifecycleEvent(ON_STOP)
public void onStop() {
- if (mPreferredNetworkModeObserver == null) {
+ if (mAllowedNetworkTypesListener == null) {
return;
}
- mPreferredNetworkModeObserver.unregister(mContext);
+ mAllowedNetworkTypesListener.unregister(mContext, mSubId);
}
@Override
@@ -145,10 +142,10 @@
mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class);
mBuilder = new PreferenceEntriesBuilder(mContext, mSubId);
- if (mPreferredNetworkModeObserver == null) {
- mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
- new Handler(Looper.getMainLooper()));
- mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+ if (mAllowedNetworkTypesListener == null) {
+ mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
+ mContext.getMainExecutor());
+ mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
() -> updatePreference());
}
@@ -164,7 +161,22 @@
}
}
- private final static class PreferenceEntriesBuilder {
+ enum EnabledNetworks {
+ ENABLED_NETWORKS_UNKNOWN,
+ ENABLED_NETWORKS_CDMA_CHOICES,
+ ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES,
+ ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES,
+ ENABLED_NETWORKS_TDSCDMA_CHOICES,
+ ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES,
+ ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES,
+ ENABLED_NETWORKS_EXCEPT_GSM_CHOICES,
+ ENABLED_NETWORKS_EXCEPT_LTE_CHOICES,
+ ENABLED_NETWORKS_4G_CHOICES,
+ ENABLED_NETWORKS_CHOICES,
+ PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE
+ }
+
+ private final class PreferenceEntriesBuilder {
private CarrierConfigManager mCarrierConfigManager;
private Context mContext;
private TelephonyManager mTelephonyManager;
@@ -181,21 +193,6 @@
private List<String> mEntries = new ArrayList<>();
private List<Integer> mEntriesValue = new ArrayList<>();
- enum EnabledNetworks {
- ENABLED_NETWORKS_UNKNOWN,
- ENABLED_NETWORKS_CDMA_CHOICES,
- ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES,
- ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES,
- ENABLED_NETWORKS_TDSCDMA_CHOICES,
- ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES,
- ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES,
- ENABLED_NETWORKS_EXCEPT_GSM_CHOICES,
- ENABLED_NETWORKS_EXCEPT_LTE_CHOICES,
- ENABLED_NETWORKS_4G_CHOICES,
- ENABLED_NETWORKS_CHOICES,
- PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE
- }
-
PreferenceEntriesBuilder(Context context, int subId) {
this.mContext = context;
this.mSubId = subId;
@@ -205,13 +202,12 @@
.createForSubscriptionId(mSubId);
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
- final boolean isNrEnabledFromCarrierConfig = carrierConfig != null
- && carrierConfig.getBoolean(CarrierConfigManager.KEY_NR_ENABLED_BOOL);
mAllowed5gNetworkType = checkSupportedRadioBitmask(
- mTelephonyManager.getAllowedNetworkTypes(),
+ mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_CARRIER),
TelephonyManager.NETWORK_TYPE_BITMASK_NR);
- mSupported5gRadioAccessFamily = isNrEnabledFromCarrierConfig
- && checkSupportedRadioBitmask(mTelephonyManager.getSupportedRadioAccessFamily(),
+ mSupported5gRadioAccessFamily = checkSupportedRadioBitmask(
+ mTelephonyManager.getSupportedRadioAccessFamily(),
TelephonyManager.NETWORK_TYPE_BITMASK_NR);
mIsGlobalCdma = mTelephonyManager.isLteCdmaEvdoGsmWcdmaEnabled()
&& carrierConfig != null
@@ -226,7 +222,7 @@
int[] entryValuesInt;
switch (getEnabledNetworkType()) {
case ENABLED_NETWORKS_CDMA_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_cdma_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 4) {
@@ -240,7 +236,7 @@
addGlobalEntry(entryValuesInt[3]);
break;
case ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_cdma_no_lte_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 2) {
@@ -251,7 +247,7 @@
add1xEntry(entryValuesInt[1]);
break;
case ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_cdma_only_lte_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 2) {
@@ -262,7 +258,7 @@
addGlobalEntry(entryValuesInt[1]);
break;
case ENABLED_NETWORKS_TDSCDMA_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_tdscdma_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 3) {
@@ -275,7 +271,7 @@
add2gEntry(entryValuesInt[2]);
break;
case ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_except_gsm_lte_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 1) {
@@ -285,7 +281,7 @@
add3gEntry(entryValuesInt[0]);
break;
case ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_except_gsm_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 2) {
@@ -297,7 +293,7 @@
add3gEntry(entryValuesInt[1]);
break;
case ENABLED_NETWORKS_EXCEPT_GSM_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_except_gsm_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 2) {
@@ -309,7 +305,7 @@
add3gEntry(entryValuesInt[1]);
break;
case ENABLED_NETWORKS_EXCEPT_LTE_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_except_lte_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 2) {
@@ -320,7 +316,7 @@
add2gEntry(entryValuesInt[1]);
break;
case ENABLED_NETWORKS_4G_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 3) {
@@ -334,7 +330,7 @@
add2gEntry(entryValuesInt[2]);
break;
case ENABLED_NETWORKS_CHOICES:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.enabled_networks_values);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 3) {
@@ -346,7 +342,7 @@
add2gEntry(entryValuesInt[2]);
break;
case PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE:
- entryValues = mContext.getResources().getStringArray(
+ entryValues = getResourcesForSubId().getStringArray(
R.array.preferred_network_mode_values_world_mode);
entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
if (entryValuesInt.length < 3) {
@@ -354,9 +350,14 @@
"PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE index error.");
}
addGlobalEntry(entryValuesInt[0]);
- addCustomEntry(mContext.getString(R.string.network_world_mode_cdma_lte),
+
+ addCustomEntry(
+ getResourcesForSubId().getString(
+ R.string.network_world_mode_cdma_lte),
entryValuesInt[1]);
- addCustomEntry(mContext.getString(R.string.network_world_mode_gsm_lte),
+ addCustomEntry(
+ getResourcesForSubId().getString(
+ R.string.network_world_mode_gsm_lte),
entryValuesInt[2]);
break;
default:
@@ -365,9 +366,9 @@
}
private int getPreferredNetworkMode() {
- int networkMode = Settings.Global.getInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + mSubId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ int networkMode = MobileNetworkUtils.getNetworkTypeFromRaf(
+ (int) mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
if (!showNrList()) {
Log.d(LOG_TAG, "Network mode :" + networkMode + " reduce NR");
networkMode = reduceNrToLteNetworkType(networkMode);
@@ -447,7 +448,7 @@
* Sets the display string for the network mode choice and selects the corresponding item
*
* @param networkMode the current network mode. The current mode might not be an option in
- * the choice list. The nearest choice is selected instead
+ * the choice list. The nearest choice is selected instead
*/
void setPreferenceValueAndSummary(int networkMode) {
setSelectedEntry(networkMode);
@@ -571,8 +572,9 @@
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_WCDMA:
setSelectedEntry(
TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
- setSummary(mContext.getString(R.string.network_5G)
- + mContext.getString(R.string.network_recommended));
+ setSummary(getResourcesForSubId().getString(R.string.network_5G)
+ + getResourcesForSubId().getString(
+ R.string.network_recommended));
break;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA:
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM:
@@ -581,13 +583,15 @@
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
setSelectedEntry(TelephonyManagerConstants
.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA);
- setSummary(mContext.getString(R.string.network_5G)
- + mContext.getString(R.string.network_recommended));
+ setSummary(getResourcesForSubId().getString(R.string.network_5G)
+ + getResourcesForSubId().getString(
+ R.string.network_recommended));
break;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO:
setSelectedEntry(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO);
- setSummary(mContext.getString(R.string.network_5G)
- + mContext.getString(R.string.network_recommended));
+ setSummary(getResourcesForSubId().getString(R.string.network_5G)
+ + getResourcesForSubId().getString(
+ R.string.network_recommended));
break;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA:
setSelectedEntry(
@@ -597,13 +601,15 @@
|| MobileNetworkUtils.isWorldMode(mContext, mSubId)) {
setSummary(R.string.network_global);
} else {
- setSummary(mContext.getString(R.string.network_5G)
- + mContext.getString(R.string.network_recommended));
+ setSummary(getResourcesForSubId().getString(R.string.network_5G)
+ + getResourcesForSubId().getString(
+ R.string.network_recommended));
}
break;
default:
setSummary(
- mContext.getString(R.string.mobile_network_mode_error, networkMode));
+ getResourcesForSubId().getString(
+ R.string.mobile_network_mode_error, networkMode));
}
}
@@ -613,7 +619,7 @@
* @param networkType an LTE network mode without 5G.
* @return the corresponding network mode with 5G.
*/
- private static int addNrToLteNetworkType(int networkType) {
+ private int addNrToLteNetworkType(int networkType) {
switch(networkType) {
case TelephonyManagerConstants.NETWORK_MODE_LTE_ONLY:
return TelephonyManagerConstants.NETWORK_MODE_NR_LTE;
@@ -647,7 +653,7 @@
* @param networkType an 5G network mode.
* @return the corresponding network mode without 5G.
*/
- private static int reduceNrToLteNetworkType(int networkType) {
+ private int reduceNrToLteNetworkType(int networkType) {
switch(networkType) {
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE:
return TelephonyManagerConstants.NETWORK_MODE_LTE_ONLY;
@@ -689,8 +695,8 @@
private void add5gEntry(int value) {
boolean isNRValue = value >= TelephonyManagerConstants.NETWORK_MODE_NR_ONLY;
if (showNrList() && isNRValue) {
- mEntries.add(mContext.getString(R.string.network_5G)
- + mContext.getString(R.string.network_recommended));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_5G)
+ + getResourcesForSubId().getString(R.string.network_recommended));
mEntriesValue.add(value);
mIs5gEntryDisplayed = true;
} else {
@@ -706,7 +712,7 @@
Log.d(LOG_TAG, "addGlobalEntry. "
+ " supported5GRadioAccessFamily: " + mSupported5gRadioAccessFamily
+ " allowed5GNetworkType: " + mAllowed5gNetworkType);
- mEntries.add(mContext.getString(R.string.network_global));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_global));
if (showNrList()) {
value = addNrToLteNetworkType(value);
}
@@ -722,9 +728,9 @@
*/
private void addLteEntry(int value) {
if (showNrList()) {
- mEntries.add(mContext.getString(R.string.network_lte_pure));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_lte_pure));
} else {
- mEntries.add(mContext.getString(R.string.network_lte));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_lte));
}
mEntriesValue.add(value);
}
@@ -734,25 +740,25 @@
*/
private void add4gEntry(int value) {
if (showNrList()) {
- mEntries.add(mContext.getString(R.string.network_4G_pure));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_4G_pure));
} else {
- mEntries.add(mContext.getString(R.string.network_4G));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_4G));
}
mEntriesValue.add(value);
}
private void add3gEntry(int value) {
- mEntries.add(mContext.getString(R.string.network_3G));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_3G));
mEntriesValue.add(value);
}
private void add2gEntry(int value) {
- mEntries.add(mContext.getString(R.string.network_2G));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_2G));
mEntriesValue.add(value);
}
private void add1xEntry(int value) {
- mEntries.add(mContext.getString(R.string.network_1x));
+ mEntries.add(getResourcesForSubId().getString(R.string.network_1x));
mEntriesValue.add(value);
}
@@ -771,7 +777,7 @@
}
private String[] getEntryValues() {
- final Integer [] intArr = mEntriesValue.toArray(new Integer[0]);
+ final Integer[] intArr = mEntriesValue.toArray(new Integer[0]);
return Arrays.stream(intArr)
.map(String::valueOf)
.toArray(String[]::new);
@@ -800,7 +806,7 @@
}
private void setSummary(int summaryResId) {
- setSummary(mContext.getString(summaryResId));
+ setSummary(getResourcesForSubId().getString(summaryResId));
}
private void setSummary(String summary) {
diff --git a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
index ba12146..989cb02 100644
--- a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
@@ -95,8 +95,9 @@
mShow5gLimitedDialog = carrierConfig.getBoolean(
CarrierConfigManager.KEY_VOLTE_5G_LIMITED_ALERT_DIALOG_BOOL);
- mIsNrEnabledFromCarrierConfig = carrierConfig.getBoolean(
- CarrierConfigManager.KEY_NR_ENABLED_BOOL);
+ mIsNrEnabledFromCarrierConfig = carrierConfig.getInt(
+ CarrierConfigManager.KEY_CARRIER_NR_AVAILABILITY_INT)
+ != CarrierConfigManager.CARRIER_NR_AVAILABILITY_NONE;
return this;
}
diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
index c61c222..2f0695d 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
@@ -339,10 +339,10 @@
}
if (isWorldMode(context, subId)) {
- final int settingsNetworkMode = android.provider.Settings.Global.getInt(
- context.getContentResolver(),
- android.provider.Settings.Global.PREFERRED_NETWORK_MODE + subId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ final int settingsNetworkMode = getNetworkTypeFromRaf(
+ (int) telephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
+
if (settingsNetworkMode == NETWORK_MODE_LTE_GSM_WCDMA
|| settingsNetworkMode == NETWORK_MODE_LTE_CDMA_EVDO
|| settingsNetworkMode == NETWORK_MODE_NR_LTE_GSM_WCDMA
@@ -368,10 +368,11 @@
if (isGsmBasicOptions(context, subId)) {
return true;
}
- final int networkMode = android.provider.Settings.Global.getInt(
- context.getContentResolver(),
- android.provider.Settings.Global.PREFERRED_NETWORK_MODE + subId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
+ .createForSubscriptionId(subId);
+ final int networkMode = getNetworkTypeFromRaf(
+ (int) telephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
if (isWorldMode(context, subId)) {
if (networkMode == NETWORK_MODE_LTE_CDMA_EVDO
|| networkMode == NETWORK_MODE_LTE_GSM_WCDMA
@@ -435,10 +436,9 @@
return false;
}
- final int networkMode = android.provider.Settings.Global.getInt(
- context.getContentResolver(),
- android.provider.Settings.Global.PREFERRED_NETWORK_MODE + subId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ final int networkMode = getNetworkTypeFromRaf(
+ (int) telephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
if (networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO
&& isWorldMode(context, subId)) {
return false;
@@ -550,15 +550,17 @@
*/
@VisibleForTesting
static boolean shouldSpeciallyUpdateGsmCdma(Context context, int subId) {
- final int networkMode = android.provider.Settings.Global.getInt(
- context.getContentResolver(),
- android.provider.Settings.Global.PREFERRED_NETWORK_MODE + subId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
+ .createForSubscriptionId(subId);
+ final int networkMode = getNetworkTypeFromRaf(
+ (int) telephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
if (networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM
|| networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA
|| networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA
|| networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA
- || networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
+ || networkMode
+ == TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
|| networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA) {
if (!isTdscdmaSupported(context, subId) && isWorldMode(context, subId)) {
return true;
diff --git a/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java b/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java
index 042eb5f..4ebcb7a 100644
--- a/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java
+++ b/src/com/android/settings/network/telephony/NetworkPreferenceCategoryController.java
@@ -20,8 +20,6 @@
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import android.content.Context;
-import android.os.Handler;
-import android.os.Looper;
import android.telephony.SubscriptionManager;
import androidx.lifecycle.Lifecycle;
@@ -29,7 +27,7 @@
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceScreen;
-import com.android.settings.network.PreferredNetworkModeContentObserver;
+import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.widget.PreferenceCategoryController;
/**
@@ -39,15 +37,15 @@
implements LifecycleObserver {
private PreferenceScreen mPreferenceScreen;
- private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
protected int mSubId;
public NetworkPreferenceCategoryController(Context context, String key) {
super(context, key);
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
- mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
- new Handler(Looper.getMainLooper()));
- mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+ mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
+ context.getMainExecutor());
+ mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
() -> updatePreference());
}
@@ -57,12 +55,12 @@
@OnLifecycleEvent(ON_START)
public void onStart() {
- mPreferredNetworkModeObserver.register(mContext, mSubId);
+ mAllowedNetworkTypesListener.register(mContext, mSubId);
}
@OnLifecycleEvent(ON_STOP)
public void onStop() {
- mPreferredNetworkModeObserver.unregister(mContext);
+ mAllowedNetworkTypesListener.unregister(mContext, mSubId);
}
@Override
diff --git a/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
index ff6ff59..2419779 100644
--- a/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
@@ -18,7 +18,6 @@
import android.content.Context;
import android.os.PersistableBundle;
-import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -80,17 +79,13 @@
public boolean onPreferenceChange(Preference preference, Object object) {
final int newPreferredNetworkMode = Integer.parseInt((String) object);
- if (mTelephonyManager.setPreferredNetworkTypeBitmask(
- MobileNetworkUtils.getRafFromNetworkType(newPreferredNetworkMode))) {
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + mSubId,
- newPreferredNetworkMode);
+ mTelephonyManager.setAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
+ MobileNetworkUtils.getRafFromNetworkType(newPreferredNetworkMode));
+
final ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(getPreferredNetworkModeSummaryResId(newPreferredNetworkMode));
return true;
- }
-
- return false;
}
public void init(int subId) {
@@ -104,9 +99,9 @@
}
private int getPreferredNetworkMode() {
- return Settings.Global.getInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + mSubId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+ return MobileNetworkUtils.getNetworkTypeFromRaf(
+ (int) mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
}
private int getPreferredNetworkModeSummaryResId(int NetworkMode) {
diff --git a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
index 2bd7de9..3972f39 100644
--- a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
@@ -17,6 +17,7 @@
package com.android.settings.network.telephony;
import android.content.Context;
+import android.content.res.Resources;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
@@ -74,4 +75,13 @@
mContext.getSystemService(CarrierConfigManager.class);
return carrierConfigMgr.getConfigForSubId(subId);
}
+
+ /**
+ * Returns the resources associated with Subscription.
+ *
+ * @return Resources associated with Subscription.
+ */
+ public Resources getResourcesForSubId() {
+ return SubscriptionManager.getResourcesForSubId(mContext, mSubId);
+ }
}
diff --git a/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java
index a100d70..4d1d319 100644
--- a/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/cdma/CdmaBasePreferenceController.java
@@ -17,11 +17,6 @@
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.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -29,6 +24,7 @@
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
+import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.TelephonyBasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -44,22 +40,25 @@
protected Preference mPreference;
protected TelephonyManager mTelephonyManager;
protected PreferenceManager mPreferenceManager;
- private DataContentObserver mDataContentObserver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
public CdmaBasePreferenceController(Context context, String key) {
super(context, key);
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
- mDataContentObserver = new DataContentObserver(new Handler(Looper.getMainLooper()));
}
@Override
public void onStart() {
- mDataContentObserver.register(mContext, mSubId);
+ if (mAllowedNetworkTypesListener != null) {
+ mAllowedNetworkTypesListener.register(mContext, mSubId);
+ }
}
@Override
public void onStop() {
- mDataContentObserver.unRegister(mContext);
+ if (mAllowedNetworkTypesListener != null) {
+ mAllowedNetworkTypesListener.unregister(mContext, mSubId);
+ }
}
@Override
@@ -74,12 +73,25 @@
mSubId = subId;
mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
.createForSubscriptionId(mSubId);
+
+ if (mAllowedNetworkTypesListener == null) {
+ mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
+ mContext.getMainExecutor());
+ mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
+ () -> updatePreference());
+ }
}
public void init(int subId) {
init(null, subId);
}
+ private void updatePreference() {
+ if (mPreference != null) {
+ updateState(mPreference);
+ }
+ }
+
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
@@ -88,30 +100,4 @@
((CdmaListPreference) mPreference).setSubId(mSubId);
}
}
-
- /**
- * 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/CdmaSystemSelectPreferenceController.java b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java
index 2b0c221..5dd4453 100644
--- a/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java
+++ b/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceController.java
@@ -26,6 +26,8 @@
import androidx.preference.ListPreference;
import androidx.preference.Preference;
+import com.android.settings.network.telephony.MobileNetworkUtils;
+
/**
* Preference controller for "System Select"
*/
@@ -50,10 +52,10 @@
resetCdmaRoamingModeToDefault();
}
}
- final int settingsNetworkMode = Settings.Global.getInt(
- mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + mSubId,
- TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+
+ final int settingsNetworkMode = MobileNetworkUtils.getNetworkTypeFromRaf(
+ (int) mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
final boolean enableList = settingsNetworkMode != NETWORK_MODE_LTE_GSM_WCDMA
&& settingsNetworkMode != NETWORK_MODE_NR_LTE_GSM_WCDMA;
listPreference.setEnabled(enableList);
diff --git a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
index d981362..5bcc655 100644
--- a/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/AutoSelectPreferenceController.java
@@ -24,6 +24,7 @@
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
+import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.PersistableBundle;
import android.os.SystemClock;
@@ -43,7 +44,7 @@
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
-import com.android.settings.network.PreferredNetworkModeContentObserver;
+import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.NetworkSelectSettings;
import com.android.settings.network.telephony.TelephonyTogglePreferenceController;
@@ -62,7 +63,7 @@
private final Handler mUiHandler;
private PreferenceScreen mPreferenceScreen;
- private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
private TelephonyManager mTelephonyManager;
private boolean mOnlyAutoSelectInHome;
private List<OnNetworkSelectModeListener> mListeners;
@@ -77,8 +78,9 @@
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
mListeners = new ArrayList<>();
mUiHandler = new Handler(Looper.getMainLooper());
- mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(mUiHandler);
- mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+ mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
+ new HandlerExecutor(mUiHandler));
+ mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
() -> updatePreference());
}
@@ -93,12 +95,12 @@
@OnLifecycleEvent(ON_START)
public void onStart() {
- mPreferredNetworkModeObserver.register(mContext, mSubId);
+ mAllowedNetworkTypesListener.register(mContext, mSubId);
}
@OnLifecycleEvent(ON_STOP)
public void onStop() {
- mPreferredNetworkModeObserver.unregister(mContext);
+ mAllowedNetworkTypesListener.unregister(mContext, mSubId);
}
@Override
diff --git a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
index 2cc5bf1..cdeb1e5 100644
--- a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
@@ -22,8 +22,6 @@
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
-import android.os.Handler;
-import android.os.Looper;
import android.provider.Settings;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
@@ -38,7 +36,7 @@
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
-import com.android.settings.network.PreferredNetworkModeContentObserver;
+import com.android.settings.network.AllowedNetworkTypesListener;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.NetworkSelectSettings;
import com.android.settings.network.telephony.TelephonyBasePreferenceController;
@@ -54,15 +52,15 @@
private TelephonyManager mTelephonyManager;
private Preference mPreference;
private PreferenceScreen mPreferenceScreen;
- private PreferredNetworkModeContentObserver mPreferredNetworkModeObserver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
public OpenNetworkSelectPagePreferenceController(Context context, String key) {
super(context, key);
mTelephonyManager = context.getSystemService(TelephonyManager.class);
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
- mPreferredNetworkModeObserver = new PreferredNetworkModeContentObserver(
- new Handler(Looper.getMainLooper()));
- mPreferredNetworkModeObserver.setPreferredNetworkModeChangedListener(
+ mAllowedNetworkTypesListener = new AllowedNetworkTypesListener(
+ context.getMainExecutor());
+ mAllowedNetworkTypesListener.setAllowedNetworkTypesListener(
() -> updatePreference());
}
@@ -85,12 +83,12 @@
@OnLifecycleEvent(ON_START)
public void onStart() {
- mPreferredNetworkModeObserver.register(mContext, mSubId);
+ mAllowedNetworkTypesListener.register(mContext, mSubId);
}
@OnLifecycleEvent(ON_STOP)
public void onStop() {
- mPreferredNetworkModeObserver.unregister(mContext);
+ mAllowedNetworkTypesListener.unregister(mContext, mSubId);
}
@Override
diff --git a/src/com/android/settings/utils/AndroidKeystoreAliasLoader.java b/src/com/android/settings/utils/AndroidKeystoreAliasLoader.java
new file mode 100644
index 0000000..bb9abb9
--- /dev/null
+++ b/src/com/android/settings/utils/AndroidKeystoreAliasLoader.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2021 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.utils;
+
+import android.os.Process;
+import android.security.keystore.AndroidKeyStoreProvider;
+import android.security.keystore.KeyProperties;
+import android.security.keystore2.AndroidKeyStoreLoadStoreParameter;
+import android.util.Log;
+
+import java.security.Key;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.Certificate;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+
+/**
+ * This class provides a portable and unified way to load the content of AndroidKeyStore through
+ * public API.
+ * @hide
+ */
+public class AndroidKeystoreAliasLoader {
+ private static final String TAG = "SettingsKeystoreUtils";
+
+ private final Collection<String> mKeyCertAliases;
+ private final Collection<String> mCaCertAliases;
+ /**
+ * This Constructor loads all aliases of asymmetric key pairs and certificates in the
+ * AndroidKeyStore within the given namespace.
+ * Viable namespaces are {@link KeyProperties#NAMESPACE_WIFI},
+ * {@link KeyProperties#NAMESPACE_APPLICATION}, or null. The latter two are equivalent in
+ * that they will load the keystore content of the app's own namespace. In case of settings,
+ * this is the namespace of the AID_SYSTEM.
+ *
+ * @param namespace {@link KeyProperties#NAMESPACE_WIFI},
+ * {@link KeyProperties#NAMESPACE_APPLICATION}, or null
+ * @hide
+ */
+ public AndroidKeystoreAliasLoader(Integer namespace) {
+ mKeyCertAliases = new ArrayList<>();
+ mCaCertAliases = new ArrayList<>();
+ KeyStore keyStore = null;
+ final Enumeration<String> aliases;
+ try {
+ if (namespace != null && namespace != KeyProperties.NAMESPACE_APPLICATION) {
+ if (AndroidKeyStoreProvider.isKeystore2Enabled()) {
+ keyStore = KeyStore.getInstance("AndroidKeyStore");
+ keyStore.load(new AndroidKeyStoreLoadStoreParameter(namespace));
+ } else {
+ // In the legacy case we pass in the WIFI UID because that is the only
+ // possible special namespace that existed as of this writing,
+ // and new namespaces must only be added using the new mechanism.
+ keyStore = AndroidKeyStoreProvider.getKeyStoreForUid(Process.WIFI_UID);
+ }
+ } else {
+ keyStore = KeyStore.getInstance("AndroidKeyStore");
+ keyStore.load(null);
+ }
+ aliases = keyStore.aliases();
+ } catch (Exception e) {
+ Log.e(TAG, "Failed to open Android Keystore.", e);
+ // Will return empty lists.
+ return;
+ }
+
+ while (aliases.hasMoreElements()) {
+ final String alias = aliases.nextElement();
+ try {
+ final Key key = keyStore.getKey(alias, null);
+ if (key != null) {
+ if (key instanceof PrivateKey) {
+ mKeyCertAliases.add(alias);
+ final Certificate[] cert = keyStore.getCertificateChain(alias);
+ if (cert != null && cert.length >= 2) {
+ mCaCertAliases.add(alias);
+ }
+ }
+ } else {
+ if (keyStore.getCertificate(alias) != null) {
+ mCaCertAliases.add(alias);
+ }
+ }
+ } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
+ Log.e(TAG, "Failed to load alias: "
+ + alias + " from Android Keystore. Ignoring.", e);
+ }
+ }
+ }
+
+ /**
+ * Returns the aliases of the key pairs and certificates stored in the Android KeyStore at the
+ * time the constructor was called.
+ * @return Collection of keystore aliases.
+ * @hide
+ */
+ public Collection<String> getKeyCertAliases() {
+ return mKeyCertAliases;
+ }
+
+ /**
+ * Returns the aliases of the trusted certificates stored in the Android KeyStore at the
+ * time the constructor was called.
+ * @return Collection of keystore aliases.
+ * @hide
+ */
+ public Collection<String> getCaCertAliases() {
+ return mCaCertAliases;
+ }
+}
diff --git a/src/com/android/settings/vpn2/ConfigDialog.java b/src/com/android/settings/vpn2/ConfigDialog.java
index a0c7a1e..42bc67d 100644
--- a/src/com/android/settings/vpn2/ConfigDialog.java
+++ b/src/com/android/settings/vpn2/ConfigDialog.java
@@ -24,8 +24,6 @@
import android.net.ProxyInfo;
import android.os.Bundle;
import android.os.SystemProperties;
-import android.security.Credentials;
-import android.security.KeyStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
@@ -42,10 +40,12 @@
import com.android.internal.net.VpnProfile;
import com.android.net.module.util.ProxyUtils;
import com.android.settings.R;
+import com.android.settings.utils.AndroidKeystoreAliasLoader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
/**
@@ -58,7 +58,7 @@
class ConfigDialog extends AlertDialog implements TextWatcher,
View.OnClickListener, AdapterView.OnItemSelectedListener,
CompoundButton.OnCheckedChangeListener {
- private final KeyStore mKeyStore = KeyStore.getInstance();
+ private static final String TAG = "ConfigDialog";
private final DialogInterface.OnClickListener mListener;
private final VpnProfile mProfile;
@@ -153,10 +153,13 @@
mL2tpSecret.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Medium);
mIpsecIdentifier.setText(mProfile.ipsecIdentifier);
mIpsecSecret.setText(mProfile.ipsecSecret);
- loadCertificates(mIpsecUserCert, Credentials.USER_PRIVATE_KEY, 0, mProfile.ipsecUserCert);
- loadCertificates(mIpsecCaCert, Credentials.CA_CERTIFICATE,
+ AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
+ new AndroidKeystoreAliasLoader(null);
+ loadCertificates(mIpsecUserCert, androidKeystoreAliasLoader.getKeyCertAliases(), 0,
+ mProfile.ipsecUserCert);
+ loadCertificates(mIpsecCaCert, androidKeystoreAliasLoader.getCaCertAliases(),
R.string.vpn_no_ca_cert, mProfile.ipsecCaCert);
- loadCertificates(mIpsecServerCert, Credentials.USER_CERTIFICATE,
+ loadCertificates(mIpsecServerCert, androidKeystoreAliasLoader.getKeyCertAliases(),
R.string.vpn_no_server_cert, mProfile.ipsecServerCert);
mSaveLogin.setChecked(mProfile.saveLogin);
mAlwaysOnVpn.setChecked(mProfile.key.equals(VpnUtils.getLockdownVpn()));
@@ -511,27 +514,30 @@
typeSpinner.setAdapter(adapter);
}
- private void loadCertificates(Spinner spinner, String prefix, int firstId, String selected) {
+ private void loadCertificates(Spinner spinner, Collection<String> choices, int firstId,
+ String selected) {
Context context = getContext();
String first = (firstId == 0) ? "" : context.getString(firstId);
- String[] certificates = mKeyStore.list(prefix);
+ String[] myChoices;
- if (certificates == null || certificates.length == 0) {
- certificates = new String[] {first};
+ if (choices == null || choices.size() == 0) {
+ myChoices = new String[] {first};
} else {
- String[] array = new String[certificates.length + 1];
- array[0] = first;
- System.arraycopy(certificates, 0, array, 1, certificates.length);
- certificates = array;
+ myChoices = new String[choices.size() + 1];
+ myChoices[0] = first;
+ int i = 1;
+ for (String c : choices) {
+ myChoices[i++] = c;
+ }
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- context, android.R.layout.simple_spinner_item, certificates);
+ context, android.R.layout.simple_spinner_item, myChoices);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
- for (int i = 1; i < certificates.length; ++i) {
- if (certificates[i].equals(selected)) {
+ for (int i = 1; i < myChoices.length; ++i) {
+ if (myChoices[i].equals(selected)) {
spinner.setSelection(i);
break;
}
diff --git a/src/com/android/settings/vpn2/ConfigDialogFragment.java b/src/com/android/settings/vpn2/ConfigDialogFragment.java
index f339a69..b8825fe 100644
--- a/src/com/android/settings/vpn2/ConfigDialogFragment.java
+++ b/src/com/android/settings/vpn2/ConfigDialogFragment.java
@@ -25,7 +25,7 @@
import android.os.RemoteException;
import android.os.UserHandle;
import android.security.Credentials;
-import android.security.KeyStore;
+import android.security.LegacyVpnProfileStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
@@ -151,9 +151,8 @@
return;
}
- // Delete from KeyStore
- KeyStore keyStore = KeyStore.getInstance();
- keyStore.delete(Credentials.VPN + profile.key, KeyStore.UID_SELF);
+ // Delete from profile store.
+ LegacyVpnProfileStore.remove(Credentials.VPN + profile.key);
updateLockdownVpn(false, profile);
}
@@ -188,8 +187,7 @@
}
private void save(VpnProfile profile, boolean lockdown) {
- KeyStore.getInstance().put(Credentials.VPN + profile.key, profile.encode(),
- KeyStore.UID_SELF, /* flags */ 0);
+ LegacyVpnProfileStore.put(Credentials.VPN + profile.key, profile.encode());
// Flush out old version of profile
disconnect(profile);
diff --git a/src/com/android/settings/vpn2/VpnSettings.java b/src/com/android/settings/vpn2/VpnSettings.java
index 0a3b2b0..7635733 100644
--- a/src/com/android/settings/vpn2/VpnSettings.java
+++ b/src/com/android/settings/vpn2/VpnSettings.java
@@ -41,7 +41,7 @@
import android.os.UserHandle;
import android.os.UserManager;
import android.security.Credentials;
-import android.security.KeyStore;
+import android.security.LegacyVpnProfileStore;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
@@ -57,7 +57,6 @@
import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig;
import com.android.internal.net.VpnProfile;
-import com.android.internal.util.ArrayUtils;
import com.android.settings.R;
import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.widget.GearPreference;
@@ -94,8 +93,6 @@
private UserManager mUserManager;
private VpnManager mVpnManager;
- private final KeyStore mKeyStore = KeyStore.getInstance();
-
private Map<String, LegacyVpnPreference> mLegacyVpnPreferences = new ArrayMap<>();
private Map<AppVpnInfo, AppPreference> mAppPreferences = new ArrayMap<>();
@@ -222,7 +219,7 @@
final Context context = activity.getApplicationContext();
// Run heavy RPCs before switching to UI thread
- final List<VpnProfile> vpnProfiles = loadVpnProfiles(mKeyStore);
+ final List<VpnProfile> vpnProfiles = loadVpnProfiles();
final List<AppVpnInfo> vpnApps = getVpnApps(context, /* includeProfiles */ true);
final Map<String, LegacyVpnInfo> connectedLegacyVpns = getConnectedLegacyVpns();
@@ -540,12 +537,13 @@
return result;
}
- static List<VpnProfile> loadVpnProfiles(KeyStore keyStore, int... excludeTypes) {
+ private static List<VpnProfile> loadVpnProfiles() {
final ArrayList<VpnProfile> result = Lists.newArrayList();
- for (String key : keyStore.list(Credentials.VPN)) {
- final VpnProfile profile = VpnProfile.decode(key, keyStore.get(Credentials.VPN + key));
- if (profile != null && !ArrayUtils.contains(excludeTypes, profile.type)) {
+ for (String key : LegacyVpnProfileStore.list(Credentials.VPN)) {
+ final VpnProfile profile = VpnProfile.decode(key,
+ LegacyVpnProfileStore.get(Credentials.VPN + key));
+ if (profile != null) {
result.add(profile);
}
}
diff --git a/src/com/android/settings/vpn2/VpnUtils.java b/src/com/android/settings/vpn2/VpnUtils.java
index 4c9338c..d6a5578 100644
--- a/src/com/android/settings/vpn2/VpnUtils.java
+++ b/src/com/android/settings/vpn2/VpnUtils.java
@@ -20,7 +20,7 @@
import android.os.RemoteException;
import android.provider.Settings;
import android.security.Credentials;
-import android.security.KeyStore;
+import android.security.LegacyVpnProfileStore;
import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig;
@@ -28,27 +28,25 @@
/**
* Utility functions for vpn.
*
- * Keystore methods should only be called in system user
+ * LegacyVpnProfileStore methods should only be called in system user
*/
public class VpnUtils {
private static final String TAG = "VpnUtils";
public static String getLockdownVpn() {
- final byte[] value = KeyStore.getInstance().get(
- Credentials.LOCKDOWN_VPN, true /* suppressKeyNotFoundWarning */);
+ final byte[] value = LegacyVpnProfileStore.get(Credentials.LOCKDOWN_VPN);
return value == null ? null : new String(value);
}
public static void clearLockdownVpn(Context context) {
- KeyStore.getInstance().delete(Credentials.LOCKDOWN_VPN);
+ LegacyVpnProfileStore.remove(Credentials.LOCKDOWN_VPN);
// Always notify VpnManager after keystore update
getVpnManager(context).updateLockdownVpn();
}
public static void setLockdownVpn(Context context, String lockdownKey) {
- KeyStore.getInstance().put(Credentials.LOCKDOWN_VPN, lockdownKey.getBytes(),
- KeyStore.UID_SELF, /* flags */ 0);
+ LegacyVpnProfileStore.put(Credentials.LOCKDOWN_VPN, lockdownKey.getBytes());
// Always notify VpnManager after keystore update
getVpnManager(context).updateLockdownVpn();
}
diff --git a/src/com/android/settings/wifi/OWNERS b/src/com/android/settings/wifi/OWNERS
index d15cd78..7f39cc7 100644
--- a/src/com/android/settings/wifi/OWNERS
+++ b/src/com/android/settings/wifi/OWNERS
@@ -1,6 +1,7 @@
# Default reviewers for this and subdirectories.
andychou@google.com
arcwang@google.com
+changbetty@google.com
goldmanj@google.com
songferngwang@google.com
wengsu@google.com
diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java
index d0b5a40..6e96ae2 100644
--- a/src/com/android/settings/wifi/WifiConfigController.java
+++ b/src/com/android/settings/wifi/WifiConfigController.java
@@ -35,8 +35,7 @@
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.UserManager;
-import android.security.Credentials;
-import android.security.KeyStore;
+import android.security.keystore.KeyProperties;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.Editable;
@@ -73,6 +72,7 @@
import com.android.net.module.util.ProxyUtils;
import com.android.settings.ProxySelector;
import com.android.settings.R;
+import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.details.WifiPrivacyPreferenceController;
import com.android.settings.wifi.details2.WifiPrivacyPreferenceController2;
import com.android.settings.wifi.dpp.WifiDppUtils;
@@ -83,7 +83,7 @@
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -1051,15 +1051,17 @@
if (refreshCertificates) {
loadSims();
+ final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
+ getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
- Credentials.CA_CERTIFICATE,
+ androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
false /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
loadCertificates(
mEapUserCertSpinner,
- Credentials.USER_PRIVATE_KEY,
+ androidKeystoreAliasLoader.getKeyCertAliases(),
mDoNotProvideEapUserCertString,
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -1142,10 +1144,13 @@
} else if (caCerts.length == 1) {
setSelection(mEapCaCertSpinner, caCerts[0]);
} else {
+ final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
+ getAndroidKeystoreAliasLoader();
+
// Reload the cert spinner with an extra "multiple certificates added" item.
loadCertificates(
mEapCaCertSpinner,
- Credentials.CA_CERTIFICATE,
+ androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
true /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
@@ -1464,8 +1469,8 @@
}
@VisibleForTesting
- KeyStore getKeyStore() {
- return KeyStore.getInstance();
+ AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
+ return new AndroidKeystoreAliasLoader(KeyProperties.NAMESPACE_WIFI);
}
@VisibleForTesting
@@ -1509,7 +1514,7 @@
@VisibleForTesting
void loadCertificates(
Spinner spinner,
- String prefix,
+ Collection<String> choices,
String noCertificateString,
boolean showMultipleCerts,
boolean showUsePreinstalledCertOption) {
@@ -1524,14 +1529,8 @@
certs.add(mUseSystemCertsString);
}
- String[] certificateNames = null;
- try {
- certificateNames = getKeyStore().list(prefix, android.os.Process.WIFI_UID);
- } catch (Exception e) {
- Log.e(TAG, "can't get the certificate list from KeyStore");
- }
- if (certificateNames != null && certificateNames.length != 0) {
- certs.addAll(Arrays.stream(certificateNames)
+ if (choices != null && choices.size() != 0) {
+ certs.addAll(choices.stream()
.filter(certificateName -> {
for (String undesired : UNDESIRED_CERTIFICATES) {
if (certificateName.startsWith(undesired)) {
diff --git a/src/com/android/settings/wifi/WifiConfigController2.java b/src/com/android/settings/wifi/WifiConfigController2.java
index 79acc59..467f32e 100644
--- a/src/com/android/settings/wifi/WifiConfigController2.java
+++ b/src/com/android/settings/wifi/WifiConfigController2.java
@@ -33,8 +33,7 @@
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.UserManager;
-import android.security.Credentials;
-import android.security.KeyStore;
+import android.security.keystore.KeyProperties;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.text.Editable;
@@ -71,6 +70,7 @@
import com.android.net.module.util.ProxyUtils;
import com.android.settings.ProxySelector;
import com.android.settings.R;
+import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.details.WifiPrivacyPreferenceController;
import com.android.settings.wifi.details2.WifiPrivacyPreferenceController2;
import com.android.settings.wifi.dpp.WifiDppUtils;
@@ -83,7 +83,7 @@
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -1034,15 +1034,17 @@
if (refreshCertificates) {
loadSims();
+ final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
+ getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
- Credentials.CA_CERTIFICATE,
+ androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
false /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
loadCertificates(
mEapUserCertSpinner,
- Credentials.USER_PRIVATE_KEY,
+ androidKeystoreAliasLoader.getKeyCertAliases(),
mDoNotProvideEapUserCertString,
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -1127,9 +1129,11 @@
setSelection(mEapCaCertSpinner, caCerts[0]);
} else {
// Reload the cert spinner with an extra "multiple certificates added" item.
+ final AndroidKeystoreAliasLoader androidKeystoreAliasLoader =
+ getAndroidKeystoreAliasLoader();
loadCertificates(
mEapCaCertSpinner,
- Credentials.CA_CERTIFICATE,
+ androidKeystoreAliasLoader.getCaCertAliases(),
null /* noCertificateString */,
true /* showMultipleCerts */,
true /* showUsePreinstalledCertOption */);
@@ -1448,8 +1452,8 @@
}
@VisibleForTesting
- KeyStore getKeyStore() {
- return KeyStore.getInstance();
+ AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
+ return new AndroidKeystoreAliasLoader(KeyProperties.NAMESPACE_WIFI);
}
@VisibleForTesting
@@ -1493,7 +1497,7 @@
@VisibleForTesting
void loadCertificates(
Spinner spinner,
- String prefix,
+ Collection<String> choices,
String noCertificateString,
boolean showMultipleCerts,
boolean showUsePreinstalledCertOption) {
@@ -1508,14 +1512,8 @@
certs.add(mUseSystemCertsString);
}
- String[] certificateNames = null;
- try {
- certificateNames = getKeyStore().list(prefix, android.os.Process.WIFI_UID);
- } catch (Exception e) {
- Log.e(TAG, "can't get the certificate list from KeyStore");
- }
- if (certificateNames != null && certificateNames.length != 0) {
- certs.addAll(Arrays.stream(certificateNames)
+ if (choices != null && choices.size() != 0) {
+ certs.addAll(choices.stream()
.filter(certificateName -> {
for (String undesired : UNDESIRED_CERTIFICATES) {
if (certificateName.startsWith(undesired)) {
diff --git a/tests/robotests/src/com/android/settings/applications/assist/OWNERS b/tests/robotests/src/com/android/settings/applications/assist/OWNERS
new file mode 100644
index 0000000..bf069f4
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/assist/OWNERS
@@ -0,0 +1 @@
+include /src/com/android/settings/applications/assist/OWNERS
diff --git a/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java b/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java
deleted file mode 100644
index 657b8b3..0000000
--- a/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2020 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;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import android.content.ContentResolver;
-import android.content.Context;
-import android.provider.Settings;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-
-@RunWith(RobolectricTestRunner.class)
-public class PreferredNetworkModeContentObserverTest {
-
- private static final int SUB_ID = 1;
-
- @Mock
- private ContentResolver mResolver;
- @Mock
- private Context mContext;
- @Mock
- private PreferredNetworkModeContentObserver.OnPreferredNetworkModeChangedListener mListener;
-
- private PreferredNetworkModeContentObserver mPreferredNetworkModeContentObserver;
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
- when(mContext.getContentResolver()).thenReturn(mResolver);
- mPreferredNetworkModeContentObserver =
- spy(new PreferredNetworkModeContentObserver(null));
- }
-
- @Test
- public void onChange_shouldCallListener() {
- mPreferredNetworkModeContentObserver.mListener = mListener;
- mPreferredNetworkModeContentObserver.onChange(true);
-
- verify(mListener).onPreferredNetworkModeChanged();
- }
-
- @Test
- public void register_shouldRegisterContentObserver() {
- mPreferredNetworkModeContentObserver.register(mContext, SUB_ID);
-
- verify(mResolver).registerContentObserver(
- Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID), false,
- mPreferredNetworkModeContentObserver);
- }
-
- @Test
- public void unregister_shouldUnregisterContentObserver() {
- mPreferredNetworkModeContentObserver.unregister(mContext);
-
- verify(mResolver).unregisterContentObserver(mPreferredNetworkModeContentObserver);
- }
-
-}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceControllerTest.java
index 1feb08b..c643815 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceControllerTest.java
@@ -18,17 +18,20 @@
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
-import static com.android.settings.network.telephony.MobileNetworkUtils.getRafFromNetworkType;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.GSM;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.RAF_TD_SCDMA;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.WCDMA;
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.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.PersistableBundle;
-import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
@@ -127,9 +130,10 @@
@Test
public void updateState_updateByNetworkMode() {
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
+ // NETWORK_MODE_TDSCDMA_GSM_WCDMA = RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (RAF_TD_SCDMA | GSM | WCDMA));
mController.updateState(mPreference);
@@ -140,28 +144,13 @@
}
@Test
- public void onPreferenceChange_updateSuccess() {
- doReturn(true).when(mTelephonyManager).setPreferredNetworkTypeBitmask(
- getRafFromNetworkType(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
-
+ public void onPreferenceChange_updateNetworkMode() {
mController.onPreferenceChange(mPreference,
String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
- assertThat(Settings.Global.getInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, 0)).isEqualTo(
- TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA);
- }
-
- @Test
- public void onPreferenceChange_updateFail() {
- doReturn(false).when(mTelephonyManager).setPreferredNetworkTypeBitmask(
- getRafFromNetworkType(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
-
- mController.onPreferenceChange(mPreference,
- String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
-
- assertThat(Settings.Global.getInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, 0)).isNotEqualTo(
- TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA);
+ verify(mTelephonyManager, times(1)).setAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
+ MobileNetworkUtils.getRafFromNetworkType(
+ TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
}
}
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java b/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java
index 8696582..a31e082 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiConfigController2Test.java
@@ -18,9 +18,6 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.anyInt;
-import static org.mockito.Mockito.anyString;
-import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -33,9 +30,6 @@
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
-import android.os.ServiceSpecificException;
-import android.security.Credentials;
-import android.security.KeyStore;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -50,9 +44,12 @@
import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
+import com.android.settings.utils.AndroidKeystoreAliasLoader;
import com.android.settings.wifi.details.WifiPrivacyPreferenceController;
import com.android.wifitrackerlib.WifiEntry;
+import com.google.common.collect.ImmutableList;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -78,7 +75,7 @@
@Mock
private WifiEntry mWifiEntry;
@Mock
- private KeyStore mKeyStore;
+ private AndroidKeystoreAliasLoader mAndroidKeystoreAliasLoader;
private View mView;
private Spinner mHiddenSettingsSpinner;
private Spinner mEapCaCertSpinner;
@@ -285,27 +282,11 @@
}
@Test
- public void loadCertificates_keyStoreListFail_shouldNotCrash() {
- // Set up
- when(mWifiEntry.getSecurity()).thenReturn(WifiEntry.SECURITY_EAP);
- when(mKeyStore.list(anyString()))
- .thenThrow(new ServiceSpecificException(-1, "permission error"));
-
- mController = new TestWifiConfigController2(mConfigUiBase, mView, mWifiEntry,
- WifiConfigUiBase2.MODE_CONNECT);
-
- // Verify that the EAP method menu is visible.
- assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.VISIBLE);
- // No Crash
- }
-
- @Test
public void loadCertificates_undesiredCertificates_shouldNotLoadUndesiredCertificates() {
final Spinner spinner = new Spinner(mContext);
- when(mKeyStore.list(anyString())).thenReturn(WifiConfigController.UNDESIRED_CERTIFICATES);
mController.loadCertificates(spinner,
- "prefix",
+ Arrays.asList(WifiConfigController.UNDESIRED_CERTIFICATES),
"doNotProvideEapUserCertString",
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -436,8 +417,8 @@
}
@Override
- KeyStore getKeyStore() {
- return mKeyStore;
+ AndroidKeystoreAliasLoader getAndroidKeystoreAliasLoader() {
+ return mAndroidKeystoreAliasLoader;
}
}
@@ -883,6 +864,7 @@
String savedUserCertificate) {
final WifiConfiguration mockWifiConfig = mock(WifiConfiguration.class);
final WifiEnterpriseConfig mockWifiEnterpriseConfig = mock(WifiEnterpriseConfig.class);
+
mockWifiConfig.enterpriseConfig = mockWifiEnterpriseConfig;
when(mWifiEntry.isSaved()).thenReturn(true);
when(mWifiEntry.getSecurity()).thenReturn(WifiEntry.SECURITY_EAP);
@@ -893,15 +875,15 @@
String[] savedCaCertificates = new String[]{savedCaCertificate};
when(mockWifiEnterpriseConfig.getCaCertificateAliases())
.thenReturn(savedCaCertificates);
- when(mKeyStore.list(eq(Credentials.CA_CERTIFICATE), anyInt()))
- .thenReturn(savedCaCertificates);
+ when(mAndroidKeystoreAliasLoader.getCaCertAliases())
+ .thenReturn(ImmutableList.of(savedCaCertificate));
}
if (savedUserCertificate != null) {
String[] savedUserCertificates = new String[]{savedUserCertificate};
when(mockWifiEnterpriseConfig.getClientCertificateAlias())
.thenReturn(savedUserCertificate);
- when(mKeyStore.list(eq(Credentials.USER_PRIVATE_KEY), anyInt()))
- .thenReturn(savedUserCertificates);
+ when(mAndroidKeystoreAliasLoader.getKeyCertAliases())
+ .thenReturn(ImmutableList.of(savedUserCertificate));
}
mController = new TestWifiConfigController2(mConfigUiBase, mView, mWifiEntry,
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
index 9e62b25..40a0dd6 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
@@ -18,7 +18,6 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -31,8 +30,6 @@
import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
-import android.os.ServiceSpecificException;
-import android.security.KeyStore;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
@@ -74,8 +71,6 @@
private Context mContext;
@Mock
private AccessPoint mAccessPoint;
- @Mock
- private KeyStore mKeyStore;
private View mView;
private Spinner mHiddenSettingsSpinner;
private ShadowSubscriptionManager mShadowSubscriptionManager;
@@ -264,27 +259,11 @@
}
@Test
- public void loadCertificates_keyStoreListFail_shouldNotCrash() {
- // Set up
- when(mAccessPoint.getSecurity()).thenReturn(AccessPoint.SECURITY_EAP);
- when(mKeyStore.list(anyString()))
- .thenThrow(new ServiceSpecificException(-1, "permission error"));
-
- mController = new TestWifiConfigController(mConfigUiBase, mView, mAccessPoint,
- WifiConfigUiBase.MODE_CONNECT);
-
- // Verify that the EAP method menu is visible.
- assertThat(mView.findViewById(R.id.eap).getVisibility()).isEqualTo(View.VISIBLE);
- // No Crash
- }
-
- @Test
public void loadCertificates_undesiredCertificates_shouldNotLoadUndesiredCertificates() {
final Spinner spinner = new Spinner(mContext);
- when(mKeyStore.list(anyString())).thenReturn(WifiConfigController.UNDESIRED_CERTIFICATES);
mController.loadCertificates(spinner,
- "prefix",
+ Arrays.asList(WifiConfigController.UNDESIRED_CERTIFICATES),
"doNotProvideEapUserCertString",
false /* showMultipleCerts */,
false /* showUsePreinstalledCertOption */);
@@ -413,9 +392,6 @@
boolean isSplitSystemUser() {
return false;
}
-
- @Override
- KeyStore getKeyStore() { return mKeyStore; }
}
@Test
diff --git a/tests/unit/src/com/android/settings/applications/autofill/OWNERS b/tests/unit/src/com/android/settings/applications/autofill/OWNERS
new file mode 100644
index 0000000..fe93c13
--- /dev/null
+++ b/tests/unit/src/com/android/settings/applications/autofill/OWNERS
@@ -0,0 +1 @@
+include /src/com/android/settings/applications/autofill/OWNERS
diff --git a/tests/unit/src/com/android/settings/network/AllowedNetworkTypesListenerTest.java b/tests/unit/src/com/android/settings/network/AllowedNetworkTypesListenerTest.java
new file mode 100644
index 0000000..d8f3c78
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/AllowedNetworkTypesListenerTest.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2020 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;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.HandlerExecutor;
+import android.telephony.RadioAccessFamily;
+import android.telephony.TelephonyCallback;
+import android.telephony.TelephonyManager;
+import android.test.mock.MockContentResolver;
+
+import androidx.test.annotation.UiThreadTest;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(AndroidJUnit4.class)
+public class AllowedNetworkTypesListenerTest {
+
+ private static final int SUB_ID = 1;
+
+ private Context mContext;
+ private MockContentResolver mResolver;
+ private AllowedNetworkTypesListener mAllowedNetworkTypesListener;
+
+ @Mock
+ private AllowedNetworkTypesListener.OnAllowedNetworkTypesListener mListener;
+ @Mock
+ private TelephonyManager mTelephonyManager;
+
+
+ @Before
+ @UiThreadTest
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+
+ mAllowedNetworkTypesListener =
+ spy(new AllowedNetworkTypesListener(mContext.getMainExecutor()));
+ }
+
+ @Test
+ public void onChange_shouldCallListener() {
+ mAllowedNetworkTypesListener.mListener = mListener;
+ Map<Integer, Long> allowedNetworkTypesList = new HashMap<>();
+ long networkType = (long) RadioAccessFamily.getRafFromNetworkType(
+ TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO);
+ allowedNetworkTypesList.put(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
+ networkType);
+ mAllowedNetworkTypesListener.onAllowedNetworkTypesChanged(allowedNetworkTypesList);
+
+ verify(mListener).onAllowedNetworkTypesChanged();
+ }
+
+ @Test
+ public void register_shouldRegisterContentObserver() {
+ mAllowedNetworkTypesListener.register(mContext, SUB_ID);
+
+ verify(mTelephonyManager, times(1)).registerTelephonyCallback(any(HandlerExecutor.class),
+ any(TelephonyCallback.class));
+ }
+
+ @Test
+ public void unregister_shouldUnregisterContentObserver() {
+ mAllowedNetworkTypesListener.unregister(mContext, SUB_ID);
+
+ verify(mTelephonyManager).unregisterTelephonyCallback(
+ mAllowedNetworkTypesListener);
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/EnabledNetworkModePreferenceControllerTest.java
similarity index 65%
rename from tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
rename to tests/unit/src/com/android/settings/network/EnabledNetworkModePreferenceControllerTest.java
index e519b9a..81841b7 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/EnabledNetworkModePreferenceControllerTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2018 The Android Open Source Project
+ * Copyright (C) 2021 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.
@@ -21,21 +21,23 @@
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.android.settings.network.telephony.MobileNetworkUtils.getRafFromNetworkType;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.CDMA;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.EVDO;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.GSM;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.LTE;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.NR;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.RAF_TD_SCDMA;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.WCDMA;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
-import android.net.Uri;
import android.os.PersistableBundle;
-import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
@@ -43,9 +45,12 @@
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.ListPreference;
+import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
+import androidx.test.annotation.UiThreadTest;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
-import com.android.settings.R;
import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -54,10 +59,8 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-@RunWith(RobolectricTestRunner.class)
+@RunWith(AndroidJUnit4.class)
public class EnabledNetworkModePreferenceControllerTest {
private static final int SUB_ID = 2;
public static final String KEY = "enabled_network";
@@ -81,19 +84,21 @@
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
+ @UiThreadTest
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
- mContext = spy(RuntimeEnvironment.application);
- doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
- doReturn(mTelephonyManager).when(mContext).getSystemService(TelephonyManager.class);
- doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
+ mContext = spy(ApplicationProvider.getApplicationContext());
+
+ when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
+ mCarrierConfigManager);
+ when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
- doReturn(mContext).when(mContext).createPackageContext(anyString(), anyInt());
doReturn(mServiceState).when(mTelephonyManager).getServiceState();
mPersistableBundle = new PersistableBundle();
doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfig();
@@ -106,6 +111,7 @@
mPreference.setKey(mController.getPreferenceKey());
}
+ @UiThreadTest
@Test
public void getAvailabilityStatus_hideCarrierNetworkSettings_returnUnavailable() {
mPersistableBundle.putBoolean(
@@ -115,6 +121,7 @@
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
+ @UiThreadTest
@Test
public void getAvailabilityStatus_hidePreferredNetworkType_returnUnavailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL,
@@ -135,6 +142,7 @@
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
+ @UiThreadTest
@Test
public void getAvailabilityStatus_notWorldPhone_returnAvailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
@@ -144,6 +152,7 @@
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
+ @UiThreadTest
@Test
public void updateState_LteWorldPhone_GlobalHasLte() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
@@ -155,9 +164,10 @@
.contains(String.valueOf(TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_5gWorldPhone_GlobalHasNr() {
- mPersistableBundle.putBoolean(CarrierConfigManager.KEY_NR_ENABLED_BOOL, true);
+ mockAllowedNetworkTypes(ALLOWED_ALL_NETWORK_TYPE);
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
mController.init(mLifecycle, SUB_ID);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
@@ -169,15 +179,18 @@
.contains(String.valueOf(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_selectedOn5gItem() {
- mPersistableBundle.putBoolean(CarrierConfigManager.KEY_NR_ENABLED_BOOL, true);
+ mockAllowedNetworkTypes(ALLOWED_ALL_NETWORK_TYPE);
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mController.init(mLifecycle, SUB_ID);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
+
+ // NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA = NR | LTE | RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (NR | LTE | RAF_TD_SCDMA | GSM | WCDMA));
mController.updateState(mPreference);
@@ -186,16 +199,18 @@
TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_disAllowed5g_5gOptionHidden() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
mController.init(mLifecycle, SUB_ID);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
+ // NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA = NR | LTE | RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (NR | LTE | RAF_TD_SCDMA | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getEntryValues())
@@ -204,16 +219,18 @@
String.valueOf(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_disAllowed5g_selectOn4gOption() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
mController.init(mLifecycle, SUB_ID);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA);
+ // NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA = NR | LTE | RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (NR | LTE | RAF_TD_SCDMA | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
@@ -221,38 +238,43 @@
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_NrEnableBoolFalse_5gOptionHidden() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
- mPersistableBundle.putBoolean(CarrierConfigManager.KEY_NR_ENABLED_BOOL, false);
+ mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
mController.init(mLifecycle, SUB_ID);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
+
+ // NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = LTE | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | CDMA | EVDO | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
String.valueOf(
- TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+ TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
assertThat(mPreference.getEntryValues())
.asList()
.doesNotContain(
String.valueOf(TelephonyManager.NETWORK_MODE_NR_LTE_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_GlobalDisAllowed5g_GlobalWithoutNR() {
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
mController.init(mLifecycle, SUB_ID);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
+ // NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA = NR | LTE | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (NR | LTE | CDMA | EVDO | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getEntryValues())
@@ -261,16 +283,18 @@
String.valueOf(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_GlobalDisAllowed5g_SelectOnGlobal() {
mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
mController.init(mLifecycle, SUB_ID);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
+ // NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA = NR | LTE | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (NR | LTE | CDMA | EVDO | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
@@ -278,13 +302,15 @@
TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void updateState_updateByNetworkMode() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
+ // NETWORK_MODE_TDSCDMA_GSM_WCDMA = RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (RAF_TD_SCDMA | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
@@ -292,33 +318,38 @@
assertThat(mPreference.getSummary()).isEqualTo("3G");
}
+ @UiThreadTest
@Test
public void updateState_updateByNetworkMode_useDefaultValue() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA);
+ // NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = LTE | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | CDMA | EVDO | GSM | WCDMA));
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
- String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+ String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void onPreferenceChange_updateSuccess() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA);
doReturn(true).when(mTelephonyManager).setPreferredNetworkTypeBitmask(
- getRafFromNetworkType(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+ getRafFromNetworkType(
+ TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
mController.updateState(mPreference);
mController.onPreferenceChange(mPreference,
- String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+ String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
assertThat(mPreference.getValue()).isEqualTo(
- String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+ String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void onPreferenceChange_updateFail() {
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA);
@@ -333,14 +364,21 @@
String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
}
+ @UiThreadTest
@Test
public void preferredNetworkModeNotification_preferenceUpdates() {
- PreferenceScreen screen = mock(PreferenceScreen.class);
+
+ final PreferenceManager preferenceManager = new PreferenceManager(mContext);
+ PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
+ mPreference.setKey(KEY);
+ screen.addPreference(mPreference);
mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
- doReturn(mPreference).when(screen).findPreference(KEY);
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
+
+ // NETWORK_MODE_TDSCDMA_GSM_WCDMA = RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (RAF_TD_SCDMA | GSM | WCDMA));
+
mController.displayPreference(screen);
mController.updateState(mPreference);
mLifecycle.handleLifecycleEvent(ON_START);
@@ -348,59 +386,59 @@
assertThat(Integer.parseInt(mPreference.getValue())).isEqualTo(
TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
assertThat(mPreference.getSummary()).isEqualTo("3G");
-
-
- Settings.Global.putInt(mContext.getContentResolver(),
- Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
- TelephonyManagerConstants.NETWORK_MODE_GSM_ONLY);
- final Uri uri = Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID);
- mContext.getContentResolver().notifyChange(uri, null);
-
- assertThat(Integer.parseInt(mPreference.getValue())).isEqualTo(
- TelephonyManagerConstants.NETWORK_MODE_GSM_ONLY);
- assertThat(mPreference.getSummary()).isEqualTo("2G");
}
+ @UiThreadTest
@Test
public void checkResource_stringArrayLength() {
- String[] entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_cdma_values);
+ int id = mController.getResourcesForSubId().getIdentifier("enabled_networks_cdma_values",
+ "array", mContext.getPackageName());
+ String[] entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(4, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_cdma_no_lte_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_cdma_no_lte_values",
+ "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(2, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_cdma_only_lte_values);
+ id = mController.getResourcesForSubId().getIdentifier(
+ "enabled_networks_cdma_only_lte_values", "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(2, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_tdscdma_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_tdscdma_values",
+ "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(3, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_except_gsm_lte_values);
+ id = mController.getResourcesForSubId().getIdentifier(
+ "enabled_networks_except_gsm_lte_values", "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(1, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_except_gsm_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_except_gsm_values",
+ "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(2, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_except_lte_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_except_lte_values",
+ "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(2, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_values", "array",
+ mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(3, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.enabled_networks_values);
+ id = mController.getResourcesForSubId().getIdentifier("enabled_networks_values", "array",
+ mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(3, entryValues.length);
- entryValues = mContext.getResources().getStringArray(
- R.array.preferred_network_mode_values_world_mode);
+ id = mController.getResourcesForSubId().getIdentifier(
+ "preferred_network_mode_values_world_mode", "array", mContext.getPackageName());
+ entryValues = mController.getResourcesForSubId().getStringArray(id);
assertEquals(3, entryValues.length);
}
@@ -408,15 +446,17 @@
if (networkMode == TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA) {
mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true);
- } else if (networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA) {
+ } else if (networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA
+ || networkMode == TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA) {
mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_PREFER_2G_BOOL, true);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_LTE_ENABLED_BOOL, true);
} else if (networkMode == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA) {
mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true);
- } else if (networkMode
- == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA) {
+ } else if (networkMode == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA
+ || networkMode
+ == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA) {
mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_PREFER_2G_BOOL, true);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_LTE_ENABLED_BOOL, true);
@@ -424,7 +464,8 @@
}
private void mockAllowedNetworkTypes(long allowedNetworkType) {
- doReturn(allowedNetworkType).when(mTelephonyManager).getAllowedNetworkTypes();
+ doReturn(allowedNetworkType).when(mTelephonyManager).getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_CARRIER);
}
private void mockAccessFamily(int networkMode) {
diff --git a/tests/robotests/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java
similarity index 79%
rename from tests/robotests/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java
rename to tests/unit/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java
index 38223b8..b269660 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/telephony/DisabledSubscriptionControllerTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2019 The Android Open Source Project
+ * Copyright (C) 2020 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.
@@ -11,7 +11,7 @@
* 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
+ * limitations under the License.
*/
package com.android.settings.network.telephony;
@@ -20,13 +20,17 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
import android.content.Context;
+import android.os.Looper;
import android.telephony.SubscriptionManager;
-import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -35,10 +39,8 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-@RunWith(RobolectricTestRunner.class)
+@RunWith(AndroidJUnit4.class)
public class DisabledSubscriptionControllerTest {
private static final String KEY = "disabled_subscription_category";
@@ -47,22 +49,28 @@
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
- private PreferenceScreen mScreen;
+ private Lifecycle mLifecycle;
+ private PreferenceScreen mScreen;
+ private PreferenceManager mPreferenceManager;
private PreferenceCategory mCategory;
private Context mContext;
- private Lifecycle mLifecycle;
private DisabledSubscriptionController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- mContext = spy(RuntimeEnvironment.application);
- LifecycleOwner lifecycleOwner = () -> mLifecycle;
- mLifecycle = new Lifecycle(lifecycleOwner);
- doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+ mPreferenceManager = new PreferenceManager(mContext);
+ mScreen = mPreferenceManager.createPreferenceScreen(mContext);
mCategory = new PreferenceCategory(mContext);
- doReturn(mCategory).when(mScreen).findPreference(KEY);
+ mCategory.setKey(KEY);
+ mScreen.addPreference(mCategory);
+
mController = new DisabledSubscriptionController(mContext, KEY);
mController.init(mLifecycle, SUB_ID);
}
diff --git a/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
new file mode 100644
index 0000000..b84af2c
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
@@ -0,0 +1,347 @@
+/*
+ * Copyright (C) 2020 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.network.telephony.TelephonyConstants.RadioAccessFamily.CDMA;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.EVDO;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.GSM;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.LTE;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.RAF_TD_SCDMA;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.WCDMA;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.PersistableBundle;
+import android.telecom.PhoneAccountHandle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+@RunWith(AndroidJUnit4.class)
+public class MobileNetworkUtilsTest {
+ private static final String PACKAGE_NAME = "com.android.app";
+ private static final int SUB_ID_1 = 1;
+ private static final int SUB_ID_2 = 2;
+ private static final int SUB_ID_INVALID = -1;
+ private static final String PLMN_FROM_TELEPHONY_MANAGER_API = "testPlmn";
+ private static final String PLMN_FROM_SUB_ID_1 = "testPlmnSub1";
+ private static final String PLMN_FROM_SUB_ID_2 = "testPlmnSub2";
+
+ @Mock
+ private TelephonyManager mTelephonyManager;
+ @Mock
+ private TelephonyManager mTelephonyManager2;
+ @Mock
+ private SubscriptionManager mSubscriptionManager;
+ @Mock
+ private SubscriptionInfo mSubscriptionInfo1;
+ @Mock
+ private SubscriptionInfo mSubscriptionInfo2;
+ @Mock
+ private PackageManager mPackageManager;
+ @Mock
+ private ResolveInfo mResolveInfo;
+ @Mock
+ private CarrierConfigManager mCarrierConfigManager;
+
+ private Context mContext;
+ private PersistableBundle mCarrierConfig;
+ private PhoneAccountHandle mPhoneAccountHandle;
+ private ComponentName mComponentName;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
+ when(mTelephonyManager.createForSubscriptionId(SUB_ID_1)).thenReturn(mTelephonyManager);
+ when(mTelephonyManager.createForSubscriptionId(SUB_ID_2)).thenReturn(mTelephonyManager2);
+ when(mContext.getPackageManager()).thenReturn(mPackageManager);
+
+ when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
+ mCarrierConfigManager);
+
+ mCarrierConfig = new PersistableBundle();
+ when(mCarrierConfigManager.getConfigForSubId(SUB_ID_1)).thenReturn(mCarrierConfig);
+
+ when(mSubscriptionInfo1.getSubscriptionId()).thenReturn(SUB_ID_1);
+ when(mSubscriptionInfo1.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_1);
+ when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(SUB_ID_2);
+ when(mSubscriptionInfo2.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_2);
+
+ when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
+ Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
+ when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
+ Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
+
+ when(mTelephonyManager.getNetworkOperatorName()).thenReturn(
+ PLMN_FROM_TELEPHONY_MANAGER_API);
+ }
+
+ @Test
+ public void setMobileDataEnabled_setEnabled_enabled() {
+ MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, false);
+
+ verify(mTelephonyManager).setDataEnabled(true);
+ verify(mTelephonyManager2, never()).setDataEnabled(anyBoolean());
+ }
+
+ @Test
+ public void setMobileDataEnabled_setDisabled_disabled() {
+ MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_2, true, false);
+
+ verify(mTelephonyManager2).setDataEnabled(true);
+ verify(mTelephonyManager, never()).setDataEnabled(anyBoolean());
+ }
+
+ @Test
+ public void setMobileDataEnabled_disableOtherSubscriptions() {
+ MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, true);
+
+ verify(mTelephonyManager).setDataEnabled(true);
+ verify(mTelephonyManager2).setDataEnabled(false);
+ }
+
+ @Test
+ public void buildConfigureIntent_nullHandle_returnNull() {
+ assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, null)).isNull();
+ }
+
+ @Test
+ public void buildConfigureIntent_noActivityHandleIntent_returnNull() {
+ when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
+ .thenReturn(new ArrayList<>());
+
+ assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
+ mPhoneAccountHandle)).isNull();
+ }
+
+ @Test
+ public void buildConfigureIntent_hasActivityHandleIntent_returnIntent() {
+ mComponentName = new ComponentName(PACKAGE_NAME, "testClass");
+ mPhoneAccountHandle = new PhoneAccountHandle(mComponentName, "");
+ when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
+ .thenReturn(Arrays.asList(mResolveInfo));
+
+ assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
+ mPhoneAccountHandle)).isNotNull();
+ }
+
+ @Test
+ public void isCdmaOptions_phoneTypeCdma_returnTrue() {
+ when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
+
+ assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void isCdmaOptions_worldModeWithGsmWcdma_returnTrue() {
+ when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ // NETWORK_MODE_LTE_GSM_WCDMA = LTE | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | GSM | WCDMA));
+
+ assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void isCdmaOptions_carrierWorldModeWithoutHideCarrier_returnTrue() {
+ when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+ false);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true);
+
+ assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void getSearchableSubscriptionId_oneActive_returnValid() {
+ when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
+ Arrays.asList(mSubscriptionInfo1));
+ assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext)).isEqualTo(SUB_ID_1);
+ }
+
+ @Test
+ public void getSearchableSubscriptionId_nonActive_returnInvalid() {
+ when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(new ArrayList<>());
+
+ assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext))
+ .isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+ }
+
+ @Test
+ public void shouldDisplayNetworkSelectOptions_HideCarrierNetwork_returnFalse() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+ true);
+
+ assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1))
+ .isFalse();
+ }
+
+ @Test
+ public void shouldDisplayNetworkSelectOptions_allCheckPass_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
+ false);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false);
+ when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
+
+ assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1))
+ .isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_notWorldMode_returnFalse() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_supportTdscdma_returnFalse() {
+ when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true);
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsm_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_TDSCDMA_GSM = LTE | RAF_TD_SCDMA | GSM
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | RAF_TD_SCDMA | GSM));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsmWcdma_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = LTE | RAF_TD_SCDMA | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | RAF_TD_SCDMA | GSM | WCDMA));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdma_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_TDSCDMA = LTE | RAF_TD_SCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | RAF_TD_SCDMA));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaWcdma_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_TDSCDMA_WCDMA = LTE | RAF_TD_SCDMA | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | RAF_TD_SCDMA | WCDMA));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaCdmaEvdoGsmWcdma_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
+ // = LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void shouldSpeciallyUpdateGsmCdma_ModeLteCdmaEvdoGsmWcdma_returnTrue() {
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
+ mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
+ // NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = LTE | CDMA | EVDO | GSM | WCDMA
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | CDMA | EVDO | GSM | WCDMA));
+
+ assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
+ }
+
+ @Test
+ public void getCurrentCarrierNameForDisplay_withoutValidSubId_returnNetworkOperatorName() {
+ assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
+ mContext, SUB_ID_INVALID)).isEqualTo(PLMN_FROM_TELEPHONY_MANAGER_API);
+ }
+
+ @Test
+ public void getCurrentCarrierNameForDisplay_withValidSubId_returnCurrentCarrierName() {
+ assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
+ mContext, SUB_ID_1)).isEqualTo(PLMN_FROM_SUB_ID_1);
+ assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
+ mContext, SUB_ID_2)).isEqualTo(PLMN_FROM_SUB_ID_2);
+ }
+
+ @Test
+ public void getCurrentCarrierNameForDisplay_withoutSubId_returnNotNull() {
+ assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
+ mContext)).isNotNull();
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java
new file mode 100644
index 0000000..027a53b
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2020 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 org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.telephony.TelephonyManager;
+
+import androidx.preference.PreferenceManager;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+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 CdmaListPreferenceTest {
+ private static final int SUB_ID = 2;
+
+ @Mock
+ private TelephonyManager mTelephonyManager;
+ @Mock
+ private PreferenceManager mPreferenceManager;
+
+ private CdmaListPreference mPreference;
+ private Context mContext;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
+ mPreference = spy(new CdmaListPreference(mContext, null));
+ mPreference.setSubId(SUB_ID);
+ }
+
+ @Test
+ public void onClick_inEcm_doNothing() {
+ doReturn(true).when(mTelephonyManager).getEmergencyCallbackMode();
+
+ mPreference.onClick();
+
+ verify(mPreferenceManager, never()).showDialog(mPreference);
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java
new file mode 100644
index 0000000..86488c6
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2020 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 static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.SystemProperties;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+
+import androidx.preference.ListPreference;
+import androidx.preference.PreferenceManager;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.After;
+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 CdmaSubscriptionPreferenceControllerTest {
+ private static final int SUB_ID = 2;
+
+ @Mock
+ private PreferenceManager mPreferenceManager;
+ @Mock
+ private TelephonyManager mTelephonyManager;
+
+ private CdmaSubscriptionPreferenceController mController;
+ private ListPreference mPreference;
+ private Context mContext;
+ private int mCdmaMode;
+ private String mSubscriptionsSupported;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+
+ mPreference = new ListPreference(mContext);
+ mController = new CdmaSubscriptionPreferenceController(mContext, "mobile_data");
+ mController.init(mPreferenceManager, SUB_ID);
+ mController.mPreference = mPreference;
+ mPreference.setKey(mController.getPreferenceKey());
+
+ mCdmaMode = Settings.Global.getInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_SUBSCRIPTION_MODE,
+ TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM);
+
+ mSubscriptionsSupported = SystemProperties.get("ril.subscription.types");
+ }
+
+ @After
+ public void tearDown() {
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_SUBSCRIPTION_MODE, mCdmaMode);
+
+ SystemProperties.set("ril.subscription.types", mSubscriptionsSupported);
+ }
+
+ @Test
+ public void onPreferenceChange_selectNV_returnNVMode() {
+ doReturn(true).when(mTelephonyManager).setCdmaSubscriptionMode(anyInt());
+
+ mController.onPreferenceChange(mPreference, Integer.toString(
+ TelephonyManager.CDMA_SUBSCRIPTION_NV));
+
+ assertThat(Settings.Global.getInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_SUBSCRIPTION_MODE,
+ TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM)).isEqualTo(
+ TelephonyManager.CDMA_SUBSCRIPTION_NV);
+ }
+
+ @Test
+ public void updateState_stateRUIM_displayRUIM() {
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_SUBSCRIPTION_MODE, TelephonyManager.CDMA_SUBSCRIPTION_NV);
+
+ mController.updateState(mPreference);
+
+ assertThat(mPreference.getValue()).isEqualTo(Integer.toString(
+ TelephonyManager.CDMA_SUBSCRIPTION_NV));
+ }
+
+ @Test
+ public void updateState_stateUnknown_doNothing() {
+ mPreference.setValue(Integer.toString(TelephonyManager.CDMA_SUBSCRIPTION_NV));
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_SUBSCRIPTION_MODE, TelephonyManager.CDMA_SUBSCRIPTION_UNKNOWN);
+
+ mController.updateState(mPreference);
+
+ // Still NV mode
+ assertThat(mPreference.getValue()).isEqualTo(Integer.toString(
+ TelephonyManager.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();
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java
new file mode 100644
index 0000000..c813316
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2020 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.network.telephony.TelephonyConstants.RadioAccessFamily.GSM;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.LTE;
+import static com.android.settings.network.telephony.TelephonyConstants.RadioAccessFamily.WCDMA;
+
+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 static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.telephony.TelephonyManager;
+
+import androidx.preference.ListPreference;
+import androidx.preference.PreferenceManager;
+import androidx.test.annotation.UiThreadTest;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.After;
+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 CdmaSystemSelectPreferenceControllerTest {
+ private static final int SUB_ID = 2;
+
+ @Mock
+ private PreferenceManager mPreferenceManager;
+ @Mock
+ private TelephonyManager mTelephonyManager;
+
+ private CdmaSystemSelectPreferenceController mController;
+ private ListPreference mPreference;
+ private Context mContext;
+ private int mCdmaRoamingMode;
+
+ @UiThreadTest
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+
+ mPreference = new ListPreference(mContext);
+ mController = new CdmaSystemSelectPreferenceController(mContext, "mobile_data");
+ mController.init(mPreferenceManager, SUB_ID);
+ mController.mPreference = mPreference;
+ mPreference.setKey(mController.getPreferenceKey());
+
+ mCdmaRoamingMode = Settings.Global.getInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_ROAMING_MODE,
+ TelephonyManager.CDMA_ROAMING_MODE_ANY);
+ }
+
+ @After
+ public void tearDown() {
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.CDMA_ROAMING_MODE, mCdmaRoamingMode);
+ }
+
+ @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_LteGSMWcdma_disabled() {
+ doReturn(TelephonyManager.CDMA_ROAMING_MODE_HOME).when(
+ mTelephonyManager).getCdmaRoamingMode();
+ when(mTelephonyManager.getAllowedNetworkTypesForReason(
+ TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn(
+ (long) (LTE | GSM | WCDMA));
+
+ mController.updateState(mPreference);
+
+ assertThat(mPreference.isEnabled()).isFalse();
+ }
+
+ @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);
+ }
+}