Preferred network list add 5G item

Preferred network list add 5G item, and change "recommended" from 4G to
5G, and add summary for 5G.

Bug: 138544359
Test: 1.fake the device network type bitmask to 5G, UI show 5G list
2.atest EnabledNetworkModePreferenceControllerTest.java :pass

Change-Id: I4a78e2e7fbb067ce841df4aaf1d5161607fdcf90
Merged-In: I4a78e2e7fbb067ce841df4aaf1d5161607fdcf90
diff --git a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
index 7d53e22..3abb36a 100644
--- a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
@@ -26,9 +26,12 @@
 import android.os.PersistableBundle;
 import android.provider.Settings;
 import android.telephony.CarrierConfigManager;
+import android.telephony.RadioAccessFamily;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.Log;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.lifecycle.Lifecycle;
@@ -42,6 +45,9 @@
 import com.android.internal.telephony.PhoneConstants;
 import com.android.settings.R;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Preference controller for "Enabled network mode"
  */
@@ -49,6 +55,7 @@
         TelephonyBasePreferenceController implements
         ListPreference.OnPreferenceChangeListener, LifecycleObserver {
 
+    private static final String LOG_TAG = "EnabledNetworkMode";
     private CarrierConfigManager mCarrierConfigManager;
     private ContentObserver mPreferredNetworkModeObserver;
     private TelephonyManager mTelephonyManager;
@@ -56,6 +63,8 @@
     @VisibleForTesting
     boolean mShow4GForLTE;
     private Preference mPreference;
+    @VisibleForTesting
+    boolean mDisplay5gList = false;
 
     public EnabledNetworkModePreferenceController(Context context, String key) {
         super(context, key);
@@ -149,6 +158,11 @@
                 ? carrierConfig.getBoolean(
                 CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL)
                 : false;
+
+        final long supportedRadioBitmask = mTelephonyManager.getSupportedRadioAccessFamily();
+        mDisplay5gList = checkSupportedRadioBitmask(
+                supportedRadioBitmask, mTelephonyManager.NETWORK_TYPE_BITMASK_NR);
+
         lifecycle.addObserver(this);
     }
 
@@ -249,6 +263,139 @@
             preference.setEntryValues(
                     R.array.preferred_network_mode_values_world_mode);
         }
+
+        if (mDisplay5gList) {
+            add5gListItem(preference);
+        }
+    }
+
+    @VisibleForTesting
+    boolean checkSupportedRadioBitmask(long supportedRadioBitmask, long targetBitmask) {
+        Log.d(LOG_TAG, "supportedRadioBitmask: " + supportedRadioBitmask);
+        if ((targetBitmask & supportedRadioBitmask) > 0) {
+            return true;
+        }
+        return false;
+    }
+
+    /***
+     * Preferred network list add 5G item.
+     *
+     * @string/enabled_networks_cdma_choices
+     *         Before            |        After
+     * @string/network_lte   , 8 |@string/network_5G + @string/network_recommended , 25
+     * @string/network_3G    , 4 |@string/network_lte_pure, 8
+     * @string/network_1x    , 5 |@string/network_3G      , 4
+     * @string/network_global, 10|@string/network_1x      , 5
+     *                           |@string/network_global  , 27
+     *
+     * @string/enabled_networks_cdma_only_lte_choices
+     *         Before            |        After
+     * @string/network_lte   , 8 |@string/network_5G + @string/network_recommended , 25
+     * @string/network_global, 10|@string/network_lte_pure, 8
+     *                           |@string/network_global  , 27
+     *
+     * @string/enabled_networks_tdscdma_choices
+     *         Before         |        After
+     * @string/network_lte, 22|@string/network_5G + @string/network_recommended , 33
+     * @string/network_3G , 18|@string/network_lte_pure, 22
+     * @string/network_2G , 1 |@string/network_3G      , 18
+     *                        |@string/network_2G      , 1
+     *
+     * @string/enabled_networks_except_gsm_4g_choices
+     *         Before         |        After
+     * @string/network_4G , 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_4G_pure , 9
+     *                        |@string/network_3G      , 0
+     *
+     * @string/enabled_networks_except_gsm_choices
+     *         Before         |        After
+     * @string/network_lte, 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_lte_pure, 9
+     *                        |@string/network_3G      , 0
+     *
+     * @string/enabled_networks_4g_choices
+     *         Before         |        After
+     * @string/network_4G , 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_4G_pure , 9
+     * @string/network_2G , 1 |@string/network_3G      , 0
+     *                        |@string/network_2G      , 1
+     *
+     * @string/enabled_networks_choices
+     *         Before         |        After
+     * @string/network_lte, 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_lte_pure, 9
+     * @string/network_2G , 1 |@string/network_3G      , 0
+     *                        |@string/network_2G      , 1
+     *
+     * @string/preferred_network_mode_choices_world_mode
+     *         Before         |        After
+     * "Global"           , 10|@string/network_global  , 27
+     * "LTE / CDMA"       , 8 |"LTE / CDMA"            , 8
+     * "LTE / GSM / UMTS" , 9 |"LTE / GSM / UMTS"      , 9
+     */
+    @VisibleForTesting
+    void add5gListItem(ListPreference preference) {
+        final CharSequence[] oldEntries = preference.getEntries();
+        final CharSequence[] oldEntryValues = preference.getEntryValues();
+        List<CharSequence> newEntries = new ArrayList<>();
+        List<CharSequence> newEntryValues = new ArrayList<>();
+
+        CharSequence oldEntry;
+        CharSequence oldEntryValue;
+        CharSequence new5gEntry;
+        CharSequence new5gEntryValue;
+
+        for (int i = 0; i < oldEntries.length; i++) {
+            oldEntry = oldEntries[i];
+            oldEntryValue = oldEntryValues[i];
+            new5gEntry = "";
+            new5gEntryValue = "";
+
+            if (mContext.getString(R.string.network_lte).equals(oldEntry)) {
+                oldEntry = mContext.getString(R.string.network_lte_pure);
+                new5gEntry = mContext.getString(R.string.network_5G)
+                        + mContext.getString(R.string.network_recommended);
+                new5gEntryValue = transformLteEntryValueTo5gEntryValue(oldEntryValue);
+            } else if (mContext.getString(R.string.network_4G).equals(oldEntry)) {
+                oldEntry = mContext.getString(R.string.network_4G_pure);
+                new5gEntry = mContext.getString(R.string.network_5G)
+                        + mContext.getString(R.string.network_recommended);
+                new5gEntryValue = transformLteEntryValueTo5gEntryValue(oldEntryValue);
+            } else if (mContext.getString(R.string.network_global).equals(oldEntry)) {
+                //oldEntry: network_global
+                //oldEntryValue: TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA
+                oldEntryValue = Integer.toString(
+                        TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
+            }
+
+            if (!TextUtils.isEmpty(new5gEntry)) {
+                newEntries.add(new5gEntry);
+                newEntryValues.add(new5gEntryValue);
+            }
+            newEntries.add(oldEntry);
+            newEntryValues.add(oldEntryValue);
+        }
+
+        preference.setEntries(newEntries.toArray(new CharSequence[newEntries.size()]));
+        preference.setEntryValues(newEntryValues.toArray(new CharSequence[newEntryValues.size()]));
+    }
+
+    /**
+     * LTE network mode transform to 5G network mode.
+     *
+     * @param networkMode this is LTE network mode.
+     * @return 5G network mode.
+     */
+    private CharSequence transformLteEntryValueTo5gEntryValue(CharSequence networkMode) {
+        int networkModeInt = Integer.valueOf(networkMode.toString());
+        return Integer.toString(addNrToNetworkType(networkModeInt));
+    }
+
+    private int addNrToNetworkType(int networkType) {
+        long networkTypeBitmasks = RadioAccessFamily.getRafFromNetworkType(networkType);
+        networkTypeBitmasks |= mTelephonyManager.NETWORK_TYPE_BITMASK_NR;
+        return RadioAccessFamily.getNetworkTypeFromRaf((int) networkTypeBitmasks);
     }
 
     private void updatePreferenceValueAndSummary(ListPreference preference, int networkMode) {
@@ -312,7 +459,8 @@
                 } else {
                     preference.setValue(
                             Integer.toString(TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO));
-                    preference.setSummary(R.string.network_lte);
+                    preference.setSummary(
+                            mDisplay5gList ? R.string.network_lte_pure : R.string.network_lte);
                 }
                 break;
             case TelephonyManager.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
@@ -347,7 +495,8 @@
                     preference.setValue(
                             Integer.toString(TelephonyManager
                                     .NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA));
-                    preference.setSummary(R.string.network_lte);
+                    preference.setSummary(
+                            mDisplay5gList ? R.string.network_lte_pure : R.string.network_lte);
                 } else {
                     preference.setValue(
                             Integer.toString(TelephonyManager
@@ -357,11 +506,34 @@
                             || MobileNetworkUtils.isWorldMode(mContext, mSubId)) {
                         preference.setSummary(R.string.network_global);
                     } else {
-                        preference.setSummary(mShow4GForLTE
-                                ? R.string.network_4G : R.string.network_lte);
+                        if (mDisplay5gList) {
+                            preference.setSummary(mShow4GForLTE
+                                    ? R.string.network_4G_pure : R.string.network_lte_pure);
+                        } else {
+                            preference.setSummary(mShow4GForLTE
+                                    ? R.string.network_4G : R.string.network_lte);
+                        }
                     }
                 }
                 break;
+            case TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO:
+            case TelephonyManager.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
+                preference.setValue(Integer.toString(networkMode));
+                preference.setSummary(mContext.getString(R.string.network_5G)
+                        + mContext.getString(R.string.network_recommended));
+                break;
+            case TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA:
+                preference.setValue(
+                        Integer.toString(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA));
+                if (mTelephonyManager.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA
+                        || mIsGlobalCdma
+                        || MobileNetworkUtils.isWorldMode(mContext, mSubId)) {
+                    preference.setSummary(R.string.network_global);
+                } else {
+                    preference.setSummary(mContext.getString(R.string.network_5G)
+                            + mContext.getString(R.string.network_recommended));
+                }
+                break;
             default:
                 preference.setSummary(
                         mContext.getString(R.string.mobile_network_mode_error, networkMode));
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 a4d708e..773041d 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
@@ -148,6 +148,26 @@
     }
 
     @Test
+    public void init_initDisplay5gList_returnTrue() {
+        long testBitmask = TelephonyManager.NETWORK_TYPE_BITMASK_NR
+                | TelephonyManager.NETWORK_TYPE_BITMASK_LTE;
+        doReturn(testBitmask).when(mTelephonyManager).getSupportedRadioAccessFamily();
+
+        mController.init(mLifecycle, SUB_ID);
+
+        assertThat(mController.mDisplay5gList).isTrue();
+    }
+
+    @Test
+    public void checkSupportedRadioBitmask_nrBitmask_returnTrue() {
+        long testBitmask = TelephonyManager.NETWORK_TYPE_BITMASK_NR
+                | TelephonyManager.NETWORK_TYPE_BITMASK_LTE;
+
+        assertThat(mController.checkSupportedRadioBitmask(testBitmask,
+                TelephonyManager.NETWORK_TYPE_BITMASK_NR)).isTrue();
+    }
+
+    @Test
     public void updateState_updateByNetworkMode() {
         Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
@@ -172,6 +192,203 @@
                 String.valueOf(TelephonyManager.NETWORK_MODE_LTE_GSM_WCDMA));
     }
 
+    /**
+     * @string/enabled_networks_cdma_choices
+     *         Before            |        After
+     * @string/network_lte   , 8 |@string/network_5G + @string/network_recommended , 25
+     * @string/network_3G    , 4 |@string/network_lte_pure, 8
+     * @string/network_1x    , 5 |@string/network_3G      , 4
+     * @string/network_global, 10|@string/network_1x      , 5
+     *                           |@string/network_global  , 27
+     *
+     * @string/enabled_networks_cdma_only_lte_choices
+     *         Before            |        After
+     * @string/network_lte   , 8 |@string/network_5G + @string/network_recommended , 25
+     * @string/network_global, 10|@string/network_lte_pure, 8
+     *                           |@string/network_global  , 27
+     */
+    @Test
+    public void add5gListItem_lteCdma_5gLteCdma() {
+        //case#1
+        mPreference.setEntries(R.array.enabled_networks_cdma_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_cdma_values);
+        CharSequence[] testEntries = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_lte_pure)
+                , mContext.getString(R.string.network_3G)
+                , mContext.getString(R.string.network_1x)
+                , mContext.getString(R.string.network_global)};
+        CharSequence[] testEntryValues = {"25", "8", "4", "5", "27"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues);
+
+        //case#2
+        mPreference.setEntries(R.array.enabled_networks_cdma_only_lte_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_cdma_only_lte_values);
+        CharSequence[] testEntries1 = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_lte_pure)
+                , mContext.getString(R.string.network_global)};
+        CharSequence[] testEntryValues1 = {"25", "8", "27"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries1);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues1);
+    }
+
+    /**
+     * @string/enabled_networks_except_gsm_4g_choices
+     *         Before         |        After
+     * @string/network_4G , 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_4G_pure , 9
+     *                        |@string/network_3G      , 0
+     *
+     * @string/enabled_networks_except_gsm_choices
+     *         Before         |        After
+     * @string/network_lte, 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_lte_pure, 9
+     *                        |@string/network_3G      , 0
+     *
+     * @string/enabled_networks_4g_choices
+     *         Before         |        After
+     * @string/network_4G , 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_4G_pure , 9
+     * @string/network_2G , 1 |@string/network_3G      , 0
+     *                        |@string/network_2G      , 1
+     *
+     * @string/enabled_networks_choices
+     *         Before         |        After
+     * @string/network_lte, 9 |@string/network_5G + @string/network_recommended , 26
+     * @string/network_3G , 0 |@string/network_lte_pure, 9
+     * @string/network_2G , 1 |@string/network_3G      , 0
+     *                        |@string/network_2G      , 1
+     */
+    @Test
+    public void add5gListItem_lteGsm_5gLteGsm() {
+        //csae#1
+        mPreference.setEntries(R.array.enabled_networks_except_gsm_4g_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_except_gsm_values);
+        CharSequence[] testEntries = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_4G_pure)
+                , mContext.getString(R.string.network_3G)};
+        CharSequence[] testEntryValues = {"26", "9", "0"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues);
+
+        //case#2
+        mPreference.setEntries(R.array.enabled_networks_except_gsm_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_except_gsm_values);
+        CharSequence[] testEntries1 = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_lte_pure)
+                , mContext.getString(R.string.network_3G)};
+        CharSequence[] testEntryValues1 = {"26", "9", "0"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries1);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues1);
+
+        //case#3
+        mPreference.setEntries(R.array.enabled_networks_4g_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_values);
+        CharSequence[] testEntries2 = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_4G_pure)
+                , mContext.getString(R.string.network_3G)
+                , mContext.getString(R.string.network_2G)};
+        CharSequence[] testEntryValues2 = {"26", "9", "0", "1"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries2);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues2);
+
+        //case#4
+        mPreference.setEntries(R.array.enabled_networks_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_values);
+        CharSequence[] testEntries3 = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_lte_pure)
+                , mContext.getString(R.string.network_3G)
+                , mContext.getString(R.string.network_2G)};
+        CharSequence[] testEntryValues3 = {"26", "9", "0", "1"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries3);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues3);
+    }
+
+    /**
+     * @string/preferred_network_mode_choices_world_mode
+     *         Before         |        After
+     * "Global"           , 10|@string/network_global  , 27
+     * "LTE / CDMA"       , 8 |"LTE / CDMA"            , 8
+     * "LTE / GSM / UMTS" , 9 |"LTE / GSM / UMTS"      , 9
+     */
+    @Test
+    public void add5gListItem_worldPhone_Global() {
+        mPreference.setEntries(R.array.preferred_network_mode_choices_world_mode);
+        mPreference.setEntryValues(R.array.preferred_network_mode_values_world_mode);
+        CharSequence[] testEntries = {mContext.getString(R.string.network_global)
+                , "LTE / CDMA"
+                , "LTE / GSM / UMTS"};
+        CharSequence[] testEntryValues = {"27", "8", "9"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues);
+    }
+
+    /**
+     * @string/enabled_networks_tdscdma_choices
+     *         Before         |        After
+     * @string/network_lte, 22|@string/network_5G + @string/network_recommended , 33
+     * @string/network_3G , 18|@string/network_lte_pure, 22
+     * @string/network_2G , 1 |@string/network_3G      , 18
+     *                        |@string/network_2G      , 1
+     */
+    @Test
+    public void add5gListItem_td_5gTd() {
+        mPreference.setEntries(R.array.enabled_networks_tdscdma_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_tdscdma_values);
+        CharSequence[] testEntries = {mContext.getString(R.string.network_5G)
+                + mContext.getString(R.string.network_recommended)
+                , mContext.getString(R.string.network_lte_pure)
+                , mContext.getString(R.string.network_3G)
+                , mContext.getString(R.string.network_2G)};
+        CharSequence[] testEntryValues = {"33", "22", "18", "1"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues);
+    }
+
+    @Test
+    public void add5gListItem_noLte_no5g() {
+        mPreference.setEntries(R.array.enabled_networks_except_lte_choices);
+        mPreference.setEntryValues(R.array.enabled_networks_except_lte_values);
+        CharSequence[] testEntries = {mContext.getString(R.string.network_3G)
+                , mContext.getString(R.string.network_2G)};
+        CharSequence[] testEntryValues = {"0", "1"};
+
+        mController.add5gListItem(mPreference);
+
+        assertThat(mPreference.getEntries()).isEqualTo(testEntries);
+        assertThat(mPreference.getEntryValues()).isEqualTo(testEntryValues);
+    }
+
     @Test
     public void onPreferenceChange_updateSuccess() {
         doReturn(true).when(mTelephonyManager).setPreferredNetworkType(SUB_ID,