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..f7517311727824b7014e77b3742eff9bc2a97540 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,18 @@ |
package org.chromium.chrome.browser.payments; |
+import android.content.Context; |
+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.util.List; |
import java.util.Locale; |
import java.util.regex.Matcher; |
import java.util.regex.Pattern; |
@@ -31,6 +37,7 @@ public class AutofillAddress extends PaymentOption { |
@Nullable private static Pattern sRegionCodePattern; |
+ private Context mContext; |
private AutofillProfile mProfile; |
@Nullable private Pattern mLanguageScriptCodePattern; |
@@ -39,11 +46,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 +67,87 @@ 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, i.e., can be sent to the merchant as-is without |
+ * editing first. And updates edit message, edit title and complete status. |
+ * |
+ * 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. |
+ */ |
+ private void checkAndUpdateAddressCompleteness() { |
+ int invalidFieldsCount = 0; |
+ int editMessageResId = 0; |
+ int editTitleResId = 0; |
+ |
+ if (!PhoneNumberUtils.isGlobalPhoneNumber( |
+ PhoneNumberUtils.stripSeparators(mProfile.getPhoneNumber().toString()))) { |
+ editMessageResId = R.string.payments_phone_number_required; |
+ editTitleResId = R.string.payments_add_phone_number; |
+ invalidFieldsCount++; |
+ } |
+ |
+ List<Integer> requiredFields = AutofillProfileBridge.getRequiredAddressFields( |
+ AutofillAddress.getCountryCode(mProfile)); |
+ for (int i = 0; i < requiredFields.size(); i++) { |
+ int fieldId = requiredFields.get(i); |
+ if (fieldId == AddressField.RECIPIENT || fieldId == AddressField.COUNTRY) continue; |
+ if (!TextUtils.isEmpty(getProfileField(mProfile, fieldId))) continue; |
+ editMessageResId = R.string.payments_invalid_address; |
+ editTitleResId = R.string.payments_add_valid_address; |
+ invalidFieldsCount++; |
+ break; |
+ } |
+ |
+ if (TextUtils.isEmpty(mProfile.getFullName())) { |
+ editMessageResId = R.string.payments_recipient_required; |
+ editTitleResId = R.string.payments_add_recipient; |
+ invalidFieldsCount++; |
+ } |
+ |
+ if (invalidFieldsCount > 1) { |
+ editMessageResId = R.string.payments_more_information_required; |
+ editTitleResId = R.string.payments_add_more_information; |
+ } |
+ |
+ mEditMessage = editMessageResId == 0 ? null : mContext.getString(editMessageResId); |
+ mEditTitle = editTitleResId == 0 ? null : mContext.getString(editTitleResId); |
+ mIsComplete = mEditMessage == null; |
+ } |
+ |
+ /** @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. */ |