Commit 78a6c50c authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Fixed crash in AA payment request.

This CL fixes two possible crashes in AA PR:
 1) When updating the contact details section, even though the contact details were not requested (and are not visible).
 2) When updating the contact details section before a contact editor was set. Converting autofill profiles to autofill contacts is currently done via ContactDetailsSection, which in turn relies on a non-null ContactEditor for the conversion.

1) is easy to fix (wrap in if statement).
As a workaround for 2), this CL creates a temporary contact editor in those cases. A proper fix would decouple the UI aspects (editor) from the backend aspects (converting autofill profiles to contacts), but this requires some more refactoring and is out-of-scope for now.

Bug: 806868
Change-Id: I26e511d405930166ebac858f8f878d924f6c1843
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1627352
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662606}
parent 7ccf3d71
......@@ -195,9 +195,18 @@ class AssistantPaymentRequestBinder
if (autofillProfiles == null) {
autofillProfiles = Collections.emptyList();
}
view.mContactDetailsSection.onProfilesChanged(autofillProfiles);
view.mPaymentMethodSection.onProfilesChanged(autofillProfiles);
view.mShippingAddressSection.onProfilesChanged(autofillProfiles);
if (shouldShowContactDetails(model)) {
view.mContactDetailsSection.onProfilesChanged(autofillProfiles,
model.get(AssistantPaymentRequestModel.REQUEST_EMAIL),
model.get(AssistantPaymentRequestModel.REQUEST_NAME),
model.get(AssistantPaymentRequestModel.REQUEST_PHONE));
}
if (model.get(AssistantPaymentRequestModel.REQUEST_PAYMENT)) {
view.mPaymentMethodSection.onProfilesChanged(autofillProfiles);
}
if (model.get(AssistantPaymentRequestModel.REQUEST_SHIPPING_ADDRESS)) {
view.mShippingAddressSection.onProfilesChanged(autofillProfiles);
}
return true;
}
return false;
......
......@@ -40,6 +40,13 @@ public class AssistantPaymentRequestContactDetailsSection
public void setEditor(ContactEditor editor) {
mEditor = editor;
if (mEditor == null) {
return;
}
for (AutofillContact contact : getItems()) {
addAutocompleteInformationToEditor(contact);
}
}
@Override
......@@ -47,7 +54,7 @@ public class AssistantPaymentRequestContactDetailsSection
if (mEditor != null) {
mIgnoreProfileChangeNotifications = true;
mEditor.edit(oldItem,
editedOption -> { onItemCreatedOrEdited(oldItem, oldFullView, editedOption); });
editedOption -> onItemCreatedOrEdited(oldItem, oldFullView, editedOption));
mIgnoreProfileChangeNotifications = false;
}
}
......@@ -98,20 +105,32 @@ public class AssistantPaymentRequestContactDetailsSection
* The Chrome profiles have changed externally. This will rebuild the UI with the new/changed
* set of profiles, while keeping the selected item if possible.
*/
void onProfilesChanged(List<AutofillProfile> profiles) {
void onProfilesChanged(List<AutofillProfile> profiles, boolean requestPayerEmail,
boolean requestPayerName, boolean requestPayerPhone) {
if (mIgnoreProfileChangeNotifications) {
return;
}
if (!requestPayerEmail && !requestPayerName && !requestPayerPhone) {
return;
}
// Note: we create a temporary editor (necessary for converting profiles to contacts)
// instead of using mEditor, which may be null.
ContactEditor tempEditor =
new ContactEditor(requestPayerName, requestPayerPhone, requestPayerEmail, false);
AutofillContact previouslySelectedContact = mSelectedOption;
// Convert profiles into a list of |AutofillContact|.
int selectedContactIndex = -1;
ContactDetailsSection sectionInformation =
new ContactDetailsSection(mContext, profiles, mEditor, null);
new ContactDetailsSection(mContext, profiles, tempEditor, null);
List<AutofillContact> contacts = new ArrayList<>();
for (int i = 0; i < sectionInformation.getSize(); i++) {
AutofillContact contact = (AutofillContact) sectionInformation.getItem(i);
if (contact == null) {
continue;
}
contacts.add(contact);
if (previouslySelectedContact != null
&& TextUtils.equals(
......@@ -126,7 +145,13 @@ public class AssistantPaymentRequestContactDetailsSection
@Override
protected void onItemAddedOrUpdated(AutofillContact contact) {
// Update autocomplete information in the editor.
addAutocompleteInformationToEditor(contact);
}
private void addAutocompleteInformationToEditor(AutofillContact contact) {
if (mEditor == null) {
return;
}
mEditor.addEmailAddressIfValid(contact.getPayerEmail());
mEditor.addPayerNameIfValid(contact.getPayerName());
mEditor.addPhoneNumberIfValid(contact.getPayerPhone());
......
......@@ -33,8 +33,8 @@ public abstract class AssistantPaymentRequestSection<T extends EditableOption> {
private final AssistantChoiceList mItemsView;
private final View mSummaryView;
private final int mFullViewResId;
private final List<Item> mItems;
private final int mTitleToContentPadding;
private final List<Item> mItems;
protected final Context mContext;
protected T mSelectedOption;
......@@ -171,6 +171,17 @@ public abstract class AssistantPaymentRequestSection<T extends EditableOption> {
}
}
/**
* Returns the list of items.
*/
List<T> getItems() {
List<T> items = new ArrayList<>();
for (Item item : mItems) {
items.add(item.mOption);
}
return items;
}
void addOrUpdateItem(@Nullable T option, boolean select) {
if (option == null) {
return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment