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.telephony.PhoneNumberUtils; | 7 import android.telephony.PhoneNumberUtils; |
8 import android.util.Patterns; | 8 import android.util.Patterns; |
9 | 9 |
10 import org.chromium.base.Callback; | 10 import org.chromium.base.Callback; |
11 import org.chromium.chrome.R; | 11 import org.chromium.chrome.R; |
12 import org.chromium.chrome.browser.autofill.PersonalDataManager; | 12 import org.chromium.chrome.browser.autofill.PersonalDataManager; |
13 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 13 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
14 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; | 14 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; |
15 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; | 15 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; |
16 import org.chromium.chrome.browser.payments.ui.EditorModel; | 16 import org.chromium.chrome.browser.payments.ui.EditorModel; |
17 | 17 |
18 import java.util.HashSet; | 18 import java.util.HashSet; |
19 import java.util.Set; | 19 import java.util.Set; |
20 | 20 |
21 import javax.annotation.Nullable; | 21 import javax.annotation.Nullable; |
22 | 22 |
23 /** | 23 /** |
24 * Contact information editor. | 24 * Payer information editor. |
25 */ | 25 */ |
26 public class ContactEditor extends EditorBase<AutofillContact> { | 26 public class PayerInfoEditor extends EditorBase<AutofillPayerInfo> { |
| 27 private final boolean mRequestPayerName; |
27 private final boolean mRequestPayerPhone; | 28 private final boolean mRequestPayerPhone; |
28 private final boolean mRequestPayerEmail; | 29 private final boolean mRequestPayerEmail; |
| 30 private final Set<CharSequence> mPayerNames; |
29 private final Set<CharSequence> mPhoneNumbers; | 31 private final Set<CharSequence> mPhoneNumbers; |
30 private final Set<CharSequence> mEmailAddresses; | 32 private final Set<CharSequence> mEmailAddresses; |
| 33 @Nullable private EditorFieldValidator mNameValidator; |
31 @Nullable private EditorFieldValidator mPhoneValidator; | 34 @Nullable private EditorFieldValidator mPhoneValidator; |
32 @Nullable private EditorFieldValidator mEmailValidator; | 35 @Nullable private EditorFieldValidator mEmailValidator; |
33 | 36 |
34 /** | 37 /** |
35 * Builds a contact information editor. | 38 * Builds a payer information editor. |
36 * | 39 * |
| 40 * @param requestPayerName Whether to request the user's name. |
37 * @param requestPayerPhone Whether to request the user's phone number. | 41 * @param requestPayerPhone Whether to request the user's phone number. |
38 * @param requestPayerEmail Whether to request the user's email address. | 42 * @param requestPayerEmail Whether to request the user's email address. |
39 */ | 43 */ |
40 public ContactEditor(boolean requestPayerPhone, boolean requestPayerEmail) { | 44 public PayerInfoEditor( |
41 assert requestPayerPhone || requestPayerEmail; | 45 boolean requestPayerName, boolean requestPayerPhone, boolean request
PayerEmail) { |
| 46 assert requestPayerName || requestPayerPhone || requestPayerEmail; |
| 47 mRequestPayerName = requestPayerName; |
42 mRequestPayerPhone = requestPayerPhone; | 48 mRequestPayerPhone = requestPayerPhone; |
43 mRequestPayerEmail = requestPayerEmail; | 49 mRequestPayerEmail = requestPayerEmail; |
| 50 mPayerNames = new HashSet<>(); |
44 mPhoneNumbers = new HashSet<>(); | 51 mPhoneNumbers = new HashSet<>(); |
45 mEmailAddresses = new HashSet<>(); | 52 mEmailAddresses = new HashSet<>(); |
46 } | 53 } |
47 | 54 |
48 /** | 55 /** |
49 * Returns whether the following contact information can be sent to the merc
hant as-is without | 56 * Returns whether the following payer information can be sent to the mercha
nt as-is without |
50 * editing first. | 57 * editing first. |
51 * | 58 * |
| 59 * @param name The payer name to check. |
52 * @param phone The phone number to check. | 60 * @param phone The phone number to check. |
53 * @param email The email address to check. | 61 * @param email The email address to check. |
54 * @return Whether the contact information is complete. | 62 * @return Whether the payer information is complete. |
55 */ | 63 */ |
56 public boolean isContactInformationComplete(@Nullable String phone, @Nullabl
e String email) { | 64 public boolean isPayerInformationComplete( |
57 return (!mRequestPayerPhone || getPhoneValidator().isValid(phone)) | 65 @Nullable String name, @Nullable String phone, @Nullable String emai
l) { |
| 66 return (!mRequestPayerName || getNameValidator().isValid(name)) |
| 67 && (!mRequestPayerPhone || getPhoneValidator().isValid(phone)) |
58 && (!mRequestPayerEmail || getEmailValidator().isValid(email)); | 68 && (!mRequestPayerEmail || getEmailValidator().isValid(email)); |
59 } | 69 } |
60 | 70 |
61 /** | 71 /** |
| 72 * Adds the given payer name to the autocomplete set, if it's valid. |
| 73 * |
| 74 * @param payerName The payer name to possibly add. |
| 75 */ |
| 76 public void addPayerNameIfValid(@Nullable CharSequence payerName) { |
| 77 if (getNameValidator().isValid(payerName)) mPayerNames.add(payerName); |
| 78 } |
| 79 |
| 80 /** |
62 * Adds the given phone number to the autocomplete set, if it's valid. | 81 * Adds the given phone number to the autocomplete set, if it's valid. |
63 * | 82 * |
64 * @param phoneNumber The phone number to possibly add. | 83 * @param phoneNumber The phone number to possibly add. |
65 */ | 84 */ |
66 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { | 85 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { |
67 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum
ber); | 86 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum
ber); |
68 } | 87 } |
69 | 88 |
70 /** | 89 /** |
71 * Adds the given email address to the autocomplete set, if it's valid. | 90 * Adds the given email address to the autocomplete set, if it's valid. |
72 * | 91 * |
73 * @param emailAddress The email address to possibly add. | 92 * @param emailAddress The email address to possibly add. |
74 */ | 93 */ |
75 public void addEmailAddressIfValid(@Nullable CharSequence emailAddress) { | 94 public void addEmailAddressIfValid(@Nullable CharSequence emailAddress) { |
76 if (getEmailValidator().isValid(emailAddress)) mEmailAddresses.add(email
Address); | 95 if (getEmailValidator().isValid(emailAddress)) mEmailAddresses.add(email
Address); |
77 } | 96 } |
78 | 97 |
79 @Override | 98 @Override |
80 public void edit(@Nullable AutofillContact toEdit, final Callback<AutofillCo
ntact> callback) { | 99 public void edit( |
| 100 @Nullable AutofillPayerInfo toEdit, final Callback<AutofillPayerInfo
> callback) { |
81 super.edit(toEdit, callback); | 101 super.edit(toEdit, callback); |
82 | 102 |
83 final AutofillContact contact = toEdit == null | 103 final AutofillPayerInfo payerInfo = toEdit == null |
84 ? new AutofillContact(new AutofillProfile(), null, null, false)
: toEdit; | 104 ? new AutofillPayerInfo(new AutofillProfile(), null, null, null,
false) : toEdit; |
| 105 |
| 106 final EditorFieldModel nameField = mRequestPayerName |
| 107 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_PERSON_NAME, |
| 108 mContext.getString(R.string.autofill_profile_editor_na
me), |
| 109 mPayerNames, getNameValidator(), |
| 110 mContext.getString(R.string.payments_field_required_va
lidation_message), |
| 111 mContext.getString(R.string.payments_name_invalid_vali
dation_message), |
| 112 payerInfo.getPayerName()) |
| 113 : null; |
85 | 114 |
86 final EditorFieldModel phoneField = mRequestPayerPhone | 115 final EditorFieldModel phoneField = mRequestPayerPhone |
87 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_PHONE, | 116 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_PHONE, |
88 mContext.getString(R.string.autofill_profile_editor_ph
one_number), | 117 mContext.getString(R.string.autofill_profile_editor_ph
one_number), |
89 mPhoneNumbers, getPhoneValidator(), | 118 mPhoneNumbers, getPhoneValidator(), |
90 mContext.getString(R.string.payments_field_required_va
lidation_message), | 119 mContext.getString(R.string.payments_field_required_va
lidation_message), |
91 mContext.getString(R.string.payments_phone_invalid_val
idation_message), | 120 mContext.getString(R.string.payments_phone_invalid_val
idation_message), |
92 contact.getPayerPhone()) | 121 payerInfo.getPayerPhone()) |
93 : null; | 122 : null; |
94 | 123 |
95 final EditorFieldModel emailField = mRequestPayerEmail | 124 final EditorFieldModel emailField = mRequestPayerEmail |
96 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_EMAIL, | 125 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_EMAIL, |
97 mContext.getString(R.string.autofill_profile_editor_em
ail_address), | 126 mContext.getString(R.string.autofill_profile_editor_em
ail_address), |
98 mEmailAddresses, getEmailValidator(), | 127 mEmailAddresses, getEmailValidator(), |
99 mContext.getString(R.string.payments_field_required_va
lidation_message), | 128 mContext.getString(R.string.payments_field_required_va
lidation_message), |
100 mContext.getString(R.string.payments_email_invalid_val
idation_message), | 129 mContext.getString(R.string.payments_email_invalid_val
idation_message), |
101 contact.getPayerEmail()) | 130 payerInfo.getPayerEmail()) |
102 : null; | 131 : null; |
103 | 132 |
104 EditorModel editor = new EditorModel( | 133 EditorModel editor = new EditorModel( |
105 mContext.getString(toEdit == null ? R.string.payments_add_contac
t_details_label | 134 mContext.getString(toEdit == null ? R.string.payments_add_contac
t_details_label |
106 : R.string.payments_edit_conta
ct_details_label)); | 135 : R.string.payments_edit_conta
ct_details_label)); |
| 136 if (nameField != null) editor.addField(nameField); |
107 if (phoneField != null) editor.addField(phoneField); | 137 if (phoneField != null) editor.addField(phoneField); |
108 if (emailField != null) editor.addField(emailField); | 138 if (emailField != null) editor.addField(emailField); |
109 | 139 |
110 editor.setCancelCallback(new Runnable() { | 140 editor.setCancelCallback(new Runnable() { |
111 @Override | 141 @Override |
112 public void run() { | 142 public void run() { |
113 callback.onResult(null); | 143 callback.onResult(null); |
114 } | 144 } |
115 }); | 145 }); |
116 | 146 |
117 editor.setDoneCallback(new Runnable() { | 147 editor.setDoneCallback(new Runnable() { |
118 @Override | 148 @Override |
119 public void run() { | 149 public void run() { |
| 150 String name = null; |
120 String phone = null; | 151 String phone = null; |
121 String email = null; | 152 String email = null; |
122 | 153 |
| 154 if (nameField != null) { |
| 155 name = nameField.getValue().toString(); |
| 156 payerInfo.getProfile().setFullName(name); |
| 157 } |
| 158 |
123 if (phoneField != null) { | 159 if (phoneField != null) { |
124 phone = phoneField.getValue().toString(); | 160 phone = phoneField.getValue().toString(); |
125 contact.getProfile().setPhoneNumber(phone); | 161 payerInfo.getProfile().setPhoneNumber(phone); |
126 } | 162 } |
127 | 163 |
128 if (emailField != null) { | 164 if (emailField != null) { |
129 email = emailField.getValue().toString(); | 165 email = emailField.getValue().toString(); |
130 contact.getProfile().setEmailAddress(email); | 166 payerInfo.getProfile().setEmailAddress(email); |
131 } | 167 } |
132 | 168 |
133 String guid = PersonalDataManager.getInstance().setProfile(conta
ct.getProfile()); | 169 String guid = PersonalDataManager.getInstance().setProfile(payer
Info.getProfile()); |
134 contact.completeContact(guid, phone, email); | 170 payerInfo.completePayerInfo(guid, name, phone, email); |
135 callback.onResult(contact); | 171 callback.onResult(payerInfo); |
136 } | 172 } |
137 }); | 173 }); |
138 | 174 |
139 mEditorView.show(editor); | 175 mEditorView.show(editor); |
140 } | 176 } |
141 | 177 |
| 178 private EditorFieldValidator getNameValidator() { |
| 179 if (mNameValidator == null) { |
| 180 mNameValidator = new EditorFieldValidator() { |
| 181 @Override |
| 182 public boolean isValid(@Nullable CharSequence value) { |
| 183 return value != null; |
| 184 } |
| 185 }; |
| 186 } |
| 187 return mNameValidator; |
| 188 } |
| 189 |
142 private EditorFieldValidator getPhoneValidator() { | 190 private EditorFieldValidator getPhoneValidator() { |
143 if (mPhoneValidator == null) { | 191 if (mPhoneValidator == null) { |
144 mPhoneValidator = new EditorFieldValidator() { | 192 mPhoneValidator = new EditorFieldValidator() { |
145 @Override | 193 @Override |
146 public boolean isValid(@Nullable CharSequence value) { | 194 public boolean isValid(@Nullable CharSequence value) { |
147 return value != null | 195 return value != null |
148 && PhoneNumberUtils.isGlobalPhoneNumber( | 196 && PhoneNumberUtils.isGlobalPhoneNumber( |
149 PhoneNumberUtils.stripSeparators(value.to
String())); | 197 PhoneNumberUtils.stripSeparators(value.to
String())); |
150 } | 198 } |
151 }; | 199 }; |
152 } | 200 } |
153 return mPhoneValidator; | 201 return mPhoneValidator; |
154 } | 202 } |
155 | 203 |
156 private EditorFieldValidator getEmailValidator() { | 204 private EditorFieldValidator getEmailValidator() { |
157 if (mEmailValidator == null) { | 205 if (mEmailValidator == null) { |
158 mEmailValidator = new EditorFieldValidator() { | 206 mEmailValidator = new EditorFieldValidator() { |
159 @Override | 207 @Override |
160 public boolean isValid(@Nullable CharSequence value) { | 208 public boolean isValid(@Nullable CharSequence value) { |
161 return value != null && Patterns.EMAIL_ADDRESS.matcher(value
).matches(); | 209 return value != null && Patterns.EMAIL_ADDRESS.matcher(value
).matches(); |
162 } | 210 } |
163 }; | 211 }; |
164 } | 212 } |
165 return mEmailValidator; | 213 return mEmailValidator; |
166 } | 214 } |
167 } | 215 } |
OLD | NEW |