| 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.text.TextUtils; |
| 9 import android.util.Pair; | 10 import android.util.Pair; |
| 10 | 11 |
| 11 import org.chromium.base.Callback; | 12 import org.chromium.base.Callback; |
| 12 import org.chromium.chrome.R; | 13 import org.chromium.chrome.R; |
| 13 import org.chromium.chrome.browser.autofill.PersonalDataManager; | 14 import org.chromium.chrome.browser.autofill.PersonalDataManager; |
| 14 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; | 15 import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| 15 import org.chromium.chrome.browser.autofill.PersonalDataManager.GetSubKeysReques
tDelegate; | 16 import org.chromium.chrome.browser.autofill.PersonalDataManager.GetSubKeysReques
tDelegate; |
| 16 import org.chromium.chrome.browser.autofill.PhoneNumberUtil; | 17 import org.chromium.chrome.browser.autofill.PhoneNumberUtil; |
| 17 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; | 18 import org.chromium.chrome.browser.payments.ui.EditorFieldModel; |
| 18 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; | 19 import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValid
ator; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 private List<AddressUiComponent> mAddressUiComponents; | 51 private List<AddressUiComponent> mAddressUiComponents; |
| 51 private boolean mAdminAreasLoaded; | 52 private boolean mAdminAreasLoaded; |
| 52 private String mRecentlySelectedCountry; | 53 private String mRecentlySelectedCountry; |
| 53 private Runnable mCountryChangeCallback; | 54 private Runnable mCountryChangeCallback; |
| 54 private AutofillProfile mProfile; | 55 private AutofillProfile mProfile; |
| 55 private EditorModel mEditor; | 56 private EditorModel mEditor; |
| 56 private ProgressDialog mProgressDialog; | 57 private ProgressDialog mProgressDialog; |
| 57 | 58 |
| 58 /** | 59 /** |
| 59 * Adds the given phone number to the autocomplete set, if it's valid. | 60 * Adds the given phone number to the autocomplete set, if it's valid. |
| 61 * Note that here we consider all non-null and non-empty numbers as valid |
| 62 * since we are doing strict validation of Autofill data. |
| 60 * | 63 * |
| 61 * @param phoneNumber The phone number to possibly add. | 64 * @param phoneNumber The phone number to possibly add. |
| 62 */ | 65 */ |
| 63 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { | 66 public void addPhoneNumberIfValid(@Nullable CharSequence phoneNumber) { |
| 64 if (getPhoneValidator().isValid(phoneNumber)) mPhoneNumbers.add(phoneNum
ber); | 67 if (TextUtils.isEmpty(phoneNumber)) { |
| 68 mPhoneNumbers.add(phoneNumber.toString()); |
| 69 } |
| 65 } | 70 } |
| 66 | 71 |
| 67 /** | 72 /** |
| 68 * Builds and shows an editor model with the following fields. | 73 * Builds and shows an editor model with the following fields. |
| 69 * | 74 * |
| 70 * [ country dropdown ] <----- country dropdown is always present. | 75 * [ country dropdown ] <----- country dropdown is always present. |
| 71 * [ an address field ] \ | 76 * [ an address field ] \ |
| 72 * [ an address field ] \ | 77 * [ an address field ] \ |
| 73 * ... <-- field order, presence, required, and labels
depend on country. | 78 * ... <-- field order, presence, required, and labels
depend on country. |
| 74 * [ an address field ] / | 79 * [ 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, | 121 * If the selected country on the country dropdown list is changed, |
| 117 * the first element of eventData is the recently selected dropdown
key, | 122 * 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 | 123 * the second element is the callback to invoke for when the dropdow
n |
| 119 * change has been processed. | 124 * change has been processed. |
| 120 */ | 125 */ |
| 121 @Override | 126 @Override |
| 122 public void onResult(Pair<String, Runnable> eventData) { | 127 public void onResult(Pair<String, Runnable> eventData) { |
| 123 mEditor.removeAllFields(); | 128 mEditor.removeAllFields(); |
| 124 showProgressDialog(); | 129 showProgressDialog(); |
| 125 mRecentlySelectedCountry = eventData.first; | 130 mRecentlySelectedCountry = eventData.first; |
| 131 mEditorDialog.updateCountryOfPhoneFormatter(mRecentlySelectedCou
ntry); |
| 126 mCountryChangeCallback = eventData.second; | 132 mCountryChangeCallback = eventData.second; |
| 127 loadAdminAreasForCountry(mRecentlySelectedCountry); | 133 loadAdminAreasForCountry(mRecentlySelectedCountry); |
| 128 } | 134 } |
| 129 }); | 135 }); |
| 130 | 136 |
| 131 // Country dropdown is cached, so the selected item needs to be updated
for the new profile | 137 // 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. | 138 // that's being edited. This will not fire the dropdown callback. |
| 133 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); | 139 mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); |
| 140 mEditorDialog.updateCountryOfPhoneFormatter(mCountryField.getValue().toS
tring()); |
| 134 | 141 |
| 135 // There's a finite number of fields for address editing. Changing the c
ountry will re-order | 142 // 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. | 143 // and relabel the fields. The meaning of each field remains the same. |
| 137 if (mAddressFields.isEmpty()) { | 144 if (mAddressFields.isEmpty()) { |
| 138 // City, dependent locality, and organization don't have any special
formatting hints. | 145 // City, dependent locality, and organization don't have any special
formatting hints. |
| 139 mAddressFields.put(AddressField.LOCALITY, EditorFieldModel.createTex
tInput()); | 146 mAddressFields.put(AddressField.LOCALITY, EditorFieldModel.createTex
tInput()); |
| 140 mAddressFields.put(AddressField.DEPENDENT_LOCALITY, EditorFieldModel
.createTextInput()); | 147 mAddressFields.put(AddressField.DEPENDENT_LOCALITY, EditorFieldModel
.createTextInput()); |
| 141 mAddressFields.put(AddressField.ORGANIZATION, EditorFieldModel.creat
eTextInput()); | 148 mAddressFields.put(AddressField.ORGANIZATION, EditorFieldModel.creat
eTextInput()); |
| 142 | 149 |
| 143 // Sorting code and postal code (a.k.a. ZIP code) should show both l
etters and digits on | 150 // 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. | 161 // Android has special formatting rules for names. |
| 155 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe
xtInput( | 162 mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTe
xtInput( |
| 156 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); | 163 EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); |
| 157 } | 164 } |
| 158 | 165 |
| 159 | 166 |
| 160 // Phone number is present and required for all countries. | 167 // Phone number is present and required for all countries. |
| 161 if (mPhoneField == null) { | 168 if (mPhoneField == null) { |
| 162 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU
T_TYPE_HINT_PHONE, | 169 mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPU
T_TYPE_HINT_PHONE, |
| 163 mContext.getString(R.string.autofill_profile_editor_phone_nu
mber), | 170 mContext.getString(R.string.autofill_profile_editor_phone_nu
mber), |
| 164 mPhoneNumbers, getPhoneValidator(), null, | 171 mPhoneNumbers, getPhoneValidator(mCountryField), null, |
| 165 mContext.getString(R.string.payments_field_required_validati
on_message), | 172 mContext.getString(R.string.payments_field_required_validati
on_message), |
| 166 mContext.getString(R.string.payments_phone_invalid_validatio
n_message), null); | 173 mContext.getString(R.string.payments_phone_invalid_validatio
n_message), null); |
| 167 } | 174 } |
| 168 | 175 |
| 169 // Phone number field is cached, so its value needs to be updated for ev
ery new profile | 176 // Phone number field is cached, so its value needs to be updated for ev
ery new profile |
| 170 // that's being edited. | 177 // that's being edited. |
| 171 mPhoneField.setValue(mProfile.getPhoneNumber()); | 178 mPhoneField.setValue(mProfile.getPhoneNumber()); |
| 172 | 179 |
| 173 // If the user clicks [Cancel], send |toEdit| address back to the caller
, which was the | 180 // 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)
. | 181 // 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)); | 390 R.string.payments_field_required_validation_message)); |
| 384 } else { | 391 } else { |
| 385 field.setRequiredErrorMessage(null); | 392 field.setRequiredErrorMessage(null); |
| 386 } | 393 } |
| 387 mEditor.addField(field); | 394 mEditor.addField(field); |
| 388 } | 395 } |
| 389 // Phone number must be the last field. | 396 // Phone number must be the last field. |
| 390 mEditor.addField(mPhoneField); | 397 mEditor.addField(mPhoneField); |
| 391 } | 398 } |
| 392 | 399 |
| 393 private EditorFieldValidator getPhoneValidator() { | 400 private EditorFieldValidator getPhoneValidator(final EditorFieldModel countr
yCodeField) { |
| 394 if (mPhoneValidator == null) { | 401 if (mPhoneValidator == null) { |
| 395 mPhoneValidator = new EditorFieldValidator() { | 402 mPhoneValidator = new EditorFieldValidator() { |
| 396 @Override | 403 @Override |
| 397 public boolean isValid(@Nullable CharSequence value) { | 404 public boolean isValid(@Nullable CharSequence value) { |
| 398 return value != null && PhoneNumberUtil.isValidNumber(value.
toString()); | 405 String countryToValidateAgainst = (countryCodeField.getValue
() == 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 |