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 and updates edit message, edit ti tle and complete | |
78 * status. | |
79 */ | |
80 private void checkAndUpdateAddressCompleteness() { | |
81 int[] editMessageResIds = getInformationRequiredMessageResIdsForEdit(mPr ofile); | |
82 | |
83 mEditMessage = editMessageResIds == null ? null : mContext.getString(edi tMessageResIds[0]); | |
84 mEditTitle = editMessageResIds == null ? null : mContext.getString(editM essageResIds[1]); | |
85 mIsComplete = mEditMessage == null; | |
86 } | |
87 | |
88 /** | |
89 * Gets information required message resource Ids for edit if the profile do es not contain | |
90 * complete address information, i.e., can not be sent to the merchant as-is without editing | |
91 * first. Otherwise returns null. | |
92 * | |
93 * If the country code is not set or invalid, but all fields for the default locale's country | |
94 * code are present, then the profile is deemed "complete." AutoflllAddress. toPaymentAddress() | |
95 * will use the default locale to fill in a blank country code before sendin g the address to the | |
96 * renderer. | |
97 * | |
98 * @param profile The autofill profile containing the address information. | |
99 * @return int[] The information required message resource Ids for edit. in t[0] contains | |
100 * required information description message resource Id. int[ 1] contains message | |
101 * resource Id for editor title. | |
102 * Returns null if the contained address information in the p rofile is complete. | |
103 */ | |
104 @Nullable | |
105 public static int[] getInformationRequiredMessageResIdsForEdit(AutofillProfi le profile) { | |
Ted C
2016/11/08 22:14:06
don't want to derail this too much, but I find thi
gogerald1
2016/11/09 01:42:46
Done. Yes, a little bit, was more worried about ti
| |
106 int invalidFieldsCount = 0; | |
107 int editMessageResId = 0; | |
108 int editTitleResId = 0; | |
109 | |
110 if (!PhoneNumberUtils.isGlobalPhoneNumber( | |
111 PhoneNumberUtils.stripSeparators(profile.getPhoneNumber().to String()))) { | |
112 editMessageResId = R.string.payments_phone_number_required; | |
113 editTitleResId = R.string.payments_add_phone_number; | |
114 invalidFieldsCount++; | |
115 } | |
116 | |
117 List<Integer> requiredFields = AutofillProfileBridge.getRequiredAddressF ields( | |
118 AutofillAddress.getCountryCode(profile)); | |
119 for (int fieldId : requiredFields) { | |
120 if (fieldId == AddressField.RECIPIENT || fieldId == AddressField.COU NTRY) continue; | |
121 if (!TextUtils.isEmpty(getProfileField(profile, fieldId))) continue; | |
122 editMessageResId = R.string.payments_invalid_address; | |
123 editTitleResId = R.string.payments_add_valid_address; | |
124 invalidFieldsCount++; | |
125 break; | |
126 } | |
127 | |
128 if (TextUtils.isEmpty(profile.getFullName())) { | |
129 editMessageResId = R.string.payments_recipient_required; | |
130 editTitleResId = R.string.payments_add_recipient; | |
131 invalidFieldsCount++; | |
132 } | |
133 | |
134 if (invalidFieldsCount > 1) { | |
135 editMessageResId = R.string.payments_more_information_required; | |
136 editTitleResId = R.string.payments_add_more_information; | |
137 } | |
138 | |
139 return editMessageResId == 0 ? null : new int[] {editMessageResId, editT itleResId}; | |
Ted C
2016/11/08 22:14:06
not really important here, but I think return a Pa
gogerald1
2016/11/09 01:42:46
not applicable now, but why it would be more time
please use gerrit instead
2016/11/09 17:04:52
I think Ted meant that Pair is not as memory effic
gogerald1
2016/11/09 17:06:03
Ah, I think I misunderstand this "low call rate" a
| |
140 } | |
141 | |
142 /** @return The given autofill profile field. */ | |
143 public static String getProfileField(AutofillProfile profile, int field) { | |
144 assert profile != null; | |
145 switch (field) { | |
146 case AddressField.COUNTRY: | |
147 return profile.getCountryCode(); | |
148 case AddressField.ADMIN_AREA: | |
149 return profile.getRegion(); | |
150 case AddressField.LOCALITY: | |
151 return profile.getLocality(); | |
152 case AddressField.DEPENDENT_LOCALITY: | |
153 return profile.getDependentLocality(); | |
154 case AddressField.SORTING_CODE: | |
155 return profile.getSortingCode(); | |
156 case AddressField.POSTAL_CODE: | |
157 return profile.getPostalCode(); | |
158 case AddressField.STREET_ADDRESS: | |
159 return profile.getStreetAddress(); | |
160 case AddressField.ORGANIZATION: | |
161 return profile.getCompanyName(); | |
162 case AddressField.RECIPIENT: | |
163 return profile.getFullName(); | |
164 } | |
165 | |
166 assert false; | |
167 return null; | |
65 } | 168 } |
66 | 169 |
67 /** @return The country code to use, e.g., when constructing an editor for t his address. */ | 170 /** @return The country code to use, e.g., when constructing an editor for t his address. */ |
68 public static String getCountryCode(@Nullable AutofillProfile profile) { | 171 public static String getCountryCode(@Nullable AutofillProfile profile) { |
69 if (sRegionCodePattern == null) sRegionCodePattern = Pattern.compile(REG ION_CODE_PATTERN); | 172 if (sRegionCodePattern == null) sRegionCodePattern = Pattern.compile(REG ION_CODE_PATTERN); |
70 | 173 |
71 return profile == null || TextUtils.isEmpty(profile.getCountryCode()) | 174 return profile == null || TextUtils.isEmpty(profile.getCountryCode()) |
72 || !sRegionCodePattern.matcher(profile.getCountryCode()) .matches() | 175 || !sRegionCodePattern.matcher(profile.getCountryCode()) .matches() |
73 ? Locale.getDefault().getCountry() : profile.getCountryCode(); | 176 ? Locale.getDefault().getCountry() : profile.getCountryCode(); |
74 } | 177 } |
(...skipping 28 matching lines...) Expand all Loading... | |
103 result.scriptCode = ensureNotNull(matcher.group(SCRIPT_CODE_GROUP)); | 206 result.scriptCode = ensureNotNull(matcher.group(SCRIPT_CODE_GROUP)); |
104 } | 207 } |
105 | 208 |
106 return result; | 209 return result; |
107 } | 210 } |
108 | 211 |
109 private static String ensureNotNull(@Nullable String value) { | 212 private static String ensureNotNull(@Nullable String value) { |
110 return value == null ? "" : value; | 213 return value == null ? "" : value; |
111 } | 214 } |
112 } | 215 } |
OLD | NEW |