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

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

Issue 2952673002: [Payments] Format and validate phone number according to selected country in address editor (Closed)
Patch Set: Created 3 years, 6 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
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.app.ProgressDialog; 7 import android.app.ProgressDialog;
8 import android.os.Handler; 8 import android.os.Handler;
9 import android.text.TextUtils; 9 import android.text.TextUtils;
10 import android.util.Pair; 10 import android.util.Pair;
(...skipping 27 matching lines...) Expand all
38 extends EditorBase<AutofillAddress> implements GetSubKeysRequestDelegate { 38 extends EditorBase<AutofillAddress> implements GetSubKeysRequestDelegate {
39 private final Handler mHandler = new Handler(); 39 private final Handler mHandler = new Handler();
40 private final Map<Integer, EditorFieldModel> mAddressFields = new HashMap<>( ); 40 private final Map<Integer, EditorFieldModel> mAddressFields = new HashMap<>( );
41 private final Set<CharSequence> mPhoneNumbers = new HashSet<>(); 41 private final Set<CharSequence> mPhoneNumbers = new HashSet<>();
42 @Nullable 42 @Nullable
43 private AutofillProfileBridge mAutofillProfileBridge; 43 private AutofillProfileBridge mAutofillProfileBridge;
44 @Nullable 44 @Nullable
45 private EditorFieldModel mCountryField; 45 private EditorFieldModel mCountryField;
46 @Nullable 46 @Nullable
47 private EditorFieldModel mPhoneField; 47 private EditorFieldModel mPhoneField;
48 @Nullable 48 private PhoneNumberUtil.CountryAwareFormatTextWatcher mPhoneFormatter;
49 private EditorFieldValidator mPhoneValidator; 49 private PhoneNumberValidator mPhoneValidator;
50 @Nullable 50 @Nullable
51 private List<AddressUiComponent> mAddressUiComponents; 51 private List<AddressUiComponent> mAddressUiComponents;
52 private boolean mAdminAreasLoaded; 52 private boolean mAdminAreasLoaded;
53 private String mRecentlySelectedCountry; 53 private String mRecentlySelectedCountry;
54 private Runnable mCountryChangeCallback; 54 private Runnable mCountryChangeCallback;
55 private AutofillProfile mProfile; 55 private AutofillProfile mProfile;
56 private EditorModel mEditor; 56 private EditorModel mEditor;
57 private ProgressDialog mProgressDialog; 57 private ProgressDialog mProgressDialog;
58 58
59 /** Builds an address editor. */
60 public AddressEditor() {
61 mPhoneFormatter = new PhoneNumberUtil.CountryAwareFormatTextWatcher();
62 mPhoneValidator = new PhoneNumberValidator();
please use gerrit instead 2017/06/20 18:48:29 Both validator and phone number are "country-aware
gogerald1 2017/06/20 19:12:39 Done.
63 }
64
59 /** 65 /**
60 * Adds the given phone number to the autocomplete set, if it's valid. 66 * Adds the given phone number to the autocomplete set, if it's valid.
67 * Note that here we consider all non-null and non-empty numbers as valid
68 * since we are doing strict validation of Autofill data.
61 * 69 *
62 * @param phoneNumber The phone number to possibly add. 70 * @param phoneNumber The phone number to possibly add.
63 */ 71 */
64 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { 72 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) {
65 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum ber); 73 if (TextUtils.isEmpty(phoneNumber)) mPhoneNumbers.add(phoneNumber.toStri ng());
66 } 74 }
67 75
68 /** 76 /**
69 * Builds and shows an editor model with the following fields. 77 * Builds and shows an editor model with the following fields.
70 * 78 *
71 * [ country dropdown ] <----- country dropdown is always present. 79 * [ country dropdown ] <----- country dropdown is always present.
72 * [ an address field ] \ 80 * [ an address field ] \
73 * [ an address field ] \ 81 * [ an address field ] \
74 * ... <-- field order, presence, required, and labels depend on country. 82 * ... <-- field order, presence, required, and labels depend on country.
75 * [ an address field ] / 83 * [ an address field ] /
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 * If the selected country on the country dropdown list is changed, 125 * If the selected country on the country dropdown list is changed,
118 * the first element of eventData is the recently selected dropdown key, 126 * the first element of eventData is the recently selected dropdown key,
119 * the second element is the callback to invoke for when the dropdow n 127 * the second element is the callback to invoke for when the dropdow n
120 * change has been processed. 128 * change has been processed.
121 */ 129 */
122 @Override 130 @Override
123 public void onResult(Pair<String, Runnable> eventData) { 131 public void onResult(Pair<String, Runnable> eventData) {
124 mEditor.removeAllFields(); 132 mEditor.removeAllFields();
125 showProgressDialog(); 133 showProgressDialog();
126 mRecentlySelectedCountry = eventData.first; 134 mRecentlySelectedCountry = eventData.first;
135 mPhoneFormatter.setCountryCode(mRecentlySelectedCountry);
136 mPhoneValidator.setCountryCode(mRecentlySelectedCountry);
127 mCountryChangeCallback = eventData.second; 137 mCountryChangeCallback = eventData.second;
128 loadAdminAreasForCountry(mRecentlySelectedCountry); 138 loadAdminAreasForCountry(mRecentlySelectedCountry);
129 } 139 }
130 }); 140 });
131 141
132 // Country dropdown is cached, so the selected item needs to be updated for the new profile 142 // Country dropdown is cached, so the selected item needs to be updated for the new profile
133 // that's being edited. This will not fire the dropdown callback. 143 // that's being edited. This will not fire the dropdown callback.
134 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); 144 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile));
135 145
136 // There's a finite number of fields for address editing. Changing the c ountry will re-order 146 // There's a finite number of fields for address editing. Changing the c ountry will re-order
(...skipping 16 matching lines...) Expand all
153 EditorFieldModel.INPUT_TYPE_HINT_STREET_LINES)); 163 EditorFieldModel.INPUT_TYPE_HINT_STREET_LINES));
154 164
155 // Android has special formatting rules for names. 165 // Android has special formatting rules for names.
156 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe xtInput( 166 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe xtInput(
157 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); 167 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME));
158 } 168 }
159 169
160 170
161 // Phone number is present and required for all countries. 171 // Phone number is present and required for all countries.
162 if (mPhoneField == null) { 172 if (mPhoneField == null) {
173 assert mCountryField.getValue() != null;
174 mPhoneValidator.setCountryCode(mCountryField.getValue().toString());
175 mPhoneFormatter.setCountryCode(mCountryField.getValue().toString());
163 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU T_TYPE_HINT_PHONE, 176 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU T_TYPE_HINT_PHONE,
164 mContext.getString(R.string.autofill_profile_editor_phone_nu mber), 177 mContext.getString(R.string.autofill_profile_editor_phone_nu mber),
165 mPhoneNumbers, getPhoneValidator(), null, 178 mPhoneNumbers, mPhoneFormatter, mPhoneValidator, null,
166 mContext.getString(R.string.payments_field_required_validati on_message), 179 mContext.getString(R.string.payments_field_required_validati on_message),
167 mContext.getString(R.string.payments_phone_invalid_validatio n_message), null); 180 mContext.getString(R.string.payments_phone_invalid_validatio n_message), null);
168 } 181 }
169 182
170 // Phone number field is cached, so its value needs to be updated for ev ery new profile 183 // Phone number field is cached, so its value needs to be updated for ev ery new profile
171 // that's being edited. 184 // that's being edited.
172 mPhoneField.setValue(mProfile.getPhoneNumber()); 185 mPhoneField.setValue(mProfile.getPhoneNumber());
173 186
174 // If the user clicks [Cancel], send |toEdit| address back to the caller , which was the 187 // If the user clicks [Cancel], send |toEdit| address back to the caller , which was the
175 // original state (could be null, a complete address, a partial address) . 188 // original state (could be null, a complete address, a partial address) .
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 R.string.payments_field_required_validation_message)); 403 R.string.payments_field_required_validation_message));
391 } else { 404 } else {
392 field.setRequiredErrorMessage(null); 405 field.setRequiredErrorMessage(null);
393 } 406 }
394 mEditor.addField(field); 407 mEditor.addField(field);
395 } 408 }
396 // Phone number must be the last field. 409 // Phone number must be the last field.
397 mEditor.addField(mPhoneField); 410 mEditor.addField(mPhoneField);
398 } 411 }
399 412
400 private EditorFieldValidator getPhoneValidator() { 413 /** Country based phone number validator. */
401 if (mPhoneValidator == null) { 414 private class PhoneNumberValidator implements EditorFieldValidator {
402 mPhoneValidator = new EditorFieldValidator() { 415 @Nullable
403 @Override 416 private String mCountryCode;
404 public boolean isValid(@Nullable CharSequence value) {
405 return value != null && PhoneNumberUtil.isValidNumber(value. toString());
406 }
407 417
408 @Override 418 /**
409 public boolean isLengthMaximum(@Nullable CharSequence value) { 419 * Sets the country code used to validate the phone number.
410 return false; 420 *
411 } 421 * @param countryCode The given country code.
412 }; 422 */
423 public void setCountryCode(@Nullable String countryCode) {
424 mCountryCode = countryCode;
413 } 425 }
414 return mPhoneValidator; 426
427 @Override
428 public boolean isValid(@Nullable CharSequence value) {
429 return value != null && PhoneNumberUtil.isValidNumber(value.toString (), mCountryCode);
430 }
431
432 @Override
433 public boolean isLengthMaximum(@Nullable CharSequence value) {
434 return false;
435 }
415 } 436 }
416 } 437 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698