diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 62afa5486..8ad8b808e 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -81,6 +81,11 @@ public class PhoneNumberUtil { // a mobile phone in Colombia. private static final String COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = "3"; + // Map of country calling codes that use a mobile token before the area code. One example of when + // this is relevant is when determining the length of the national destination code, which should + // be the length of the area code plus the length of the mobile token. + private static final Map MOBILE_TOKEN_MAPPINGS; + // The PLUS_SIGN signifies the international prefix. static final char PLUS_SIGN = '+'; @@ -107,6 +112,11 @@ public class PhoneNumberUtil { private static final Map ALL_PLUS_NUMBER_GROUPING_SYMBOLS; static { + HashMap mobileTokenMap = new HashMap(); + mobileTokenMap.put(52, "1"); + mobileTokenMap.put(54, "9"); + MOBILE_TOKEN_MAPPINGS = Collections.unmodifiableMap(mobileTokenMap); + // Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and // ALL_PLUS_NUMBER_GROUPING_SYMBOLS. HashMap asciiDigitMappings = new HashMap(); @@ -878,19 +888,35 @@ public class PhoneNumberUtil { return 0; } - if (getRegionCodeForCountryCode(number.getCountryCode()).equals("AR") && - getNumberType(number) == PhoneNumberType.MOBILE) { - // Argentinian mobile numbers, when formatted in the international format, are in the form of - // +54 9 NDC XXXX.... As a result, we take the length of the third group (NDC) and add 1 for - // the digit 9, which also forms part of the national significant number. - // - // TODO: Investigate the possibility of better modeling the metadata to make it - // easier to obtain the NDC. - return numberGroups[3].length() + 1; + if (getNumberType(number) == PhoneNumberType.MOBILE) { + // For example Argentinian mobile numbers, when formatted in the international format, are in + // the form of +54 9 NDC XXXX.... As a result, we take the length of the third group (NDC) and + // add the length of the second group (which is the mobile token), which also forms part of + // the national significant number. This assumes that the mobile token is always formatted + // separately from the rest of the phone number. + String mobileToken = getCountryMobileToken(number.getCountryCode()); + if (!mobileToken.equals("")) { + return numberGroups[2].length() + numberGroups[3].length(); + } } return numberGroups[2].length(); } + /** + * Returns the mobile token for the provided country calling code if it has one, otherwise + * returns an empty string. A mobile token is a number inserted before the area code when dialing + * a mobile number from that country from abroad. + * + * @param countryCallingCode the country calling code for which we want the mobile token + * @return the mobile token, as a string, for the given country calling code + */ + public static String getCountryMobileToken(int countryCallingCode) { + if (MOBILE_TOKEN_MAPPINGS.containsKey(countryCallingCode)) { + return MOBILE_TOKEN_MAPPINGS.get(countryCallingCode); + } + return ""; + } + /** * Normalizes a string of characters representing a phone number by replacing all characters found * in the accompanying map with the values therein, and stripping all other characters if diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 701558f3f..3207709c9 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -292,6 +292,15 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(INTERNATIONAL_TOLL_FREE)); } + public void testGetCountryMobileToken() { + assertEquals("1", PhoneNumberUtil.getCountryMobileToken(phoneUtil.getCountryCodeForRegion( + RegionCode.MX))); + + // Country calling code for Sweden, which has no mobile token. + assertEquals("", PhoneNumberUtil.getCountryMobileToken(phoneUtil.getCountryCodeForRegion( + RegionCode.SE))); + } + public void testGetNationalSignificantNumber() { assertEquals("6502530000", phoneUtil.getNationalSignificantNumber(US_NUMBER)); diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java index 1dfa5c433..66e1a63e4 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java @@ -48,6 +48,7 @@ final class RegionCode { static final String PG = "PG"; static final String PL = "PL"; static final String RE = "RE"; + static final String SE = "SE"; static final String SG = "SG"; static final String US = "US"; static final String YT = "YT"; diff --git a/java/release_notes.txt b/java/release_notes.txt index c2035168f..6fcdb38fd 100644 --- a/java/release_notes.txt +++ b/java/release_notes.txt @@ -1,3 +1,7 @@ +Sep 19, 2013: libphonenumber-5.8.2 +* Code changes: + - New method in the PhoneNumberUtil API - getCountryMobileToken. + Sep 19, 2013: libphonenumber-5.8.1 * Metadata changes: - New alternate formatting data for country calling code(s): 675