Make ApnEditor can show the customized default value on UI.

 - Edittext on edittextpreference
 - Summary on edittextpreference

Bug: 142440775
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ApnEditorTest -j
Test: Sanity test pass with customized carrier data
   - data in summary and text of edittext preference correctly correctly
   display on.
Change-Id: I3162de19659df79c5873c730d7d32e4ed998bcbe
diff --git a/src/com/android/settings/network/ApnEditor.java b/src/com/android/settings/network/ApnEditor.java
index 5da6e2c..f9eccfa 100644
--- a/src/com/android/settings/network/ApnEditor.java
+++ b/src/com/android/settings/network/ApnEditor.java
@@ -68,6 +68,7 @@
     private final static boolean VDBG = false;   // STOPSHIP if true
 
     private final static String KEY_AUTH_TYPE = "auth_type";
+    private static final String KEY_APN_TYPE = "apn_type";
     private final static String KEY_PROTOCOL = "apn_protocol";
     private final static String KEY_ROAMING_PROTOCOL = "apn_roaming_protocol";
     private final static String KEY_CARRIER_ENABLED = "carrier_enabled";
@@ -344,6 +345,7 @@
     public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
         super.onViewStateRestored(savedInstanceState);
         fillUI(savedInstanceState == null);
+        setCarrierCustomizedConfigToUi();
     }
 
     @VisibleForTesting
@@ -645,7 +647,9 @@
      * return null.
      */
     private String protocolDescription(String raw, ListPreference protocol) {
-        final int protocolIndex = protocol.findIndexOfValue(raw);
+        String uRaw = checkNull(raw).toUpperCase();
+        uRaw = uRaw.equals("IPV4") ? "IP" : uRaw;
+        final int protocolIndex = protocol.findIndexOfValue(uRaw);
         if (protocolIndex == -1) {
             return null;
         } else {
@@ -745,6 +749,13 @@
             } catch (NumberFormatException e) {
                 return false;
             }
+        } else if (KEY_APN_TYPE.equals(key)) {
+            String data = (TextUtils.isEmpty((String) newValue)
+                    && !ArrayUtils.isEmpty(mDefaultApnTypes))
+                    ? getEditableApnType(mDefaultApnTypes) : (String) newValue;
+            if (!TextUtils.isEmpty(data)) {
+                mApnType.setSummary(data);
+            }
         } else if (KEY_PROTOCOL.equals(key)) {
             final String protocol = protocolDescription((String) newValue, mProtocol);
             if (protocol == null) {
@@ -780,7 +791,6 @@
         } else {
             preference.setSummary(checkNull(newValue != null ? String.valueOf(newValue) : null));
         }
-
         return true;
     }
 
@@ -1002,13 +1012,13 @@
 
         callUpdate = setStringValueAndCheckIfDiff(values,
                 Telephony.Carriers.PROTOCOL,
-                getUserEnteredApnProtocol(mProtocol, mDefaultApnProtocol),
+                checkNotSet(mProtocol.getValue()),
                 callUpdate,
                 PROTOCOL_INDEX);
 
         callUpdate = setStringValueAndCheckIfDiff(values,
                 Telephony.Carriers.ROAMING_PROTOCOL,
-                getUserEnteredApnProtocol(mRoamingProtocol, mDefaultApnRoamingProtocol),
+                checkNotSet(mRoamingProtocol.getValue()),
                 callUpdate,
                 ROAMING_PROTOCOL_INDEX);
 
@@ -1205,33 +1215,17 @@
     }
 
     @VisibleForTesting
-    String getUserEnteredApnProtocol(ListPreference preference, String defaultApnProtocol) {
-        // if user has not specified a protocol or enter empty type, map it just for default
-        final String userEnteredApnProtocol = checkNotSet(
-                ((preference == null) ? null : preference.getValue()));
-        if (TextUtils.isEmpty(userEnteredApnProtocol)) {
-            return defaultApnProtocol;
-        }
-        return userEnteredApnProtocol.trim();
-    }
-
-    @VisibleForTesting
     String getUserEnteredApnType() {
         // if user has not specified a type, map it to "ALL APN TYPES THAT ARE NOT READ-ONLY"
         // but if user enter empty type, map it just for default
         String userEnteredApnType = mApnType.getText();
         if (userEnteredApnType != null) userEnteredApnType = userEnteredApnType.trim();
         if ((TextUtils.isEmpty(userEnteredApnType)
-                || APN_TYPE_ALL.equals(userEnteredApnType))
-                && !ArrayUtils.isEmpty(mReadOnlyApnTypes)) {
-            String[] apnTypeList = APN_TYPES;
-            if (TextUtils.isEmpty(userEnteredApnType) && !ArrayUtils.isEmpty(mDefaultApnTypes)) {
-                apnTypeList = mDefaultApnTypes;
-            }
-            userEnteredApnType = getEditableApnType(apnTypeList);
-            Log.d(TAG, "getUserEnteredApnType: changed apn type to editable apn types: "
-                    + userEnteredApnType);
+                || APN_TYPE_ALL.equals(userEnteredApnType))) {
+            userEnteredApnType = getEditableApnType(APN_TYPES);
         }
+        Log.d(TAG, "getUserEnteredApnType: changed apn type to editable apn types: "
+                + userEnteredApnType);
         return userEnteredApnType;
     }
 
@@ -1324,6 +1318,26 @@
         }
     }
 
+    private void setCarrierCustomizedConfigToUi() {
+        if (TextUtils.isEmpty(mApnType.getText()) && !ArrayUtils.isEmpty(mDefaultApnTypes)) {
+            String value = getEditableApnType(mDefaultApnTypes);
+            mApnType.setText(value);
+            mApnType.setSummary(value);
+        }
+
+        String protocol = protocolDescription(mDefaultApnProtocol, mProtocol);
+        if (TextUtils.isEmpty(mProtocol.getValue()) && !TextUtils.isEmpty(protocol)) {
+            mProtocol.setValue(mDefaultApnProtocol);
+            mProtocol.setSummary(protocol);
+        }
+
+        String roamingProtocol = protocolDescription(mDefaultApnRoamingProtocol, mRoamingProtocol);
+        if (TextUtils.isEmpty(mRoamingProtocol.getValue()) && !TextUtils.isEmpty(roamingProtocol)) {
+            mRoamingProtocol.setValue(mDefaultApnRoamingProtocol);
+            mRoamingProtocol.setSummary(roamingProtocol);
+        }
+    }
+
     public static class ErrorDialog extends InstrumentedDialogFragment {
 
         public static void showError(ApnEditor editor) {
diff --git a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
index 20334e0..7ec1174 100644
--- a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
+++ b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
@@ -81,11 +81,11 @@
             "" /* MMS port */,
             0 /* Authentication type */,
             "default,supl,ia" /* APN type */,
-            "IPv6" /* APN protocol */,
+            "IP" /* APN protocol */,
             1 /* APN enable/disable */,
             0 /* Bearer */,
             0 /* Bearer BITMASK*/,
-            "IPv4" /* APN roaming protocol */,
+            "IPV6" /* APN roaming protocol */,
             "None" /* MVNO type */,
             "", /* MVNO value */
     };
@@ -464,33 +464,25 @@
     }
 
     @Test
-    public void getUserEnteredApnProtocol_emptyApnProtocol_shouldReturnDefaultIPv4v6() {
-        // GIVEN read default APN protocol with IPV4V6
-        mApnEditorUT.mDefaultApnProtocol = "IPV4V6";
+    public void testOnViewStateRestored_customizedValueWithoutDefault_shouldShowCustomized() {
+        mApnEditorUT.mDefaultApnProtocol = "IP";
+        mApnEditorUT.mApnData.mData[ApnEditor.PROTOCOL_INDEX] = null;
+        mApnEditorUT.mProtocol.setEntryValues(new CharSequence[]{"IP", "IPV6", "IPV4V6"});
 
-        // Input empty in TYPE
-        mApnEditorUT.mApnData.mData[ApnEditor.PROTOCOL_INDEX] = "";
-        mApnEditorUT.fillUI(true /* firstTime */);
+        mApnEditorUT.onViewStateRestored(null);
 
-        // THEN APN type should be IPV4V6
-        assertThat(mApnEditorUT.getUserEnteredApnProtocol(
-                mApnEditorUT.mProtocol, mApnEditorUT.mDefaultApnProtocol))
-                .isEqualTo("IPV4V6");
+        assertThat(mApnEditorUT.mProtocol.getSummary()).isEqualTo("IPv4");
     }
 
     @Test
-    public void getUserEnteredApnProtocol_emptyApnProtocol_shouldReturnDefaultIP() {
-        // GIVEN read default APN protocol with IP
+    public void testOnViewStateRestored_customizedValueWithDefault_shouldShowDefault() {
         mApnEditorUT.mDefaultApnProtocol = "IP";
+        mApnEditorUT.mApnData.mData[ApnEditor.PROTOCOL_INDEX] = "IPV6";
+        mApnEditorUT.mProtocol.setEntryValues(new CharSequence[]{"IP", "IPV6", "IPV4V6"});
 
-        // Input empty in TYPE
-        mApnEditorUT.mApnData.mData[ApnEditor.PROTOCOL_INDEX] = "";
-        mApnEditorUT.fillUI(true /* firstTime */);
+        mApnEditorUT.onViewStateRestored(null);
 
-        // THEN APN type should be IPV4V6
-        assertThat(mApnEditorUT.getUserEnteredApnProtocol(
-                mApnEditorUT.mProtocol, mApnEditorUT.mDefaultApnProtocol))
-                .isEqualTo("IP");
+        assertThat(mApnEditorUT.mProtocol.getSummary()).isEqualTo("IPv6");
     }
 
     @Test
@@ -503,7 +495,7 @@
 
         // Input empty in TYPE
         mApnEditorUT.mApnData.mData[ApnEditor.TYPE_INDEX] = "";
-        mApnEditorUT.fillUI(true /* firstTime */);
+        mApnEditorUT.onViewStateRestored(null);
 
         // THEN APN type should be default
         assertThat(mApnEditorUT.getUserEnteredApnType()).isEqualTo("default");
@@ -516,7 +508,7 @@
 
         // Input empty in TYPE
         mApnEditorUT.mApnData.mData[ApnEditor.TYPE_INDEX] = "";
-        mApnEditorUT.fillUI(true /* firstTime */);
+        mApnEditorUT.onViewStateRestored(null);
 
         // THEN APN type should be default
         assertThat(mApnEditorUT.getUserEnteredApnType()).isEqualTo("default");