To fix bug, summary is wrong if nr_enabled_bool=false

The carrier config nr_enabled_bool is false, preferred network mode
should hide 5G UI. This bug is that the summary show 5G item when
nr_enabled_bool is false.
Solution: if nr_enabled_bool is false, it checks capability and then
removes 5G capability of network mode.

Bug: 170460943
Test: make RunSettingsRoboTests ROBOTEST_FILTER=\
EnabledNetworkModePreferenceControllerTest   (PASS)
Change-Id: I3f7d41c196569c0cb61e89cfc270b957e2f0add5
diff --git a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
index 6327375..fd585a1 100644
--- a/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceController.java
@@ -365,9 +365,15 @@
         }
 
         private int getPreferredNetworkMode() {
-            return Settings.Global.getInt(mContext.getContentResolver(),
+            int networkMode = Settings.Global.getInt(mContext.getContentResolver(),
                     Settings.Global.PREFERRED_NETWORK_MODE + mSubId,
                     TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE);
+            if (!showNrList()) {
+                Log.d(LOG_TAG, "Network mode :" + networkMode + " reduce NR");
+                networkMode = reduceNrToLteNetworkType(networkMode);
+            }
+            Log.d(LOG_TAG, "getPreferredNetworkMode: " + networkMode);
+            return networkMode;
         }
 
         private EnabledNetworks getEnabledNetworkType() {
@@ -635,6 +641,40 @@
             }
         }
 
+        /**
+         * Transform NR5G network mode to LTE network mode.
+         *
+         * @param networkType an 5G network mode.
+         * @return the corresponding network mode without 5G.
+         */
+        private static int reduceNrToLteNetworkType(int networkType) {
+            switch(networkType) {
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_ONLY;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_WCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_WCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_WCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA:
+                    return TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
+                case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
+                    return TelephonyManagerConstants
+                            .NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
+                default:
+                    return networkType; // do nothing
+            }
+        }
+
         private void setPreferenceValueAndSummary() {
             setPreferenceValueAndSummary(getPreferredNetworkMode());
         }
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 e38bd62..e519b9a 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/EnabledNetworkModePreferenceControllerTest.java
@@ -222,6 +222,28 @@
     }
 
     @Test
+    public void updateState_NrEnableBoolFalse_5gOptionHidden() {
+        mockEnabledNetworkMode(TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
+        mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
+        mPersistableBundle.putBoolean(CarrierConfigManager.KEY_NR_ENABLED_BOOL, false);
+
+        mController.init(mLifecycle, SUB_ID);
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
+                TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getValue()).isEqualTo(
+                String.valueOf(
+                        TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA));
+        assertThat(mPreference.getEntryValues())
+                .asList()
+                .doesNotContain(
+                        String.valueOf(TelephonyManager.NETWORK_MODE_NR_LTE_GSM_WCDMA));
+    }
+
+    @Test
     public void updateState_GlobalDisAllowed5g_GlobalWithoutNR() {
         mockAccessFamily(TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA);
         mockAllowedNetworkTypes(DISABLED_5G_NETWORK_TYPE);
@@ -393,6 +415,11 @@
         } else if (networkMode == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA) {
             mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
             mPersistableBundle.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true);
+        } else if (networkMode
+                == TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA) {
+            mockPhoneType(TelephonyManager.PHONE_TYPE_GSM);
+            mPersistableBundle.putBoolean(CarrierConfigManager.KEY_PREFER_2G_BOOL, true);
+            mPersistableBundle.putBoolean(CarrierConfigManager.KEY_LTE_ENABLED_BOOL, true);
         }
     }
 
@@ -407,6 +434,6 @@
     }
 
     private void mockPhoneType(int phoneType) {
-        doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
+        doReturn(phoneType).when(mTelephonyManager).getPhoneType();
     }
 }