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 * Contact information editor. |
25 */ | 25 */ |
26 public class ContactEditor extends EditorBase<AutofillContact> { | 26 public class ContactEditor extends EditorBase<AutofillContact> { |
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; | |
please use gerrit instead
2016/10/13 20:20:33
No need for a fancy format validator for names, be
zino
2016/10/14 19:01:56
Done.
| |
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 contact 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 ContactEditor(boolean requestPayerName, |
41 assert requestPayerPhone || requestPayerEmail; | 45 boolean requestPayerPhone, boolean requestPayerEmail) { |
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 contact information can be sent to the merc hant 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 contact information is complete. |
55 */ | 63 */ |
56 public boolean isContactInformationComplete(@Nullable String phone, @Nullabl e String email) { | 64 public boolean isContactInformationComplete( |
57 return (!mRequestPayerPhone || getPhoneValidator().isValid(phone)) | 65 @Nullable String name, @Nullable String phone, @Nullable String emai l) { |
66 return (!mRequestPayerName || getNameValidator().isValid(name)) | |
please use gerrit instead
2016/10/13 20:20:33
Use the built-in !TextUtils.isEmpty(payerName) ins
zino
2016/10/14 19:01:56
Done.
| |
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); | |
please use gerrit instead
2016/10/13 20:20:33
Use the built-in !TextUtils.isEmpty(payerName) ins
zino
2016/10/14 19:01:56
Done.
| |
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(@Nullable AutofillContact toEdit, final Callback<AutofillCo ntact> callback) { |
81 super.edit(toEdit, callback); | 100 super.edit(toEdit, callback); |
82 | 101 |
83 final AutofillContact contact = toEdit == null | 102 final AutofillContact contact = toEdit == null |
84 ? new AutofillContact(new AutofillProfile(), null, null, false) : toEdit; | 103 ? new AutofillContact(new AutofillProfile(), null, null, null, f alse) : toEdit; |
104 | |
105 final EditorFieldModel nameField = mRequestPayerName | |
106 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_PERSON_NAME, | |
107 mContext.getString(R.string.autofill_profile_editor_na me), | |
108 mPayerNames, getNameValidator(), | |
109 mContext.getString(R.string.payments_field_required_va lidation_message), | |
110 mContext.getString(R.string.payments_name_invalid_vali dation_message), | |
please use gerrit instead
2016/10/13 20:20:33
The only validation for payer names is that it's n
zino
2016/10/14 19:01:56
Done.
| |
111 contact.getPayerName()) | |
112 : null; | |
85 | 113 |
86 final EditorFieldModel phoneField = mRequestPayerPhone | 114 final EditorFieldModel phoneField = mRequestPayerPhone |
87 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_PHONE, | 115 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_PHONE, |
88 mContext.getString(R.string.autofill_profile_editor_ph one_number), | 116 mContext.getString(R.string.autofill_profile_editor_ph one_number), |
89 mPhoneNumbers, getPhoneValidator(), | 117 mPhoneNumbers, getPhoneValidator(), |
90 mContext.getString(R.string.payments_field_required_va lidation_message), | 118 mContext.getString(R.string.payments_field_required_va lidation_message), |
91 mContext.getString(R.string.payments_phone_invalid_val idation_message), | 119 mContext.getString(R.string.payments_phone_invalid_val idation_message), |
92 contact.getPayerPhone()) | 120 contact.getPayerPhone()) |
93 : null; | 121 : null; |
94 | 122 |
95 final EditorFieldModel emailField = mRequestPayerEmail | 123 final EditorFieldModel emailField = mRequestPayerEmail |
96 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_EMAIL, | 124 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_EMAIL, |
97 mContext.getString(R.string.autofill_profile_editor_em ail_address), | 125 mContext.getString(R.string.autofill_profile_editor_em ail_address), |
98 mEmailAddresses, getEmailValidator(), | 126 mEmailAddresses, getEmailValidator(), |
99 mContext.getString(R.string.payments_field_required_va lidation_message), | 127 mContext.getString(R.string.payments_field_required_va lidation_message), |
100 mContext.getString(R.string.payments_email_invalid_val idation_message), | 128 mContext.getString(R.string.payments_email_invalid_val idation_message), |
101 contact.getPayerEmail()) | 129 contact.getPayerEmail()) |
102 : null; | 130 : null; |
103 | 131 |
104 EditorModel editor = new EditorModel( | 132 EditorModel editor = new EditorModel( |
105 mContext.getString(toEdit == null ? R.string.payments_add_contac t_details_label | 133 mContext.getString(toEdit == null ? R.string.payments_add_contac t_details_label |
106 : R.string.payments_edit_conta ct_details_label)); | 134 : R.string.payments_edit_conta ct_details_label)); |
135 if (nameField != null) editor.addField(nameField); | |
107 if (phoneField != null) editor.addField(phoneField); | 136 if (phoneField != null) editor.addField(phoneField); |
108 if (emailField != null) editor.addField(emailField); | 137 if (emailField != null) editor.addField(emailField); |
109 | 138 |
110 editor.setCancelCallback(new Runnable() { | 139 editor.setCancelCallback(new Runnable() { |
111 @Override | 140 @Override |
112 public void run() { | 141 public void run() { |
113 callback.onResult(null); | 142 callback.onResult(null); |
114 } | 143 } |
115 }); | 144 }); |
116 | 145 |
117 editor.setDoneCallback(new Runnable() { | 146 editor.setDoneCallback(new Runnable() { |
118 @Override | 147 @Override |
119 public void run() { | 148 public void run() { |
149 String name = null; | |
120 String phone = null; | 150 String phone = null; |
121 String email = null; | 151 String email = null; |
122 | 152 |
153 if (nameField != null) { | |
154 name = nameField.getValue().toString(); | |
155 contact.getProfile().setFullName(name); | |
156 } | |
157 | |
123 if (phoneField != null) { | 158 if (phoneField != null) { |
124 phone = phoneField.getValue().toString(); | 159 phone = phoneField.getValue().toString(); |
125 contact.getProfile().setPhoneNumber(phone); | 160 contact.getProfile().setPhoneNumber(phone); |
126 } | 161 } |
127 | 162 |
128 if (emailField != null) { | 163 if (emailField != null) { |
129 email = emailField.getValue().toString(); | 164 email = emailField.getValue().toString(); |
130 contact.getProfile().setEmailAddress(email); | 165 contact.getProfile().setEmailAddress(email); |
131 } | 166 } |
132 | 167 |
133 String guid = PersonalDataManager.getInstance().setProfile(conta ct.getProfile()); | 168 String guid = PersonalDataManager.getInstance().setProfile(conta ct.getProfile()); |
134 contact.completeContact(guid, phone, email); | 169 contact.completeContact(guid, name, phone, email); |
135 callback.onResult(contact); | 170 callback.onResult(contact); |
136 } | 171 } |
137 }); | 172 }); |
138 | 173 |
139 mEditorView.show(editor); | 174 mEditorView.show(editor); |
140 } | 175 } |
141 | 176 |
177 private EditorFieldValidator getNameValidator() { | |
178 if (mNameValidator == null) { | |
179 mNameValidator = new EditorFieldValidator() { | |
180 @Override | |
181 public boolean isValid(@Nullable CharSequence value) { | |
182 return value != null; | |
183 } | |
184 }; | |
185 } | |
186 return mNameValidator; | |
187 } | |
please use gerrit instead
2016/10/13 20:20:33
There's no need for an EditorFieldValidator for na
zino
2016/10/14 19:01:56
Done.
| |
188 | |
142 private EditorFieldValidator getPhoneValidator() { | 189 private EditorFieldValidator getPhoneValidator() { |
143 if (mPhoneValidator == null) { | 190 if (mPhoneValidator == null) { |
144 mPhoneValidator = new EditorFieldValidator() { | 191 mPhoneValidator = new EditorFieldValidator() { |
145 @Override | 192 @Override |
146 public boolean isValid(@Nullable CharSequence value) { | 193 public boolean isValid(@Nullable CharSequence value) { |
147 return value != null | 194 return value != null |
148 && PhoneNumberUtils.isGlobalPhoneNumber( | 195 && PhoneNumberUtils.isGlobalPhoneNumber( |
149 PhoneNumberUtils.stripSeparators(value.to String())); | 196 PhoneNumberUtils.stripSeparators(value.to String())); |
150 } | 197 } |
151 }; | 198 }; |
152 } | 199 } |
153 return mPhoneValidator; | 200 return mPhoneValidator; |
154 } | 201 } |
155 | 202 |
156 private EditorFieldValidator getEmailValidator() { | 203 private EditorFieldValidator getEmailValidator() { |
157 if (mEmailValidator == null) { | 204 if (mEmailValidator == null) { |
158 mEmailValidator = new EditorFieldValidator() { | 205 mEmailValidator = new EditorFieldValidator() { |
159 @Override | 206 @Override |
160 public boolean isValid(@Nullable CharSequence value) { | 207 public boolean isValid(@Nullable CharSequence value) { |
161 return value != null && Patterns.EMAIL_ADDRESS.matcher(value ).matches(); | 208 return value != null && Patterns.EMAIL_ADDRESS.matcher(value ).matches(); |
162 } | 209 } |
163 }; | 210 }; |
164 } | 211 } |
165 return mEmailValidator; | 212 return mEmailValidator; |
166 } | 213 } |
167 } | 214 } |
OLD | NEW |