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.text.TextUtils; |
8 import android.util.Patterns; | 9 import android.util.Patterns; |
9 | 10 |
10 import org.chromium.base.Callback; | 11 import org.chromium.base.Callback; |
11 import org.chromium.chrome.R; | 12 import org.chromium.chrome.R; |
12 import org.chromium.chrome.browser.autofill.PersonalDataManager; | 13 import org.chromium.chrome.browser.autofill.PersonalDataManager; |
13 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 14 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
14 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; | 15 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; |
15 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; | 16 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; |
16 import org.chromium.chrome.browser.payments.ui.EditorModel; | 17 import org.chromium.chrome.browser.payments.ui.EditorModel; |
17 | 18 |
18 import java.util.HashSet; | 19 import java.util.HashSet; |
19 import java.util.Set; | 20 import java.util.Set; |
20 | 21 |
21 import javax.annotation.Nullable; | 22 import javax.annotation.Nullable; |
22 | 23 |
23 /** | 24 /** |
24 * Contact information editor. | 25 * Contact information editor. |
25 */ | 26 */ |
26 public class ContactEditor extends EditorBase<AutofillContact> { | 27 public class ContactEditor extends EditorBase<AutofillContact> { |
| 28 private final boolean mRequestPayerName; |
27 private final boolean mRequestPayerPhone; | 29 private final boolean mRequestPayerPhone; |
28 private final boolean mRequestPayerEmail; | 30 private final boolean mRequestPayerEmail; |
| 31 private final Set<CharSequence> mPayerNames; |
29 private final Set<CharSequence> mPhoneNumbers; | 32 private final Set<CharSequence> mPhoneNumbers; |
30 private final Set<CharSequence> mEmailAddresses; | 33 private final Set<CharSequence> mEmailAddresses; |
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 || !TextUtils.isEmpty(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 (!TextUtils.isEmpty(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(@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.payments_name_field_in_con
tact_details), |
| 108 mPayerNames, null, |
| 109 mContext.getString(R.string.payments_field_required_va
lidation_message), |
| 110 null, contact.getPayerName()) |
| 111 : null; |
85 | 112 |
86 final EditorFieldModel phoneField = mRequestPayerPhone | 113 final EditorFieldModel phoneField = mRequestPayerPhone |
87 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_PHONE, | 114 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_PHONE, |
88 mContext.getString(R.string.autofill_profile_editor_ph
one_number), | 115 mContext.getString(R.string.autofill_profile_editor_ph
one_number), |
89 mPhoneNumbers, getPhoneValidator(), | 116 mPhoneNumbers, getPhoneValidator(), |
90 mContext.getString(R.string.payments_field_required_va
lidation_message), | 117 mContext.getString(R.string.payments_field_required_va
lidation_message), |
91 mContext.getString(R.string.payments_phone_invalid_val
idation_message), | 118 mContext.getString(R.string.payments_phone_invalid_val
idation_message), |
92 contact.getPayerPhone()) | 119 contact.getPayerPhone()) |
93 : null; | 120 : null; |
94 | 121 |
95 final EditorFieldModel emailField = mRequestPayerEmail | 122 final EditorFieldModel emailField = mRequestPayerEmail |
96 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_EMAIL, | 123 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H
INT_EMAIL, |
97 mContext.getString(R.string.autofill_profile_editor_em
ail_address), | 124 mContext.getString(R.string.autofill_profile_editor_em
ail_address), |
98 mEmailAddresses, getEmailValidator(), | 125 mEmailAddresses, getEmailValidator(), |
99 mContext.getString(R.string.payments_field_required_va
lidation_message), | 126 mContext.getString(R.string.payments_field_required_va
lidation_message), |
100 mContext.getString(R.string.payments_email_invalid_val
idation_message), | 127 mContext.getString(R.string.payments_email_invalid_val
idation_message), |
101 contact.getPayerEmail()) | 128 contact.getPayerEmail()) |
102 : null; | 129 : null; |
103 | 130 |
104 EditorModel editor = new EditorModel( | 131 EditorModel editor = new EditorModel( |
105 mContext.getString(toEdit == null ? R.string.payments_add_contac
t_details_label | 132 mContext.getString(toEdit == null ? R.string.payments_add_contac
t_details_label |
106 : R.string.payments_edit_conta
ct_details_label)); | 133 : R.string.payments_edit_conta
ct_details_label)); |
| 134 if (nameField != null) editor.addField(nameField); |
107 if (phoneField != null) editor.addField(phoneField); | 135 if (phoneField != null) editor.addField(phoneField); |
108 if (emailField != null) editor.addField(emailField); | 136 if (emailField != null) editor.addField(emailField); |
109 | 137 |
110 editor.setCancelCallback(new Runnable() { | 138 editor.setCancelCallback(new Runnable() { |
111 @Override | 139 @Override |
112 public void run() { | 140 public void run() { |
113 callback.onResult(null); | 141 callback.onResult(null); |
114 } | 142 } |
115 }); | 143 }); |
116 | 144 |
117 editor.setDoneCallback(new Runnable() { | 145 editor.setDoneCallback(new Runnable() { |
118 @Override | 146 @Override |
119 public void run() { | 147 public void run() { |
| 148 String name = null; |
120 String phone = null; | 149 String phone = null; |
121 String email = null; | 150 String email = null; |
122 | 151 |
| 152 if (nameField != null) { |
| 153 name = nameField.getValue().toString(); |
| 154 contact.getProfile().setFullName(name); |
| 155 } |
| 156 |
123 if (phoneField != null) { | 157 if (phoneField != null) { |
124 phone = phoneField.getValue().toString(); | 158 phone = phoneField.getValue().toString(); |
125 contact.getProfile().setPhoneNumber(phone); | 159 contact.getProfile().setPhoneNumber(phone); |
126 } | 160 } |
127 | 161 |
128 if (emailField != null) { | 162 if (emailField != null) { |
129 email = emailField.getValue().toString(); | 163 email = emailField.getValue().toString(); |
130 contact.getProfile().setEmailAddress(email); | 164 contact.getProfile().setEmailAddress(email); |
131 } | 165 } |
132 | 166 |
133 String guid = PersonalDataManager.getInstance().setProfile(conta
ct.getProfile()); | 167 String guid = PersonalDataManager.getInstance().setProfile(conta
ct.getProfile()); |
134 contact.completeContact(guid, phone, email); | 168 contact.completeContact(guid, name, phone, email); |
135 callback.onResult(contact); | 169 callback.onResult(contact); |
136 } | 170 } |
137 }); | 171 }); |
138 | 172 |
139 mEditorView.show(editor); | 173 mEditorView.show(editor); |
140 } | 174 } |
141 | 175 |
142 private EditorFieldValidator getPhoneValidator() { | 176 private EditorFieldValidator getPhoneValidator() { |
143 if (mPhoneValidator == null) { | 177 if (mPhoneValidator == null) { |
144 mPhoneValidator = new EditorFieldValidator() { | 178 mPhoneValidator = new EditorFieldValidator() { |
(...skipping 13 matching lines...) Expand all Loading... |
158 mEmailValidator = new EditorFieldValidator() { | 192 mEmailValidator = new EditorFieldValidator() { |
159 @Override | 193 @Override |
160 public boolean isValid(@Nullable CharSequence value) { | 194 public boolean isValid(@Nullable CharSequence value) { |
161 return value != null && Patterns.EMAIL_ADDRESS.matcher(value
).matches(); | 195 return value != null && Patterns.EMAIL_ADDRESS.matcher(value
).matches(); |
162 } | 196 } |
163 }; | 197 }; |
164 } | 198 } |
165 return mEmailValidator; | 199 return mEmailValidator; |
166 } | 200 } |
167 } | 201 } |
OLD | NEW |