Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/payments/ContactEditor.java

Issue 2413833002: PaymentRequest: Rename ContactInfo to PayerInfo.
Patch Set: test Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.payments;
6
7 import android.telephony.PhoneNumberUtils;
8 import android.util.Patterns;
9
10 import org.chromium.base.Callback;
11 import org.chromium.chrome.R;
12 import org.chromium.chrome.browser.autofill.PersonalDataManager;
13 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.EditorFieldValid ator;
16 import org.chromium.chrome.browser.payments.ui.EditorModel;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import javax.annotation.Nullable;
22
23 /**
24 * Contact information editor.
25 */
26 public class ContactEditor extends EditorBase<AutofillContact> {
27 private final boolean mRequestPayerPhone;
28 private final boolean mRequestPayerEmail;
29 private final Set<CharSequence> mPhoneNumbers;
30 private final Set<CharSequence> mEmailAddresses;
31 @Nullable private EditorFieldValidator mPhoneValidator;
32 @Nullable private EditorFieldValidator mEmailValidator;
33
34 /**
35 * Builds a contact information editor.
36 *
37 * @param requestPayerPhone Whether to request the user's phone number.
38 * @param requestPayerEmail Whether to request the user's email address.
39 */
40 public ContactEditor(boolean requestPayerPhone, boolean requestPayerEmail) {
41 assert requestPayerPhone || requestPayerEmail;
42 mRequestPayerPhone = requestPayerPhone;
43 mRequestPayerEmail = requestPayerEmail;
44 mPhoneNumbers = new HashSet<>();
45 mEmailAddresses = new HashSet<>();
46 }
47
48 /**
49 * Returns whether the following contact information can be sent to the merc hant as-is without
50 * editing first.
51 *
52 * @param phone The phone number to check.
53 * @param email The email address to check.
54 * @return Whether the contact information is complete.
55 */
56 public boolean isContactInformationComplete(@Nullable String phone, @Nullabl e String email) {
57 return (!mRequestPayerPhone || getPhoneValidator().isValid(phone))
58 && (!mRequestPayerEmail || getEmailValidator().isValid(email));
59 }
60
61 /**
62 * Adds the given phone number to the autocomplete set, if it's valid.
63 *
64 * @param phoneNumber The phone number to possibly add.
65 */
66 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) {
67 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum ber);
68 }
69
70 /**
71 * Adds the given email address to the autocomplete set, if it's valid.
72 *
73 * @param emailAddress The email address to possibly add.
74 */
75 public void addEmailAddressIfValid(@Nullable CharSequence emailAddress) {
76 if (getEmailValidator().isValid(emailAddress)) mEmailAddresses.add(email Address);
77 }
78
79 @Override
80 public void edit(@Nullable AutofillContact toEdit, final Callback<AutofillCo ntact> callback) {
81 super.edit(toEdit, callback);
82
83 final AutofillContact contact = toEdit == null
84 ? new AutofillContact(new AutofillProfile(), null, null, false) : toEdit;
85
86 final EditorFieldModel phoneField = mRequestPayerPhone
87 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_PHONE,
88 mContext.getString(R.string.autofill_profile_editor_ph one_number),
89 mPhoneNumbers, getPhoneValidator(),
90 mContext.getString(R.string.payments_field_required_va lidation_message),
91 mContext.getString(R.string.payments_phone_invalid_val idation_message),
92 contact.getPayerPhone())
93 : null;
94
95 final EditorFieldModel emailField = mRequestPayerEmail
96 ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_H INT_EMAIL,
97 mContext.getString(R.string.autofill_profile_editor_em ail_address),
98 mEmailAddresses, getEmailValidator(),
99 mContext.getString(R.string.payments_field_required_va lidation_message),
100 mContext.getString(R.string.payments_email_invalid_val idation_message),
101 contact.getPayerEmail())
102 : null;
103
104 EditorModel editor = new EditorModel(
105 mContext.getString(toEdit == null ? R.string.payments_add_contac t_details_label
106 : R.string.payments_edit_conta ct_details_label));
107 if (phoneField != null) editor.addField(phoneField);
108 if (emailField != null) editor.addField(emailField);
109
110 editor.setCancelCallback(new Runnable() {
111 @Override
112 public void run() {
113 callback.onResult(null);
114 }
115 });
116
117 editor.setDoneCallback(new Runnable() {
118 @Override
119 public void run() {
120 String phone = null;
121 String email = null;
122
123 if (phoneField != null) {
124 phone = phoneField.getValue().toString();
125 contact.getProfile().setPhoneNumber(phone);
126 }
127
128 if (emailField != null) {
129 email = emailField.getValue().toString();
130 contact.getProfile().setEmailAddress(email);
131 }
132
133 String guid = PersonalDataManager.getInstance().setProfile(conta ct.getProfile());
134 contact.completeContact(guid, phone, email);
135 callback.onResult(contact);
136 }
137 });
138
139 mEditorView.show(editor);
140 }
141
142 private EditorFieldValidator getPhoneValidator() {
143 if (mPhoneValidator == null) {
144 mPhoneValidator = new EditorFieldValidator() {
145 @Override
146 public boolean isValid(@Nullable CharSequence value) {
147 return value != null
148 && PhoneNumberUtils.isGlobalPhoneNumber(
149 PhoneNumberUtils.stripSeparators(value.to String()));
150 }
151 };
152 }
153 return mPhoneValidator;
154 }
155
156 private EditorFieldValidator getEmailValidator() {
157 if (mEmailValidator == null) {
158 mEmailValidator = new EditorFieldValidator() {
159 @Override
160 public boolean isValid(@Nullable CharSequence value) {
161 return value != null && Patterns.EMAIL_ADDRESS.matcher(value ).matches();
162 }
163 };
164 }
165 return mEmailValidator;
166 }
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698