[Settings Telephony] Carrier name API replacement

Replacing TelephonyManager.getNetworkOperatorName() by
Obtaining carrier name from SubscriptionInfo.getCarrierName().

TelephonyManager.getNetworkOperatorName() only be used when there's no
SubscriptionInfo can be found.

Bug: 140443508
Test: Manual testing
  atest MobileNetworkUtilsTest
  atest OpenNetworkSelectPagePreferenceControllerTest

Change-Id: I7a86395c86d31fe2ba54c04ac16b1a0ebfc843f3
diff --git a/src/com/android/settings/network/MobileNetworkPreferenceController.java b/src/com/android/settings/network/MobileNetworkPreferenceController.java
index 3813435..646f10c 100644
--- a/src/com/android/settings/network/MobileNetworkPreferenceController.java
+++ b/src/com/android/settings/network/MobileNetworkPreferenceController.java
@@ -37,6 +37,7 @@
 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 @@
 
     @Override
     public CharSequence getSummary() {
-        return mTelephonyManager.getNetworkOperatorName();
+        return MobileNetworkUtils.getCurrentCarrierNameForDisplay(mContext);
     }
 }
diff --git a/src/com/android/settings/network/telephony/MobileNetworkUtils.java b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
index 0e5eaa8..753009c 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkUtils.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkUtils.java
@@ -535,4 +535,60 @@
         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();
+    }
 }
diff --git a/src/com/android/settings/network/telephony/NetworkSelectSettings.java b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
index 62947d1..e288763 100644
--- a/src/com/android/settings/network/telephony/NetworkSelectSettings.java
+++ b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
@@ -310,9 +310,8 @@
      * 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 @@
             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)
diff --git a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
index 46171bc..d679373 100644
--- a/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceController.java
@@ -75,7 +75,7 @@
     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 void onNetworkSelectModeChanged() {
         updateState(mPreference);
     }
-}
\ No newline at end of file
+}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
index 2bfaeca..c6b8689 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java
@@ -22,6 +22,7 @@
 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 @@
     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 @@
 
         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 @@
         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 @@
                 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();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
index d37d934..6150675 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/gsm/OpenNetworkSelectPagePreferenceControllerTest.java
@@ -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.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 @@
     private CarrierConfigManager mCarrierConfigManager;
     @Mock
     private ServiceState mServiceState;
+    @Mock
+    private SubscriptionInfo mSubscriptionInfo;
 
     private PersistableBundle mCarrierConfig;
     private OpenNetworkSelectPagePreferenceController mController;
@@ -75,6 +80,16 @@
         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 @@
     @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 @@
     @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));