use of org.chromium.chrome.browser.payments.ui.EditorFieldModel 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(new AutofillProfile(), false) : 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(mContext.getString(isNewAddress ? R.string.autofill_create_profile : R.string.autofill_edit_profile));
// 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());
}
// 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(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(), 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);
}
use of org.chromium.chrome.browser.payments.ui.EditorFieldModel in project AndroidChromium by JackyAndroid.
the class AddressEditor method commitChanges.
/** Saves the edited profile on disk. */
private void commitChanges(AutofillProfile profile) {
// Country code and phone number are always required and are always collected from the
// editor model.
profile.setCountryCode(mCountryField.getValue().toString());
profile.setPhoneNumber(mPhoneField.getValue().toString());
// Autofill profile bridge normalizes the language code for the autofill profile.
profile.setLanguageCode(mAutofillProfileBridge.getCurrentBestLanguageCode());
// Collect data from all visible fields and store it in the autofill profile.
Set<Integer> visibleFields = new HashSet<>();
for (int i = 0; i < mAddressUiComponents.size(); i++) {
AddressUiComponent component = mAddressUiComponents.get(i);
visibleFields.add(component.id);
if (component.id != AddressField.COUNTRY) {
setProfileField(profile, component.id, mAddressFields.get(component.id).getValue());
}
}
// AutofillAddress.toPaymentAddress() will send them to the renderer as empty strings.
for (Map.Entry<Integer, EditorFieldModel> entry : mAddressFields.entrySet()) {
if (!visibleFields.contains(entry.getKey())) {
setProfileField(profile, entry.getKey(), "");
}
}
// Calculate the label for this profile. The label's format depends on the country and
// language code for the profile.
PersonalDataManager pdm = PersonalDataManager.getInstance();
profile.setLabel(pdm.getAddressLabelForPaymentRequest(profile));
// Save the edited autofill profile.
profile.setGUID(pdm.setProfile(profile));
}
use of org.chromium.chrome.browser.payments.ui.EditorFieldModel in project AndroidChromium by JackyAndroid.
the class ContactEditor method edit.
@Override
public void edit(@Nullable AutofillContact toEdit, final Callback<AutofillContact> callback) {
super.edit(toEdit, callback);
final AutofillContact contact = toEdit == null ? new AutofillContact(new AutofillProfile(), null, null, false) : toEdit;
final EditorFieldModel phoneField = mRequestPayerPhone ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_PHONE, mContext.getString(R.string.autofill_profile_editor_phone_number), mPhoneNumbers, getPhoneValidator(), mContext.getString(R.string.payments_field_required_validation_message), mContext.getString(R.string.payments_phone_invalid_validation_message), contact.getPayerPhone()) : null;
final EditorFieldModel emailField = mRequestPayerEmail ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_EMAIL, mContext.getString(R.string.autofill_profile_editor_email_address), mEmailAddresses, getEmailValidator(), mContext.getString(R.string.payments_field_required_validation_message), mContext.getString(R.string.payments_email_invalid_validation_message), contact.getPayerEmail()) : null;
EditorModel editor = new EditorModel(mContext.getString(toEdit == null ? R.string.payments_add_contact_details_label : R.string.payments_edit_contact_details_label));
if (phoneField != null)
editor.addField(phoneField);
if (emailField != null)
editor.addField(emailField);
editor.setCancelCallback(new Runnable() {
@Override
public void run() {
callback.onResult(null);
}
});
editor.setDoneCallback(new Runnable() {
@Override
public void run() {
String phone = null;
String email = null;
if (phoneField != null) {
phone = phoneField.getValue().toString();
contact.getProfile().setPhoneNumber(phone);
}
if (emailField != null) {
email = emailField.getValue().toString();
contact.getProfile().setEmailAddress(email);
}
String guid = PersonalDataManager.getInstance().setProfile(contact.getProfile());
contact.completeContact(guid, phone, email);
callback.onResult(contact);
}
});
mEditorView.show(editor);
}
use of org.chromium.chrome.browser.payments.ui.EditorFieldModel in project AndroidChromium by JackyAndroid.
the class AddressEditor method addAddressTextFieldsToEditor.
/**
* Adds text 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);
for (int i = 0; i < mAddressUiComponents.size(); i++) {
AddressUiComponent component = mAddressUiComponents.get(i);
// The country field is a dropdown, so there's no need to add a text field for it.
if (component.id == AddressField.COUNTRY)
continue;
EditorFieldModel field = mAddressFields.get(component.id);
// Labels depend on country, e.g., state is called province in some countries. These are
// already localized.
field.setLabel(component.label);
field.setIsFullLine(component.isFullLine);
// PaymentRequest does.
if (component.isRequired || component.id == AddressField.RECIPIENT) {
field.setRequiredErrorMessage(mContext.getString(R.string.payments_field_required_validation_message));
} else {
field.setRequiredErrorMessage(null);
}
container.addField(field);
}
}
Aggregations