| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.autofill; | 5 package org.chromium.chrome.browser.autofill; |
| 6 | 6 |
| 7 import android.text.Editable; | 7 import android.text.Editable; |
| 8 import android.text.TextWatcher; | 8 import android.text.TextWatcher; |
| 9 | 9 |
| 10 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
| 11 | 11 |
| 12 import javax.annotation.Nullable; |
| 13 |
| 12 /** | 14 /** |
| 13 * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides conveni
ent methods to | 15 * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides conveni
ent methods to |
| 14 * format and validate phone number. | 16 * format and validate phone number. |
| 15 */ | 17 */ |
| 16 @JNINamespace("autofill") | 18 @JNINamespace("autofill") |
| 17 public class PhoneNumberUtil { | 19 public class PhoneNumberUtil { |
| 18 // Avoid instantiation by accident. | 20 // Avoid instantiation by accident. |
| 19 private PhoneNumberUtil() {} | 21 private PhoneNumberUtil() {} |
| 20 | 22 |
| 21 /** TextWatcher to watch phone number changes so as to format it dynamically
*/ | 23 /** |
| 22 public static class FormatTextWatcher implements TextWatcher { | 24 * TextWatcher to watch phone number changes so as to format it based on cou
ntry code. |
| 25 */ |
| 26 public static class CountryAwareFormatTextWatcher implements TextWatcher { |
| 23 /** Indicates the change was caused by ourselves. */ | 27 /** Indicates the change was caused by ourselves. */ |
| 24 private boolean mSelfChange; | 28 private boolean mSelfChange; |
| 29 private String mCountryCode; |
| 30 |
| 31 /** |
| 32 * Update the country code used to format phone numbers. |
| 33 */ |
| 34 public void setCountryCode(String countryCode) { |
| 35 mCountryCode = countryCode; |
| 36 } |
| 25 | 37 |
| 26 @Override | 38 @Override |
| 27 public void afterTextChanged(Editable s) { | 39 public void afterTextChanged(Editable s) { |
| 28 if (mSelfChange) return; | 40 if (mSelfChange) return; |
| 29 | 41 |
| 30 String formattedNumber = formatForDisplay(s.toString()); | 42 String formattedNumber = formatForDisplay(s.toString(), mCountryCode
); |
| 31 mSelfChange = true; | 43 mSelfChange = true; |
| 32 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length(
)); | 44 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length(
)); |
| 33 mSelfChange = false; | 45 mSelfChange = false; |
| 34 } | 46 } |
| 35 | 47 |
| 36 @Override | 48 @Override |
| 37 public void beforeTextChanged(CharSequence s, int start, int count, int
after) {} | 49 public void beforeTextChanged(CharSequence s, int start, int count, int
after) {} |
| 38 | 50 |
| 39 @Override | 51 @Override |
| 40 public void onTextChanged(CharSequence s, int start, int before, int cou
nt) {} | 52 public void onTextChanged(CharSequence s, int start, int before, int cou
nt) {} |
| 41 } | 53 } |
| 42 | 54 |
| 43 /** | 55 /** |
| 44 * Formats the given phone number in INTERNATIONAL format | 56 * Formats the given phone number in INTERNATIONAL format |
| 45 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL],
returning the | 57 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL] b
ased |
| 46 * original number if no formatting can be made. For example, the number of
the Google Zürich | 58 * on region code, returning the original number if no formatting can be mad
e. |
| 47 * office will be formatted as "+41 44 668 1800" in INTERNATIONAL format. | 59 * For example, the number of the Google Zürich office will be formatted as |
| 60 * "+41 44 668 1800" in INTERNATIONAL format. |
| 61 * |
| 62 * Note that the region code is from the given phone number if it starts wit
h |
| 63 * '+', otherwise the region code is deduced from the given country code or |
| 64 * from application locale if the given country code is null. |
| 48 * | 65 * |
| 49 * @param phoneNumber The given phone number. | 66 * @param phoneNumber The given phone number. |
| 67 * @param countryCode The given country code. |
| 50 * @return Formatted phone number. | 68 * @return Formatted phone number. |
| 51 */ | 69 */ |
| 52 public static String formatForDisplay(String phoneNumber) { | 70 public static String formatForDisplay(String phoneNumber, @Nullable String c
ountryCode) { |
| 53 return nativeFormatForDisplay(phoneNumber); | 71 return nativeFormatForDisplay(phoneNumber, countryCode); |
| 54 } | 72 } |
| 55 | 73 |
| 56 /** | 74 /** |
| 57 * Formats the given phone number in E.164 format as specified in the Paymen
t Request spec | 75 * Formats the given phone number in E.164 format as specified in the Paymen
t Request spec |
| 58 * (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algori
thm) | 76 * (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algori
thm) |
| 59 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164], returning
the original number | 77 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164], returning
the original number |
| 60 * if no formatting can be made. For example, the number of the Google Züric
h office will be | 78 * if no formatting can be made. For example, the number of the Google Züric
h office will be |
| 61 * formatted as "+41446681800" in E.164 format. | 79 * formatted as "+41446681800" in E.164 format. |
| 62 * | 80 * |
| 63 * @param phoneNumber The given phone number. | 81 * @param phoneNumber The given phone number. |
| 64 * @return Formatted phone number. | 82 * @return Formatted phone number. |
| 65 */ | 83 */ |
| 66 public static String formatForResponse(String phoneNumber) { | 84 public static String formatForResponse(String phoneNumber) { |
| 67 return nativeFormatForResponse(phoneNumber); | 85 return nativeFormatForResponse(phoneNumber); |
| 68 } | 86 } |
| 69 | 87 |
| 70 /** | 88 /** |
| 71 * Checks whether the given phone number matches a valid pattern according t
o region code. The | 89 * Checks whether the given phone number matches a valid pattern according t
o |
| 72 * region code is from the given phone number if it starts with '+', otherwi
se application | 90 * region code. |
| 73 * locale is used to figure out the region code. | 91 * The region code is from the given phone number if it starts with '+', |
| 92 * otherwise the region code is deduced from the given country code or from |
| 93 * application locale if the given country code is null. |
| 74 * | 94 * |
| 75 * @param phoneNumber The given phone number. | 95 * @param phoneNumber The given phone number. |
| 96 * @param countryCode The given country code. |
| 97 * |
| 76 * @return True if the given number is valid, otherwise return false. | 98 * @return True if the given number is valid, otherwise return false. |
| 77 */ | 99 */ |
| 78 public static boolean isValidNumber(String phoneNumber) { | 100 public static boolean isValidNumber(String phoneNumber, @Nullable String cou
ntryCode) { |
| 79 return nativeIsValidNumber(phoneNumber); | 101 return nativeIsValidNumber(phoneNumber, countryCode); |
| 80 } | 102 } |
| 81 | 103 |
| 82 private static native String nativeFormatForDisplay(String phoneNumber); | 104 private static native String nativeFormatForDisplay(String phoneNumber, Stri
ng countryCode); |
| 83 private static native String nativeFormatForResponse(String phoneNumber); | 105 private static native String nativeFormatForResponse(String phoneNumber); |
| 84 private static native boolean nativeIsValidNumber(String phoneNumber); | 106 private static native boolean nativeIsValidNumber(String phoneNumber, String
countryCode); |
| 85 } | 107 } |
| OLD | NEW |