OSDN Git Service

Preserve leading 0s in mcc mnc
authorJordan Liu <jminjie@google.com>
Fri, 13 Jul 2018 17:43:47 +0000 (10:43 -0700)
committerJordan Liu <jminjie@google.com>
Wed, 8 Aug 2018 18:35:15 +0000 (11:35 -0700)
Fixes: 79408450
Test: ApnEditorTest.java
Change-Id: Iad7ffe04f23b30857588e50d7f5f0dd307bd2c6e
Merged-In: Iad7ffe04f23b30857588e50d7f5f0dd307bd2c6e

src/com/android/settings/network/ApnEditor.java
tests/robotests/src/com/android/settings/network/ApnEditorTest.java

index cceb31d..5133d45 100644 (file)
@@ -313,13 +313,23 @@ public class ApnEditor extends SettingsPreferenceFragment
     static String formatInteger(String value) {
         try {
             final int intValue = Integer.parseInt(value);
-            return String.format("%d", intValue);
+            return String.format(getCorrectDigitsFormat(value), intValue);
         } catch (NumberFormatException e) {
             return value;
         }
     }
 
     /**
+     * Get the digits format so we preserve leading 0's.
+     * MCCs are 3 digits and MNCs are either 2 or 3.
+     */
+    static String getCorrectDigitsFormat(String value) {
+        if (value.length() == 2) return "%02d";
+        else return "%03d";
+    }
+
+
+    /**
      * Check if passed in array of APN types indicates all APN types
      * @param apnTypes array of APN types. "*" indicates all types.
      * @return true if all apn types are included in the array, false otherwise
index 35f68a0..2fa9de1 100644 (file)
@@ -440,6 +440,8 @@ public class ApnEditorTest {
     @Test
     public void formatInteger_shouldParseString() {
         assertThat(ApnEditor.formatInteger("42")).isEqualTo("42");
+        assertThat(ApnEditor.formatInteger("01")).isEqualTo("01");
+        assertThat(ApnEditor.formatInteger("001")).isEqualTo("001");
     }
 
     @Test
@@ -489,4 +491,4 @@ public class ApnEditorTest {
             mUri = uri;
         }
     }
-}
\ No newline at end of file
+}