Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/ui/ContactDetailsSection.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/ContactDetailsSection.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/ContactDetailsSection.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c9b59a37441e3569dee34f3c5ac4eebf0cf4afa7 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/ContactDetailsSection.java |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.payments.ui; |
| + |
| +import android.content.Context; |
| +import android.text.TextUtils; |
| + |
| +import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| +import org.chromium.chrome.browser.payments.AutofillAddress; |
| +import org.chromium.chrome.browser.payments.AutofillContact; |
| +import org.chromium.chrome.browser.payments.ContactEditor; |
| +import org.chromium.chrome.browser.payments.PaymentRequestImpl; |
| +import org.chromium.chrome.browser.payments.PaymentRequestJourneyLogger; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Collection; |
| +import java.util.Collections; |
| +import java.util.HashSet; |
| +import java.util.List; |
| +import java.util.Set; |
| + |
| +/** |
| + * The data to show in the contact details section where the user can select something. |
| + */ |
| +public class ContactDetailsSection extends SectionInformation { |
| + private final Context mContext; |
| + private final ContactEditor mContactEditor; |
| + private final PaymentRequestJourneyLogger mJourneyLogger = new PaymentRequestJourneyLogger(); |
| + |
| + private List<AutofillProfile> mProfiles; |
| + |
| + /** |
| + * Builds a Contact section from a list of AutofillProfile. |
| + * |
| + * @param context Context |
| + * @param autofillProfiles The list of profiles to build from. |
| + * @param contactEditor The Contact Editor associated with this flow. |
| + */ |
| + public ContactDetailsSection(Context context, Collection<AutofillProfile> autofillProfiles, |
| + ContactEditor contactEditor) { |
| + // Initially no items are selected, but they are updated later in the constructor. |
| + super(PaymentRequestUI.TYPE_CONTACT_DETAILS, null); |
| + |
| + mContext = context; |
| + mContactEditor = contactEditor; |
| + // Copy the profiles from which this section is derived. |
| + mProfiles = new ArrayList<AutofillProfile>(autofillProfiles); |
| + |
| + // Refresh the contact section items and selection. |
| + updateContactListFromAutofillProfiles(); |
| + } |
| + |
| + /** |
| + * Update the contact details section items (and potentially the selection) with an address. |
| + * |
| + * @param editedAddress the new or edited address with which to update the contacts section. |
| + */ |
| + public void updateWithAutofillAddress(final AutofillAddress editedAddress) { |
|
please use gerrit instead
2016/12/20 18:45:21
Does not appear to be a need for finality here. Th
Mathieu
2016/12/20 20:21:41
Done.
|
| + boolean profileExisted = false; |
| + // If the profile is currently being displayed, remove the profile and add the updated one |
| + // to the front of the list in anticipation of the contacts section refresh. |
| + for (int i = 0; i < mProfiles.size(); i++) { |
| + if (mProfiles.get(i).getGUID().equals(editedAddress.getProfile().getGUID())) { |
| + mProfiles.remove(i); |
| + mProfiles.add(0, editedAddress.getProfile()); |
|
please use gerrit instead
2016/12/20 18:45:21
Need to update selected item index in the parent.
Mathieu
2016/12/20 20:21:41
Acknowledged.
|
| + profileExisted = true; |
| + break; |
| + } |
| + } |
| + if (!profileExisted) { |
| + // Add the new address to |mProfiles| in anticipation of the contacts section refresh |
| + // below. We add it first so it comes up towards the top of the list. |
| + mProfiles.add(0, editedAddress.getProfile()); |
|
please use gerrit instead
2016/12/20 18:45:21
Ditto
Mathieu
2016/12/20 20:21:41
Acknowledged.
|
| + } |
| + |
| + // Refresh the contact section items and selection. |
| + updateContactListFromAutofillProfiles(); |
|
please use gerrit instead
2016/12/20 18:45:21
Let's not cut-off the bottom contact information a
Mathieu
2016/12/20 20:21:41
Acknowledged.
|
| + } |
| + |
| + /** Recomputes the list of displayed contacts and possibly updates the selection. */ |
| + private void updateContactListFromAutofillProfiles() { |
|
please use gerrit instead
2016/12/20 18:45:21
Need to update selected item index after the sorti
Mathieu
2016/12/20 20:21:41
Acknowledged.
|
| + Set<String> uniqueContactInfos = new HashSet<>(); |
| + List<AutofillContact> contacts = new ArrayList<>(); |
| + |
| + for (int i = 0; i < mProfiles.size(); i++) { |
| + AutofillProfile profile = mProfiles.get(i); |
| + String name = mContactEditor.getRequestPayerName() |
| + && !TextUtils.isEmpty(profile.getFullName()) |
| + ? profile.getFullName() |
| + : null; |
| + String phone = mContactEditor.getRequestPayerPhone() |
| + && !TextUtils.isEmpty(profile.getPhoneNumber()) |
| + ? profile.getPhoneNumber() |
| + : null; |
| + String email = mContactEditor.getRequestPayerEmail() |
| + && !TextUtils.isEmpty(profile.getEmailAddress()) |
| + ? profile.getEmailAddress() |
| + : null; |
| + mContactEditor.addPayerNameIfValid(name); |
| + mContactEditor.addPhoneNumberIfValid(phone); |
| + mContactEditor.addEmailAddressIfValid(email); |
| + |
| + if (name != null || phone != null || email != null) { |
| + // Different profiles can have identical contact info. Do not add the same |
| + // contact info to the list twice. |
| + String uniqueContactInfo = name + phone + email; |
|
please use gerrit instead
2016/12/20 18:45:21
FYI, Seb is working on improving this algorithm.
Mathieu
2016/12/20 20:21:41
Yep. Hopefully he can just drop his code in here a
|
| + if (!uniqueContactInfos.contains(uniqueContactInfo)) { |
| + uniqueContactInfos.add(uniqueContactInfo); |
| + |
| + @ContactEditor.CompletionStatus |
| + int completionStatus = |
| + mContactEditor.checkContactCompletionStatus(name, phone, email); |
| + contacts.add(new AutofillContact( |
| + mContext, profile, name, phone, email, completionStatus)); |
| + } |
| + } |
| + } |
| + |
| + // Suggest complete contact infos first. |
| + Collections.sort(contacts, PaymentRequestImpl.COMPLETENESS_COMPARATOR); |
| + |
| + // Limit the number of suggestions. |
| + contacts = contacts.subList( |
| + 0, Math.min(contacts.size(), PaymentRequestImpl.SUGGESTIONS_LIMIT)); |
| + |
| + // Log the number of suggested contact infos. |
| + mJourneyLogger.setNumberOfSuggestionsShown( |
| + PaymentRequestJourneyLogger.SECTION_CONTACT_INFO, contacts.size()); |
| + |
| + // Automatically select the first address if it is complete. |
|
please use gerrit instead
2016/12/20 18:45:21
s/address/contact/
Mathieu
2016/12/20 20:21:41
Done.
|
| + int firstCompleteContactIndex = SectionInformation.NO_SELECTION; |
| + if (!contacts.isEmpty() && contacts.get(0).isComplete()) { |
| + firstCompleteContactIndex = 0; |
| + } |
| + |
| + updateItemsWithCollection(firstCompleteContactIndex, contacts); |
| + } |
| +} |