Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java |
| index e6df271838bbdfb6c8e5270a61570f2670b84aa5..2a6dfe8457d3b9b2d9357e91ab79142e71a488b5 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/AddressEditor.java |
| @@ -12,6 +12,7 @@ import org.chromium.base.Callback; |
| import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| +import org.chromium.chrome.browser.autofill.PersonalDataManager.GetSubKeysRequestDelegate; |
| import org.chromium.chrome.browser.payments.ui.EditorFieldModel; |
| import org.chromium.chrome.browser.payments.ui.EditorFieldModel.EditorFieldValidator; |
| import org.chromium.chrome.browser.payments.ui.EditorModel; |
| @@ -31,15 +32,26 @@ import javax.annotation.Nullable; |
| /** |
| * An address editor. Can be used for either shipping or billing address editing. |
| */ |
| -public class AddressEditor extends EditorBase<AutofillAddress> { |
| +public class AddressEditor |
| + extends EditorBase<AutofillAddress> implements GetSubKeysRequestDelegate { |
| private final Handler mHandler = new Handler(); |
| private final Map<Integer, EditorFieldModel> mAddressFields = new HashMap<>(); |
| private final Set<CharSequence> mPhoneNumbers = new HashSet<>(); |
| - @Nullable private AutofillProfileBridge mAutofillProfileBridge; |
| - @Nullable private EditorFieldModel mCountryField; |
| - @Nullable private EditorFieldModel mPhoneField; |
| - @Nullable private EditorFieldValidator mPhoneValidator; |
| - @Nullable private List<AddressUiComponent> mAddressUiComponents; |
| + @Nullable |
| + private AutofillProfileBridge mAutofillProfileBridge; |
| + @Nullable |
| + private EditorFieldModel mCountryField; |
| + @Nullable |
| + private EditorFieldModel mPhoneField; |
| + @Nullable |
| + private EditorFieldValidator mPhoneValidator; |
| + @Nullable |
| + private List<AddressUiComponent> mAddressUiComponents; |
| + private boolean mAdminAreasLoaded; |
| + private boolean mIsUpdate; |
| + private Pair<String, Runnable> mEventData; |
| + private AutofillProfile mProfile; |
| + private EditorModel mEditor; |
| /** |
| * Adds the given phone number to the autocomplete set, if it's valid. |
| @@ -72,24 +84,22 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| // default locale on this device. |
| boolean isNewAddress = toEdit == null; |
| - // Ensure that |address| and |profile| are always not null. |
| + // Ensure that |address| and |mProfile| are always not null. |
| final AutofillAddress address = |
| isNewAddress ? new AutofillAddress(mContext, new AutofillProfile()) : toEdit; |
| - final AutofillProfile profile = address.getProfile(); |
| + mProfile = address.getProfile(); |
| // The title of the editor depends on whether we're adding a new address or editing an |
| // existing address. |
| - final EditorModel editor = |
| - new EditorModel(isNewAddress |
| - ? mContext.getString(R.string.autofill_create_profile) |
| - : toEdit.getEditTitle()); |
| + mEditor = |
| + new EditorModel(isNewAddress ? mContext.getString(R.string.autofill_create_profile) |
| + : toEdit.getEditTitle()); |
| // The country dropdown is always present on the editor. |
| if (mCountryField == null) { |
| mCountryField = EditorFieldModel.createDropdown( |
| mContext.getString(R.string.autofill_profile_editor_country), |
| - AutofillProfileBridge.getSupportedCountries(), |
| - null /* hint */); |
| + AutofillProfileBridge.getSupportedCountries(), null /* hint */); |
| } |
| // Changing the country will update which fields are in the model. The actual fields are not |
| @@ -97,22 +107,16 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| mCountryField.setDropdownCallback(new Callback<Pair<String, Runnable>>() { |
| @Override |
| public void onResult(Pair<String, Runnable> eventData) { |
| - editor.removeAllFields(); |
| - editor.addField(mCountryField); |
| - addAddressTextFieldsToEditor(editor, eventData.first, |
| - Locale.getDefault().getLanguage()); |
| - editor.addField(mPhoneField); |
| - |
| - // Notify EditorView that the fields in the model have changed. EditorView should |
| - // re-read the model and update the UI accordingly. |
| - mHandler.post(eventData.second); |
| + mEditor.removeAllFields(); |
| + mEventData = eventData; |
| + mIsUpdate = true; |
| + loadAdminAreas(); |
| } |
| }); |
| // Country dropdown is cached, so the selected item needs to be updated for the new profile |
| // that's being edited. This will not fire the dropdown callback. |
| - mCountryField.setValue(AutofillAddress.getCountryCode(profile)); |
| - editor.addField(mCountryField); |
| + mCountryField.setValue(AutofillAddress.getCountryCode(mProfile)); |
| // There's a finite number of fields for address editing. Changing the country will re-order |
| // and relabel the fields. The meaning of each field remains the same. |
| @@ -122,38 +126,25 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| mAddressFields.put(AddressField.DEPENDENT_LOCALITY, EditorFieldModel.createTextInput()); |
| mAddressFields.put(AddressField.ORGANIZATION, EditorFieldModel.createTextInput()); |
| - // State should be formatted in all capitals. |
| - mAddressFields.put(AddressField.ADMIN_AREA, EditorFieldModel.createTextInput( |
| - EditorFieldModel.INPUT_TYPE_HINT_REGION)); |
| - |
| // Sorting code and postal code (a.k.a. ZIP code) should show both letters and digits on |
| // the keyboard, if possible. |
| - mAddressFields.put(AddressField.SORTING_CODE, EditorFieldModel.createTextInput( |
| - EditorFieldModel.INPUT_TYPE_HINT_ALPHA_NUMERIC)); |
| - mAddressFields.put(AddressField.POSTAL_CODE, EditorFieldModel.createTextInput( |
| - EditorFieldModel.INPUT_TYPE_HINT_ALPHA_NUMERIC)); |
| + mAddressFields.put(AddressField.SORTING_CODE, |
| + EditorFieldModel.createTextInput( |
| + EditorFieldModel.INPUT_TYPE_HINT_ALPHA_NUMERIC)); |
| + mAddressFields.put(AddressField.POSTAL_CODE, |
| + EditorFieldModel.createTextInput( |
| + EditorFieldModel.INPUT_TYPE_HINT_ALPHA_NUMERIC)); |
| // Street line field can contain \n to indicate line breaks. |
| - mAddressFields.put(AddressField.STREET_ADDRESS, EditorFieldModel.createTextInput( |
| - EditorFieldModel.INPUT_TYPE_HINT_STREET_LINES)); |
| + mAddressFields.put(AddressField.STREET_ADDRESS, |
| + EditorFieldModel.createTextInput( |
| + EditorFieldModel.INPUT_TYPE_HINT_STREET_LINES)); |
| // Android has special formatting rules for names. |
| - mAddressFields.put(AddressField.RECIPIENT, EditorFieldModel.createTextInput( |
| - EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); |
| - } |
| - |
| - // Address fields are cached, so their values need to be updated for every new profile |
| - // that's being edited. |
| - for (Map.Entry<Integer, EditorFieldModel> entry : mAddressFields.entrySet()) { |
| - entry.getValue().setValue(AutofillAddress.getProfileField(profile, entry.getKey())); |
| + mAddressFields.put(AddressField.RECIPIENT, |
| + EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME)); |
| } |
| - // Both country code and language code dictate which fields should be added to the editor. |
| - // For example, "US" will not add dependent locality to the editor. A "JP" address will |
| - // start with a person's full name or a with a prefecture name, depending on whether the |
| - // language code is "ja-Latn" or "ja". |
| - addAddressTextFieldsToEditor(editor, profile.getCountryCode(), profile.getLanguageCode()); |
| - |
| // Phone number is present and required for all countries. |
| if (mPhoneField == null) { |
| mPhoneField = EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_PHONE, |
| @@ -165,30 +156,34 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| // Phone number field is cached, so its value needs to be updated for every new profile |
| // that's being edited. |
| - mPhoneField.setValue(profile.getPhoneNumber()); |
| - editor.addField(mPhoneField); |
| + mPhoneField.setValue(mProfile.getPhoneNumber()); |
| // If the user clicks [Cancel], send |toEdit| address back to the caller, which was the |
| // original state (could be null, a complete address, a partial address). |
| - editor.setCancelCallback(new Runnable() { |
| + mEditor.setCancelCallback(new Runnable() { |
| @Override |
| public void run() { |
| + mAdminAreasLoaded = true; |
| + PersonalDataManager.getInstance().cancelPendingGetSubKeys(); |
| callback.onResult(toEdit); |
| } |
| }); |
| // If the user clicks [Done], save changes on disk, mark the address "complete," and send it |
| // back to the caller. |
| - editor.setDoneCallback(new Runnable() { |
| + mEditor.setDoneCallback(new Runnable() { |
| @Override |
| public void run() { |
| - commitChanges(profile); |
| - address.completeAddress(profile); |
| + mAdminAreasLoaded = true; |
| + PersonalDataManager.getInstance().cancelPendingGetSubKeys(); |
| + commitChanges(mProfile); |
| + address.completeAddress(mProfile); |
| callback.onResult(address); |
| } |
| }); |
| - mEditorView.show(editor); |
| + mIsUpdate = false; |
| + loadAdminAreas(); |
| } |
| /** Saves the edited profile on disk. */ |
| @@ -220,7 +215,7 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| } |
| // Save the edited autofill profile locally. |
| - profile.setGUID(PersonalDataManager.getInstance().setProfileToLocal(profile)); |
| + profile.setGUID(PersonalDataManager.getInstance().setProfileToLocal(mProfile)); |
| profile.setIsLocal(true); |
| } |
| @@ -265,15 +260,85 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| return value == null ? "" : value.toString(); |
| } |
| + private void setAddressFieldValuesFromCache() { |
| + // Address fields are cached, so their values need to be updated for every new profile |
| + // that's being edited. |
| + for (Map.Entry<Integer, EditorFieldModel> entry : mAddressFields.entrySet()) { |
| + entry.getValue().setValue(AutofillAddress.getProfileField(mProfile, entry.getKey())); |
| + } |
| + } |
| + /* Callback of the sub keys request that is called when the sub keys are loaded. |
| + Here the sub keys are admin areas (sub keys of the region=country.) */ |
| + @Override |
| + public void onSubKeysReceived(String[] adminAreas) { |
| + if (mAdminAreasLoaded) return; |
| + |
| + mAdminAreasLoaded = true; |
| + |
| + // If can't get admin areas from server or there is no admin area on the |
| + // server, then use text field. Otherwise, use drop down list. |
| + if (adminAreas == null || adminAreas.length == 0) { |
| + mAddressFields.put(AddressField.ADMIN_AREA, EditorFieldModel.createTextInput()); |
| + } else { |
| + mAddressFields.put(AddressField.ADMIN_AREA, EditorFieldModel.createDropdown()); |
| + } |
| + |
| + if (mIsUpdate) { |
|
sebsg
2017/02/10 16:00:32
I don't fully understand what update means. Could
Parastoo
2017/02/13 21:57:23
Done.
|
| + // Both country code and language code dictate which fields should be added to the |
| + // editor. |
| + // For example, "US" will not add dependent locality to the editor. A "JP" address will |
| + // start with a person's full name or a with a prefecture name, depending on whether the |
| + // language code is "ja-Latn" or "ja". |
| + addAddressFieldsToEditor( |
| + mEventData.first, Locale.getDefault().getLanguage(), adminAreas); |
| + // Notify EditorView that the fields in the model have changed. EditorView should |
| + // re-read the model and update the UI accordingly. |
| + mHandler.post(mEventData.second); |
| + } else { |
| + // This should be called when all required fields are put in mAddressField. |
| + setAddressFieldValuesFromCache(); |
| + addAddressFieldsToEditor( |
| + mProfile.getCountryCode(), mProfile.getLanguageCode(), adminAreas); |
| + mEditorView.show(mEditor); |
| + } |
| + } |
| + |
| + /** Requests the list of admin areas. */ |
| + public void loadAdminAreas() { |
| + String countryCode = mIsUpdate ? mEventData.first : mProfile.getCountryCode(); |
| + |
| + // used to check if the callback is called (for time-out). |
|
sebsg
2017/02/10 16:00:32
Nit: Capitalize first letter.
Parastoo
2017/02/13 21:57:23
Done.
|
| + mAdminAreasLoaded = false; |
| + |
| + // In each rule, admin area keys are saved under sub keys of country. |
| + PersonalDataManager.getInstance().loadRulesForRegion(countryCode); |
| + PersonalDataManager.getInstance().getRegionSubKeys(countryCode, this); |
| + onAdminAreasLoading(); |
| + } |
| + |
| + /* In case the the admin areas are not loaded yet, starts a timer to cancel the request. */ |
| + public void onAdminAreasLoading() { |
| + if (!mAdminAreasLoaded) { |
| + new Handler().postDelayed(new Runnable() { |
| + @Override |
| + public void run() { |
| + if (!mAdminAreasLoaded) onSubKeysReceived(null); |
| + PersonalDataManager.getInstance().cancelPendingGetSubKeys(); |
| + } |
| + }, PersonalDataManager.getNormalizationTimeoutMS()); |
| + } |
| + } |
| + |
| /** |
| - * Adds text fields to the editor model based on the country and language code of the profile |
| + * Adds fields to the editor model based on the country and language code of the profile |
| * that's being edited. |
| */ |
| - private void addAddressTextFieldsToEditor( |
| - EditorModel container, String countryCode, String languageCode) { |
| - mAddressUiComponents = mAutofillProfileBridge.getAddressUiComponents(countryCode, |
| - languageCode); |
| + private void addAddressFieldsToEditor( |
| + String countryCode, String languageCode, String[] adminAreas) { |
| + mAddressUiComponents = |
| + mAutofillProfileBridge.getAddressUiComponents(countryCode, languageCode); |
| + mEditor.addField(mCountryField); |
| for (int i = 0; i < mAddressUiComponents.size(); i++) { |
| AddressUiComponent component = mAddressUiComponents.get(i); |
| @@ -286,17 +351,22 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| field.setLabel(component.label); |
| field.setIsFullLine(component.isFullLine); |
| + if (field.isDropdownField()) { |
| + field.setDropdownKeyValues( |
| + mAutofillProfileBridge.getAdminAreaDropdownList(adminAreas)); |
| + } |
| + |
| // Libaddressinput formats do not always require the full name (RECIPIENT), but |
| // PaymentRequest does. |
| if (component.isRequired || component.id == AddressField.RECIPIENT) { |
| - field.setRequiredErrorMessage(mContext.getString( |
| - R.string.payments_field_required_validation_message)); |
| + field.setRequiredErrorMessage( |
| + mContext.getString(R.string.payments_field_required_validation_message)); |
| } else { |
| field.setRequiredErrorMessage(null); |
| } |
| - |
| - container.addField(field); |
| + mEditor.addField(field); |
| } |
| + mEditor.addField(mPhoneField); |
|
sebsg
2017/02/10 16:00:31
I don't fully understand the full concept, like wh
Parastoo
2017/02/13 21:57:23
Done.
|
| } |
| private EditorFieldValidator getPhoneValidator() { |
| @@ -306,7 +376,7 @@ public class AddressEditor extends EditorBase<AutofillAddress> { |
| public boolean isValid(@Nullable CharSequence value) { |
| return value != null |
| && PhoneNumberUtils.isGlobalPhoneNumber( |
| - PhoneNumberUtils.stripSeparators(value.toString())); |
| + PhoneNumberUtils.stripSeparators(value.toString())); |
| } |
| @Override |