diff options
author | Neil Fuller <nfuller@google.com> | 2019-11-15 11:26:06 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-11-15 11:26:06 +0000 |
commit | 88c9da6f7e554a10bb3183175515f2c22ca10b10 (patch) | |
tree | 670f4aa175a7fdc59e4d308fc32535050ebc937f | |
parent | 33af5cbb9be00d1ae2d6fffeed804fd20103357e (diff) | |
parent | 3e4a5b594d66a1717ff9dc67c11334f70b5f6ae2 (diff) |
Merge "Switch MCC / MNC values to string"
7 files changed, 57 insertions, 52 deletions
diff --git a/luni/src/main/java/libcore/timezone/TelephonyLookup.java b/luni/src/main/java/libcore/timezone/TelephonyLookup.java index 97775825eb..10d9a35b98 100644 --- a/luni/src/main/java/libcore/timezone/TelephonyLookup.java +++ b/luni/src/main/java/libcore/timezone/TelephonyLookup.java @@ -21,7 +21,6 @@ import static libcore.timezone.XmlUtils.consumeUntilEndTag; import static libcore.timezone.XmlUtils.findNextStartTagOrEndTagNoRecurse; import static libcore.timezone.XmlUtils.findNextStartTagOrThrowNoRecurse; import static libcore.timezone.XmlUtils.normalizeCountryIso; -import static libcore.timezone.XmlUtils.parseIntegerAttribute; import libcore.timezone.TelephonyNetwork.MccMnc; import libcore.timezone.XmlUtils.ReaderSupplier; @@ -213,13 +212,15 @@ public final class TelephonyLookup { // Skip over any unexpected elements and process <network> elements. while (findNextStartTagOrEndTagNoRecurse(parser, NETWORK_ELEMENT)) { - Integer mcc = parseIntegerAttribute(parser, MOBILE_COUNTRY_CODE_ATTRIBUTE, null); + String mcc = parser.getAttributeValue( + null /* namespace */, MOBILE_COUNTRY_CODE_ATTRIBUTE); if (mcc == null) { throw new XmlPullParserException( "Unable to find mcc: " + parser.getPositionDescription()); } - Integer mnc = parseIntegerAttribute(parser, MOBILE_NETWORK_CODE_ATTRIBUTE, null); + String mnc = parser.getAttributeValue( + null /* namespace */, MOBILE_NETWORK_CODE_ATTRIBUTE); if (mnc == null) { throw new XmlPullParserException( "Unable to find mnc: " + parser.getPositionDescription()); @@ -253,7 +254,7 @@ public final class TelephonyLookup { /** * Process network data. Problems with the data are reported as an exception. */ - void processNetwork(int mcc, int mnc, String countryIso, String debugInfo) + void processNetwork(String mcc, String mnc, String countryIso, String debugInfo) throws XmlPullParserException; } @@ -267,12 +268,23 @@ public final class TelephonyLookup { private final Set<MccMnc> knownMccMncs = new HashSet<>(); @Override - public void processNetwork(int mcc, int mnc, String countryIso, String debugInfo) + public void processNetwork(String mcc, String mnc, String countryIso, String debugInfo) throws XmlPullParserException { + if (mcc == null || mcc.length() != 3 || !isAsciiNumeric(mcc)) { + throw new XmlPullParserException( + "MCC is not valid: mcc=" + mcc + " at " + debugInfo); + } + + if (mnc == null || !(mnc.length() == 2 || mnc.length() == 3) || !isAsciiNumeric(mnc)) { + throw new XmlPullParserException( + "MNC is not valid: mnc=" + mnc + " at " + debugInfo); + } + if (!normalizeCountryIso(countryIso).equals(countryIso)) { throw new XmlPullParserException("Country code: " + countryIso + " is not normalized at " + debugInfo); } + MccMnc mccMnc = new MccMnc(mcc, mnc); if (knownMccMncs.contains(mccMnc)) { throw new XmlPullParserException("Second entry for MCC + MNC: " + mccMnc @@ -280,6 +292,16 @@ public final class TelephonyLookup { } knownMccMncs.add(mccMnc); } + + private static boolean isAsciiNumeric(String string) { + for (int i = 0; i < string.length(); i++) { + char character = string.charAt(i); + if (character < '0' || character > '9') { + return false; + } + } + return true; + } } /** @@ -290,7 +312,7 @@ public final class TelephonyLookup { private List<TelephonyNetwork> networksList = new ArrayList<>(10 /* default */); @Override - public void processNetwork(int mcc, int mnc, String countryIso, String debugInfo) + public void processNetwork(String mcc, String mnc, String countryIso, String debugInfo) throws XmlPullParserException { TelephonyNetwork network = TelephonyNetwork.create(mcc, mnc, countryIso); networksList.add(network); diff --git a/luni/src/main/java/libcore/timezone/TelephonyNetwork.java b/luni/src/main/java/libcore/timezone/TelephonyNetwork.java index f2b4b7491a..e5e5efb03f 100644 --- a/luni/src/main/java/libcore/timezone/TelephonyNetwork.java +++ b/luni/src/main/java/libcore/timezone/TelephonyNetwork.java @@ -35,10 +35,10 @@ public class TelephonyNetwork { * @hide */ public static final class MccMnc { - final int mcc; - final int mnc; + final String mcc; + final String mnc; - public MccMnc(int mcc, int mnc) { + public MccMnc(String mcc, String mnc) { this.mcc = mcc; this.mnc = mnc; } @@ -52,8 +52,8 @@ public class TelephonyNetwork { return false; } MccMnc mccMnc = (MccMnc) o; - return mcc == mccMnc.mcc - && mnc == mccMnc.mnc; + return Objects.equals(mcc, mccMnc.mcc) + && Objects.equals(mnc, mccMnc.mnc); } @Override @@ -73,7 +73,7 @@ public class TelephonyNetwork { private final MccMnc mccMnc; private final String countryIsoCode; - public static TelephonyNetwork create(int mcc, int mnc, String countryIsoCode) { + public static TelephonyNetwork create(String mcc, String mnc, String countryIsoCode) { String normalizedCountryIso = normalizeCountryIso(countryIsoCode); return new TelephonyNetwork(new MccMnc(mcc, mnc), normalizedCountryIso); } @@ -88,12 +88,12 @@ public class TelephonyNetwork { } @libcore.api.CorePlatformApi - public int getMcc() { + public String getMcc() { return mccMnc.mcc; } @libcore.api.CorePlatformApi - public int getMnc() { + public String getMnc() { return mccMnc.mnc; } diff --git a/luni/src/main/java/libcore/timezone/TelephonyNetworkFinder.java b/luni/src/main/java/libcore/timezone/TelephonyNetworkFinder.java index 0a04ae411e..3d09f7a995 100644 --- a/luni/src/main/java/libcore/timezone/TelephonyNetworkFinder.java +++ b/luni/src/main/java/libcore/timezone/TelephonyNetworkFinder.java @@ -71,7 +71,7 @@ public class TelephonyNetworkFinder { } @libcore.api.CorePlatformApi - public TelephonyNetwork findNetworkByMccMnc(int mcc, int mnc) { + public TelephonyNetwork findNetworkByMccMnc(String mcc, String mnc) { return networksMap.get(new MccMnc(mcc, mnc)); } diff --git a/luni/src/main/java/libcore/timezone/XmlUtils.java b/luni/src/main/java/libcore/timezone/XmlUtils.java index 91e12cdf85..a9616f96e9 100644 --- a/luni/src/main/java/libcore/timezone/XmlUtils.java +++ b/luni/src/main/java/libcore/timezone/XmlUtils.java @@ -38,25 +38,6 @@ class XmlUtils { private XmlUtils() {} /** - * Parses an attribute value, which must be either {@code null} or a valid signed integer value. - * If the attribute value is {@code null} then {@code defaultValue} is returned. If the - * attribute is present but not a valid integer value then an XmlPullParserException is thrown. - */ - static Integer parseIntegerAttribute(XmlPullParser parser, String attributeName, - Integer defaultValue) throws XmlPullParserException { - String attributeValueString = parser.getAttributeValue(null /* namespace */, attributeName); - if (attributeValueString == null) { - return defaultValue; - } - try { - return Integer.parseInt(attributeValueString); - } catch (NumberFormatException e) { - throw new XmlPullParserException("Attribute \"" + attributeName - + "\" is not an int value: " + parser.getPositionDescription()); - } - } - - /** * Parses an attribute value, which must be either {@code null} or a valid signed long value. * If the attribute value is {@code null} then {@code defaultValue} is returned. If the * attribute is present but not a valid long value then an XmlPullParserException is thrown. diff --git a/luni/src/test/java/libcore/libcore/timezone/TelephonyLookupTest.java b/luni/src/test/java/libcore/libcore/timezone/TelephonyLookupTest.java index 115826f8c9..8b4d8677ac 100644 --- a/luni/src/test/java/libcore/libcore/timezone/TelephonyLookupTest.java +++ b/luni/src/test/java/libcore/libcore/timezone/TelephonyLookupTest.java @@ -77,7 +77,7 @@ public class TelephonyLookupTest { + " </networks>\n" + "</telephony_lookup>\n"; TelephonyNetwork expectedTelephonyNetwork1 = - TelephonyNetwork.create(123, 456, "gb"); + TelephonyNetwork.create("123", "456", "gb"); String validXml2 = "<telephony_lookup>\n" + " <networks>\n" @@ -85,7 +85,7 @@ public class TelephonyLookupTest { + " </networks>\n" + "</telephony_lookup>\n"; TelephonyNetwork expectedTelephonyNetwork2 = - TelephonyNetwork.create(234, 567, "fr"); + TelephonyNetwork.create("234", "567", "fr"); String invalidXml = "<foo></foo>\n"; checkValidateThrowsParserException(invalidXml); @@ -163,7 +163,7 @@ public class TelephonyLookupTest { @Test public void xmlParsing_unexpectedComments() throws Exception { TelephonyNetwork expectedTelephonyNetwork = - TelephonyNetwork.create(123, 456, "gb"); + TelephonyNetwork.create("123", "456", "gb"); TelephonyLookup telephonyLookup = validate("<telephony_lookup>\n" + " <networks>\n" @@ -177,7 +177,7 @@ public class TelephonyLookupTest { @Test public void xmlParsing_unexpectedElementsIgnored() throws Exception { TelephonyNetwork expectedTelephonyNetwork = - TelephonyNetwork.create(123, 456, "gb"); + TelephonyNetwork.create("123", "456", "gb"); List<TelephonyNetwork> expectedNetworks = list(expectedTelephonyNetwork); String unexpectedElement = "<unexpected-element>\n<a /></unexpected-element>\n"; @@ -205,7 +205,7 @@ public class TelephonyLookupTest { assertEquals(expectedNetworks, telephonyLookup.getTelephonyNetworkFinder().getAll()); expectedNetworks = list(expectedTelephonyNetwork, - TelephonyNetwork.create(234, 567, "fr")); + TelephonyNetwork.create("234", "567", "fr")); telephonyLookup = validate("<telephony_lookup>\n" + " <networks>\n" + " <network mcc=\"123\" mnc=\"456\" country=\"gb\"/>\n" @@ -219,7 +219,7 @@ public class TelephonyLookupTest { @Test public void xmlParsing_unexpectedTextIgnored() throws Exception { TelephonyNetwork expectedTelephonyNetwork = - TelephonyNetwork.create(123, 456, "gb"); + TelephonyNetwork.create("123", "456", "gb"); List<TelephonyNetwork> expectedNetworks = list(expectedTelephonyNetwork); String unexpectedText = "unexpected-text"; @@ -292,12 +292,12 @@ public class TelephonyLookupTest { + "</telephony_lookup>\n"); TelephonyNetworkFinder telephonyNetworkFinder = telephonyLookup.getTelephonyNetworkFinder(); - TelephonyNetwork expectedNetwork1 = TelephonyNetwork.create(123, 456, "gb"); - TelephonyNetwork expectedNetwork2 = TelephonyNetwork.create(234, 567, "fr"); + TelephonyNetwork expectedNetwork1 = TelephonyNetwork.create("123", "456", "gb"); + TelephonyNetwork expectedNetwork2 = TelephonyNetwork.create("234", "567", "fr"); assertEquals(list(expectedNetwork1, expectedNetwork2), telephonyNetworkFinder.getAll()); - assertEquals(expectedNetwork1, telephonyNetworkFinder.findNetworkByMccMnc(123, 456)); - assertEquals(expectedNetwork2, telephonyNetworkFinder.findNetworkByMccMnc(234, 567)); - assertNull(telephonyNetworkFinder.findNetworkByMccMnc(999, 999)); + assertEquals(expectedNetwork1, telephonyNetworkFinder.findNetworkByMccMnc("123", "456")); + assertEquals(expectedNetwork2, telephonyNetworkFinder.findNetworkByMccMnc("234", "567")); + assertNull(telephonyNetworkFinder.findNetworkByMccMnc("999", "999")); } @Test diff --git a/luni/src/test/java/libcore/libcore/timezone/TelephonyNetworkFinderTest.java b/luni/src/test/java/libcore/libcore/timezone/TelephonyNetworkFinderTest.java index 659ef0520b..44dfc8178c 100644 --- a/luni/src/test/java/libcore/libcore/timezone/TelephonyNetworkFinderTest.java +++ b/luni/src/test/java/libcore/libcore/timezone/TelephonyNetworkFinderTest.java @@ -31,12 +31,14 @@ public class TelephonyNetworkFinderTest { @Test public void testCreateAndLookups() { - TelephonyNetwork network = TelephonyNetwork.create(123, 456, "gb"); + TelephonyNetwork network = TelephonyNetwork.create("123", "456", "gb"); List<TelephonyNetwork> networkList = list(network); TelephonyNetworkFinder finder = TelephonyNetworkFinder.create(networkList); - assertEquals(network, finder.findNetworkByMccMnc(123, 456)); - assertNull(finder.findNetworkByMccMnc(456, 123)); - assertNull(finder.findNetworkByMccMnc(111, 222)); + assertEquals(network, finder.findNetworkByMccMnc("123", "456")); + assertNull(finder.findNetworkByMccMnc("XXX", "XXX")); + assertNull(finder.findNetworkByMccMnc("123", "XXX")); + assertNull(finder.findNetworkByMccMnc("456", "123")); + assertNull(finder.findNetworkByMccMnc("111", "222")); assertEquals(networkList, finder.getAll()); } diff --git a/mmodules/core_platform_api/api/platform/current-api.txt b/mmodules/core_platform_api/api/platform/current-api.txt index 83d05e8e2d..fe99431f54 100644 --- a/mmodules/core_platform_api/api/platform/current-api.txt +++ b/mmodules/core_platform_api/api/platform/current-api.txt @@ -1134,12 +1134,12 @@ package libcore.timezone { public class TelephonyNetwork { method public String getCountryIsoCode(); - method public int getMcc(); - method public int getMnc(); + method public String getMcc(); + method public String getMnc(); } public class TelephonyNetworkFinder { - method public libcore.timezone.TelephonyNetwork findNetworkByMccMnc(int, int); + method public libcore.timezone.TelephonyNetwork findNetworkByMccMnc(String, String); } public final class TimeZoneDataFiles { |