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

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

Issue 2109573002: Revert of Refactor contact editor controller into its own class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.content.Context;
8 import android.telephony.PhoneNumberUtils;
9 import android.util.Patterns;
10
11 import org.chromium.base.Callback;
12 import org.chromium.chrome.R;
13 import org.chromium.chrome.browser.autofill.PersonalDataManager;
14 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
15 import org.chromium.chrome.browser.payments.ui.EditorFieldModel;
16 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid ator;
17 import org.chromium.chrome.browser.payments.ui.EditorModel;
18 import org.chromium.chrome.browser.payments.ui.EditorView;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.annotation.Nullable;
24
25 /**
26 * Contact information editor.
27 */
28 public class ContactEditor {
29 private final boolean mRequestPayerPhone;
30 private final boolean mRequestPayerEmail;
31 private final List<CharSequence> mPhoneNumbers;
32 private final List<CharSequence> mEmailAddresses;
33 @Nullable private EditorView mEditorView;
34 @Nullable private Context mContext;
35 @Nullable private EditorFieldValidator mPhoneValidator;
36 @Nullable private EditorFieldValidator mEmailValidator;
37
38 /**
39 * Builds a contact information editor.
40 *
41 * @param requestPayerPhone Whether to request the user's phone number.
42 * @param requestPayerEmail Whether to request the user's email address.
43 */
44 public ContactEditor(boolean requestPayerPhone, boolean requestPayerEmail) {
45 assert requestPayerPhone || requestPayerEmail;
46 mRequestPayerPhone = requestPayerPhone;
47 mRequestPayerEmail = requestPayerEmail;
48 mPhoneNumbers = new ArrayList<>();
49 mEmailAddresses = new ArrayList<>();
50 }
51
52 /**
53 * Returns whether the following contact information can be sent to the merc hant as-is without
54 * editing first.
55 *
56 * @param phone The phone number to check.
57 * @param email The email address to check.
58 * @return Whether the contact information is complete.
59 */
60 public boolean isContactInformationComplete(@Nullable String phone, @Nullabl e String email) {
61 return (!mRequestPayerPhone || getPhoneValidator().isValid(phone))
62 && (!mRequestPayerEmail || getEmailValidator().isValid(email));
63 }
64
65 /**
66 * Sets the user interface to be used for editing contact information.
67 *
68 * @param editorView The user interface to be used.
69 */
70 public void setEditorView(EditorView editorView) {
71 assert editorView != null;
72 mEditorView = editorView;
73 mContext = mEditorView.getContext();
74 }
75
76 /**
77 * Adds the given phone number to the autocomplete list, if it's valid.
78 *
79 * @param phoneNumber The phone number to possibly add.
80 */
81 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) {
82 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum ber);
83 }
84
85 /**
86 * Adds the given email address to the autocomplete list, if it's valid.
87 *
88 * @param emailAddress The email address to possibly add.
89 */
90 public void addEmailAddressIfValid(@Nullable CharSequence emailAddress) {
91 if (getEmailValidator().isValid(emailAddress)) mEmailAddresses.add(email Address);
92 }
93
94 /**
95 * Shows the user interface for editing the given contact. The contact is al so updated on disk,
96 * so there's no need to do that in the calling code.
97 *
98 * @param toEdit The contact to edit. Can be null if the user is adding a new contact instead
99 * of editing an existing one.
100 * @param callback The callback to invoke with the complete and valid contac t information. Can
101 * be invoked with null if the user clicked Cancel.
102 */
103 public void editContact(
104 @Nullable AutofillContact toEdit, final Callback<AutofillContact> ca llback) {
105 assert mEditorView != null;
106 assert mContext != null;
107
108 final AutofillContact contact = toEdit == null
109 ? new AutofillContact(new AutofillProfile(), null, null, false) : toEdit;
110
111 final EditorFieldModel phoneField = mRequestPayerPhone
112 ? new EditorFieldModel(EditorFieldModel.INPUT_TYPE_HINT_PHONE,
113 mContext.getString(R.string.autofill_profile_editor_ph one_number),
114 mPhoneNumbers, getPhoneValidator(),
115 mContext.getString(R.string.payments_phone_required_va lidation_message),
116 mContext.getString(R.string.payments_phone_invalid_val idation_message),
117 contact.getPayerPhone())
118 : null;
119
120 final EditorFieldModel emailField = mRequestPayerEmail
121 ? new EditorFieldModel(EditorFieldModel.INPUT_TYPE_HINT_EMAIL,
122 mContext.getString(R.string.autofill_profile_editor_em ail_address),
123 mEmailAddresses, getEmailValidator(),
124 mContext.getString(R.string.payments_email_required_va lidation_message),
125 mContext.getString(R.string.payments_email_invalid_val idation_message),
126 contact.getPayerEmail())
127 : null;
128
129 EditorModel editor =
130 new EditorModel(mContext.getString(R.string.payments_add_contact _details_label));
131 if (phoneField != null) editor.addField(phoneField);
132 if (emailField != null) editor.addField(emailField);
133
134 editor.setCancelCallback(new Runnable() {
135 @Override
136 public void run() {
137 callback.onResult(null);
138 }
139 });
140
141 editor.setDoneCallback(new Runnable() {
142 @Override
143 public void run() {
144 String phone = null;
145 String email = null;
146
147 if (phoneField != null) {
148 phone = phoneField.getValue().toString();
149 contact.getProfile().setPhoneNumber(phone);
150 }
151
152 if (emailField != null) {
153 email = emailField.getValue().toString();
154 contact.getProfile().setEmailAddress(email);
155 }
156
157 PersonalDataManager.getInstance().setProfile(contact.getProfile( ));
158 contact.completeContact(phone, email);
159 callback.onResult(contact);
160 }
161 });
162
163 mEditorView.show(editor);
164 }
165
166 private EditorFieldValidator getPhoneValidator() {
167 if (mPhoneValidator == null) {
168 mPhoneValidator = new EditorFieldValidator() {
169 @Override
170 public boolean isValid(@Nullable CharSequence value) {
171 return value != null
172 && PhoneNumberUtils.isGlobalPhoneNumber(
173 PhoneNumberUtils.stripSeparators(value.to String()));
174 }
175 };
176 }
177 return mPhoneValidator;
178 }
179
180 private EditorFieldValidator getEmailValidator() {
181 if (mEmailValidator == null) {
182 mEmailValidator = new EditorFieldValidator() {
183 @Override
184 public boolean isValid(@Nullable CharSequence value) {
185 return value != null && Patterns.EMAIL_ADDRESS.matcher(value ).matches();
186 }
187 };
188 }
189 return mEmailValidator;
190 }
191 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698