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 cou ntry 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 /** | |
30 * Update the country code used to format phone numbers. | |
31 */ | |
32 public void setCountryCode(String countryCode) { | |
33 mCountryCode = countryCode; | |
34 } | |
25 | 35 |
26 @Override | 36 @Override |
27 public void afterTextChanged(Editable s) { | 37 public void afterTextChanged(Editable s) { |
28 if (mSelfChange) return; | 38 if (mSelfChange) return; |
29 | 39 |
30 String formattedNumber = formatForDisplay(s.toString()); | 40 String formattedNumber = formatForDisplay(s.toString(), mCountryCode ); |
31 mSelfChange = true; | 41 mSelfChange = true; |
32 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( )); | 42 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( )); |
33 mSelfChange = false; | 43 mSelfChange = false; |
34 } | 44 } |
35 | 45 |
36 @Override | 46 @Override |
37 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | 47 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
38 | 48 |
39 @Override | 49 @Override |
40 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} | 50 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} |
41 } | 51 } |
42 | 52 |
43 /** | 53 /** |
44 * Formats the given phone number in INTERNATIONAL format | 54 * Formats the given phone number in INTERNATIONAL format |
45 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL], returning the | 55 * [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 | 56 * 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. | 57 * For example, the number of the Google Zürich office will be formatted as "+41 44 668 1800" |
58 * in INTERNATIONAL format. | |
48 * | 59 * |
49 * @param phoneNumber The given phone number. | 60 * @param phoneNumber The given phone number. |
61 * @param countryCode The country code used to format phone number. Applicat ion locale will be | |
62 * used if it is set to null. | |
50 * @return Formatted phone number. | 63 * @return Formatted phone number. |
51 */ | 64 */ |
52 public static String formatForDisplay(String phoneNumber) { | 65 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.
| |
53 return nativeFormatForDisplay(phoneNumber); | 66 return countryCode == null |
67 ? 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.
| |
68 : nativeFormatForDisplayWithCountryCode(phoneNumber, countryCode ); | |
54 } | 69 } |
55 | 70 |
56 /** | 71 /** |
57 * Formats the given phone number in E.164 format as specified in the Paymen t Request spec | 72 * 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) | 73 * (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algori thm) |
59 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164], returning the original number | 74 * [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 | 75 * 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. | 76 * formatted as "+41446681800" in E.164 format. |
62 * | 77 * |
63 * @param phoneNumber The given phone number. | 78 * @param phoneNumber The given phone number. |
64 * @return Formatted phone number. | 79 * @return Formatted phone number. |
65 */ | 80 */ |
66 public static String formatForResponse(String phoneNumber) { | 81 public static String formatForResponse(String phoneNumber) { |
67 return nativeFormatForResponse(phoneNumber); | 82 return nativeFormatForResponse(phoneNumber); |
68 } | 83 } |
69 | 84 |
70 /** | 85 /** |
71 * Checks whether the given phone number matches a valid pattern according t o region code. The | 86 * 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 | 87 * region/country code. |
73 * locale is used to figure out the region code. | |
74 * | 88 * |
75 * @param phoneNumber The given phone number. | 89 * @param phoneNumber The given phone number. |
90 * @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.
| |
91 * code is from the given phone number if it starts with '+'. | |
76 * @return True if the given number is valid, otherwise return false. | 92 * @return True if the given number is valid, otherwise return false. |
77 */ | 93 */ |
78 public static boolean isValidNumber(String phoneNumber) { | 94 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.
| |
79 return nativeIsValidNumber(phoneNumber); | 95 return nativeIsValidNumberWithCountryCode(phoneNumber, countryCode); |
80 } | 96 } |
81 | 97 |
82 private static native String nativeFormatForDisplay(String phoneNumber); | 98 private static native String nativeFormatForDisplay(String phoneNumber); |
99 private static native String nativeFormatForDisplayWithCountryCode( | |
100 String phoneNumber, String countryCode); | |
83 private static native String nativeFormatForResponse(String phoneNumber); | 101 private static native String nativeFormatForResponse(String phoneNumber); |
84 private static native boolean nativeIsValidNumber(String phoneNumber); | 102 private static native boolean nativeIsValidNumber(String phoneNumber); |
103 private static native boolean nativeIsValidNumberWithCountryCode( | |
104 String phoneNumber, String countryCode); | |
85 } | 105 } |
OLD | NEW |