| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillLocalCardEditor.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillLocalCardEditor.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillLocalCardEditor.java
|
| index 8c642db4c17b4606ff57c9b54516f8b0cae0841e..9971acc9941d6e66c9c1adb6ad6862f2672baf8a 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillLocalCardEditor.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillLocalCardEditor.java
|
| @@ -5,7 +5,9 @@
|
| package org.chromium.chrome.browser.preferences.autofill;
|
|
|
| import android.os.Bundle;
|
| +import android.text.Editable;
|
| import android.text.TextUtils;
|
| +import android.text.TextWatcher;
|
| import android.view.LayoutInflater;
|
| import android.view.View;
|
| import android.view.ViewGroup;
|
| @@ -195,4 +197,94 @@
|
| || !TextUtils.isEmpty(mNumberText.getText());
|
| ((Button) getView().findViewById(R.id.button_primary)).setEnabled(enabled);
|
| }
|
| +
|
| + /**
|
| + * Watch a TextView and if a credit card number is entered, it will format the number.
|
| + * Disable formatting when user:
|
| + * 1. Inputs dashes or spaces.
|
| + * 2. Removes separators in the middle of the string
|
| + * 3. Enters a number longer than 16 digits.
|
| + *
|
| + * Formatting will be re-enabled once text is cleared.
|
| + */
|
| + private static class CreditCardNumberFormattingTextWatcher implements TextWatcher {
|
| + /** Character for card number section separator. */
|
| + private static final String SEPARATOR = " ";
|
| +
|
| + /**
|
| + * Whether to format the credit card number. If true, spaces will be inserted
|
| + * automatically between each group of 4 digits in the credit card number as the user types.
|
| + * This is set to false if the user types a dash or deletes one of the auto-inserted spaces.
|
| + */
|
| + private boolean mFormattingEnabled = true;
|
| +
|
| + /**
|
| + * Whether the change was caused by ourselves.
|
| + * This is set true when we are manipulating the text of EditText,
|
| + * and all callback functions should check this boolean to avoid infinite recursion.
|
| + */
|
| + private boolean mSelfChange = false;
|
| +
|
| + @Override
|
| + public void onTextChanged(CharSequence s, int start, int before, int count) {
|
| + if (mSelfChange || !mFormattingEnabled) return;
|
| + // If user enters non-digit characters, do not format.
|
| + if (count > 0 && hasDashOrSpace(s, start, count)) {
|
| + mFormattingEnabled = false;
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
| + if (mSelfChange || !mFormattingEnabled) return;
|
| + // If user deletes non-digit characters, do not format.
|
| + if (count > 0 && hasDashOrSpace(s, start, count)) {
|
| + mFormattingEnabled = false;
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void afterTextChanged(Editable s) {
|
| + if (mSelfChange) return;
|
| + mSelfChange = true;
|
| +
|
| + if (mFormattingEnabled) {
|
| + removeSeparators(s);
|
| + // If number is too long, do not format it and remove all
|
| + // previous separators.
|
| + if (s.length() > 16) {
|
| + mFormattingEnabled = false;
|
| + } else {
|
| + insertSeparators(s);
|
| + }
|
| + }
|
| + // If user clears the input, re-enable formatting
|
| + if (s.length() == 0) mFormattingEnabled = true;
|
| +
|
| + mSelfChange = false;
|
| + }
|
| +
|
| + public static void removeSeparators(Editable s) {
|
| + int index = TextUtils.indexOf(s, SEPARATOR);
|
| + while (index >= 0) {
|
| + s.delete(index, index + 1);
|
| + index = TextUtils.indexOf(s, SEPARATOR, index + 1);
|
| + }
|
| + }
|
| +
|
| + public static void insertSeparators(Editable s) {
|
| + final int[] positions = {4, 9, 14 };
|
| + for (int i : positions) {
|
| + if (s.length() > i) {
|
| + s.insert(i, SEPARATOR);
|
| + }
|
| + }
|
| + }
|
| +
|
| + public static boolean hasDashOrSpace(final CharSequence s, final int start,
|
| + final int count) {
|
| + return TextUtils.indexOf(s, " ", start, start + count) != -1
|
| + || TextUtils.indexOf(s, "-", start, start + count) != -1;
|
| + }
|
| + }
|
| }
|
|
|