From 69fe84219a9624ba65e79bd94d9fc18bd8f33cc1 Mon Sep 17 00:00:00 2001 From: Zoey Chen Date: Mon, 6 Feb 2023 20:02:29 +0800 Subject: [PATCH] [Settings] Add the CarrierNetworkChange case Bug: 262927206 Test: atest SubscriptionsPreferenceControllerTest Change-Id: If7eeb185224179d59ab841af0062ec5b98b956e9 --- .../network/ProviderModelSliceHelper.java | 3 +- .../SubscriptionsPreferenceController.java | 66 ++++++++++--------- .../network/telephony/MobileNetworkUtils.java | 5 +- .../telephony/NetworkOperatorPreference.java | 2 +- ...SubscriptionsPreferenceControllerTest.java | 31 +++++---- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/com/android/settings/network/ProviderModelSliceHelper.java b/src/com/android/settings/network/ProviderModelSliceHelper.java index 32a475a7d00..e0d1eb1d8b6 100644 --- a/src/com/android/settings/network/ProviderModelSliceHelper.java +++ b/src/com/android/settings/network/ProviderModelSliceHelper.java @@ -61,6 +61,7 @@ import java.util.stream.Collectors; /** * The helper is for slice of carrier and non-Carrier, used by ProviderModelSlice. + * TODO: Remove the class in U because Settings does not use slice anymore. */ public class ProviderModelSliceHelper { private static final String TAG = "ProviderModelSlice"; @@ -140,7 +141,7 @@ public class ProviderModelSliceHelper { numLevels += 1; } return MobileNetworkUtils.getSignalStrengthIcon(mContext, level, numLevels, - NO_CELL_DATA_TYPE_ICON, false); + NO_CELL_DATA_TYPE_ICON, false, false); } /** diff --git a/src/com/android/settings/network/SubscriptionsPreferenceController.java b/src/com/android/settings/network/SubscriptionsPreferenceController.java index 4ce7d26f78e..9cd49c84b9c 100644 --- a/src/com/android/settings/network/SubscriptionsPreferenceController.java +++ b/src/com/android/settings/network/SubscriptionsPreferenceController.java @@ -36,10 +36,12 @@ import android.telephony.ServiceState; import android.telephony.SignalStrength; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; +import android.telephony.TelephonyCallback; import android.telephony.TelephonyDisplayInfo; import android.telephony.TelephonyManager; import android.text.Html; import android.util.ArraySet; +import android.util.Log; import androidx.annotation.VisibleForTesting; import androidx.collection.ArrayMap; @@ -78,7 +80,8 @@ import java.util.Set; public class SubscriptionsPreferenceController extends AbstractPreferenceController implements LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient, MobileDataEnabledListener.Client, DataConnectivityListener.Client, - SignalStrengthListener.Callback, TelephonyDisplayInfoListener.Callback { + SignalStrengthListener.Callback, TelephonyDisplayInfoListener.Callback, + TelephonyCallback.CarrierNetworkListener { private static final String TAG = "SubscriptionsPrefCntrlr"; private UpdateListener mUpdateListener; @@ -93,6 +96,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl private TelephonyDisplayInfoListener mTelephonyDisplayInfoListener; private WifiPickerTrackerHelper mWifiPickerTrackerHelper; private final WifiManager mWifiManager; + private boolean mCarrierNetworkChangeMode; @VisibleForTesting final BroadcastReceiver mConnectionChangeReceiver = new BroadcastReceiver() { @@ -283,8 +287,8 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl ? false : regInfo.isRegistered(); final boolean isCarrierNetworkActive = isCarrierNetworkActive(); - String result = mSubsPrefCtrlInjector.getNetworkType( - mContext, mConfig, mTelephonyDisplayInfo, subId, isCarrierNetworkActive); + String result = mSubsPrefCtrlInjector.getNetworkType(mContext, mConfig, + mTelephonyDisplayInfo, subId, isCarrierNetworkActive, mCarrierNetworkChangeMode); if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext) || isCarrierNetworkActive) { if (result.isEmpty()) { result = mContext.getString(isDds ? R.string.mobile_data_connection_active @@ -335,7 +339,8 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl || (!isDds(subId) && tmForSubId.isMobileDataPolicyEnabled( TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH)); if (isDataInService || isVoiceInService || isCarrierNetworkActive) { - icon = mSubsPrefCtrlInjector.getIcon(mContext, level, numLevels, !isDataEnabled); + icon = mSubsPrefCtrlInjector.getIcon(mContext, level, numLevels, !isDataEnabled, + mCarrierNetworkChangeMode); } final boolean isActiveCellularNetwork = @@ -472,6 +477,12 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl update(); } + @Override + public void onCarrierNetworkChange(boolean active) { + mCarrierNetworkChangeMode = active; + update(); + } + public void setWifiPickerTrackerHelper(WifiPickerTrackerHelper helper) { mWifiPickerTrackerHelper = helper; } @@ -549,44 +560,39 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl } /** - * Gets current mobile network type. + * Gets current network type of Carrier Wi-Fi Network or Cellular. */ public String getNetworkType(Context context, Config config, - TelephonyDisplayInfo telephonyDisplayInfo, int subId) { - String iconKey = getIconKey(telephonyDisplayInfo); - MobileIconGroup iconGroup = mapIconSets(config).get(iconKey); - int resId = 0; - if (iconGroup != null) { - resId = iconGroup.dataContentDescription; + TelephonyDisplayInfo telephonyDisplayInfo, int subId, boolean isCarrierWifiNetwork, + boolean carrierNetworkChanged) { + MobileIconGroup iconGroup = null; + if (isCarrierWifiNetwork) { + iconGroup = TelephonyIcons.CARRIER_MERGED_WIFI; + } else if (carrierNetworkChanged) { + iconGroup = TelephonyIcons.CARRIER_NETWORK_CHANGE; + } else { + String iconKey = getIconKey(telephonyDisplayInfo); + iconGroup = mapIconSets(config).get(iconKey); } + + if (iconGroup == null) { + Log.d(TAG, "Can not get the network's icon and description."); + return ""; + } + + int resId = iconGroup.dataContentDescription; return resId != 0 ? SubscriptionManager.getResourcesForSubId(context, subId).getString(resId) : ""; } - /** - * Gets current network type of Carrier Wi-Fi Network or Cellular. - */ - public String getNetworkType(Context context, Config config, - TelephonyDisplayInfo telephonyDisplayInfo, int subId, - boolean isCarrierWifiNetwork) { - if (isCarrierWifiNetwork) { - MobileIconGroup carrierMergedWifiIconGroup = TelephonyIcons.CARRIER_MERGED_WIFI; - int resId = carrierMergedWifiIconGroup.dataContentDescription; - return resId != 0 - ? SubscriptionManager.getResourcesForSubId(context, subId) - .getString(resId) : ""; - } else { - return getNetworkType(context, config, telephonyDisplayInfo, subId); - } - } - /** * Gets signal icon with different signal level. */ - public Drawable getIcon(Context context, int level, int numLevels, boolean cutOut) { + public Drawable getIcon(Context context, int level, int numLevels, boolean cutOut, + boolean carrierNetworkChanged) { return MobileNetworkUtils.getSignalStrengthIcon(context, level, numLevels, - NO_CELL_DATA_TYPE_ICON, cutOut); + NO_CELL_DATA_TYPE_ICON, cutOut, carrierNetworkChanged); } } } diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java index 599fa6b1077..423ba2f1e68 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java +++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java @@ -613,10 +613,11 @@ public class MobileNetworkUtils { } public static Drawable getSignalStrengthIcon(Context context, int level, int numLevels, - int iconType, boolean cutOut) { + int iconType, boolean cutOut, boolean carrierNetworkChanged) { final SignalDrawable signalDrawable = new SignalDrawable(context); signalDrawable.setLevel( - SignalDrawable.getState(level, numLevels, cutOut)); + carrierNetworkChanged ? SignalDrawable.getCarrierChangeState(numLevels) + : SignalDrawable.getState(level, numLevels, cutOut)); // Make the network type drawable final Drawable networkDrawable = diff --git a/src/com/android/settings/network/telephony/NetworkOperatorPreference.java b/src/com/android/settings/network/telephony/NetworkOperatorPreference.java index 397bf36a1c1..7404aa47353 100644 --- a/src/com/android/settings/network/telephony/NetworkOperatorPreference.java +++ b/src/com/android/settings/network/telephony/NetworkOperatorPreference.java @@ -260,6 +260,6 @@ public class NetworkOperatorPreference extends Preference { } final Context context = getContext(); setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS, - getIconIdForCell(mCellInfo), false)); + getIconIdForCell(mCellInfo), false, false)); } } diff --git a/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java index ebcb102e7ab..c4abdd19841 100644 --- a/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java +++ b/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java @@ -275,7 +275,8 @@ public class SubscriptionsPreferenceControllerTest { true, ServiceState.STATE_IN_SERVICE); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); mController.onResume(); mController.displayPreference(mPreferenceScreen); @@ -295,7 +296,7 @@ public class SubscriptionsPreferenceControllerTest { true, ServiceState.STATE_IN_SERVICE); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(true)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(true), eq(false)); doReturn(true).when(mWifiPickerTrackerHelper).isCarrierNetworkActive(); mController.onResume(); @@ -317,7 +318,8 @@ public class SubscriptionsPreferenceControllerTest { setupGetIconConditions(sub.get(0).getSubscriptionId(), false, false, true, ServiceState.STATE_IN_SERVICE); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); mController.onResume(); mController.displayPreference(mPreferenceScreen); @@ -336,7 +338,8 @@ public class SubscriptionsPreferenceControllerTest { setupGetIconConditions(sub.get(0).getSubscriptionId(), false, true, true, ServiceState.STATE_IN_SERVICE); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); when(mTelephonyManager.isDataEnabled()).thenReturn(true); mController.onResume(); @@ -371,7 +374,8 @@ public class SubscriptionsPreferenceControllerTest { true, ServiceState.STATE_IN_SERVICE); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); when(mTelephonyManager.isDataEnabled()).thenReturn(true); mController.onResume(); @@ -397,7 +401,8 @@ public class SubscriptionsPreferenceControllerTest { true, ServiceState.STATE_IN_SERVICE); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); mController.onResume(); mController.displayPreference(mPreferenceScreen); @@ -424,7 +429,8 @@ public class SubscriptionsPreferenceControllerTest { false, ServiceState.STATE_OUT_OF_SERVICE); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(networkType) - .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false)); + .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false), + eq(false)); mController.onResume(); mController.displayPreference(mPreferenceScreen); @@ -491,7 +497,7 @@ public class SubscriptionsPreferenceControllerTest { doReturn(sub.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); Drawable icon = mock(Drawable.class); when(mTelephonyManager.isDataEnabled()).thenReturn(true); - doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(false)); + doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(false), eq(false)); setupGetIconConditions(sub.get(0).getSubscriptionId(), true, true, true, ServiceState.STATE_IN_SERVICE); @@ -509,7 +515,7 @@ public class SubscriptionsPreferenceControllerTest { doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); Drawable icon = mock(Drawable.class); when(mTelephonyManager.isDataEnabled()).thenReturn(true); - doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(false)); + doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(false), eq(false)); setupGetIconConditions(subId, false, true, true, ServiceState.STATE_IN_SERVICE); mController.onResume(); @@ -527,7 +533,7 @@ public class SubscriptionsPreferenceControllerTest { doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); Drawable icon = mock(Drawable.class); when(mTelephonyManager.isDataEnabled()).thenReturn(false); - doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(true)); + doReturn(icon).when(sInjector).getIcon(any(), anyInt(), anyInt(), eq(true), eq(false)); setupGetIconConditions(subId, false, false, false, ServiceState.STATE_IN_SERVICE); @@ -559,7 +565,8 @@ public class SubscriptionsPreferenceControllerTest { mController.getIcon(SUB_ID); - verify(sInjector).getIcon(any(), eq(SIGNAL_STRENGTH_GOOD), anyInt(), anyBoolean()); + verify(sInjector).getIcon(any(), eq(SIGNAL_STRENGTH_GOOD), anyInt(), anyBoolean(), + anyBoolean()); } @Test @@ -576,7 +583,7 @@ public class SubscriptionsPreferenceControllerTest { mController.getIcon(SUB_ID); - verify(sInjector).getIcon(any(), eq(WIFI_LEVEL_MAX), anyInt(), anyBoolean()); + verify(sInjector).getIcon(any(), eq(WIFI_LEVEL_MAX), anyInt(), anyBoolean(), anyBoolean()); } @Test