Use SubscriptionManager.getResourcesForSubId to get Resources

To support multi-SIM card case. The code uses the
SubscriptionManager.getResourcesForSubId to get Resources.

Bug: 171843666
Test: make RunSettingsRoboTests ROBOTEST_FILTER=\
EnabledNetworkModePreferenceControllerTest

Change-Id: Ib31e23b81bdbd3b5336b45fc73994f40d021718b
Merged-In: Ib31e23b81bdbd3b5336b45fc73994f40d021718b
diff --git a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
index fd585a1..8a32a95 100644
--- a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
@@ -164,7 +164,22 @@
         }
     }
 
-    private final static class PreferenceEntriesBuilder {
+    enum EnabledNetworks {
+        ENABLED_NETWORKS_UNKNOWN,
+        ENABLED_NETWORKS_CDMA_CHOICES,
+        ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES,
+        ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES,
+        ENABLED_NETWORKS_TDSCDMA_CHOICES,
+        ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES,
+        ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES,
+        ENABLED_NETWORKS_EXCEPT_GSM_CHOICES,
+        ENABLED_NETWORKS_EXCEPT_LTE_CHOICES,
+        ENABLED_NETWORKS_4G_CHOICES,
+        ENABLED_NETWORKS_CHOICES,
+        PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE
+    }
+
+    private final class PreferenceEntriesBuilder {
         private CarrierConfigManager mCarrierConfigManager;
         private Context mContext;
         private TelephonyManager mTelephonyManager;
@@ -181,21 +196,6 @@
         private List<String> mEntries = new ArrayList<>();
         private List<Integer> mEntriesValue = new ArrayList<>();
 
-        enum EnabledNetworks {
-            ENABLED_NETWORKS_UNKNOWN,
-            ENABLED_NETWORKS_CDMA_CHOICES,
-            ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES,
-            ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES,
-            ENABLED_NETWORKS_TDSCDMA_CHOICES,
-            ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES,
-            ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES,
-            ENABLED_NETWORKS_EXCEPT_GSM_CHOICES,
-            ENABLED_NETWORKS_EXCEPT_LTE_CHOICES,
-            ENABLED_NETWORKS_4G_CHOICES,
-            ENABLED_NETWORKS_CHOICES,
-            PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE
-        }
-
         PreferenceEntriesBuilder(Context context, int subId) {
             this.mContext = context;
             this.mSubId = subId;
@@ -226,7 +226,7 @@
             int[] entryValuesInt;
             switch (getEnabledNetworkType()) {
                 case ENABLED_NETWORKS_CDMA_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_cdma_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 4) {
@@ -240,7 +240,7 @@
                     addGlobalEntry(entryValuesInt[3]);
                     break;
                 case ENABLED_NETWORKS_CDMA_NO_LTE_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_cdma_no_lte_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 2) {
@@ -251,7 +251,7 @@
                     add1xEntry(entryValuesInt[1]);
                     break;
                 case ENABLED_NETWORKS_CDMA_ONLY_LTE_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_cdma_only_lte_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 2) {
@@ -262,7 +262,7 @@
                     addGlobalEntry(entryValuesInt[1]);
                     break;
                 case ENABLED_NETWORKS_TDSCDMA_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_tdscdma_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 3) {
@@ -275,7 +275,7 @@
                     add2gEntry(entryValuesInt[2]);
                     break;
                 case ENABLED_NETWORKS_EXCEPT_GSM_LTE_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_except_gsm_lte_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 1) {
@@ -285,7 +285,7 @@
                     add3gEntry(entryValuesInt[0]);
                     break;
                 case ENABLED_NETWORKS_EXCEPT_GSM_4G_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_except_gsm_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 2) {
@@ -297,7 +297,7 @@
                     add3gEntry(entryValuesInt[1]);
                     break;
                 case ENABLED_NETWORKS_EXCEPT_GSM_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_except_gsm_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 2) {
@@ -309,7 +309,7 @@
                     add3gEntry(entryValuesInt[1]);
                     break;
                 case ENABLED_NETWORKS_EXCEPT_LTE_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_except_lte_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 2) {
@@ -320,7 +320,7 @@
                     add2gEntry(entryValuesInt[1]);
                     break;
                 case ENABLED_NETWORKS_4G_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 3) {
@@ -334,7 +334,7 @@
                     add2gEntry(entryValuesInt[2]);
                     break;
                 case ENABLED_NETWORKS_CHOICES:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.enabled_networks_values);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 3) {
@@ -346,7 +346,7 @@
                     add2gEntry(entryValuesInt[2]);
                     break;
                 case PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE:
-                    entryValues = mContext.getResources().getStringArray(
+                    entryValues = getResourcesForSubId().getStringArray(
                             R.array.preferred_network_mode_values_world_mode);
                     entryValuesInt = Stream.of(entryValues).mapToInt(Integer::parseInt).toArray();
                     if (entryValuesInt.length < 3) {
@@ -354,9 +354,14 @@
                                 "PREFERRED_NETWORK_MODE_CHOICES_WORLD_MODE index error.");
                     }
                     addGlobalEntry(entryValuesInt[0]);
-                    addCustomEntry(mContext.getString(R.string.network_world_mode_cdma_lte),
+
+                    addCustomEntry(
+                            getResourcesForSubId().getString(
+                                    R.string.network_world_mode_cdma_lte),
                             entryValuesInt[1]);
-                    addCustomEntry(mContext.getString(R.string.network_world_mode_gsm_lte),
+                    addCustomEntry(
+                            getResourcesForSubId().getString(
+                                    R.string.network_world_mode_gsm_lte),
                             entryValuesInt[2]);
                     break;
                 default:
@@ -571,8 +576,9 @@
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_WCDMA:
                     setSelectedEntry(
                             TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
-                    setSummary(mContext.getString(R.string.network_5G)
-                            + mContext.getString(R.string.network_recommended));
+                    setSummary(getResourcesForSubId().getString(R.string.network_5G)
+                            + getResourcesForSubId().getString(
+                            R.string.network_recommended));
                     break;
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA:
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM:
@@ -581,13 +587,15 @@
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
                     setSelectedEntry(TelephonyManagerConstants
                             .NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA);
-                    setSummary(mContext.getString(R.string.network_5G)
-                            + mContext.getString(R.string.network_recommended));
+                    setSummary(getResourcesForSubId().getString(R.string.network_5G)
+                            + getResourcesForSubId().getString(
+                            R.string.network_recommended));
                     break;
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO:
                     setSelectedEntry(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO);
-                    setSummary(mContext.getString(R.string.network_5G)
-                            + mContext.getString(R.string.network_recommended));
+                    setSummary(getResourcesForSubId().getString(R.string.network_5G)
+                            + getResourcesForSubId().getString(
+                            R.string.network_recommended));
                     break;
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA:
                     setSelectedEntry(
@@ -597,13 +605,15 @@
                             || MobileNetworkUtils.isWorldMode(mContext, mSubId)) {
                         setSummary(R.string.network_global);
                     } else {
-                        setSummary(mContext.getString(R.string.network_5G)
-                                + mContext.getString(R.string.network_recommended));
+                        setSummary(getResourcesForSubId().getString(R.string.network_5G)
+                                + getResourcesForSubId().getString(
+                                R.string.network_recommended));
                     }
                     break;
                 default:
                     setSummary(
-                            mContext.getString(R.string.mobile_network_mode_error, networkMode));
+                            getResourcesForSubId().getString(
+                                    R.string.mobile_network_mode_error, networkMode));
             }
         }
 
@@ -613,7 +623,7 @@
          * @param networkType an LTE network mode without 5G.
          * @return the corresponding network mode with 5G.
          */
-        private static int addNrToLteNetworkType(int networkType) {
+        private int addNrToLteNetworkType(int networkType) {
             switch(networkType) {
                 case TelephonyManagerConstants.NETWORK_MODE_LTE_ONLY:
                     return TelephonyManagerConstants.NETWORK_MODE_NR_LTE;
@@ -647,7 +657,7 @@
          * @param networkType an 5G network mode.
          * @return the corresponding network mode without 5G.
          */
-        private static int reduceNrToLteNetworkType(int networkType) {
+        private int reduceNrToLteNetworkType(int networkType) {
             switch(networkType) {
                 case TelephonyManagerConstants.NETWORK_MODE_NR_LTE:
                     return TelephonyManagerConstants.NETWORK_MODE_LTE_ONLY;
@@ -689,8 +699,8 @@
         private void add5gEntry(int value) {
             boolean isNRValue = value >= TelephonyManagerConstants.NETWORK_MODE_NR_ONLY;
             if (showNrList() && isNRValue) {
-                mEntries.add(mContext.getString(R.string.network_5G)
-                        + mContext.getString(R.string.network_recommended));
+                mEntries.add(getResourcesForSubId().getString(R.string.network_5G)
+                        + getResourcesForSubId().getString(R.string.network_recommended));
                 mEntriesValue.add(value);
                 mIs5gEntryDisplayed = true;
             } else {
@@ -706,7 +716,7 @@
             Log.d(LOG_TAG, "addGlobalEntry. "
                     + " supported5GRadioAccessFamily: " + mSupported5gRadioAccessFamily
                     + " allowed5GNetworkType: " + mAllowed5gNetworkType);
-            mEntries.add(mContext.getString(R.string.network_global));
+            mEntries.add(getResourcesForSubId().getString(R.string.network_global));
             if (showNrList()) {
                 value = addNrToLteNetworkType(value);
             }
@@ -722,9 +732,9 @@
          */
         private void addLteEntry(int value) {
             if (showNrList()) {
-                mEntries.add(mContext.getString(R.string.network_lte_pure));
+                mEntries.add(getResourcesForSubId().getString(R.string.network_lte_pure));
             } else {
-                mEntries.add(mContext.getString(R.string.network_lte));
+                mEntries.add(getResourcesForSubId().getString(R.string.network_lte));
             }
             mEntriesValue.add(value);
         }
@@ -734,25 +744,25 @@
          */
         private void add4gEntry(int value) {
             if (showNrList()) {
-                mEntries.add(mContext.getString(R.string.network_4G_pure));
+                mEntries.add(getResourcesForSubId().getString(R.string.network_4G_pure));
             } else {
-                mEntries.add(mContext.getString(R.string.network_4G));
+                mEntries.add(getResourcesForSubId().getString(R.string.network_4G));
             }
             mEntriesValue.add(value);
         }
 
         private void add3gEntry(int value) {
-            mEntries.add(mContext.getString(R.string.network_3G));
+            mEntries.add(getResourcesForSubId().getString(R.string.network_3G));
             mEntriesValue.add(value);
         }
 
         private void add2gEntry(int value) {
-            mEntries.add(mContext.getString(R.string.network_2G));
+            mEntries.add(getResourcesForSubId().getString(R.string.network_2G));
             mEntriesValue.add(value);
         }
 
         private void add1xEntry(int value) {
-            mEntries.add(mContext.getString(R.string.network_1x));
+            mEntries.add(getResourcesForSubId().getString(R.string.network_1x));
             mEntriesValue.add(value);
         }
 
@@ -800,7 +810,7 @@
         }
 
         private void setSummary(int summaryResId) {
-            setSummary(mContext.getString(summaryResId));
+            setSummary(getResourcesForSubId().getString(summaryResId));
         }
 
         private void setSummary(String summary) {
diff --git a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
index 2bd7de9..3972f39 100644
--- a/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/TelephonyBasePreferenceController.java
@@ -17,6 +17,7 @@
 package com.android.settings.network.telephony;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.os.PersistableBundle;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
@@ -74,4 +75,13 @@
                 mContext.getSystemService(CarrierConfigManager.class);
         return carrierConfigMgr.getConfigForSubId(subId);
     }
+
+    /**
+     * Returns the resources associated with Subscription.
+     *
+     * @return Resources associated with Subscription.
+     */
+    public Resources getResourcesForSubId() {
+        return SubscriptionManager.getResourcesForSubId(mContext, mSubId);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
index e519b9a..ca36f1f 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
@@ -363,43 +363,43 @@
 
     @Test
     public void checkResource_stringArrayLength() {
-        String[] entryValues = mContext.getResources().getStringArray(
+        String[] entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_cdma_values);
         assertEquals(4, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_cdma_no_lte_values);
         assertEquals(2, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_cdma_only_lte_values);
         assertEquals(2, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_tdscdma_values);
         assertEquals(3, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_except_gsm_lte_values);
         assertEquals(1, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_except_gsm_values);
         assertEquals(2, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_except_lte_values);
         assertEquals(2, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_values);
         assertEquals(3, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.enabled_networks_values);
         assertEquals(3, entryValues.length);
 
-        entryValues = mContext.getResources().getStringArray(
+        entryValues = mController.getResourcesForSubId().getStringArray(
                 R.array.preferred_network_mode_values_world_mode);
         assertEquals(3, entryValues.length);
     }