Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| index 360ab4ad67ee38eef702f31cb2ffa1f82a82e97e..47fa6873468e0e40b6283ef5f99bbd29f7e21e39 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/PaymentRequestImpl.java |
| @@ -45,6 +45,8 @@ import org.json.JSONObject; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| +import java.util.Collections; |
| +import java.util.Comparator; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.List; |
| @@ -75,6 +77,8 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| private static final String TAG = "cr_PaymentRequest"; |
| + private static final int SUGGESTIONS_LIMIT = 4; |
| + |
| private static PaymentRequestServiceObserverForTest sObserverForTest; |
| private final Handler mHandler = new Handler(); |
| @@ -225,7 +229,6 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| if (requestShipping) { |
| mAddressEditor = new AddressEditor(); |
| List<AutofillAddress> addresses = new ArrayList<>(); |
| - int firstCompleteAddressIndex = SectionInformation.NO_SELECTION; |
| for (int i = 0; i < profiles.size(); i++) { |
| AutofillProfile profile = profiles.get(i); |
| @@ -233,23 +236,41 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| boolean isComplete = mAddressEditor.isProfileComplete(profile); |
| addresses.add(new AutofillAddress(profile, isComplete)); |
| - if (isComplete && firstCompleteAddressIndex == SectionInformation.NO_SELECTION |
| - && !mMerchantNeedsShippingAddress) { |
| - firstCompleteAddressIndex = i; |
| - } |
| } |
| - // The shipping address section automatically selects the first complete entry. |
| + // Suggest complete addresses first. |
| + Collections.sort(addresses, new Comparator<AutofillAddress>() { |
| + @Override |
| + public int compare(AutofillAddress a, AutofillAddress b) { |
| + if (a.isComplete() == b.isComplete()) { |
|
please use gerrit instead
2016/07/07 13:59:36
Make AutofillAddress and AutofillContact implement
sebsg
2016/07/08 07:54:52
Done.
|
| + return 0; |
| + } else if (a.isComplete()) { |
| + return -1; |
| + } else { |
| + return 1; |
| + } |
| + } |
| + }); |
| + |
| + // Limit the number of suggestions. |
| + List<AutofillAddress> topAddresses = new ArrayList<AutofillAddress>( |
|
please use gerrit instead
2016/07/07 13:59:36
addresses = addresses.subList(0, Math.min(addresse
sebsg
2016/07/08 07:54:51
Done.
|
| + addresses.subList(0, Math.min(addresses.size(), SUGGESTIONS_LIMIT))); |
| + |
| + // Automatically select the first complete address if one is present and if the merchant |
| + // does not require a shipping address to calculate shipping costs. |
| + int firstCompleteAddressIndex = |
| + (!mMerchantNeedsShippingAddress && topAddresses.size() > 0 |
|
please use gerrit instead
2016/07/07 13:59:36
I think "!addresses.isEmpty()" is more canonical t
sebsg
2016/07/08 07:54:52
Done.
|
| + && topAddresses.get(0).isComplete()) |
| + ? 0 : SectionInformation.NO_SELECTION; |
| mShippingAddressesSection = |
| new SectionInformation(PaymentRequestUI.TYPE_SHIPPING_ADDRESSES, |
| - firstCompleteAddressIndex, addresses); |
| + firstCompleteAddressIndex, topAddresses); |
| } |
| if (requestPayerPhone || requestPayerEmail) { |
| Set<String> uniqueContactInfos = new HashSet<>(); |
| mContactEditor = new ContactEditor(requestPayerPhone, requestPayerEmail); |
| List<AutofillContact> contacts = new ArrayList<>(); |
| - int firstCompleteContactIndex = SectionInformation.NO_SELECTION; |
| for (int i = 0; i < profiles.size(); i++) { |
| AutofillProfile profile = profiles.get(i); |
| @@ -270,17 +291,34 @@ public class PaymentRequestImpl implements PaymentRequest, PaymentRequestUI.Clie |
| boolean isComplete = |
| mContactEditor.isContactInformationComplete(phone, email); |
| contacts.add(new AutofillContact(profile, phone, email, isComplete)); |
| - if (isComplete |
| - && firstCompleteContactIndex == SectionInformation.NO_SELECTION) { |
| - firstCompleteContactIndex = i; |
| - } |
| } |
| } |
| } |
| - // The contact section automatically selects the first complete entry. |
| + // Suggest complete contact infos first. |
| + Collections.sort(contacts, new Comparator<AutofillContact>() { |
| + @Override |
| + public int compare(AutofillContact a, AutofillContact b) { |
| + if (a.isComplete() == b.isComplete()) { |
|
please use gerrit instead
2016/07/07 13:59:36
Would this be considered bad? ;-)
return (a.isCom
sebsg
2016/07/08 07:54:52
I prefer the current version but I'll defer decisi
|
| + return 0; |
| + } else if (a.isComplete()) { |
| + return -1; |
| + } else { |
| + return 1; |
| + } |
| + } |
| + }); |
| + |
| + // Limit the number of suggestions. |
| + List<AutofillContact> topContacts = new ArrayList<AutofillContact>( |
|
please use gerrit instead
2016/07/07 13:59:36
contacts = contacts.subList(...);
sebsg
2016/07/08 07:54:52
Done.
|
| + contacts.subList(0, Math.min(contacts.size(), SUGGESTIONS_LIMIT))); |
| + |
| + // Automatically select the first complete address if one is present. |
| + int firstCompleteContactIndex = (topContacts.size() > 0 |
|
please use gerrit instead
2016/07/07 13:59:36
!contacts.isEmpty()
sebsg
2016/07/08 07:54:51
Done.
|
| + && topContacts.get(0).isComplete()) |
| + ? 0 : SectionInformation.NO_SELECTION; |
| mContactSection = new SectionInformation( |
| - PaymentRequestUI.TYPE_CONTACT_DETAILS, firstCompleteContactIndex, contacts); |
| + PaymentRequestUI.TYPE_CONTACT_DETAILS, firstCompleteContactIndex, topContacts); |
| } |
| mPendingApps = new ArrayList<>(mApps); |