Search in sources :

Example 1 with EditorModel

use of org.chromium.chrome.browser.payments.ui.EditorModel 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);
}
Also used : EditorModel(org.chromium.chrome.browser.payments.ui.EditorModel) AutofillProfile(org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile) EditorFieldModel(org.chromium.chrome.browser.payments.ui.EditorFieldModel) HashMap(java.util.HashMap) Map(java.util.Map) AutofillProfileBridge(org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge) Pair(android.util.Pair)

Example 2 with EditorModel

use of org.chromium.chrome.browser.payments.ui.EditorModel 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);
}
Also used : EditorFieldModel(org.chromium.chrome.browser.payments.ui.EditorFieldModel) EditorModel(org.chromium.chrome.browser.payments.ui.EditorModel) AutofillProfile(org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile)

Example 3 with EditorModel

use of org.chromium.chrome.browser.payments.ui.EditorModel in project AndroidChromium by JackyAndroid.

the class CardEditor method edit.

/**
     * Builds and shows an editor model with the following fields for local cards.
     *
     * [ accepted card types hint images     ]
     * [ card number                         ]
     * [ name on card                        ]
     * [ expiration month ][ expiration year ]
     * [ billing address dropdown            ]
     * [ save this card checkbox             ] <-- Shown only for new cards.
     *
     * Server cards have the following fields instead.
     *
     * [ card's obfuscated number            ]
     * [ billing address dropdown            ]
     */
@Override
public void edit(@Nullable final AutofillPaymentInstrument toEdit, final Callback<AutofillPaymentInstrument> callback) {
    super.edit(toEdit, callback);
    // If |toEdit| is null, we're creating a new credit card.
    final boolean isNewCard = toEdit == null;
    // Ensure that |instrument| and |card| are never null.
    final AutofillPaymentInstrument instrument = isNewCard ? new AutofillPaymentInstrument(mWebContents, new CreditCard(), null) : toEdit;
    final CreditCard card = instrument.getCard();
    // The title of the editor depends on whether we're adding a new card or editing an existing
    // card.
    final EditorModel editor = new EditorModel(mContext.getString(isNewCard ? R.string.payments_create_card : R.string.payments_edit_card));
    if (card.getIsLocal()) {
        Calendar calendar = null;
        try {
            calendar = mCalendar.get();
        } catch (InterruptedException | ExecutionException e) {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    callback.onResult(null);
                }
            });
            return;
        }
        assert calendar != null;
        // Let user edit any part of the local card.
        addLocalCardInputs(editor, card, calendar);
    } else {
        // Display some information about the server card.
        editor.addField(EditorFieldModel.createLabel(card.getObfuscatedNumber(), card.getName(), card.getFormattedExpirationDate(mContext), card.getIssuerIconDrawableId()));
    }
    // Always show the billing address dropdown.
    addBillingAddressDropdown(editor, card);
    // Allow saving new cards on disk.
    if (isNewCard)
        addSaveCardCheckbox(editor);
    // If the user clicks [Cancel], send a null card 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 card "complete," and send it
    // back to the caller.
    editor.setDoneCallback(new Runnable() {

        @Override
        public void run() {
            commitChanges(card, isNewCard);
            instrument.completeInstrument(card, mProfilesForBillingAddress.get(card.getBillingAddressId()));
            callback.onResult(instrument);
        }
    });
    mEditorView.show(editor);
}
Also used : EditorModel(org.chromium.chrome.browser.payments.ui.EditorModel) Calendar(java.util.Calendar) ExecutionException(java.util.concurrent.ExecutionException) CreditCard(org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard)

Aggregations

EditorModel (org.chromium.chrome.browser.payments.ui.EditorModel)3 AutofillProfile (org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile)2 EditorFieldModel (org.chromium.chrome.browser.payments.ui.EditorFieldModel)2 Pair (android.util.Pair)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 CreditCard (org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard)1 AutofillProfileBridge (org.chromium.chrome.browser.preferences.autofill.AutofillProfileBridge)1