OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
6 | 6 |
| 7 import android.content.Context; |
| 8 import android.telephony.PhoneNumberUtils; |
7 import android.text.TextUtils; | 9 import android.text.TextUtils; |
8 | 10 |
| 11 import org.chromium.chrome.R; |
9 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 12 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
10 import org.chromium.chrome.browser.payments.ui.PaymentOption; | 13 import org.chromium.chrome.browser.payments.ui.PaymentOption; |
| 14 import org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge; |
| 15 import org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge.Ad
dressField; |
11 import org.chromium.payments.mojom.PaymentAddress; | 16 import org.chromium.payments.mojom.PaymentAddress; |
12 | 17 |
| 18 import java.util.List; |
13 import java.util.Locale; | 19 import java.util.Locale; |
14 import java.util.regex.Matcher; | 20 import java.util.regex.Matcher; |
15 import java.util.regex.Pattern; | 21 import java.util.regex.Pattern; |
16 | 22 |
17 import javax.annotation.Nullable; | 23 import javax.annotation.Nullable; |
18 | 24 |
19 /** | 25 /** |
20 * The locally stored autofill address. | 26 * The locally stored autofill address. |
21 */ | 27 */ |
22 public class AutofillAddress extends PaymentOption { | 28 public class AutofillAddress extends PaymentOption { |
23 /** The pattern for a valid region code. */ | 29 /** The pattern for a valid region code. */ |
24 private static final String REGION_CODE_PATTERN = "^[A-Z]{2}$"; | 30 private static final String REGION_CODE_PATTERN = "^[A-Z]{2}$"; |
25 | 31 |
26 // Language/script code pattern and capture group numbers. | 32 // Language/script code pattern and capture group numbers. |
27 private static final String LANGUAGE_SCRIPT_CODE_PATTERN = | 33 private static final String LANGUAGE_SCRIPT_CODE_PATTERN = |
28 "^([a-z]{2})(-([A-Z][a-z]{3}))?(-[A-Za-z]+)*$"; | 34 "^([a-z]{2})(-([A-Z][a-z]{3}))?(-[A-Za-z]+)*$"; |
29 private static final int LANGUAGE_CODE_GROUP = 1; | 35 private static final int LANGUAGE_CODE_GROUP = 1; |
30 private static final int SCRIPT_CODE_GROUP = 3; | 36 private static final int SCRIPT_CODE_GROUP = 3; |
31 | 37 |
32 @Nullable private static Pattern sRegionCodePattern; | 38 @Nullable private static Pattern sRegionCodePattern; |
33 | 39 |
| 40 private Context mContext; |
34 private AutofillProfile mProfile; | 41 private AutofillProfile mProfile; |
35 @Nullable private Pattern mLanguageScriptCodePattern; | 42 @Nullable private Pattern mLanguageScriptCodePattern; |
36 | 43 |
37 /** | 44 /** |
38 * Builds the autofill address. | 45 * Builds the autofill address. |
39 * | 46 * |
40 * @param profile The autofill profile containing the address information. | 47 * @param profile The autofill profile containing the address information. |
41 */ | 48 */ |
42 public AutofillAddress(AutofillProfile profile, boolean isComplete) { | 49 public AutofillAddress(Context context, AutofillProfile profile) { |
43 super(profile.getGUID(), profile.getFullName(), profile.getLabel(), | 50 super(profile.getGUID(), profile.getFullName(), profile.getLabel(), |
44 profile.getPhoneNumber(), null); | 51 profile.getPhoneNumber(), null); |
| 52 mContext = context; |
45 mProfile = profile; | 53 mProfile = profile; |
46 mIsComplete = isComplete; | 54 checkAndUpdateAddressCompleteness(); |
47 } | 55 } |
48 | 56 |
49 /** @return The autofill profile where this address data lives. */ | 57 /** @return The autofill profile where this address data lives. */ |
50 public AutofillProfile getProfile() { | 58 public AutofillProfile getProfile() { |
51 return mProfile; | 59 return mProfile; |
52 } | 60 } |
53 | 61 |
54 /** | 62 /** |
55 * Updates the address and marks it "complete." Called after the user has ed
ited this address. | 63 * Updates the address and marks it "complete." Called after the user has ed
ited this address. |
56 * Updates the identifier and labels. | 64 * Updates the identifier and labels. |
57 * | 65 * |
58 * @param profile The new profile to use. | 66 * @param profile The new profile to use. |
59 */ | 67 */ |
60 public void completeAddress(AutofillProfile profile) { | 68 public void completeAddress(AutofillProfile profile) { |
61 mProfile = profile; | 69 mProfile = profile; |
62 mIsComplete = true; | |
63 updateIdentifierAndLabels(mProfile.getGUID(), mProfile.getFullName(), mP
rofile.getLabel(), | 70 updateIdentifierAndLabels(mProfile.getGUID(), mProfile.getFullName(), mP
rofile.getLabel(), |
64 mProfile.getPhoneNumber()); | 71 mProfile.getPhoneNumber()); |
| 72 checkAndUpdateAddressCompleteness(); |
| 73 assert mIsComplete; |
| 74 } |
| 75 |
| 76 /** |
| 77 * Checks whether this address is complete, i.e., can be sent to the merchan
t as-is without |
| 78 * editing first. And updates edit message, edit title and complete status. |
| 79 * |
| 80 * If the country code is not set or invalid, but all fields for the default
locale's country |
| 81 * code are present, then the profile is deemed "complete." AutoflllAddress.
toPaymentAddress() |
| 82 * will use the default locale to fill in a blank country code before sendin
g the address to the |
| 83 * renderer. |
| 84 */ |
| 85 private void checkAndUpdateAddressCompleteness() { |
| 86 int invalidFieldsCount = 0; |
| 87 int editMessageResId = 0; |
| 88 int editTitleResId = 0; |
| 89 |
| 90 if (!PhoneNumberUtils.isGlobalPhoneNumber( |
| 91 PhoneNumberUtils.stripSeparators(mProfile.getPhoneNumber().t
oString()))) { |
| 92 editMessageResId = R.string.payments_phone_number_required; |
| 93 editTitleResId = R.string.payments_add_phone_number; |
| 94 invalidFieldsCount++; |
| 95 } |
| 96 |
| 97 List<Integer> requiredFields = AutofillProfileBridge.getRequiredAddressF
ields( |
| 98 AutofillAddress.getCountryCode(mProfile)); |
| 99 for (int i = 0; i < requiredFields.size(); i++) { |
| 100 int fieldId = requiredFields.get(i); |
| 101 if (fieldId == AddressField.RECIPIENT || fieldId == AddressField.COU
NTRY) continue; |
| 102 if (!TextUtils.isEmpty(getProfileField(mProfile, fieldId))) continue
; |
| 103 editMessageResId = R.string.payments_invalid_address; |
| 104 editTitleResId = R.string.payments_add_valid_address; |
| 105 invalidFieldsCount++; |
| 106 break; |
| 107 } |
| 108 |
| 109 if (TextUtils.isEmpty(mProfile.getFullName())) { |
| 110 editMessageResId = R.string.payments_recipient_required; |
| 111 editTitleResId = R.string.payments_add_recipient; |
| 112 invalidFieldsCount++; |
| 113 } |
| 114 |
| 115 if (invalidFieldsCount > 1) { |
| 116 editMessageResId = R.string.payments_more_information_required; |
| 117 editTitleResId = R.string.payments_add_more_information; |
| 118 } |
| 119 |
| 120 mEditMessage = editMessageResId == 0 ? null : mContext.getString(editMes
sageResId); |
| 121 mEditTitle = editTitleResId == 0 ? null : mContext.getString(editTitleRe
sId); |
| 122 mIsComplete = mEditMessage == null; |
| 123 } |
| 124 |
| 125 /** @return The given autofill profile field. */ |
| 126 public static String getProfileField(AutofillProfile profile, int field) { |
| 127 assert profile != null; |
| 128 switch (field) { |
| 129 case AddressField.COUNTRY: |
| 130 return profile.getCountryCode(); |
| 131 case AddressField.ADMIN_AREA: |
| 132 return profile.getRegion(); |
| 133 case AddressField.LOCALITY: |
| 134 return profile.getLocality(); |
| 135 case AddressField.DEPENDENT_LOCALITY: |
| 136 return profile.getDependentLocality(); |
| 137 case AddressField.SORTING_CODE: |
| 138 return profile.getSortingCode(); |
| 139 case AddressField.POSTAL_CODE: |
| 140 return profile.getPostalCode(); |
| 141 case AddressField.STREET_ADDRESS: |
| 142 return profile.getStreetAddress(); |
| 143 case AddressField.ORGANIZATION: |
| 144 return profile.getCompanyName(); |
| 145 case AddressField.RECIPIENT: |
| 146 return profile.getFullName(); |
| 147 } |
| 148 |
| 149 assert false; |
| 150 return null; |
65 } | 151 } |
66 | 152 |
67 /** @return The country code to use, e.g., when constructing an editor for t
his address. */ | 153 /** @return The country code to use, e.g., when constructing an editor for t
his address. */ |
68 public static String getCountryCode(@Nullable AutofillProfile profile) { | 154 public static String getCountryCode(@Nullable AutofillProfile profile) { |
69 if (sRegionCodePattern == null) sRegionCodePattern = Pattern.compile(REG
ION_CODE_PATTERN); | 155 if (sRegionCodePattern == null) sRegionCodePattern = Pattern.compile(REG
ION_CODE_PATTERN); |
70 | 156 |
71 return profile == null || TextUtils.isEmpty(profile.getCountryCode()) | 157 return profile == null || TextUtils.isEmpty(profile.getCountryCode()) |
72 || !sRegionCodePattern.matcher(profile.getCountryCode())
.matches() | 158 || !sRegionCodePattern.matcher(profile.getCountryCode())
.matches() |
73 ? Locale.getDefault().getCountry() : profile.getCountryCode(); | 159 ? Locale.getDefault().getCountry() : profile.getCountryCode(); |
74 } | 160 } |
(...skipping 28 matching lines...) Expand all Loading... |
103 result.scriptCode = ensureNotNull(matcher.group(SCRIPT_CODE_GROUP)); | 189 result.scriptCode = ensureNotNull(matcher.group(SCRIPT_CODE_GROUP)); |
104 } | 190 } |
105 | 191 |
106 return result; | 192 return result; |
107 } | 193 } |
108 | 194 |
109 private static String ensureNotNull(@Nullable String value) { | 195 private static String ensureNotNull(@Nullable String value) { |
110 return value == null ? "" : value; | 196 return value == null ? "" : value; |
111 } | 197 } |
112 } | 198 } |
OLD | NEW |