Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java |
| index d69cb1fb20ed772af84f14b89e09a9738f735abe..bafccb49059474421628c9cf483c36bbd55d2a4a 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java |
| @@ -18,16 +18,26 @@ public class PhoneNumberUtil { |
| // Avoid instantiation by accident. |
| private PhoneNumberUtil() {} |
| - /** TextWatcher to watch phone number changes so as to format it dynamically */ |
| - public static class FormatTextWatcher implements TextWatcher { |
| + /** |
| + * TextWatcher to watch phone number changes so as to format it based on country code. |
| + */ |
| + public static class CountryAwareFormatTextWatcher implements TextWatcher { |
| /** Indicates the change was caused by ourselves. */ |
| private boolean mSelfChange; |
| + private String mCountryCode; |
| + |
| + /** |
| + * Update the country code used to format phone numbers. |
| + */ |
| + public void setCountryCode(String countryCode) { |
| + mCountryCode = countryCode; |
| + } |
| @Override |
| public void afterTextChanged(Editable s) { |
| if (mSelfChange) return; |
| - String formattedNumber = formatForDisplay(s.toString()); |
| + String formattedNumber = formatForDisplay(s.toString(), mCountryCode); |
| mSelfChange = true; |
| s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length()); |
| mSelfChange = false; |
| @@ -42,15 +52,20 @@ public class PhoneNumberUtil { |
| /** |
| * Formats the given phone number in INTERNATIONAL format |
| - * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL], returning the |
| - * original number if no formatting can be made. For example, the number of the Google Zürich |
| - * office will be formatted as "+41 44 668 1800" in INTERNATIONAL format. |
| + * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL] based on |
| + * given country code, returning the original number if no formatting can be made. |
| + * For example, the number of the Google Zürich office will be formatted as "+41 44 668 1800" |
| + * in INTERNATIONAL format. |
| * |
| * @param phoneNumber The given phone number. |
| + * @param countryCode The country code used to format phone number. Application locale will be |
| + * used if it is set to null. |
| * @return Formatted phone number. |
| */ |
| - public static String formatForDisplay(String phoneNumber) { |
| - return nativeFormatForDisplay(phoneNumber); |
| + public static String formatForDisplay(String phoneNumber, String countryCode) { |
|
gogerald1
2017/06/15 17:41:35
@Nullable countryCode
wuandy1
2017/06/19 15:11:14
Done.
|
| + return countryCode == null |
| + ? nativeFormatForDisplay(phoneNumber) |
|
gogerald1
2017/06/15 17:41:35
For simplicity, you no need two interfaces here, '
wuandy1
2017/06/19 15:11:14
Done.
|
| + : nativeFormatForDisplayWithCountryCode(phoneNumber, countryCode); |
| } |
| /** |
| @@ -68,18 +83,23 @@ public class PhoneNumberUtil { |
| } |
| /** |
| - * Checks whether the given phone number matches a valid pattern according to region code. The |
| - * region code is from the given phone number if it starts with '+', otherwise application |
| - * locale is used to figure out the region code. |
| + * Checks whether the given phone number matches a valid pattern according to |
| + * region/country code. |
| * |
| * @param phoneNumber The given phone number. |
| + * @param countryCode The country code used to validate the number against. The region |
|
gogerald1
2017/06/15 17:41:35
The comments for countryCode should be the same as
wuandy1
2017/06/19 15:11:14
Done.
|
| + * code is from the given phone number if it starts with '+'. |
| * @return True if the given number is valid, otherwise return false. |
| */ |
| - public static boolean isValidNumber(String phoneNumber) { |
| - return nativeIsValidNumber(phoneNumber); |
| + public static boolean isValidNumber(String phoneNumber, String countryCode) { |
|
gogerald1
2017/06/15 17:41:35
@Nullable countryCode?
wuandy1
2017/06/19 15:11:14
Done.
|
| + return nativeIsValidNumberWithCountryCode(phoneNumber, countryCode); |
| } |
| private static native String nativeFormatForDisplay(String phoneNumber); |
| + private static native String nativeFormatForDisplayWithCountryCode( |
| + String phoneNumber, String countryCode); |
| private static native String nativeFormatForResponse(String phoneNumber); |
| private static native boolean nativeIsValidNumber(String phoneNumber); |
| + private static native boolean nativeIsValidNumberWithCountryCode( |
| + String phoneNumber, String countryCode); |
| } |