Chromium Code Reviews| 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 /** | 12 /** |
| 13 * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides conveni ent methods to | 13 * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides conveni ent methods to |
| 14 * format and validate phone number. | 14 * format and validate phone number. |
| 15 */ | 15 */ |
| 16 @JNINamespace("autofill") | 16 @JNINamespace("autofill") |
| 17 public class PhoneNumberUtil { | 17 public class PhoneNumberUtil { |
| 18 // Avoid instantiation by accident. | 18 // Avoid instantiation by accident. |
| 19 private PhoneNumberUtil() {} | 19 private PhoneNumberUtil() {} |
| 20 | 20 |
| 21 /** TextWatcher to watch phone number changes so as to format it dynamically */ | 21 /** |
| 22 public static class FormatTextWatcher implements TextWatcher { | 22 * TextWatcher to watch phone number changes so as to format it based on one country code. |
| 23 */ | |
| 24 public static class CountryAwareFormatTextWatcher implements TextWatcher { | |
| 23 /** Indicates the change was caused by ourselves. */ | 25 /** Indicates the change was caused by ourselves. */ |
| 24 private boolean mSelfChange; | 26 private boolean mSelfChange; |
| 27 private String mCountryCode; | |
| 28 | |
| 29 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.
| |
| 30 mCountryCode = countryCode; | |
| 31 } | |
| 25 | 32 |
| 26 @Override | 33 @Override |
| 27 public void afterTextChanged(Editable s) { | 34 public void afterTextChanged(Editable s) { |
| 28 if (mSelfChange) return; | 35 if (mSelfChange) return; |
| 29 | 36 |
| 30 String formattedNumber = formatForDisplay(s.toString()); | 37 String formattedNumber = formatForDisplay(s.toString(), mCountryCode ); |
| 31 mSelfChange = true; | 38 mSelfChange = true; |
| 32 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( )); | 39 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( )); |
| 33 mSelfChange = false; | 40 mSelfChange = false; |
| 34 } | 41 } |
| 35 | 42 |
| 36 @Override | 43 @Override |
| 37 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | 44 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
| 38 | 45 |
| 39 @Override | 46 @Override |
| 40 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} | 47 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} |
| 41 } | 48 } |
| 42 | 49 |
| 43 /** | 50 /** |
| 44 * Formats the given phone number in INTERNATIONAL format | 51 * Formats the given phone number in INTERNATIONAL format |
| 45 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL], returning the | 52 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL] b ased on |
| 46 * original number if no formatting can be made. For example, the number of the Google Zürich | 53 * given country code, returning the original number if no formatting can be made. |
| 47 * office will be formatted as "+41 44 668 1800" in INTERNATIONAL format. | 54 * For example, the number of the Google Zürich office will be formatted as "+41 44 668 1800" |
| 55 * in INTERNATIONAL format. | |
| 48 * | 56 * |
| 49 * @param phoneNumber The given phone number. | 57 * @param phoneNumber The given phone number. |
| 58 * @param countryCode The country code used to format phone number. Applicat ion locale will be | |
| 59 * used if it is set to null. | |
| 50 * @return Formatted phone number. | 60 * @return Formatted phone number. |
| 51 */ | 61 */ |
| 52 public static String formatForDisplay(String phoneNumber) { | 62 public static String formatForDisplay(String phoneNumber, String countryCode ) { |
| 53 return nativeFormatForDisplay(phoneNumber); | 63 return countryCode == null |
| 64 ? nativeFormatForDisplay(phoneNumber) | |
| 65 : nativeFormatForDisplayWithCountryCode(phoneNumber, countryCode ); | |
| 54 } | 66 } |
| 55 | 67 |
| 56 /** | 68 /** |
| 57 * Formats the given phone number in E.164 format as specified in the Paymen t Request spec | 69 * 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) | 70 * (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algori thm) |
| 59 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164], returning the original number | 71 * [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 | 72 * 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. | 73 * formatted as "+41446681800" in E.164 format. |
| 62 * | 74 * |
| 63 * @param phoneNumber The given phone number. | 75 * @param phoneNumber The given phone number. |
| 64 * @return Formatted phone number. | 76 * @return Formatted phone number. |
| 65 */ | 77 */ |
| 66 public static String formatForResponse(String phoneNumber) { | 78 public static String formatForResponse(String phoneNumber) { |
| 67 return nativeFormatForResponse(phoneNumber); | 79 return nativeFormatForResponse(phoneNumber); |
| 68 } | 80 } |
| 69 | 81 |
| 70 /** | 82 /** |
| 71 * Checks whether the given phone number matches a valid pattern according t o region code. The | 83 * 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 | 84 * region/country code. |
| 73 * locale is used to figure out the region code. | |
| 74 * | 85 * |
| 75 * @param phoneNumber The given phone number. | 86 * @param phoneNumber The given phone number. |
| 87 * @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.
| |
| 88 * try to deduce it from the number if it starts with '+' , otherwise use | |
| 89 * application locale. | |
| 76 * @return True if the given number is valid, otherwise return false. | 90 * @return True if the given number is valid, otherwise return false. |
| 77 */ | 91 */ |
| 78 public static boolean isValidNumber(String phoneNumber) { | 92 public static boolean isValidNumber(String phoneNumber, String countryCode) { |
| 79 return nativeIsValidNumber(phoneNumber); | 93 return countryCode == null ? nativeIsValidNumber(phoneNumber) |
| 94 : nativeIsValidNumberWithCountryCode(phoneNum ber, countryCode); | |
| 80 } | 95 } |
| 81 | 96 |
| 82 private static native String nativeFormatForDisplay(String phoneNumber); | 97 private static native String nativeFormatForDisplay(String phoneNumber); |
| 98 private static native String nativeFormatForDisplayWithCountryCode( | |
| 99 String phoneNumber, String countryCode); | |
| 83 private static native String nativeFormatForResponse(String phoneNumber); | 100 private static native String nativeFormatForResponse(String phoneNumber); |
| 84 private static native boolean nativeIsValidNumber(String phoneNumber); | 101 private static native boolean nativeIsValidNumber(String phoneNumber); |
| 102 private static native boolean nativeIsValidNumberWithCountryCode( | |
| 103 String phoneNumber, String countryCode); | |
| 85 } | 104 } |
| OLD | NEW |