From fa3c931ab63baf4c166d8d91b89bf6a3bf0bbde0 Mon Sep 17 00:00:00 2001 From: tom hsu Date: Sat, 18 Apr 2020 00:35:29 +0800 Subject: [PATCH] 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 --- .../android/settings/network/ApnEditor.java | 62 ++++++++++++------- .../settings/network/ApnEditorTest.java | 38 +++++------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/com/android/settings/network/ApnEditor.java b/src/com/android/settings/network/ApnEditor.java index 5da6e2c7401..f9eccfaeb55 100644 --- a/src/com/android/settings/network/ApnEditor.java +++ b/src/com/android/settings/network/ApnEditor.java @@ -68,6 +68,7 @@ public class ApnEditor extends SettingsPreferenceFragment 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 class ApnEditor extends SettingsPreferenceFragment public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); fillUI(savedInstanceState == null); + setCarrierCustomizedConfigToUi(); } @VisibleForTesting @@ -645,7 +647,9 @@ public class ApnEditor extends SettingsPreferenceFragment * 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 @@ public class ApnEditor extends SettingsPreferenceFragment } 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 @@ public class ApnEditor extends SettingsPreferenceFragment } else { preference.setSummary(checkNull(newValue != null ? String.valueOf(newValue) : null)); } - return true; } @@ -1002,13 +1012,13 @@ public class ApnEditor extends SettingsPreferenceFragment 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); @@ -1204,17 +1214,6 @@ public class ApnEditor extends SettingsPreferenceFragment return sNotSet.equals(value) ? null : value; } - @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" @@ -1222,16 +1221,11 @@ public class ApnEditor extends SettingsPreferenceFragment 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 @@ public class ApnEditor extends SettingsPreferenceFragment } } + 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 20334e045b6..7ec11742604 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 @@ public class ApnEditorTest { "" /* 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 @@ public class ApnEditorTest { } @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 @@ public class ApnEditorTest { // 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 @@ public class ApnEditorTest { // 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");