Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.autofill; | |
| 6 | |
| 7 import android.text.Editable; | |
| 8 import android.text.TextWatcher; | |
| 9 | |
| 10 import org.chromium.base.ThreadUtils; | |
| 11 import org.chromium.base.annotations.JNINamespace; | |
| 12 | |
| 13 /** | |
| 14 * Android wrapper of i18n::phonenumbers::PhoneNumberUtil which provides conveni ent methods to | |
| 15 * format and validate phone number. | |
| 16 */ | |
| 17 @JNINamespace("autofill") | |
| 18 public class PhoneNumberUtil { | |
| 19 private static PhoneNumberUtil sPhoneNumberUtil; | |
| 20 | |
| 21 /** Phone number formatter to monitor changes so as to format it dynamically */ | |
| 22 public class Formatter implements TextWatcher { | |
| 23 /** Indicates the change was caused by ourselves. */ | |
| 24 private boolean mSelfChange = false; | |
| 25 | |
| 26 @Override | |
| 27 public void afterTextChanged(Editable s) { | |
| 28 if (mSelfChange) return; | |
| 29 | |
| 30 String formatedNumber = sPhoneNumberUtil.format(s.toString()); | |
| 31 mSelfChange = true; | |
| 32 s.replace(0, s.length(), formatedNumber, 0, formatedNumber.length()) ; | |
| 33 mSelfChange = false; | |
| 34 } | |
| 35 | |
| 36 @Override | |
| 37 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
| 38 | |
| 39 @Override | |
| 40 public void onTextChanged(CharSequence s, int start, int before, int cou nt) {} | |
| 41 } | |
| 42 | |
| 43 private final long mPhoneNumberUtilAndroid; | |
| 44 private PhoneNumberUtil() { | |
| 45 mPhoneNumberUtilAndroid = nativeInit(); | |
|
Mathieu
2017/03/10 20:42:51
As mentioned, consider removing the class from the
gogerald1
2017/03/10 23:17:36
Done.
| |
| 46 } | |
| 47 | |
| 48 public static PhoneNumberUtil getInstance() { | |
| 49 ThreadUtils.assertOnUiThread(); | |
| 50 if (sPhoneNumberUtil == null) { | |
| 51 sPhoneNumberUtil = new PhoneNumberUtil(); | |
| 52 } | |
| 53 return sPhoneNumberUtil; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Gets a formatter to monitor phone number changes so as to format it dynam ically. | |
| 58 * | |
| 59 * @return The formatter. | |
| 60 */ | |
| 61 public Formatter getFormatter() { | |
| 62 return new Formatter(); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Formats the given phone number. | |
| 67 * | |
| 68 * @param phoneNumber The given phone number. | |
| 69 * @return formatted phone number. | |
| 70 */ | |
| 71 public String format(String phoneNumber) { | |
| 72 return nativeFormat(mPhoneNumberUtilAndroid, phoneNumber); | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Checks whether the given phone number is valid. | |
| 77 * | |
| 78 * @param phoneNumber The given phone number. | |
| 79 * @return True if the given number is valid, otherwise return false. | |
| 80 */ | |
| 81 public boolean isValidNumber(String phoneNumber) { | |
| 82 return nativeIsValidNumber(mPhoneNumberUtilAndroid, phoneNumber); | |
| 83 } | |
| 84 | |
| 85 private native long nativeInit(); | |
| 86 private native String nativeFormat(long nativePhoneNumberUtilAndroid, String phoneNumber); | |
| 87 private native boolean nativeIsValidNumber( | |
| 88 long nativePhoneNumberUtilAndroid, String phoneNumber); | |
| 89 } | |
| OLD | NEW |