org.chromium.chrome.browser.payments.ui.EditorFieldModel Java Examples

The following examples show how to use org.chromium.chrome.browser.payments.ui.EditorFieldModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AddressEditor.java    From delion with Apache License 2.0 5 votes vote down vote up
/** 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());
        }
    }

    // Clear the fields that are hidden from the user interface, so
    // 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 pmd = PersonalDataManager.getInstance();
    profile.setLabel(pmd.getGetAddressLabelForPaymentRequest(profile));

    // Save the edited autofill profile.
    pmd.setProfile(profile);
}
 
Example #2
Source File: AddressEditor.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

        // 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_address_field_required_validation_message));
        } else {
            field.setRequiredErrorMessage(null);
        }

        container.addField(field);
    }
}
 
Example #3
Source File: CardEditor.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/** Adds the "save this card" checkbox to the editor. */
private void addSaveCardCheckbox(EditorModel editor) {
    if (mSaveCardCheckbox == null) {
        mSaveCardCheckbox = EditorFieldModel.createCheckbox(
                mContext.getString(R.string.payments_save_card_to_device_checkbox),
                CHECK_SAVE_CARD_TO_DEVICE);
    }
    editor.addField(mSaveCardCheckbox);
}
 
Example #4
Source File: AddressEditor.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/** 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());
        }
    }

    // Clear the fields that are hidden from the user interface, so
    // 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();
    // TODO(crbug.com/666048): New billing address label is wrong.
    profile.setLabel(pdm.getAddressLabelForPaymentRequest(profile));

    // Save the edited autofill profile locally.
    profile.setGUID(pdm.setProfileToLocal(profile));
    profile.setIsLocal(true);
}
 
Example #5
Source File: AddressEditor.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

        // 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));
        } else {
            field.setRequiredErrorMessage(null);
        }

        container.addField(field);
    }
}
 
Example #6
Source File: CardEditor.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** Adds the "save this card" checkbox to the editor. */
private void addSaveCardCheckbox(EditorModel editor) {
    if (mSaveCardCheckbox == null) {
        mSaveCardCheckbox = EditorFieldModel.createCheckbox(
                mContext.getString(R.string.payments_save_card_to_device_checkbox),
                CHECK_SAVE_CARD_TO_DEVICE);
    }
    editor.addField(mSaveCardCheckbox);
}
 
Example #7
Source File: AddressEditor.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** 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());
        }
    }

    // Clear the fields that are hidden from the user interface, so
    // 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(), "");
        }
    }

    // Save the edited autofill profile locally.
    profile.setGUID(PersonalDataManager.getInstance().setProfileToLocal(mProfile));
    profile.setIsLocal(true);
}
 
Example #8
Source File: AddressEditor.java    From 365browser with Apache License 2.0 5 votes vote down vote up
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()));
    }
}
 
Example #9
Source File: AddressEditor.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubKeysReceived(String[] adminAreas) {
    if (mAdminAreasLoaded) return;
    mAdminAreasLoaded = true;

    // If Chrome can't get admin areas from the server or there is no admin area on the server,
    // then use the text field.
    // Otherwise, use the dropdown list.
    if (adminAreas == null || adminAreas.length == 0) {
        mAddressFields.put(AddressField.ADMIN_AREA, EditorFieldModel.createTextInput());
    } else {
        mAddressFields.put(AddressField.ADMIN_AREA, EditorFieldModel.createDropdown());
    }

    // Admin areas need to be fetched in two cases:
    // 1. Initial loading of the form.
    // 2. When the selected country is changed in the form.
    // mRecentlySelectedCountry is not null if and only if it's the second case
    if (mRecentlySelectedCountry != null) {
        dismissProgressDialog();
        // 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(
                mRecentlySelectedCountry, Locale.getDefault().getLanguage(), adminAreas);
        // Notify EditorDialog that the fields in the model have changed. EditorDialog should
        // re-read the model and update the UI accordingly.
        mHandler.post(mCountryChangeCallback);
    } else {
        // This should be called when all required fields are put in mAddressField.
        setAddressFieldValuesFromCache();
        addAddressFieldsToEditor(
                mProfile.getCountryCode(), mProfile.getLanguageCode(), adminAreas);
        mEditorDialog.show(mEditor);
    }
}
 
Example #10
Source File: AddressEditor.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Adds fields to the editor model based on the country and language code of
 * the profile that's being edited.
 */
private void addAddressFieldsToEditor(
        String countryCode, String languageCode, String[] adminAreas) {
    mAddressUiComponents =
            mAutofillProfileBridge.getAddressUiComponents(countryCode, languageCode);
    // In terms of order, country must be the first field.
    mEditor.addField(mCountryField);
    for (int i = 0; i < mAddressUiComponents.size(); i++) {
        AddressUiComponent component = mAddressUiComponents.get(i);

        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 || component.id == AddressField.LOCALITY
                || component.id == AddressField.DEPENDENT_LOCALITY);

        if (component.id == AddressField.ADMIN_AREA && 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));
        } else {
            field.setRequiredErrorMessage(null);
        }
        mEditor.addField(field);
    }
    // Phone number must be the last field.
    mEditor.addField(mPhoneField);
}
 
Example #11
Source File: ContactEditor.java    From delion with Apache License 2.0 4 votes vote down vote up
@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
            ? new EditorFieldModel(EditorFieldModel.INPUT_TYPE_HINT_PHONE,
                      mContext.getString(R.string.autofill_profile_editor_phone_number),
                      mPhoneNumbers, getPhoneValidator(),
                      mContext.getString(R.string.payments_phone_required_validation_message),
                      mContext.getString(R.string.payments_phone_invalid_validation_message),
                      contact.getPayerPhone())
            : null;

    final EditorFieldModel emailField = mRequestPayerEmail
            ? new EditorFieldModel(EditorFieldModel.INPUT_TYPE_HINT_EMAIL,
                      mContext.getString(R.string.autofill_profile_editor_email_address),
                      mEmailAddresses, getEmailValidator(),
                      mContext.getString(R.string.payments_email_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);
            }

            PersonalDataManager.getInstance().setProfile(contact.getProfile());
            contact.completeContact(phone, email);
            callback.onResult(contact);
        }
    });

    mEditorView.show(editor);
}
 
Example #12
Source File: CardEditor.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Adds the billing address dropdown to the editor with the following items.
 *
 * | "select"           |
 * | complete address 1 |
 * | complete address 2 |
 *      ...
 * | complete address n |
 * | "add address"      |
 */
private void addBillingAddressDropdown(EditorModel editor, final CreditCard card) {
    final List<DropdownKeyValue> billingAddresses = new ArrayList<>();

    for (int i = 0; i < mProfilesForBillingAddress.size(); ++i) {
        billingAddresses.add(new DropdownKeyValue(mProfilesForBillingAddress.get(i).getGUID(),
                mProfilesForBillingAddress.get(i).getLabel()));
    }

    billingAddresses.add(new DropdownKeyValue(BILLING_ADDRESS_ADD_NEW,
            mContext.getString(R.string.autofill_create_profile)));

    // Don't cache the billing address dropdown, because the user may have added or removed
    // profiles. Also pass the "Select" dropdown item as a hint to the dropdown constructor.
    mBillingAddressField = EditorFieldModel.createDropdown(
            mContext.getString(R.string.autofill_credit_card_editor_billing_address),
            billingAddresses, mContext.getString(R.string.select));

    // The billing address is required.
    mBillingAddressField.setRequiredErrorMessage(
            mContext.getString(R.string.payments_field_required_validation_message));

    mBillingAddressField.setDropdownCallback(new Callback<Pair<String, Runnable>>() {
        @Override
        public void onResult(final Pair<String, Runnable> eventData) {
            if (!BILLING_ADDRESS_ADD_NEW.equals(eventData.first)) {
                if (mObserverForTest != null) {
                    mObserverForTest.onPaymentRequestServiceBillingAddressChangeProcessed();
                }
                return;
            }

            mAddressEditor.edit(null, new Callback<AutofillAddress>() {
                @Override
                public void onResult(AutofillAddress billingAddress) {
                    if (billingAddress == null) {
                        // User has cancelled the address editor.
                        mBillingAddressField.setValue(null);
                    } else {
                        // User has added a new complete address. Add it to the top of the
                        // dropdown.
                        mProfilesForBillingAddress.add(billingAddress.getProfile());
                        billingAddresses.add(
                                0, new DropdownKeyValue(billingAddress.getIdentifier(),
                                           billingAddress.getSublabel()));
                        mBillingAddressField.setDropdownKeyValues(billingAddresses);
                        mBillingAddressField.setValue(billingAddress.getIdentifier());
                    }

                    // Let the card editor UI re-read the model and re-create UI elements.
                    mHandler.post(eventData.second);
                }
            });
        }
    });

    if (mBillingAddressField.getDropdownKeys().contains(card.getBillingAddressId())) {
        mBillingAddressField.setValue(card.getBillingAddressId());
    }

    editor.addField(mBillingAddressField);
}
 
Example #13
Source File: ContactEditor.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@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, null, false) : toEdit;

    final EditorFieldModel nameField = mRequestPayerName
            ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME,
                      mContext.getString(R.string.payments_name_field_in_contact_details),
                      mPayerNames, null, null,
                      mContext.getString(R.string.payments_field_required_validation_message),
                      null, contact.getPayerName())
            : null;

    final EditorFieldModel phoneField = mRequestPayerPhone
            ? 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),
                      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(), null,
                      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 (nameField != null) editor.addField(nameField);
    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 name = null;
            String phone = null;
            String email = null;
            AutofillProfile profile = contact.getProfile();

            if (nameField != null) {
                name = nameField.getValue().toString();
                profile.setFullName(name);
            }

            if (phoneField != null) {
                phone = phoneField.getValue().toString();
                profile.setPhoneNumber(phone);
            }

            if (emailField != null) {
                email = emailField.getValue().toString();
                profile.setEmailAddress(email);
            }

            profile.setGUID(PersonalDataManager.getInstance().setProfileToLocal(profile));
            profile.setIsLocal(true);
            contact.completeContact(profile.getGUID(), name, phone, email);
            callback.onResult(contact);
        }
    });

    mEditorView.show(editor);
}
 
Example #14
Source File: ContactEditor.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void edit(
        @Nullable final AutofillContact toEdit, final Callback<AutofillContact> callback) {
    super.edit(toEdit, callback);

    final AutofillContact contact = toEdit == null
            ? new AutofillContact(mContext, new AutofillProfile(), null, null, null,
                      INVALID_NAME | INVALID_PHONE_NUMBER | INVALID_EMAIL, mRequestPayerName,
                      mRequestPayerPhone, mRequestPayerEmail)
            : toEdit;

    final EditorFieldModel nameField = mRequestPayerName
            ? EditorFieldModel.createTextInput(EditorFieldModel.INPUT_TYPE_HINT_PERSON_NAME,
                      mContext.getString(R.string.payments_name_field_in_contact_details),
                      mPayerNames, null, null,
                      mContext.getString(R.string.payments_field_required_validation_message),
                      null, contact.getPayerName())
            : null;

    final EditorFieldModel phoneField = mRequestPayerPhone
            ? 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),
                      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(), null,
                      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(toEdit == null
            ? mContext.getString(R.string.payments_add_contact_details_label)
            : toEdit.getEditTitle());

    if (nameField != null) editor.addField(nameField);
    if (phoneField != null) editor.addField(phoneField);
    if (emailField != null) editor.addField(emailField);

    // If the user clicks [Cancel], send |toEdit| contact back to the caller, which was the
    // original state (could be null, a complete contact, a partial contact).
    editor.setCancelCallback(new Runnable() {
        @Override
        public void run() {
            callback.onResult(toEdit);
        }
    });

    editor.setDoneCallback(new Runnable() {
        @Override
        public void run() {
            String name = null;
            String phone = null;
            String email = null;
            AutofillProfile profile = contact.getProfile();

            if (nameField != null) {
                name = nameField.getValue().toString();
                profile.setFullName(name);
            }

            if (phoneField != null) {
                phone = phoneField.getValue().toString();
                profile.setPhoneNumber(phone);
            }

            if (emailField != null) {
                email = emailField.getValue().toString();
                profile.setEmailAddress(email);
            }

            profile.setGUID(PersonalDataManager.getInstance().setProfileToLocal(profile));
            profile.setIsLocal(true);
            contact.completeContact(profile.getGUID(), name, phone, email);
            callback.onResult(contact);
        }
    });

    mEditorDialog.show(editor);
}