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.app.ProgressDialog; | 7 import android.app.ProgressDialog; |
8 import android.os.Handler; | 8 import android.os.Handler; |
9 import android.util.Pair; | 9 import android.util.Pair; |
10 | 10 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 private boolean mAdminAreasLoaded; | 51 private boolean mAdminAreasLoaded; |
52 private String mRecentlySelectedCountry; | 52 private String mRecentlySelectedCountry; |
53 private Runnable mCountryChangeCallback; | 53 private Runnable mCountryChangeCallback; |
54 private AutofillProfile mProfile; | 54 private AutofillProfile mProfile; |
55 private EditorModel mEditor; | 55 private EditorModel mEditor; |
56 private ProgressDialog mProgressDialog; | 56 private ProgressDialog mProgressDialog; |
57 | 57 |
58 /** | 58 /** |
59 * Adds the given phone number to the autocomplete set, if it's valid. | 59 * Adds the given phone number to the autocomplete set, if it's valid. |
60 * | 60 * |
61 * @param phoneNumber The phone number to possibly add. | 61 * @param phoneNumber The phone number to possibly add, if it is not null or empty. |
62 * We don't validate for number format since these are su ggestions only. | |
gogerald1
2017/06/19 21:30:38
The reason we do not validate it here is not becau
wuandy1
2017/06/20 15:33:26
Done.
| |
62 */ | 63 */ |
63 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { | 64 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { |
64 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum ber); | 65 if (phoneNumber != null && !phoneNumber.toString().isEmpty()) { |
gogerald1
2017/06/19 21:30:38
use TextUtils.isEmpty() which do both, https://cs.
wuandy1
2017/06/20 15:33:27
Done.
| |
66 mPhoneNumbers.add(phoneNumber.toString()); | |
67 } | |
65 } | 68 } |
66 | 69 |
67 /** | 70 /** |
68 * Builds and shows an editor model with the following fields. | 71 * Builds and shows an editor model with the following fields. |
69 * | 72 * |
70 * [ country dropdown ] <----- country dropdown is always present. | 73 * [ country dropdown ] <----- country dropdown is always present. |
71 * [ an address field ] \ | 74 * [ an address field ] \ |
72 * [ an address field ] \ | 75 * [ an address field ] \ |
73 * ... <-- field order, presence, required, and labels depend on country. | 76 * ... <-- field order, presence, required, and labels depend on country. |
74 * [ an address field ] / | 77 * [ an address field ] / |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 * If the selected country on the country dropdown list is changed, | 119 * If the selected country on the country dropdown list is changed, |
117 * the first element of eventData is the recently selected dropdown key, | 120 * the first element of eventData is the recently selected dropdown key, |
118 * the second element is the callback to invoke for when the dropdow n | 121 * the second element is the callback to invoke for when the dropdow n |
119 * change has been processed. | 122 * change has been processed. |
120 */ | 123 */ |
121 @Override | 124 @Override |
122 public void onResult(Pair<String, Runnable> eventData) { | 125 public void onResult(Pair<String, Runnable> eventData) { |
123 mEditor.removeAllFields(); | 126 mEditor.removeAllFields(); |
124 showProgressDialog(); | 127 showProgressDialog(); |
125 mRecentlySelectedCountry = eventData.first; | 128 mRecentlySelectedCountry = eventData.first; |
129 mEditorDialog.updateCountryOfPhoneFormatter(mRecentlySelectedCou ntry); | |
126 mCountryChangeCallback = eventData.second; | 130 mCountryChangeCallback = eventData.second; |
127 loadAdminAreasForCountry(mRecentlySelectedCountry); | 131 loadAdminAreasForCountry(mRecentlySelectedCountry); |
128 } | 132 } |
129 }); | 133 }); |
130 | 134 |
131 // Country dropdown is cached, so the selected item needs to be updated for the new profile | 135 // Country dropdown is cached, so the selected item needs to be updated for the new profile |
132 // that's being edited. This will not fire the dropdown callback. | 136 // that's being edited. This will not fire the dropdown callback. |
133 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); | 137 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); |
138 mEditorDialog.updateCountryOfPhoneFormatter(mCountryField.getValue().toS tring()); | |
134 | 139 |
135 // There's a finite number of fields for address editing. Changing the c ountry will re-order | 140 // There's a finite number of fields for address editing. Changing the c ountry will re-order |
136 // and relabel the fields. The meaning of each field remains the same. | 141 // and relabel the fields. The meaning of each field remains the same. |
137 if (mAddressFields.isEmpty()) { | 142 if (mAddressFields.isEmpty()) { |
138 // City, dependent locality, and organization don't have any special formatting hints. | 143 // City, dependent locality, and organization don't have any special formatting hints. |
139 mAddressFields.put(AddressField.LOCALITY, EditorFieldModel.createTex tInput()); | 144 mAddressFields.put(AddressField.LOCALITY, EditorFieldModel.createTex tInput()); |
140 mAddressFields.put(AddressField.DEPENDENT_LOCALITY, EditorFieldModel .createTextInput()); | 145 mAddressFields.put(AddressField.DEPENDENT_LOCALITY, EditorFieldModel .createTextInput()); |
141 mAddressFields.put(AddressField.ORGANIZATION, EditorFieldModel.creat eTextInput()); | 146 mAddressFields.put(AddressField.ORGANIZATION, EditorFieldModel.creat eTextInput()); |
142 | 147 |
143 // Sorting code and postal code (a.k.a. ZIP code) should show both l etters and digits on | 148 // Sorting code and postal code (a.k.a. ZIP code) should show both l etters and digits on |
(...skipping 10 matching lines...) Expand all Loading... | |
154 // Android has special formatting rules for names. | 159 // Android has special formatting rules for names. |
155 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe xtInput( | 160 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe xtInput( |
156 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); | 161 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); |
157 } | 162 } |
158 | 163 |
159 | 164 |
160 // Phone number is present and required for all countries. | 165 // Phone number is present and required for all countries. |
161 if (mPhoneField == null) { | 166 if (mPhoneField == null) { |
162 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU T_TYPE_HINT_PHONE, | 167 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU T_TYPE_HINT_PHONE, |
163 mContext.getString(R.string.autofill_profile_editor_phone_nu mber), | 168 mContext.getString(R.string.autofill_profile_editor_phone_nu mber), |
164 mPhoneNumbers, getPhoneValidator(), null, | 169 mPhoneNumbers, getPhoneValidator(mCountryField), null, |
165 mContext.getString(R.string.payments_field_required_validati on_message), | 170 mContext.getString(R.string.payments_field_required_validati on_message), |
166 mContext.getString(R.string.payments_phone_invalid_validatio n_message), null); | 171 mContext.getString(R.string.payments_phone_invalid_validatio n_message), null); |
167 } | 172 } |
168 | 173 |
169 // Phone number field is cached, so its value needs to be updated for ev ery new profile | 174 // Phone number field is cached, so its value needs to be updated for ev ery new profile |
170 // that's being edited. | 175 // that's being edited. |
171 mPhoneField.setValue(mProfile.getPhoneNumber()); | 176 mPhoneField.setValue(mProfile.getPhoneNumber()); |
172 | 177 |
173 // If the user clicks [Cancel], send |toEdit| address back to the caller , which was the | 178 // If the user clicks [Cancel], send |toEdit| address back to the caller , which was the |
174 // original state (could be null, a complete address, a partial address) . | 179 // original state (could be null, a complete address, a partial address) . |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 R.string.payments_field_required_validation_message)); | 388 R.string.payments_field_required_validation_message)); |
384 } else { | 389 } else { |
385 field.setRequiredErrorMessage(null); | 390 field.setRequiredErrorMessage(null); |
386 } | 391 } |
387 mEditor.addField(field); | 392 mEditor.addField(field); |
388 } | 393 } |
389 // Phone number must be the last field. | 394 // Phone number must be the last field. |
390 mEditor.addField(mPhoneField); | 395 mEditor.addField(mPhoneField); |
391 } | 396 } |
392 | 397 |
393 private EditorFieldValidator getPhoneValidator() { | 398 private EditorFieldValidator getPhoneValidator( |
399 @Nullable final EditorFieldModel countryCodeField) { | |
gogerald1
2017/06/19 21:30:38
Why are you using EditorFieldModel instead of coun
wuandy1
2017/06/20 15:33:26
because validator can then run validation after us
| |
394 if (mPhoneValidator == null) { | 400 if (mPhoneValidator == null) { |
395 mPhoneValidator = new EditorFieldValidator() { | 401 mPhoneValidator = new EditorFieldValidator() { |
396 @Override | 402 @Override |
397 public boolean isValid(@Nullable CharSequence value) { | 403 public boolean isValid(@Nullable CharSequence value) { |
398 return value != null && PhoneNumberUtil.isValidNumber(value. toString()); | 404 String countryToValidateAgainst = |
405 (countryCodeField == null || countryCodeField.getVal ue() == null) | |
406 ? null | |
407 : countryCodeField.getValue().toString(); | |
408 return value != null | |
409 && PhoneNumberUtil.isValidNumber( | |
410 value.toString(), countryToValidateAgains t); | |
399 } | 411 } |
400 | 412 |
401 @Override | 413 @Override |
402 public boolean isLengthMaximum(@Nullable CharSequence value) { | 414 public boolean isLengthMaximum(@Nullable CharSequence value) { |
403 return false; | 415 return false; |
404 } | 416 } |
405 }; | 417 }; |
406 } | 418 } |
407 return mPhoneValidator; | 419 return mPhoneValidator; |
408 } | 420 } |
409 } | 421 } |
OLD | NEW |