Merge "[Settings Telephony] Carrier name API replacement" into qt-qpr1-dev

am: 556b34d03d

Change-Id: Ibffb60c57aa4152b4681f3fe95dae7175f2e878e
This commit is contained in:
Bonian Chen
2019-10-01 19:53:31 -07:00
committed by android-build-merger
6 changed files with 113 additions and 10 deletions

View File

@@ -37,6 +37,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.core.FeatureFlags;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.network.telephony.MobileNetworkActivity;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.Utils;
@@ -162,6 +163,6 @@ public class MobileNetworkPreferenceController extends AbstractPreferenceControl
@Override
public CharSequence getSummary() {
return mTelephonyManager.getNetworkOperatorName();
return MobileNetworkUtils.getCurrentCarrierNameForDisplay(mContext);
}
}

View File

@@ -536,4 +536,60 @@ public class MobileNetworkUtils {
icons.setTintList(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
return icons;
}
/**
* This method is migrated from
* {@link android.telephony.TelephonyManager.getNetworkOperatorName}. Which provides
*
* 1. Better support under multi-SIM environment.
* 2. Similar design which aligned with operator name displayed in status bar
*/
public static CharSequence getCurrentCarrierNameForDisplay(Context context, int subId) {
SubscriptionManager sm = context.getSystemService(SubscriptionManager.class);
if (sm != null) {
SubscriptionInfo subInfo = getSubscriptionInfo(sm, subId);
if (subInfo != null) {
return subInfo.getCarrierName();
}
}
return getOperatorNameFromTelephonyManager(context);
}
public static CharSequence getCurrentCarrierNameForDisplay(Context context) {
SubscriptionManager sm = context.getSystemService(SubscriptionManager.class);
if (sm != null) {
int subId = sm.getDefaultSubscriptionId();
SubscriptionInfo subInfo = getSubscriptionInfo(sm, subId);
if (subInfo != null) {
return subInfo.getCarrierName();
}
}
return getOperatorNameFromTelephonyManager(context);
}
private static SubscriptionInfo getSubscriptionInfo(SubscriptionManager subManager,
int subId) {
List<SubscriptionInfo> subInfos = subManager.getAccessibleSubscriptionInfoList();
if (subInfos == null) {
subInfos = subManager.getActiveSubscriptionInfoList();
}
if (subInfos == null) {
return null;
}
for (SubscriptionInfo subInfo : subInfos) {
if (subInfo.getSubscriptionId() == subId) {
return subInfo;
}
}
return null;
}
private static String getOperatorNameFromTelephonyManager(Context context) {
TelephonyManager tm =
(TelephonyManager) context.getSystemService(TelephonyManager.class);
if (tm == null) {
return null;
}
return tm.getNetworkOperatorName();
}
}

View File

@@ -310,9 +310,8 @@ public class NetworkSelectSettings extends DashboardFragment {
* 1. use {@code ServiceState#getNetworkRegistrationInfoList()} to get the currently
* registered cellIdentity, wrap it into a CellInfo;
* 2. set the signal strength level as strong;
* 3. use {@link TelephonyManager#getNetworkOperatorName()} to get the title of the
* previously connected network operator, since the CellIdentity got from step 1 only has
* PLMN.
* 3. get the title of the previously connected network operator, since the CellIdentity
* got from step 1 only has PLMN.
* - If the device has no data, we will remove the connected network operators list from the
* screen.
*/
@@ -333,7 +332,8 @@ public class NetworkSelectSettings extends DashboardFragment {
if (cellInfo != null) {
NetworkOperatorPreference pref = new NetworkOperatorPreference(
cellInfo, getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
pref.setTitle(mTelephonyManager.getNetworkOperatorName());
pref.setTitle(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
getPrefContext(), mSubId));
pref.setSummary(R.string.network_connected);
// Update the signal strength icon, since the default signalStrength value would be
// zero (it would be quite confusing why the connected network has no signal)

View File

@@ -75,7 +75,7 @@ public class OpenNetworkSelectPagePreferenceController extends
public CharSequence getSummary() {
final ServiceState ss = mTelephonyManager.getServiceState();
if (ss != null && ss.getState() == ServiceState.STATE_IN_SERVICE) {
return mTelephonyManager.getNetworkOperatorName();
return MobileNetworkUtils.getCurrentCarrierNameForDisplay(mContext, mSubId);
} else {
return mContext.getString(R.string.network_disconnected);
}
@@ -108,4 +108,4 @@ public class OpenNetworkSelectPagePreferenceController extends
public void onNetworkSelectModeChanged() {
updateState(mPreference);
}
}
}

View File

@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -58,6 +59,10 @@ 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;
@@ -89,6 +94,7 @@ public class MobileNetworkUtilsTest {
mContext = spy(RuntimeEnvironment.application);
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);
@@ -102,10 +108,17 @@ public class MobileNetworkUtilsTest {
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(eq(true))).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(
PLMN_FROM_TELEPHONY_MANAGER_API);
}
@Test
@@ -301,4 +314,24 @@ public class MobileNetworkUtilsTest {
TelephonyManager.NETWORK_MODE_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();
}
}

View File

@@ -17,7 +17,7 @@
package com.android.settings.network.telephony.gsm;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -26,12 +26,15 @@ import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.network.telephony.MobileNetworkUtils;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@@ -54,6 +57,8 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ServiceState mServiceState;
@Mock
private SubscriptionInfo mSubscriptionInfo;
private PersistableBundle mCarrierConfig;
private OpenNetworkSelectPagePreferenceController mController;
@@ -75,6 +80,16 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
when(mSubscriptionInfo.getCarrierName()).thenReturn(OPERATOR_NAME);
when(mSubscriptionManager.getActiveSubscriptionInfoList(eq(true))).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(OPERATOR_NAME);
mPreference = new Preference(mContext);
mController = new OpenNetworkSelectPagePreferenceController(mContext,
"open_network_select");
@@ -94,7 +109,6 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
@Test
public void getSummary_inService_returnOperatorName() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
doReturn(OPERATOR_NAME).when(mTelephonyManager).getNetworkOperatorName();
assertThat(mController.getSummary()).isEqualTo(OPERATOR_NAME);
}
@@ -102,7 +116,6 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
@Test
public void getSummary_notInService_returnDisconnect() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE);
doReturn(OPERATOR_NAME).when(mTelephonyManager).getNetworkOperatorName();
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.network_disconnected));