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

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

Issue 2743763003: [Payments] Format and validate phone number by using libphonenumber (Closed)
Patch Set: Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
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..29a437b1154c2635f2ddbf56d53e9423708264fe
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PhoneNumberUtil.java
@@ -0,0 +1,89 @@
+// 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.ThreadUtils;
+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 {
+ private static PhoneNumberUtil sPhoneNumberUtil;
+
+ /** Phone number formatter to monitor changes so as to format it dynamically */
+ public class Formatter implements TextWatcher {
+ /** Indicates the change was caused by ourselves. */
+ private boolean mSelfChange = false;
+
+ @Override
+ public void afterTextChanged(Editable s) {
+ if (mSelfChange) return;
+
+ String formatedNumber = sPhoneNumberUtil.format(s.toString());
+ 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) {}
+ }
+
+ private final long mPhoneNumberUtilAndroid;
+ private PhoneNumberUtil() {
+ 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.
+ }
+
+ public static PhoneNumberUtil getInstance() {
+ ThreadUtils.assertOnUiThread();
+ if (sPhoneNumberUtil == null) {
+ sPhoneNumberUtil = new PhoneNumberUtil();
+ }
+ return sPhoneNumberUtil;
+ }
+
+ /**
+ * Gets a formatter to monitor phone number changes so as to format it dynamically.
+ *
+ * @return The formatter.
+ */
+ public Formatter getFormatter() {
+ return new Formatter();
+ }
+
+ /**
+ * Formats the given phone number.
+ *
+ * @param phoneNumber The given phone number.
+ * @return formatted phone number.
+ */
+ public String format(String phoneNumber) {
+ return nativeFormat(mPhoneNumberUtilAndroid, phoneNumber);
+ }
+
+ /**
+ * Checks whether the given phone number is valid.
+ *
+ * @param phoneNumber The given phone number.
+ * @return True if the given number is valid, otherwise return false.
+ */
+ public boolean isValidNumber(String phoneNumber) {
+ return nativeIsValidNumber(mPhoneNumberUtilAndroid, phoneNumber);
+ }
+
+ private native long nativeInit();
+ private native String nativeFormat(long nativePhoneNumberUtilAndroid, String phoneNumber);
+ private native boolean nativeIsValidNumber(
+ long nativePhoneNumberUtilAndroid, String phoneNumber);
+}

Powered by Google App Engine
This is Rietveld 408576698