Update libphonenumber to v4.3.

Change-Id: Ib6b4bcae322265200c80241672244fac1a2acd6e
diff --git a/README.android b/README.android
index 352969a..51c5fc1 100644
--- a/README.android
+++ b/README.android
@@ -1,5 +1,5 @@
 URL: http://code.google.com/p/libphonenumber/
-Version: 4.2 (r385)
+Version: 4.3 (r398)
 License: Apache 2
 Description: Google Phone Number Library.
 Local Modification:
diff --git a/java/release_notes.txt b/java/release_notes.txt
index 0fb5dc6..d73b7d6 100644
--- a/java/release_notes.txt
+++ b/java/release_notes.txt
@@ -1,3 +1,16 @@
+November 24th, 2011: libphonenumber-4.3
+* Code changes
+ - Fix the problems with AYTF crashing for longer numbers entered with +CountryCode, and incorrectly
+   removing national prefix for some numbers.
+ - Improve PhoneNumberMatcher to not match numbers ending with '%'.
+ - Fix formatNumberForMobileDialing to handle Israeli star numbers, Peruvian and Colombian numbers.
+ - Modify formatInOriginalFormat to use the raw input if we don't have a formatting pattern for a
+   number.
+ - Simple offline geocoding function which takes into account the user's region.
+
+* Metadata changes
+ - Updates for CR, GN, JP, KE, PL, SG, SR, SX, TH, TK
+
 November 10th, 2011: libphonenumber-4.2
 * Code changes
  - Providing an "exact match" isEmergencyNumber method
diff --git a/java/src/com/android/i18n/phonenumbers/AsYouTypeFormatter.java b/java/src/com/android/i18n/phonenumbers/AsYouTypeFormatter.java
index d658086..554fcd6 100644
--- a/java/src/com/android/i18n/phonenumbers/AsYouTypeFormatter.java
+++ b/java/src/com/android/i18n/phonenumbers/AsYouTypeFormatter.java
@@ -428,8 +428,7 @@
     // number (excluding national prefix) have been entered.
     if (nationalNumber.length() >= MIN_LEADING_DIGITS_LENGTH) {
       getAvailableFormats(nationalNumber.substring(0, MIN_LEADING_DIGITS_LENGTH));
-      maybeCreateNewTemplate();
-      return inputAccruedNationalNumber();
+      return maybeCreateNewTemplate() ? inputAccruedNationalNumber() : accruedInput.toString();
     } else {
       return prefixBeforeNationalNumber + nationalNumber.toString();
     }
diff --git a/java/src/com/android/i18n/phonenumbers/PhoneNumberMatcher.java b/java/src/com/android/i18n/phonenumbers/PhoneNumberMatcher.java
index 1cd106c..3ddc773 100644
--- a/java/src/com/android/i18n/phonenumbers/PhoneNumberMatcher.java
+++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberMatcher.java
@@ -294,8 +294,8 @@
         block.equals(UnicodeBlock.COMBINING_DIACRITICAL_MARKS);
   }
 
-  private static boolean isCurrencySymbol(char character) {
-    return Character.getType(character) == Character.CURRENCY_SYMBOL;
+  private static boolean isInvalidPunctuationSymbol(char character) {
+    return character == '%' || Character.getType(character) == Character.CURRENCY_SYMBOL;
   }
 
   /**
@@ -407,15 +407,15 @@
         // punctuation, check the previous character.
         if (offset > 0 && !LEAD_CLASS.matcher(candidate).lookingAt()) {
           char previousChar = text.charAt(offset - 1);
-          // We return null if it is a latin letter or a currency symbol.
-          if (isCurrencySymbol(previousChar) || isLatinLetter(previousChar)) {
+          // We return null if it is a latin letter or an invalid punctuation symbol.
+          if (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) {
             return null;
           }
         }
         int lastCharIndex = offset + candidate.length();
         if (lastCharIndex < text.length()) {
           char nextChar = text.charAt(lastCharIndex);
-          if (isCurrencySymbol(nextChar) || isLatinLetter(nextChar)) {
+          if (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) {
             return null;
           }
         }
diff --git a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
index 4a435b8..735f2af 100644
--- a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
@@ -97,6 +97,11 @@
 
   private static final String RFC3966_EXTN_PREFIX = ";ext=";
 
+  // A map that contains characters that are essential when dialling. That means any of the
+  // characters in this map must not be removed from a number when dialing, otherwise the call will
+  // not reach the intended destination.
+  private static final Map<Character, Character> DIALLABLE_CHAR_MAPPINGS;
+
   // Only upper-case variants of alpha characters are stored.
   private static final Map<Character, Character> ALPHA_MAPPINGS;
 
@@ -156,6 +161,12 @@
     combinedMap.putAll(asciiDigitMappings);
     ALPHA_PHONE_MAPPINGS = Collections.unmodifiableMap(combinedMap);
 
+    HashMap<Character, Character> diallableCharMap = new HashMap<Character, Character>();
+    diallableCharMap.putAll(asciiDigitMappings);
+    diallableCharMap.put('+', '+');
+    diallableCharMap.put('*', '*');
+    DIALLABLE_CHAR_MAPPINGS = Collections.unmodifiableMap(diallableCharMap);
+
     HashMap<Character, Character> allPlusNumberGroupings = new HashMap<Character, Character>();
     // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
     for (char c : ALPHA_MAPPINGS.keySet()) {
@@ -407,9 +418,8 @@
    */
   public enum Leniency {
     /**
-     * Phone numbers accepted are
-     * {@linkplain PhoneNumberUtil#isPossibleNumber(Phonenumber.PhoneNumber) possible}, but not
-     * necessarily {@linkplain PhoneNumberUtil#isValidNumber(Phonenumber.PhoneNumber) valid}.
+     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
+     * possible}, but not necessarily {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}.
      */
     POSSIBLE {
       @Override
@@ -418,9 +428,8 @@
       }
     },
     /**
-     * Phone numbers accepted are
-     * {@linkplain PhoneNumberUtil#isPossibleNumber(Phonenumber.PhoneNumber) possible} and
-     * {@linkplain PhoneNumberUtil#isValidNumber(Phonenumber.PhoneNumber) valid}. Numbers written
+     * Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
+     * possible} and {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}. Numbers written
      * in national format must have their national-prefix present if it is usually written for a
      * number of this type.
      */
@@ -1059,9 +1068,8 @@
   }
 
   /**
-   * Same as {@link #format(Phonenumber.PhoneNumber, PhoneNumberUtil.PhoneNumberFormat)}, but
-   * accepts a mutable StringBuilder as a parameter to decrease object creation when invoked many
-   * times.
+   * Same as {@link #format(PhoneNumber, PhoneNumberFormat)}, but accepts a mutable StringBuilder as
+   * a parameter to decrease object creation when invoked many times.
    */
   public void format(PhoneNumber number, PhoneNumberFormat numberFormat,
                      StringBuilder formattedNumber) {
@@ -1222,7 +1230,7 @@
    */
   public String formatNumberForMobileDialing(PhoneNumber number, String regionCallingFrom,
                                              boolean withFormatting) {
-    String regionCode = getRegionCodeForNumber(number);
+    String regionCode = getRegionCodeForCountryCode(number.getCountryCode());
     if (!isValidRegionCode(regionCode)) {
       return number.hasRawInput() ? number.getRawInput() : "";
     }
@@ -1231,11 +1239,19 @@
     // Clear the extension, as that part cannot normally be dialed together with the main number.
     PhoneNumber numberNoExt = new PhoneNumber().mergeFrom(number).clearExtension();
     PhoneNumberType numberType = getNumberType(numberNoExt);
-    if ((regionCode.equals("CO")) && (regionCallingFrom.equals("CO")) &&
-        (numberType == PhoneNumberType.FIXED_LINE)) {
-      formattedNumber =
-          formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
-    } else if ((regionCode.equals("BR")) && (regionCallingFrom.equals("BR")) &&
+    if (regionCode.equals("CO") && regionCallingFrom.equals("CO")) {
+      if (numberType == PhoneNumberType.FIXED_LINE) {
+        formattedNumber =
+            formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
+      } else {
+        // E164 doesn't work at all when dialing within Colombia.
+        formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
+      }
+    } else if (regionCode.equals("PE") && regionCallingFrom.equals("PE")) {
+      // In Peru, numbers cannot be dialled using E164 format from a mobile phone for Movistar.
+      // Instead they must be dialled in national format.
+      formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
+    } else if (regionCode.equals("BR") && regionCallingFrom.equals("BR") &&
         ((numberType == PhoneNumberType.FIXED_LINE) || (numberType == PhoneNumberType.MOBILE) ||
          (numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE))) {
       formattedNumber = numberNoExt.hasPreferredDomesticCarrierCode()
@@ -1251,7 +1267,9 @@
       formattedNumber = (regionCallingFrom.equals(regionCode))
           ? format(numberNoExt, PhoneNumberFormat.NATIONAL) : "";
     }
-    return withFormatting ? formattedNumber : normalizeDigitsOnly(formattedNumber);
+    return withFormatting ? formattedNumber
+                          : normalizeHelper(formattedNumber, DIALLABLE_CHAR_MAPPINGS,
+                                            true /* remove non matches */);
   }
 
   /**
@@ -2044,10 +2062,10 @@
   /**
    * Check whether a phone number is a possible number given a number in the form of a string, and
    * the region where the number could be dialed from. It provides a more lenient check than
-   * {@link #isValidNumber}. See {@link #isPossibleNumber(Phonenumber.PhoneNumber)} for details.
+   * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details.
    *
-   * <p>This method first parses the number, then invokes
-   * {@link #isPossibleNumber(Phonenumber.PhoneNumber)} with the resultant PhoneNumber object.
+   * <p>This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)}
+   * with the resultant PhoneNumber object.
    *
    * @param number  the number that needs to be checked, in the form of a string
    * @param regionDialingFrom  the region that we are expecting the number to be dialed from.
@@ -2699,15 +2717,14 @@
 
   /**
    * Takes two phone numbers as strings and compares them for equality. This is a convenience
-   * wrapper for {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No
-   * default region is known.
+   * wrapper for {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
    *
    * @param firstNumber  first number to compare. Can contain formatting, and can have country
    *     calling code specified with + at the start.
    * @param secondNumber  second number to compare. Can contain formatting, and can have country
    *     calling code specified with + at the start.
    * @return  NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
-   *     {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
+   *     {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
    */
   public MatchType isNumberMatch(String firstNumber, String secondNumber) {
     try {
@@ -2739,14 +2756,13 @@
 
   /**
    * Takes two phone numbers and compares them for equality. This is a convenience wrapper for
-   * {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No default region is
-   * known.
+   * {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
    *
    * @param firstNumber  first number to compare in proto buffer format.
    * @param secondNumber  second number to compare. Can contain formatting, and can have country
    *     calling code specified with + at the start.
    * @return  NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
-   *     {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
+   *     {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
    */
   public MatchType isNumberMatch(PhoneNumber firstNumber, String secondNumber) {
     // First see if the second number has an implicit country calling code, by attempting to parse
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
index cbc064b..a1d4d34 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN
index b0b7406..468f4d2 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
index 9db7e6e..1754045 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
index 68de57a..a87c7e8 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
index 816ed3f..47f4390 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL
index 69d172c..4d33462 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG
index e8dfd31..cb7d012 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR
index a7260aa..4633905 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX
index 50b2362..5500274 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH
index 8b56a1f..7800483 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK
index 849f1c7..48c9c31 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java b/java/src/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
index 1845f28..658a6e2 100644
--- a/java/src/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
+++ b/java/src/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
@@ -130,15 +130,24 @@
    */
   private String getCountryNameForNumber(PhoneNumber number, Locale language) {
     String regionCode = phoneUtil.getRegionCodeForNumber(number);
+    return getRegionDisplayName(regionCode, language);
+  }
+
+  /**
+   * Returns the customary display name in the given language for the given region.
+   */
+  private String getRegionDisplayName(String regionCode, Locale language) {
     return (regionCode == null || regionCode.equals("ZZ"))
         ? "" : new Locale("", regionCode).getDisplayCountry(language);
   }
 
   /**
-   * Returns a text description for the given language code for the given phone number. The
-   * description might consist of the name of the country where the phone number is from and/or the
-   * name of the geographical area the phone number is from. This method assumes the validity of the
-   * number passed in has already been checked.
+   * Returns a text description for the given phone number, in the language provided. The
+   * description might consist of the name of the country where the phone number is from, or the
+   * name of the geographical area the phone number is from if more detailed information is
+   * available.
+   *
+   * <p>This method assumes the validity of the number passed in has already been checked.
    *
    * @param number  a valid phone number for which we want to get a text description
    * @param languageCode  the language code for which the description should be written
@@ -156,10 +165,44 @@
   }
 
   /**
-   * Returns a text description for the given language code for the given phone number. The
-   * description might consist of the name of the country where the phone number is from and/or the
-   * name of the geographical area the phone number is from. This method explictly checkes the
-   * validity of the number passed in.
+   * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but also considers the
+   * region of the user. If the phone number is from the same region as the user, only a lower-level
+   * description will be returned, if one exists. Otherwise, the phone number's region will be
+   * returned, with optionally some more detailed information.
+   *
+   * <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
+   * CA" for a particular number, omitting the United States from the description. For a user from
+   * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
+   * States" or even just "United States".
+   *
+   * <p>This method assumes the validity of the number passed in has already been checked.
+   *
+   * @param number  the phone number for which we want to get a text description
+   * @param languageCode  the language code for which the description should be written
+   * @param userRegion  the region code for a given user. This region will be omitted from the
+   *     description if the phone number comes from this region. It is a two-letter uppercase ISO
+   *     country code as defined by ISO 3166-1.
+   * @return  a text description for the given language code for the given phone number, or empty
+   *     string if the number passed in is invalid
+   */
+  public String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode,
+                                             String userRegion) {
+    // If the user region matches the number's region, then we just show the lower-level
+    // description, if one exists - if no description exists, we will show the region(country) name
+    // for the number.
+    String regionCode = phoneUtil.getRegionCodeForNumber(number);
+    if (userRegion.equals(regionCode)) {
+      return getDescriptionForValidNumber(number, languageCode);
+    }
+    // Otherwise, we just show the region(country) name for now.
+    return getRegionDisplayName(regionCode, languageCode);
+    // TODO: Concatenate the lower-level and country-name information in an appropriate
+    // way for each language.
+  }
+
+  /**
+   * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but explicitly checks
+   * the validity of the number passed in.
    *
    * @param number  the phone number for which we want to get a text description
    * @param languageCode  the language code for which the description should be written
@@ -174,6 +217,26 @@
   }
 
   /**
+   * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale, String)} but
+   * explicitly checks the validity of the number passed in.
+   *
+   * @param number  the phone number for which we want to get a text description
+   * @param languageCode  the language code for which the description should be written
+   * @param userRegion  the region code for a given user. This region will be omitted from the
+   *     description if the phone number comes from this region. It is a two-letter uppercase ISO
+   *     country code as defined by ISO 3166-1.
+   * @return  a text description for the given language code for the given phone number, or empty
+   *     string if the number passed in is invalid
+   */
+  public String getDescriptionForNumber(PhoneNumber number, Locale languageCode,
+                                        String userRegion) {
+    if (!phoneUtil.isValidNumber(number)) {
+      return "";
+    }
+    return getDescriptionForValidNumber(number, languageCode, userRegion);
+  }
+
+  /**
    * Returns an area-level text description in the given language for the given phone number.
    *
    * @param number  the phone number for which we want to get a text description
diff --git a/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java b/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
index 857561b..b202324 100644
--- a/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
+++ b/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
@@ -93,6 +93,7 @@
     assertEquals("+81 90 1234 5678", formatter.inputDigit('8'));
     assertEquals("+81 90 12 345 6789", formatter.inputDigit('9'));
     assertEquals("+81901234567890", formatter.inputDigit('0'));
+    assertEquals("+819012345678901", formatter.inputDigit('1'));
   }
 
   public void testAYTFUS() {
@@ -716,6 +717,15 @@
     assertEquals("+81 222 12 567", formatter.inputDigit('7'));
     assertEquals("+81 222 12 5678", formatter.inputDigit('8'));
 
+    // 011113
+    formatter.clear();
+    assertEquals("0", formatter.inputDigit('0'));
+    assertEquals("01", formatter.inputDigit('1'));
+    assertEquals("011", formatter.inputDigit('1'));
+    assertEquals("011 1", formatter.inputDigit('1'));
+    assertEquals("011 11", formatter.inputDigit('1'));
+    assertEquals("011113", formatter.inputDigit('3'));
+
     // +81 3332 2 5678
     formatter.clear();
     assertEquals("+", formatter.inputDigit('+'));
diff --git a/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java b/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
index 7bbbf2c..692fffa 100644
--- a/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
+++ b/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
@@ -266,6 +266,13 @@
     findMatchesInContexts(possibleOnlyContexts, false, true);
   }
 
+  public void testPercentageNotSeenAsPhoneNumber() throws Exception {
+    ArrayList<NumberContext> possibleOnlyContexts = new ArrayList<NumberContext>();
+    possibleOnlyContexts.add(new NumberContext("", "%"));
+    // Numbers followed by % should be dropped.
+    findMatchesInContexts(possibleOnlyContexts, false, true);
+  }
+
   public void testPhoneNumberWithLeadingOrTrailingMoneyMatches() throws Exception {
     // Because of the space after the 20 (or before the 100) these dollar amounts should not stop
     // the actual number from being found.
diff --git a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
index 8033be2..5113ec6 100644
--- a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
+++ b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
@@ -71,6 +71,8 @@
   private static final PhoneNumber IT_NUMBER =
       new PhoneNumber().setCountryCode(39).setNationalNumber(236618300L).
       setItalianLeadingZero(true);
+  private static final PhoneNumber JP_STAR_NUMBER =
+      new PhoneNumber().setCountryCode(81).setNationalNumber(2345);
   // Numbers to test the formatting rules from Mexico.
   private static final PhoneNumber MX_MOBILE1 =
       new PhoneNumber().setCountryCode(52).setNationalNumber(12345678900L);
@@ -614,7 +616,8 @@
     // US toll free numbers are marked as noInternationalDialling in the test metadata for testing
     // purposes.
     assertEquals("800 253 0000",
-        phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, true));
+        phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
+                                               true /*  keep formatting */));
     assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true));
     assertEquals("+1 650 253 0000",
         phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true));
@@ -623,12 +626,26 @@
         phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, true));
 
     assertEquals("8002530000",
-        phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, false));
+        phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
+                                               false /* remove formatting */));
     assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, false));
     assertEquals("+16502530000",
         phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false));
     assertEquals("+16502530000",
         phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, false));
+
+    // An invalid US number, which is one digit too long.
+    assertEquals("+165025300001",
+        phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, false));
+    assertEquals("+1 65025300001",
+        phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, true));
+
+    // Star numbers. In real life they appear in Israel, but we have them in JP in our test
+    // metadata.
+    assertEquals("*2345",
+        phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, false));
+    assertEquals("*2345",
+        phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, true));
   }
 
   public void testFormatByPattern() {
diff --git a/java/test/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP b/java/test/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP
index 1dbb2b1..06f685a 100644
--- a/java/test/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP
+++ b/java/test/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP
Binary files differ
diff --git a/java/test/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java b/java/test/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
index feb2f36..0f5e7da 100644
--- a/java/test/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
+++ b/java/test/com/android/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
@@ -116,6 +116,26 @@
         geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN));
   }
 
+  public void testGetDescriptionForNumberWithUserRegion() {
+    // User in Italy, American number. We should just show United States, in Spanish, and not more
+    // detailed information.
+    assertEquals("Estados Unidos",
+        geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "IT"));
+    // Unknown region - should just show country name.
+    assertEquals("Estados Unidos",
+        geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("es", "ES"), "ZZ"));
+    // User in the States, language German, should show detailed data.
+    assertEquals("Kalifornien",
+        geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "US"));
+    // User in the States, language French, no data for French, so we fallback to English detailed
+    // data.
+    assertEquals("CA",
+        geocoder.getDescriptionForNumber(US_NUMBER1, Locale.FRENCH, "US"));
+    // Invalid number - return an empty string.
+    assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH,
+                                                      "US"));
+  }
+
   public void testGetDescriptionForInvalidNumber() {
     assertEquals("", geocoder.getDescriptionForNumber(KO_INVALID_NUMBER, Locale.ENGLISH));
     assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH));