If the user add APN with empty APN type, map it for default type

Original behavior will map all APN type if enter empty APN type. But it
sometimes cause problem that MMS or IMS not working when select to this
custom APN. So if the user doesn't enter any APN type, just map to
valure of carrier config 'apn_settings_default_apn_types_string_array'
to avoid all the functions broken.

Bug: 129704611
Test: 1.Enter empty APN type and check if only default is added into
APN. 2.atest ApnEditorTest pass.
Change-Id: Icec274e5b402af06822479bcc60294b347f5254f
diff --git a/src/com/android/settings/network/ApnEditor.java b/src/com/android/settings/network/ApnEditor.java
index 542a869..1451a40 100644
--- a/src/com/android/settings/network/ApnEditor.java
+++ b/src/com/android/settings/network/ApnEditor.java
@@ -135,7 +135,10 @@
     private int mBearerInitialVal = 0;
     private String mMvnoTypeStr;
     private String mMvnoMatchDataStr;
-    private String[] mReadOnlyApnTypes;
+    @VisibleForTesting
+    String[] mReadOnlyApnTypes;
+    @VisibleForTesting
+    String[] mDefaultApnTypes;
     private String[] mReadOnlyApnFields;
     private boolean mReadOnlyApn;
     private Uri mCarrierUri;
@@ -189,7 +192,8 @@
     private static final int MMSPROXY_INDEX = 12;
     private static final int MMSPORT_INDEX = 13;
     private static final int AUTH_TYPE_INDEX = 14;
-    private static final int TYPE_INDEX = 15;
+    @VisibleForTesting
+    static final int TYPE_INDEX = 15;
     private static final int PROTOCOL_INDEX = 16;
     @VisibleForTesting
     static final int CARRIER_ENABLED_INDEX = 17;
@@ -250,12 +254,17 @@
                 mReadOnlyApnTypes = b.getStringArray(
                         CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY);
                 if (!ArrayUtils.isEmpty(mReadOnlyApnTypes)) {
-                    for (String apnType : mReadOnlyApnTypes) {
-                        Log.d(TAG, "onCreate: read only APN type: " + apnType);
-                    }
+                    Log.d(TAG,
+                            "onCreate: read only APN type: " + Arrays.toString(mReadOnlyApnTypes));
                 }
                 mReadOnlyApnFields = b.getStringArray(
                         CarrierConfigManager.KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY);
+
+                mDefaultApnTypes = b.getStringArray(
+                        CarrierConfigManager.KEY_APN_SETTINGS_DEFAULT_APN_TYPES_STRING_ARRAY);
+                if (!ArrayUtils.isEmpty(mDefaultApnTypes)) {
+                    Log.d(TAG, "onCreate: default apn types: " + Arrays.toString(mDefaultApnTypes));
+                }
             }
         }
 
@@ -1150,17 +1159,24 @@
         return sNotSet.equals(value) ? null : value;
     }
 
-    private String getUserEnteredApnType() {
+    @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)
                 || PhoneConstants.APN_TYPE_ALL.equals(userEnteredApnType))
                 && !ArrayUtils.isEmpty(mReadOnlyApnTypes)) {
+            String[] apnTypeList = PhoneConstants.APN_TYPES;
+            if (TextUtils.isEmpty(userEnteredApnType) && !ArrayUtils.isEmpty(mDefaultApnTypes)) {
+                apnTypeList = mDefaultApnTypes;
+            }
+
             StringBuilder editableApnTypes = new StringBuilder();
             List<String> readOnlyApnTypes = Arrays.asList(mReadOnlyApnTypes);
             boolean first = true;
-            for (String apnType : PhoneConstants.APN_TYPES) {
+            for (String apnType : apnTypeList) {
                 // add APN type if it is not read-only and is not wild-cardable
                 if (!readOnlyApnTypes.contains(apnType)
                         && !apnType.equals(PhoneConstants.APN_TYPE_IA)
diff --git a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
index 8624eb9..dc14418 100644
--- a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
+++ b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
@@ -463,6 +463,43 @@
         verify(mApnEditorUT).finish();
     }
 
+    @Test
+    public void getUserEnteredApnType_emptyApnType_shouldReturnDefault() {
+        // case 1
+        // GIVEN read only APN types with DUN
+        String[] readOnlyApnTypes = {"dun"};
+        mApnEditorUT.mReadOnlyApnTypes = readOnlyApnTypes;
+        // GIVEN read specificApnTypeForEmptyInput with DEFAULT,DUN
+        String[] defaultApnTypes = {"default", "dun"};
+        mApnEditorUT.mDefaultApnTypes = defaultApnTypes;
+
+        // Input empty in TYPE
+        final FakeApnData apnData = new FakeApnData(APN_DATA);
+        apnData.mData[ApnEditor.TYPE_INDEX] = "";
+        mApnEditorUT.mApnData = apnData;
+        mApnEditorUT.fillUI(true /* firstTime */);
+
+        // THEN APN type should be default
+        assertThat(mApnEditorUT.getUserEnteredApnType()).isEqualTo("default");
+
+        // case 2
+        // GIVEN read only APN types with DUN
+        String[] readOnlyApnTypesCase2 = {"dun"};
+        mApnEditorUT.mReadOnlyApnTypes = readOnlyApnTypesCase2;
+        // GIVEN read specificApnTypeForEmptyInput with DEFAULT
+        String[] defaultApnTypesCase2 = {"default"};
+        mApnEditorUT.mDefaultApnTypes = defaultApnTypesCase2;
+
+        // Input empty in TYPE
+        final FakeApnData apnDataCase2 = new FakeApnData(APN_DATA);
+        apnDataCase2.mData[ApnEditor.TYPE_INDEX] = "";
+        mApnEditorUT.mApnData = apnDataCase2;
+        mApnEditorUT.fillUI(true /* firstTime */);
+
+        // THEN APN type should be default
+        assertThat(mApnEditorUT.getUserEnteredApnType()).isEqualTo("default");
+    }
+
     private void initCursor() {
         doReturn(2).when(mCursor).getColumnCount();
         doReturn(2).when(mCursor).getInt(CURSOR_INTEGER_INDEX);