Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java |
| index db3f746d68ffd1a37a4f4d440e8a8ba56683f94d..42d5f6aa01bc3c2fd84f9db3a9c73cbc0098c4d8 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AutofillAddress.java |
| @@ -4,12 +4,21 @@ |
| package org.chromium.chrome.browser.payments; |
| +import android.content.Context; |
| +import android.support.annotation.IntDef; |
| +import android.telephony.PhoneNumberUtils; |
| import android.text.TextUtils; |
| +import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| import org.chromium.chrome.browser.payments.ui.PaymentOption; |
| +import org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge; |
| +import org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge.AddressField; |
| import org.chromium.payments.mojom.PaymentAddress; |
| +import java.lang.annotation.Retention; |
| +import java.lang.annotation.RetentionPolicy; |
| +import java.util.List; |
| import java.util.Locale; |
| import java.util.regex.Matcher; |
| import java.util.regex.Pattern; |
| @@ -29,8 +38,24 @@ public class AutofillAddress extends PaymentOption { |
| private static final int LANGUAGE_CODE_GROUP = 1; |
| private static final int SCRIPT_CODE_GROUP = 3; |
| + @IntDef({COMPLETE, INVALID_ADDRESS, INVALID_PHONE_NUMBER, INVALID_RECIPIENT, |
| + INVALID_MULTIPLE_FIELDS}) |
|
Ted C
2016/11/09 18:25:49
nice! even better with the IntDef
gogerald1
2016/11/09 18:35:12
Acknowledged.
|
| + @Retention(RetentionPolicy.SOURCE) |
| + public @interface CompletionStatus {} |
| + /** Can be sent to the merchant as-is without editing first. */ |
| + public static final int COMPLETE = 0; |
| + /** The address is invalid. For example, missing state or city name. */ |
| + public static final int INVALID_ADDRESS = 1; |
| + /** The phone number is invalid or missing. */ |
| + public static final int INVALID_PHONE_NUMBER = 2; |
| + /** The recipient is missing. */ |
| + public static final int INVALID_RECIPIENT = 3; |
| + /** Multiple fields are invalid or missing. */ |
| + public static final int INVALID_MULTIPLE_FIELDS = 4; |
| + |
| @Nullable private static Pattern sRegionCodePattern; |
| + private Context mContext; |
| private AutofillProfile mProfile; |
| @Nullable private Pattern mLanguageScriptCodePattern; |
| @@ -39,11 +64,12 @@ public class AutofillAddress extends PaymentOption { |
| * |
| * @param profile The autofill profile containing the address information. |
| */ |
| - public AutofillAddress(AutofillProfile profile, boolean isComplete) { |
| + public AutofillAddress(Context context, AutofillProfile profile) { |
| super(profile.getGUID(), profile.getFullName(), profile.getLabel(), |
| profile.getPhoneNumber(), null); |
| + mContext = context; |
| mProfile = profile; |
| - mIsComplete = isComplete; |
| + checkAndUpdateAddressCompleteness(); |
| } |
| /** @return The autofill profile where this address data lives. */ |
| @@ -59,9 +85,118 @@ public class AutofillAddress extends PaymentOption { |
| */ |
| public void completeAddress(AutofillProfile profile) { |
| mProfile = profile; |
| - mIsComplete = true; |
| updateIdentifierAndLabels(mProfile.getGUID(), mProfile.getFullName(), mProfile.getLabel(), |
| mProfile.getPhoneNumber()); |
| + checkAndUpdateAddressCompleteness(); |
| + assert mIsComplete; |
| + } |
| + |
| + /** |
| + * Checks whether this address is complete and updates edit message, edit title and complete |
| + * status. |
| + */ |
| + private void checkAndUpdateAddressCompleteness() { |
| + int editMessageResId = 0; |
| + int editTitleResId = 0; |
| + |
| + switch (checkAddressCompletionStatus(mProfile)) { |
| + case COMPLETE: |
| + break; |
| + case INVALID_ADDRESS: |
| + editMessageResId = R.string.payments_invalid_address; |
| + editTitleResId = R.string.payments_add_valid_address; |
| + break; |
| + case INVALID_PHONE_NUMBER: |
| + editMessageResId = R.string.payments_phone_number_required; |
| + editTitleResId = R.string.payments_add_phone_number; |
| + break; |
| + case INVALID_RECIPIENT: |
| + editMessageResId = R.string.payments_recipient_required; |
| + editTitleResId = R.string.payments_add_recipient; |
| + break; |
| + case INVALID_MULTIPLE_FIELDS: |
| + editMessageResId = R.string.payments_more_information_required; |
| + editTitleResId = R.string.payments_add_more_information; |
| + break; |
| + default: |
| + assert false : "Invalid completion status"; |
| + } |
| + |
| + mEditMessage = editMessageResId == 0 ? null : mContext.getString(editMessageResId); |
| + mEditTitle = editTitleResId == 0 ? null : mContext.getString(editTitleResId); |
| + mIsComplete = mEditMessage == null; |
| + } |
| + |
| + /** |
| + * Checks address completion status in the given profile. |
| + * |
| + * If the country code is not set or invalid, but all fields for the default locale's country |
| + * code are present, then the profile is deemed "complete." AutoflllAddress.toPaymentAddress() |
| + * will use the default locale to fill in a blank country code before sending the address to the |
| + * renderer. |
| + * |
| + * @param profile The autofill profile containing the address information. |
| + * @return int The completion status. |
| + */ |
| + @CompletionStatus |
| + public static int checkAddressCompletionStatus(AutofillProfile profile) { |
| + int invalidFieldsCount = 0; |
| + int completionStatus = COMPLETE; |
| + |
| + if (!PhoneNumberUtils.isGlobalPhoneNumber( |
| + PhoneNumberUtils.stripSeparators(profile.getPhoneNumber().toString()))) { |
| + completionStatus = INVALID_PHONE_NUMBER; |
| + invalidFieldsCount++; |
| + } |
| + |
| + List<Integer> requiredFields = AutofillProfileBridge.getRequiredAddressFields( |
| + AutofillAddress.getCountryCode(profile)); |
| + for (int fieldId : requiredFields) { |
| + if (fieldId == AddressField.RECIPIENT || fieldId == AddressField.COUNTRY) continue; |
| + if (!TextUtils.isEmpty(getProfileField(profile, fieldId))) continue; |
| + completionStatus = INVALID_ADDRESS; |
| + invalidFieldsCount++; |
| + break; |
| + } |
| + |
| + if (TextUtils.isEmpty(profile.getFullName())) { |
| + completionStatus = INVALID_RECIPIENT; |
| + invalidFieldsCount++; |
| + } |
| + |
| + if (invalidFieldsCount > 1) { |
| + completionStatus = INVALID_MULTIPLE_FIELDS; |
| + } |
| + |
| + return completionStatus; |
| + } |
| + |
| + /** @return The given autofill profile field. */ |
| + public static String getProfileField(AutofillProfile profile, int field) { |
| + assert profile != null; |
| + switch (field) { |
| + case AddressField.COUNTRY: |
| + return profile.getCountryCode(); |
| + case AddressField.ADMIN_AREA: |
| + return profile.getRegion(); |
| + case AddressField.LOCALITY: |
| + return profile.getLocality(); |
| + case AddressField.DEPENDENT_LOCALITY: |
| + return profile.getDependentLocality(); |
| + case AddressField.SORTING_CODE: |
| + return profile.getSortingCode(); |
| + case AddressField.POSTAL_CODE: |
| + return profile.getPostalCode(); |
| + case AddressField.STREET_ADDRESS: |
| + return profile.getStreetAddress(); |
| + case AddressField.ORGANIZATION: |
| + return profile.getCompanyName(); |
| + case AddressField.RECIPIENT: |
| + return profile.getFullName(); |
| + } |
| + |
| + assert false; |
| + return null; |
| } |
| /** @return The country code to use, e.g., when constructing an editor for this address. */ |