[Settings] Add the CarrierNetworkChange case

Bug: 262927206
Test: atest SubscriptionsPreferenceControllerTest
Change-Id: If7eeb185224179d59ab841af0062ec5b98b956e9
This commit is contained in:
Zoey Chen
2023-02-06 20:02:29 +08:00
parent e5cebf9452
commit 69fe84219a
5 changed files with 61 additions and 46 deletions

View File

@@ -61,6 +61,7 @@ import java.util.stream.Collectors;
/** /**
* The helper is for slice of carrier and non-Carrier, used by ProviderModelSlice. * 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 { public class ProviderModelSliceHelper {
private static final String TAG = "ProviderModelSlice"; private static final String TAG = "ProviderModelSlice";
@@ -140,7 +141,7 @@ public class ProviderModelSliceHelper {
numLevels += 1; numLevels += 1;
} }
return MobileNetworkUtils.getSignalStrengthIcon(mContext, level, numLevels, return MobileNetworkUtils.getSignalStrengthIcon(mContext, level, numLevels,
NO_CELL_DATA_TYPE_ICON, false); NO_CELL_DATA_TYPE_ICON, false, false);
} }
/** /**

View File

@@ -36,10 +36,12 @@ import android.telephony.ServiceState;
import android.telephony.SignalStrength; import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyDisplayInfo; import android.telephony.TelephonyDisplayInfo;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.Html; import android.text.Html;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.collection.ArrayMap; import androidx.collection.ArrayMap;
@@ -78,7 +80,8 @@ import java.util.Set;
public class SubscriptionsPreferenceController extends AbstractPreferenceController implements public class SubscriptionsPreferenceController extends AbstractPreferenceController implements
LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient,
MobileDataEnabledListener.Client, DataConnectivityListener.Client, MobileDataEnabledListener.Client, DataConnectivityListener.Client,
SignalStrengthListener.Callback, TelephonyDisplayInfoListener.Callback { SignalStrengthListener.Callback, TelephonyDisplayInfoListener.Callback,
TelephonyCallback.CarrierNetworkListener {
private static final String TAG = "SubscriptionsPrefCntrlr"; private static final String TAG = "SubscriptionsPrefCntrlr";
private UpdateListener mUpdateListener; private UpdateListener mUpdateListener;
@@ -93,6 +96,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
private TelephonyDisplayInfoListener mTelephonyDisplayInfoListener; private TelephonyDisplayInfoListener mTelephonyDisplayInfoListener;
private WifiPickerTrackerHelper mWifiPickerTrackerHelper; private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
private boolean mCarrierNetworkChangeMode;
@VisibleForTesting @VisibleForTesting
final BroadcastReceiver mConnectionChangeReceiver = new BroadcastReceiver() { final BroadcastReceiver mConnectionChangeReceiver = new BroadcastReceiver() {
@@ -283,8 +287,8 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
? false ? false
: regInfo.isRegistered(); : regInfo.isRegistered();
final boolean isCarrierNetworkActive = isCarrierNetworkActive(); final boolean isCarrierNetworkActive = isCarrierNetworkActive();
String result = mSubsPrefCtrlInjector.getNetworkType( String result = mSubsPrefCtrlInjector.getNetworkType(mContext, mConfig,
mContext, mConfig, mTelephonyDisplayInfo, subId, isCarrierNetworkActive); mTelephonyDisplayInfo, subId, isCarrierNetworkActive, mCarrierNetworkChangeMode);
if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext) || isCarrierNetworkActive) { if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext) || isCarrierNetworkActive) {
if (result.isEmpty()) { if (result.isEmpty()) {
result = mContext.getString(isDds ? R.string.mobile_data_connection_active result = mContext.getString(isDds ? R.string.mobile_data_connection_active
@@ -335,7 +339,8 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
|| (!isDds(subId) && tmForSubId.isMobileDataPolicyEnabled( || (!isDds(subId) && tmForSubId.isMobileDataPolicyEnabled(
TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH)); TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH));
if (isDataInService || isVoiceInService || isCarrierNetworkActive) { if (isDataInService || isVoiceInService || isCarrierNetworkActive) {
icon = mSubsPrefCtrlInjector.getIcon(mContext, level, numLevels, !isDataEnabled); icon = mSubsPrefCtrlInjector.getIcon(mContext, level, numLevels, !isDataEnabled,
mCarrierNetworkChangeMode);
} }
final boolean isActiveCellularNetwork = final boolean isActiveCellularNetwork =
@@ -472,6 +477,12 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
update(); update();
} }
@Override
public void onCarrierNetworkChange(boolean active) {
mCarrierNetworkChangeMode = active;
update();
}
public void setWifiPickerTrackerHelper(WifiPickerTrackerHelper helper) { public void setWifiPickerTrackerHelper(WifiPickerTrackerHelper helper) {
mWifiPickerTrackerHelper = 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, public String getNetworkType(Context context, Config config,
TelephonyDisplayInfo telephonyDisplayInfo, int subId) { 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); String iconKey = getIconKey(telephonyDisplayInfo);
MobileIconGroup iconGroup = mapIconSets(config).get(iconKey); iconGroup = mapIconSets(config).get(iconKey);
int resId = 0;
if (iconGroup != null) {
resId = iconGroup.dataContentDescription;
} }
if (iconGroup == null) {
Log.d(TAG, "Can not get the network's icon and description.");
return "";
}
int resId = iconGroup.dataContentDescription;
return resId != 0 return resId != 0
? SubscriptionManager.getResourcesForSubId(context, subId).getString(resId) ? 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. * 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, return MobileNetworkUtils.getSignalStrengthIcon(context, level, numLevels,
NO_CELL_DATA_TYPE_ICON, cutOut); NO_CELL_DATA_TYPE_ICON, cutOut, carrierNetworkChanged);
} }
} }
} }

View File

@@ -613,10 +613,11 @@ public class MobileNetworkUtils {
} }
public static Drawable getSignalStrengthIcon(Context context, int level, int numLevels, 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); final SignalDrawable signalDrawable = new SignalDrawable(context);
signalDrawable.setLevel( signalDrawable.setLevel(
SignalDrawable.getState(level, numLevels, cutOut)); carrierNetworkChanged ? SignalDrawable.getCarrierChangeState(numLevels)
: SignalDrawable.getState(level, numLevels, cutOut));
// Make the network type drawable // Make the network type drawable
final Drawable networkDrawable = final Drawable networkDrawable =

View File

@@ -260,6 +260,6 @@ public class NetworkOperatorPreference extends Preference {
} }
final Context context = getContext(); final Context context = getContext();
setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS, setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS,
getIconIdForCell(mCellInfo), false)); getIconIdForCell(mCellInfo), false, false));
} }
} }

View File

@@ -275,7 +275,8 @@ public class SubscriptionsPreferenceControllerTest {
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
doReturn(networkType) 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.onResume();
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
@@ -295,7 +296,7 @@ public class SubscriptionsPreferenceControllerTest {
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
doReturn(networkType) 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(); doReturn(true).when(mWifiPickerTrackerHelper).isCarrierNetworkActive();
mController.onResume(); mController.onResume();
@@ -317,7 +318,8 @@ public class SubscriptionsPreferenceControllerTest {
setupGetIconConditions(sub.get(0).getSubscriptionId(), false, false, setupGetIconConditions(sub.get(0).getSubscriptionId(), false, false,
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(networkType) 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.onResume();
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
@@ -336,7 +338,8 @@ public class SubscriptionsPreferenceControllerTest {
setupGetIconConditions(sub.get(0).getSubscriptionId(), false, true, setupGetIconConditions(sub.get(0).getSubscriptionId(), false, true,
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(networkType) 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); when(mTelephonyManager.isDataEnabled()).thenReturn(true);
mController.onResume(); mController.onResume();
@@ -371,7 +374,8 @@ public class SubscriptionsPreferenceControllerTest {
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
doReturn(networkType) 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); when(mTelephonyManager.isDataEnabled()).thenReturn(true);
mController.onResume(); mController.onResume();
@@ -397,7 +401,8 @@ public class SubscriptionsPreferenceControllerTest {
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
doReturn(networkType) 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.onResume();
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
@@ -424,7 +429,8 @@ public class SubscriptionsPreferenceControllerTest {
false, ServiceState.STATE_OUT_OF_SERVICE); false, ServiceState.STATE_OUT_OF_SERVICE);
doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext); doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
doReturn(networkType) 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.onResume();
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
@@ -491,7 +497,7 @@ public class SubscriptionsPreferenceControllerTest {
doReturn(sub.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); doReturn(sub.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo();
Drawable icon = mock(Drawable.class); Drawable icon = mock(Drawable.class);
when(mTelephonyManager.isDataEnabled()).thenReturn(true); 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, setupGetIconConditions(sub.get(0).getSubscriptionId(), true, true,
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
@@ -509,7 +515,7 @@ public class SubscriptionsPreferenceControllerTest {
doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo();
Drawable icon = mock(Drawable.class); Drawable icon = mock(Drawable.class);
when(mTelephonyManager.isDataEnabled()).thenReturn(true); 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, setupGetIconConditions(subId, false, true,
true, ServiceState.STATE_IN_SERVICE); true, ServiceState.STATE_IN_SERVICE);
mController.onResume(); mController.onResume();
@@ -527,7 +533,7 @@ public class SubscriptionsPreferenceControllerTest {
doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo(); doReturn(subs.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo();
Drawable icon = mock(Drawable.class); Drawable icon = mock(Drawable.class);
when(mTelephonyManager.isDataEnabled()).thenReturn(false); 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, setupGetIconConditions(subId, false, false,
false, ServiceState.STATE_IN_SERVICE); false, ServiceState.STATE_IN_SERVICE);
@@ -559,7 +565,8 @@ public class SubscriptionsPreferenceControllerTest {
mController.getIcon(SUB_ID); 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 @Test
@@ -576,7 +583,7 @@ public class SubscriptionsPreferenceControllerTest {
mController.getIcon(SUB_ID); 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 @Test