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..ff67c28c922edf66cfd791316744a1fdb8d0025f 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,23 @@ 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 one country code. |
+ */ |
+ public static class CountryAwareFormatTextWatcher implements TextWatcher { |
/** Indicates the change was caused by ourselves. */ |
private boolean mSelfChange; |
+ private String mCountryCode; |
+ |
+ public void setCountryCode(String countryCode) { |
gogerald1
2017/06/08 16:20:43
Add comments for this public interface,
wuandy1
2017/06/08 20:34:58
Done.
|
+ 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 +49,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) { |
+ return countryCode == null |
+ ? nativeFormatForDisplay(phoneNumber) |
+ : nativeFormatForDisplayWithCountryCode(phoneNumber, countryCode); |
} |
/** |
@@ -68,18 +80,25 @@ 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. If null, the function will |
gogerald1
2017/06/08 16:20:43
This is incorrect, even the country code is not nu
wuandy1
2017/06/08 20:34:58
Done.
|
+ * try to deduce it from the number if it starts with '+', otherwise use |
+ * application locale. |
* @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) { |
+ return countryCode == null ? nativeIsValidNumber(phoneNumber) |
+ : 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); |
} |