Commit cf85b0d1 authored by Lukasz Suder's avatar Lukasz Suder Committed by Commit Bot

[Autofill Assistant] Sorts profiles on PR using the default email.

When default Email is available (defined as email used to start
Autofill Assistant) it is used to change the order of Profiles inside
Payment Request.

The Profiles containing this email will be displayed on top,
making them a default choice.

Bug: 806868
Change-Id: Iae54abcf009a563a120a6afa09ef02187a82abd8
Reviewed-on: https://chromium-review.googlesource.com/c/1350955
Commit-Queue: Lukasz Suder <lsuder@chromium.org>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611213}
parent d8c78a28
...@@ -6,10 +6,12 @@ package org.chromium.chrome.browser.autofill_assistant; ...@@ -6,10 +6,12 @@ package org.chromium.chrome.browser.autofill_assistant;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap; import android.support.v4.util.ArrayMap;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
...@@ -63,6 +65,7 @@ public class AutofillAssistantPaymentRequest { ...@@ -63,6 +65,7 @@ public class AutofillAssistantPaymentRequest {
private final AddressEditor mAddressEditor; private final AddressEditor mAddressEditor;
private final Map<String, PaymentMethodData> mMethodData; private final Map<String, PaymentMethodData> mMethodData;
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private final String mDefaultEmail;
private PaymentRequestUI mUI; private PaymentRequestUI mUI;
private ContactEditor mContactEditor; private ContactEditor mContactEditor;
...@@ -104,12 +107,15 @@ public class AutofillAssistantPaymentRequest { ...@@ -104,12 +107,15 @@ public class AutofillAssistantPaymentRequest {
* @supportedBasicCardNetworks Optional array of supported basic card networks (see {@link * @supportedBasicCardNetworks Optional array of supported basic card networks (see {@link
* BasicCardUtils}). If non-empty, only the specified card networks * BasicCardUtils}). If non-empty, only the specified card networks
* will be available for the basic-card payment method. * will be available for the basic-card payment method.
* @defaultEmail Optional email. When provided Profiles with this email will be
* shown on top.
*/ */
public AutofillAssistantPaymentRequest(WebContents webContents, PaymentOptions paymentOptions, public AutofillAssistantPaymentRequest(WebContents webContents, PaymentOptions paymentOptions,
String title, String[] supportedBasicCardNetworks) { String title, String[] supportedBasicCardNetworks, @Nullable String defaultEmail) {
mWebContents = webContents; mWebContents = webContents;
mPaymentOptions = paymentOptions; mPaymentOptions = paymentOptions;
mTitle = title; mTitle = title;
mDefaultEmail = defaultEmail;
// This feature should only works in non-incognito mode. // This feature should only works in non-incognito mode.
mAddressEditor = new AddressEditor(/* emailFieldIncluded= */ true, /* saveToDisk= */ true); mAddressEditor = new AddressEditor(/* emailFieldIncluded= */ true, /* saveToDisk= */ true);
...@@ -175,6 +181,18 @@ public class AutofillAssistantPaymentRequest { ...@@ -175,6 +181,18 @@ public class AutofillAssistantPaymentRequest {
|| mPaymentOptions.requestPayerPhone || mPaymentOptions.requestPayerEmail) { || mPaymentOptions.requestPayerPhone || mPaymentOptions.requestPayerEmail) {
profiles = PersonalDataManager.getInstance().getProfilesToSuggest( profiles = PersonalDataManager.getInstance().getProfilesToSuggest(
/* includeNameInLabel= */ false); /* includeNameInLabel= */ false);
if (mDefaultEmail != null && profiles != null) {
// The profile with default email should be shown as first. Following profiles are
// sorted in an alphabetic order.
Collections.sort(profiles, (a, b) -> {
int compareResult = ApiCompatibilityUtils.compareBoolean(
mDefaultEmail.equals(b.getEmailAddress()),
mDefaultEmail.equals(a.getEmailAddress()));
if (compareResult != 0) return compareResult;
return b.getEmailAddress().compareTo(a.getEmailAddress());
});
}
} }
if (mPaymentOptions.requestShipping) { if (mPaymentOptions.requestShipping) {
......
...@@ -38,6 +38,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -38,6 +38,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
/** OAuth2 scope that RPCs require. */ /** OAuth2 scope that RPCs require. */
private static final String AUTH_TOKEN_TYPE = private static final String AUTH_TOKEN_TYPE =
"oauth2:https://www.googleapis.com/auth/userinfo.profile"; "oauth2:https://www.googleapis.com/auth/userinfo.profile";
private static final String PARAMETER_USER_EMAIL = "USER_EMAIL";
private final WebContents mWebContents; private final WebContents mWebContents;
private final String mInitialUrl; private final String mInitialUrl;
...@@ -57,9 +58,12 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -57,9 +58,12 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
private boolean mAccountInitialized; private boolean mAccountInitialized;
/** /**
* Account to authenticate as when sending RPCs. Not relevant until the accounts have been * Account that was used to initiate AutofillAssistant.
* fetched, and mAccountInitialized set to true. Can still be null after the accounts are *
* fetched, in which case authentication is disabled. * <p>This account is used to authenticate when sending RPCs and as default account for Payment
* Request. Not relevant until the accounts have been fetched, and mAccountInitialized set to
* true. Can still be null after the accounts are fetched, in which case authentication is
* disabled.
*/ */
@Nullable @Nullable
private Account mAccount; private Account mAccount;
...@@ -81,7 +85,8 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -81,7 +85,8 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
parameters.values().toArray(new String[parameters.size()]), parameters.values().toArray(new String[parameters.size()]),
LocaleUtils.getDefaultLocaleString(), getCountryIso()); LocaleUtils.getDefaultLocaleString(), getCountryIso());
chooseAccountAsync(activity.getInitialIntent().getExtras()); chooseAccountAsync(
parameters.get(PARAMETER_USER_EMAIL), activity.getInitialIntent().getExtras());
} }
void setUiDelegateHolder(UiDelegateHolder uiDelegateHolder) { void setUiDelegateHolder(UiDelegateHolder uiDelegateHolder) {
...@@ -283,9 +288,11 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -283,9 +288,11 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
paymentOptions.requestPayerEmail = requestPayerEmail; paymentOptions.requestPayerEmail = requestPayerEmail;
paymentOptions.shippingType = shippingType; paymentOptions.shippingType = shippingType;
String defaultEmail = mAccount != null ? mAccount.name : "";
mUiDelegateHolder.performUiOperation(uiDelegate -> { mUiDelegateHolder.performUiOperation(uiDelegate -> {
uiDelegate.showPaymentRequest(mWebContents, paymentOptions, title, uiDelegate.showPaymentRequest(mWebContents, paymentOptions, title,
supportedBasicCardNetworks, (selectedPaymentInformation -> { supportedBasicCardNetworks, defaultEmail, (selectedPaymentInformation -> {
uiDelegate.closePaymentRequest(); uiDelegate.closePaymentRequest();
if (selectedPaymentInformation.succeed) { if (selectedPaymentInformation.succeed) {
nativeOnGetPaymentInformation(mUiControllerAndroid, nativeOnGetPaymentInformation(mUiControllerAndroid,
...@@ -406,7 +413,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -406,7 +413,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
} }
/** Choose an account to authenticate as for making RPCs to the backend. */ /** Choose an account to authenticate as for making RPCs to the backend. */
private void chooseAccountAsync(Bundle extras) { private void chooseAccountAsync(@Nullable String accountFromParameter, Bundle extras) {
AccountManagerFacade.get().tryGetGoogleAccounts(accounts -> { AccountManagerFacade.get().tryGetGoogleAccounts(accounts -> {
if (accounts.size() == 1) { if (accounts.size() == 1) {
// If there's only one account, there aren't any doubts. // If there's only one account, there aren't any doubts.
...@@ -421,7 +428,17 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -421,7 +428,17 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
onAccountChosen(signedIn); onAccountChosen(signedIn);
return; return;
} }
if (accountFromParameter != null) {
Account account = findAccountByName(accounts, accountFromParameter);
if (account != null) {
onAccountChosen(account);
return;
}
}
for (String extra : extras.keySet()) { for (String extra : extras.keySet()) {
// TODO(crbug.com/806868): Deprecate ACCOUNT_NAME.
if (extra.endsWith("ACCOUNT_NAME")) { if (extra.endsWith("ACCOUNT_NAME")) {
Account account = findAccountByName(accounts, extras.getString(extra)); Account account = findAccountByName(accounts, extras.getString(extra));
if (account != null) { if (account != null) {
......
...@@ -801,16 +801,18 @@ class AutofillAssistantUiDelegate { ...@@ -801,16 +801,18 @@ class AutofillAssistantUiDelegate {
* *
* @param webContents The webContents. * @param webContents The webContents.
* @param paymentOptions Options to request payment information. * @param paymentOptions Options to request payment information.
* @param title Unused title. * @param unusedTitle Unused title.
* @param supportedBasicCardNetworks Optional array of supported basic card networks. * @param supportedBasicCardNetworks Optional array of supported basic card networks.
* @param defaultEmail Optional email. If present Profiles containing this email will be shown
* on top.
* @param callback Callback to return selected info. * @param callback Callback to return selected info.
*/ */
public void showPaymentRequest(WebContents webContents, PaymentOptions paymentOptions, public void showPaymentRequest(WebContents webContents, PaymentOptions paymentOptions,
String unusedTitle, String[] supportedBasicCardNetworks, String unusedTitle, String[] supportedBasicCardNetworks, @Nullable String defaultEmail,
Callback<AutofillAssistantPaymentRequest.SelectedPaymentInformation> callback) { Callback<AutofillAssistantPaymentRequest.SelectedPaymentInformation> callback) {
assert mPaymentRequest == null; assert mPaymentRequest == null;
mPaymentRequest = new AutofillAssistantPaymentRequest( mPaymentRequest = new AutofillAssistantPaymentRequest(
webContents, paymentOptions, unusedTitle, supportedBasicCardNetworks); webContents, paymentOptions, unusedTitle, supportedBasicCardNetworks, defaultEmail);
// Make sure we wrap content in the container. // Make sure we wrap content in the container.
mBottomBarAnimations.setBottomBarHeightToWrapContent(); mBottomBarAnimations.setBottomBarHeightToWrapContent();
// Note: We show and hide (below) the carousel so that the margins are adjusted correctly. // Note: We show and hide (below) the carousel so that the margins are adjusted correctly.
......
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