Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..48b5c43963d6717cb080b3a4b60eb7a19b946e54 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.autofill; |
| + |
| +import android.text.Editable; |
| +import android.text.TextWatcher; |
| + |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +/** |
| + * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides convenient methods to |
| + * format and validate phone number. |
| + */ |
| +@JNINamespace("autofill") |
| +public class PhoneNumberUtil { |
| + // Avoid instantiation by accident. |
| + private PhoneNumberUtil() {} |
| + |
| + /** TextWatcher to watch phone number changes so as to format it dynamically */ |
| + private static class FormatTextWatcher implements TextWatcher { |
| + /** Indicates the change was caused by ourselves. */ |
| + private boolean mSelfChange = false; |
| + |
| + @Override |
| + public void afterTextChanged(Editable s) { |
| + if (mSelfChange) return; |
| + |
| + String formatedNumber = format(s.toString()); |
|
Mathieu
2017/03/14 00:36:23
*formattedNumber
gogerald1
2017/03/14 15:32:59
Done.
|
| + mSelfChange = true; |
| + s.replace(0, s.length(), formatedNumber, 0, formatedNumber.length()); |
| + mSelfChange = false; |
| + } |
| + |
| + @Override |
| + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
| + |
| + @Override |
| + public void onTextChanged(CharSequence s, int start, int before, int count) {} |
| + } |
| + |
| + /** |
| + * Gets a TextWatcher to watch phone number changes so as to format it dynamically. |
| + * |
| + * @return A TextWatcher. |
| + */ |
| + public static TextWatcher getFormatTextWatcher() { |
|
Mathieu
2017/03/14 00:36:23
Is this function needed? Sounds like callers could
gogerald1
2017/03/14 15:32:59
Done.
|
| + return new FormatTextWatcher(); |
| + } |
| + |
| + /** |
| + * Formats the given phone number. |
|
Mathieu
2017/03/14 00:36:22
I think examples would make this clearer. "Formats
gogerald1
2017/03/14 15:32:59
Done.
|
| + * |
| + * @param phoneNumber The given phone number. |
| + * @return formatted phone number. |
| + */ |
| + public static String format(String phoneNumber) { |
| + return nativeFormat(phoneNumber); |
| + } |
| + |
| + /** |
| + * Checks whether the given phone number is valid. |
|
Mathieu
2017/03/14 00:36:23
Valid according to what?
gogerald1
2017/03/14 15:32:59
Done.
|
| + * |
| + * @param phoneNumber The given phone number. |
| + * @return True if the given number is valid, otherwise return false. |
| + */ |
| + public static boolean isValidNumber(String phoneNumber) { |
| + return nativeIsValidNumber(phoneNumber); |
| + } |
| + |
| + private static native String nativeFormat(String phoneNumber); |
| + private static native boolean nativeIsValidNumber(String phoneNumber); |
| +} |