use of org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge in project AndroidChromium by JackyAndroid.
the class AddressEditor method edit.
/**
* Builds and shows an editor model with the following fields.
*
* [ country dropdown ] <----- country dropdown is always present.
* [ an address field ] \
* [ an address field ] \
* ... <-- field order, presence, required, and labels depend on country.
* [ an address field ] /
* [ an address field ] /
* [ phone number field ] <----- phone is always present and required.
*/
@Override
public void edit(@Nullable AutofillAddress toEdit, final Callback<AutofillAddress> callback) {
super.edit(toEdit, callback);
if (mAutofillProfileBridge == null)
mAutofillProfileBridge = new AutofillProfileBridge();
// If |toEdit| is null, we're creating a new autofill profile with the country code of the
// default locale on this device.
boolean isNewAddress = toEdit == null;
// Ensure that |address| and |profile| are always not null.
final AutofillAddress address = isNewAddress ? new AutofillAddress(mContext, new AutofillProfile()) : toEdit;
final AutofillProfile profile = 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());
// 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);
}
// Changing the country will update which fields are in the model. The actual fields are not
// discarded, so their contents are preserved.
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);
}
});
// 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);
// and relabel the fields. The meaning of each field remains the same.
if (mAddressFields.isEmpty()) {
// City, dependent locality, and organization don't have any special formatting hints.
mAddressFields.put(AddressField.LOCALITY, EditorFieldModel.createTextInput());
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));
// Street line field can contain \n to indicate line breaks.
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));
}
// that's being edited.
for (Map.Entry<Integer, EditorFieldModel> entry : mAddressFields.entrySet()) {
entry.getValue().setValue(AutofillAddress.getProfileField(profile, entry.getKey()));
}
// 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, mContext.getString(R.string.autofill_profile_editor_phone_number), mPhoneNumbers, getPhoneValidator(), null, mContext.getString(R.string.payments_field_required_validation_message), mContext.getString(R.string.payments_phone_invalid_validation_message), null);
}
// 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);
// If the user clicks [Cancel], send a null address back to the caller.
editor.setCancelCallback(new Runnable() {
@Override
public void run() {
callback.onResult(null);
}
});
// If the user clicks [Done], save changes on disk, mark the address "complete," and send it
// back to the caller.
editor.setDoneCallback(new Runnable() {
@Override
public void run() {
commitChanges(profile);
address.completeAddress(profile);
callback.onResult(address);
}
});
mEditorView.show(editor);
}
Aggregations