From af007418cf449e72bb6e2bd4689d249c8af087d6 Mon Sep 17 00:00:00 2001 From: Lara Scheidegger Date: Thu, 11 Sep 2014 13:11:28 +0000 Subject: [PATCH] Changing the offline geocoder to not return any country at all if the number could belong to multiple countries. --- .../geocoding/PhoneNumberOfflineGeocoder.java | 23 +++++++++++++++--- .../PhoneNumberOfflineGeocoderTest.java | 9 +++++++ .../PhoneNumberMetadataProtoForTesting_CC | Bin 162 -> 225 bytes .../PhoneNumberMetadataProtoForTesting_CX | Bin 162 -> 225 bytes resources/PhoneNumberMetadataForTesting.xml | 16 ++++++++++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java b/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java index c3247c913..32bc68185 100644 --- a/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java +++ b/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java @@ -22,6 +22,7 @@ import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader; +import java.util.List; import java.util.Locale; /** @@ -60,11 +61,27 @@ public class PhoneNumberOfflineGeocoder { /** * Returns the customary display name in the given language for the given territory the phone - * number is from. + * number is from. If it could be from many territories, nothing is returned. */ private String getCountryNameForNumber(PhoneNumber number, Locale language) { - String regionCode = phoneUtil.getRegionCodeForNumber(number); - return getRegionDisplayName(regionCode, language); + List regionCodes = + phoneUtil.getRegionCodesForCountryCode(number.getCountryCode()); + if (regionCodes.size() == 1) { + return getRegionDisplayName(regionCodes.get(0), language); + } else { + String regionWhereNumberIsValid = "ZZ"; + for (String regionCode : regionCodes) { + if (phoneUtil.isValidNumberForRegion(number, regionCode)) { + if (!regionWhereNumberIsValid.equals("ZZ")) { + // If we can't assign the phone number as definitely belonging to only one territory, + // then we return nothing. + return ""; + } + regionWhereNumberIsValid = regionCode; + } + } + return getRegionDisplayName(regionWhereNumberIsValid, language); + } } /** diff --git a/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java b/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java index 5e7ed0c3f..ed9bd3b03 100644 --- a/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java +++ b/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java @@ -51,6 +51,8 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase { new PhoneNumber().setCountryCode(1).setNationalNumber(6174240000L); private static final PhoneNumber US_INVALID_NUMBER = new PhoneNumber().setCountryCode(1).setNationalNumber(123456789L); + private static final PhoneNumber NANPA_TOLL_FREE = + new PhoneNumber().setCountryCode(1).setNationalNumber(8002431234L); private static final PhoneNumber BS_NUMBER1 = new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L); private static final PhoneNumber AU_NUMBER = @@ -84,6 +86,13 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase { geocoder.getDescriptionForNumber(US_NUMBER4, new Locale("en", "US"))); } + public void testGetDescriptionForNumberBelongingToMultipleCountriesIsEmpty() { + // Test that nothing is returned when the number passed in is valid but not + // covered by the geocoding data file and belongs to multiple countries + assertEquals("", + geocoder.getDescriptionForNumber(NANPA_TOLL_FREE, new Locale("en", "US"))); + } + public void testGetDescriptionForNumber_en_US() { assertEquals("CA", geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US"))); diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC index 97fa27e9418c466d948464392da6e6ca5d4784dd..f715e55a8f738caa89b6e1bc01e7e5c4b7f7db36 100644 GIT binary patch literal 225 zcmZ4UmVveWHUk3#BO@aNr)f+|wXu$&Ni8D-2ast5VghBzkYMt2WMlvmAl+19qZkBs VFTz{~CTC}$kS!2{03(EgkO1h69mN0u delta 32 hcmaFJxQJ17&07Z6@;M9)42+B*0)i(dcut(_0swwr2K)d3 diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX index ad6bff5c31c895feb2044834184713478e410522..b6984d8b6918acf0f79b8ab256da7040feafea81 100644 GIT binary patch literal 225 zcmZ4UmVveWHUk3#BO@aNr)f+|wXu$&Ni8D-2ast5VghBzkYMt2WMlvmAl+19qZkBs VFTz{~Cg%vCkS!2{03(EgkO1nL9ohf@ delta 32 hcmaFJxQJ17&07Z6@;M9)42+B*0)i(dcut(_0swwr2K)d3 diff --git a/resources/PhoneNumberMetadataForTesting.xml b/resources/PhoneNumberMetadataForTesting.xml index 280373d81..ae670caa5 100644 --- a/resources/PhoneNumberMetadataForTesting.xml +++ b/resources/PhoneNumberMetadataForTesting.xml @@ -223,8 +223,14 @@ + recognised by the library. However, we don't want all numbers to be considered valid for + it, so we set at least a general description that allows our test geocoding numbers to be + valid only for Australia. --> + + 5\d{3,14} + \d{2,14} + @@ -249,8 +255,14 @@ + recognised by the library. However, we don't want all numbers to be considered valid for + it, so we set at least a general description that allows our test geocoding numbers to be + valid only for Australia. --> + + 5\d{3,14} + \d{2,14} +