Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java

Issue 2952673002: [Payments] Format and validate phone number according to selected country in address editor (Closed)
Patch Set: Fix tests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 @Nullable
30 private String mCountryCode;
31
32 /**
33 * Updates the country code used to format phone numbers.
34 *
35 * @param countryCode The given country code.
36 */
37 public void setCountryCode(@Nullable String countryCode) {
38 mCountryCode = countryCode;
39 }
25 40
26 @Override 41 @Override
27 public void afterTextChanged(Editable s) { 42 public void afterTextChanged(Editable s) {
28 if (mSelfChange) return; 43 if (mSelfChange) return;
29 44
30 String formattedNumber = formatForDisplay(s.toString()); 45 String formattedNumber = formatForDisplay(s.toString(), mCountryCode );
31 mSelfChange = true; 46 mSelfChange = true;
32 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( )); 47 s.replace(0, s.length(), formattedNumber, 0, formattedNumber.length( ));
33 mSelfChange = false; 48 mSelfChange = false;
34 } 49 }
35 50
36 @Override 51 @Override
37 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 52 public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
38 53
39 @Override 54 @Override
40 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} 55 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {}
41 } 56 }
42 57
43 /** 58 /**
44 * Formats the given phone number in INTERNATIONAL format 59 * Formats the given phone number in INTERNATIONAL format
45 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL], returning the 60 * [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 61 * 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. 62 * For example, the number of the Google Zürich office will be formatted as
63 * "+41 44 668 1800" in INTERNATIONAL format.
64 *
65 * Note that the region code is from the given phone number if it starts wit h
66 * '+', otherwise the region code is deduced from the given country code or
67 * from application locale if the given country code is null.
48 * 68 *
49 * @param phoneNumber The given phone number. 69 * @param phoneNumber The given phone number.
70 * @param countryCode The given country code.
50 * @return Formatted phone number. 71 * @return Formatted phone number.
51 */ 72 */
52 public static String formatForDisplay(String phoneNumber) { 73 public static String formatForDisplay(String phoneNumber, @Nullable String c ountryCode) {
53 return nativeFormatForDisplay(phoneNumber); 74 return nativeFormatForDisplay(phoneNumber, countryCode);
54 } 75 }
55 76
56 /** 77 /**
57 * Formats the given phone number in E.164 format as specified in the Paymen t Request spec 78 * 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) 79 * (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algori thm)
59 * [i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164], returning the original number 80 * [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 81 * 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. 82 * formatted as "+41446681800" in E.164 format.
62 * 83 *
63 * @param phoneNumber The given phone number. 84 * @param phoneNumber The given phone number.
64 * @return Formatted phone number. 85 * @return Formatted phone number.
65 */ 86 */
66 public static String formatForResponse(String phoneNumber) { 87 public static String formatForResponse(String phoneNumber) {
67 return nativeFormatForResponse(phoneNumber); 88 return nativeFormatForResponse(phoneNumber);
68 } 89 }
69 90
70 /** 91 /**
71 * Checks whether the given phone number matches a valid pattern according t o region code. The 92 * 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 93 * region code.
73 * locale is used to figure out the region code. 94 * The region code is from the given phone number if it starts with '+',
95 * otherwise the region code is deduced from the given country code or from
96 * application locale if the given country code is null.
74 * 97 *
75 * @param phoneNumber The given phone number. 98 * @param phoneNumber The given phone number.
99 * @param countryCode The given country code.
100 *
76 * @return True if the given number is valid, otherwise return false. 101 * @return True if the given number is valid, otherwise return false.
77 */ 102 */
78 public static boolean isValidNumber(String phoneNumber) { 103 public static boolean isValidNumber(String phoneNumber, @Nullable String cou ntryCode) {
79 return nativeIsValidNumber(phoneNumber); 104 return nativeIsValidNumber(phoneNumber, countryCode);
80 } 105 }
81 106
82 private static native String nativeFormatForDisplay(String phoneNumber); 107 private static native String nativeFormatForDisplay(String phoneNumber, Stri ng countryCode);
83 private static native String nativeFormatForResponse(String phoneNumber); 108 private static native String nativeFormatForResponse(String phoneNumber);
84 private static native boolean nativeIsValidNumber(String phoneNumber); 109 private static native boolean nativeIsValidNumber(String phoneNumber, String countryCode);
85 } 110 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698