Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 556b34d0 authored by Bonian Chen's avatar Bonian Chen Committed by Android (Google) Code Review
Browse files

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

parents d57831b7 c7c84d2c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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);
    }
}
+56 −0
Original line number Diff line number Diff line
@@ -535,4 +535,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();
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -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)
+2 −2
Original line number Diff line number Diff line
@@ -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);
        }
+33 −0
Original line number Diff line number Diff line
@@ -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();
    }
}
Loading