Commit 895f6277 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Commit Bot

[Autofill Assistant] Implement UiControllerAndroid::ChooseAddress and ::ChooseCard.

Bug: 806868
Change-Id: I055e151ab2b4f1df0c1a374d946ffbf1572f4165
Reviewed-on: https://chromium-review.googlesource.com/c/1256248
Commit-Queue: Jordan Demeulenaere <jdemeulenaere@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595934}
parent 933c4c53
...@@ -8,6 +8,7 @@ import android.os.Bundle; ...@@ -8,6 +8,7 @@ import android.os.Bundle;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.autofill.PersonalDataManager;
import org.chromium.chrome.browser.customtabs.CustomTabActivity; import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.tab.EmptyTabObserver; import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
...@@ -19,7 +20,6 @@ import org.chromium.content_public.browser.WebContents; ...@@ -19,7 +20,6 @@ import org.chromium.content_public.browser.WebContents;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
...@@ -108,6 +108,16 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -108,6 +108,16 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
nativeOnScriptSelected(mUiControllerAndroid, scriptPath); nativeOnScriptSelected(mUiControllerAndroid, scriptPath);
} }
@Override
public void onAddressSelected(String guid) {
nativeOnAddressSelected(mUiControllerAndroid, guid);
}
@Override
public void onCardSelected(String guid) {
nativeOnCardSelected(mUiControllerAndroid, guid);
}
/** Return the value if the given boolean parameter from the extras. */ /** Return the value if the given boolean parameter from the extras. */
private static boolean getBooleanParameter(Bundle extras, String parameterName) { private static boolean getBooleanParameter(Bundle extras, String parameterName) {
return extras.getBoolean(INTENT_EXTRA_PREFIX + parameterName, false); return extras.getBoolean(INTENT_EXTRA_PREFIX + parameterName, false);
...@@ -151,7 +161,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -151,7 +161,7 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
@CalledByNative @CalledByNative
private void onUpdateScripts(String[] scriptNames, String[] scriptPaths) { private void onUpdateScripts(String[] scriptNames, String[] scriptPaths) {
List<AutofillAssistantUiDelegate.ScriptHandle> scriptHandles = new ArrayList<>(); ArrayList<AutofillAssistantUiDelegate.ScriptHandle> scriptHandles = new ArrayList<>();
// Note that scriptNames and scriptPaths are one-on-one matched by index. // Note that scriptNames and scriptPaths are one-on-one matched by index.
for (int i = 0; i < scriptNames.length; i++) { for (int i = 0; i < scriptNames.length; i++) {
scriptHandles.add( scriptHandles.add(
...@@ -160,9 +170,23 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat ...@@ -160,9 +170,23 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
mUiDelegate.updateScripts(scriptHandles); mUiDelegate.updateScripts(scriptHandles);
} }
@CalledByNative
private void onChooseAddress() {
mUiDelegate.showProfiles(PersonalDataManager.getInstance().getProfilesToSuggest(
true /* includeNameInLabel */));
}
@CalledByNative
private void onChooseCard() {
mUiDelegate.showCards(PersonalDataManager.getInstance().getCreditCardsToSuggest(
true /* includeServerCards */));
}
// native methods. // native methods.
private native long nativeInit( private native long nativeInit(
WebContents webContents, String[] parameterNames, String[] parameterValues); WebContents webContents, String[] parameterNames, String[] parameterValues);
private native void nativeDestroy(long nativeUiControllerAndroid); private native void nativeDestroy(long nativeUiControllerAndroid);
private native void nativeOnScriptSelected(long nativeUiControllerAndroid, String scriptPath); private native void nativeOnScriptSelected(long nativeUiControllerAndroid, String scriptPath);
private native void nativeOnAddressSelected(long nativeUiControllerAndroid, String guid);
private native void nativeOnCardSelected(long nativeUiControllerAndroid, String guid);
} }
...@@ -12,8 +12,10 @@ import android.widget.LinearLayout; ...@@ -12,8 +12,10 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import java.util.List; import java.util.ArrayList;
import javax.annotation.Nullable; import javax.annotation.Nullable;
...@@ -24,7 +26,7 @@ class AutofillAssistantUiDelegate { ...@@ -24,7 +26,7 @@ class AutofillAssistantUiDelegate {
private final View mFullContainer; private final View mFullContainer;
private final View mOverlay; private final View mOverlay;
private final LinearLayout mBottomBar; private final LinearLayout mBottomBar;
private final ViewGroup mScriptsViewContainer; private final ViewGroup mChipsViewContainer;
private final TextView mStatusMessageView; private final TextView mStatusMessageView;
/** /**
...@@ -49,6 +51,20 @@ class AutofillAssistantUiDelegate { ...@@ -49,6 +51,20 @@ class AutofillAssistantUiDelegate {
* @param scriptPath The path for the selected script. * @param scriptPath The path for the selected script.
*/ */
void onScriptSelected(String scriptPath); void onScriptSelected(String scriptPath);
/**
* Called when an address has been selected.
*
* @param guid The GUID of the selected address.
*/
void onAddressSelected(String guid);
/**
* Called when a credit card has been selected.
*
* @param guid The GUID of the selected card.
*/
void onCardSelected(String guid);
} }
/** /**
...@@ -113,7 +129,7 @@ class AutofillAssistantUiDelegate { ...@@ -113,7 +129,7 @@ class AutofillAssistantUiDelegate {
// TODO(crbug.com/806868): Send feedback. // TODO(crbug.com/806868): Send feedback.
} }
}); });
mScriptsViewContainer = mBottomBar.findViewById(R.id.carousel); mChipsViewContainer = mBottomBar.findViewById(R.id.carousel);
mStatusMessageView = mBottomBar.findViewById(R.id.status_message); mStatusMessageView = mBottomBar.findViewById(R.id.status_message);
// TODO(crbug.com/806868): Listen for contextual search shown so as to hide this UI. // TODO(crbug.com/806868): Listen for contextual search shown so as to hide this UI.
...@@ -125,33 +141,43 @@ class AutofillAssistantUiDelegate { ...@@ -125,33 +141,43 @@ class AutofillAssistantUiDelegate {
* @param message Message to display. * @param message Message to display.
*/ */
public void showStatusMessage(@Nullable String message) { public void showStatusMessage(@Nullable String message) {
if (!mFullContainer.isShown()) mFullContainer.setVisibility(View.VISIBLE); ensureFullContainerIsShown();
mStatusMessageView.setText(message); mStatusMessageView.setText(message);
} }
private void ensureFullContainerIsShown() {
if (!mFullContainer.isShown()) mFullContainer.setVisibility(View.VISIBLE);
}
/** /**
* Updates the list of scripts in the bar. * Updates the list of scripts in the bar.
* *
* @param scripts List of scripts to show. * @param scriptHandles List of scripts to show.
*/ */
public void updateScripts(List<ScriptHandle> scriptHandles) { public void updateScripts(ArrayList<ScriptHandle> scriptHandles) {
mScriptsViewContainer.removeAllViews(); mChipsViewContainer.removeAllViews();
if (scriptHandles.isEmpty()) { if (scriptHandles.isEmpty()) {
return; return;
} }
for (ScriptHandle scriptHandle : scriptHandles) { for (int i = 0; i < scriptHandles.size(); i++) {
TextView scriptView = (TextView) (LayoutInflater.from(mActivity).inflate( ScriptHandle scriptHandle = scriptHandles.get(i);
R.layout.autofill_assistant_chip, null)); TextView chipView = createChipView(scriptHandle.getName());
scriptView.setText(scriptHandle.getName()); chipView.setOnClickListener(
scriptView.setOnClickListener( (unusedView) -> mClient.onScriptSelected(scriptHandle.getPath()));
(unusedView) -> { mClient.onScriptSelected(scriptHandle.getPath()); }); mChipsViewContainer.addView(chipView);
mScriptsViewContainer.addView(scriptView);
} }
if (!mFullContainer.isShown()) mFullContainer.setVisibility(View.VISIBLE); ensureFullContainerIsShown();
}
private TextView createChipView(String text) {
TextView chipView = (TextView) (LayoutInflater.from(mActivity).inflate(
R.layout.autofill_assistant_chip, null /* root */));
chipView.setText(text);
return chipView;
} }
/** Called to show overlay. */ /** Called to show overlay. */
...@@ -171,4 +197,47 @@ class AutofillAssistantUiDelegate { ...@@ -171,4 +197,47 @@ class AutofillAssistantUiDelegate {
mFullContainer.setVisibility(View.GONE); mFullContainer.setVisibility(View.GONE);
mClient.onDismiss(); mClient.onDismiss();
} }
/**
* Show profiles in the bar.
*
* @param profiles List of profiles to show.
*/
public void showProfiles(ArrayList<AutofillProfile> profiles) {
mChipsViewContainer.removeAllViews();
if (profiles.isEmpty()) return;
for (int i = 0; i < profiles.size(); i++) {
AutofillProfile profile = profiles.get(i);
// TODO(crbug.com/806868): Show more information than the street.
TextView chipView = createChipView(profile.getStreetAddress());
chipView.setOnClickListener(
(unusedView) -> mClient.onAddressSelected(profile.getGUID()));
mChipsViewContainer.addView(chipView);
}
ensureFullContainerIsShown();
}
/**
* Show credit cards in the bar.
*
* @param cards List of cards to show.
*/
public void showCards(ArrayList<CreditCard> cards) {
mChipsViewContainer.removeAllViews();
if (cards.isEmpty()) return;
for (int i = 0; i < cards.size(); i++) {
CreditCard card = cards.get(i);
// TODO(crbug.com/806868): Show more information than the card number.
TextView chipView = createChipView(card.getObfuscatedNumber());
chipView.setOnClickListener((unusedView) -> mClient.onCardSelected(card.getGUID()));
mChipsViewContainer.addView(chipView);
}
ensureFullContainerIsShown();
}
} }
...@@ -124,16 +124,42 @@ void UiControllerAndroid::OnScriptSelected( ...@@ -124,16 +124,42 @@ void UiControllerAndroid::OnScriptSelected(
ui_delegate_->OnScriptSelected(script_path); ui_delegate_->OnScriptSelected(script_path);
} }
void UiControllerAndroid::OnAddressSelected(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& jaddress_guid) {
DCHECK(address_or_card_callback_);
std::string guid;
base::android::ConvertJavaStringToUTF8(env, jaddress_guid, &guid);
std::move(address_or_card_callback_).Run(guid);
}
void UiControllerAndroid::OnCardSelected(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& jcard_guid) {
DCHECK(address_or_card_callback_);
std::string guid;
base::android::ConvertJavaStringToUTF8(env, jcard_guid, &guid);
std::move(address_or_card_callback_).Run(guid);
}
void UiControllerAndroid::ChooseAddress( void UiControllerAndroid::ChooseAddress(
base::OnceCallback<void(const std::string&)> callback) { base::OnceCallback<void(const std::string&)> callback) {
// TODO(crbug.com/806868): Implement ChooseAddress. DCHECK(!address_or_card_callback_);
std::move(callback).Run(""); address_or_card_callback_ = std::move(callback);
JNIEnv* env = AttachCurrentThread();
Java_AutofillAssistantUiController_onChooseAddress(
env, java_autofill_assistant_ui_controller_);
} }
void UiControllerAndroid::ChooseCard( void UiControllerAndroid::ChooseCard(
base::OnceCallback<void(const std::string&)> callback) { base::OnceCallback<void(const std::string&)> callback) {
// TODO(crbug.com/806868): Implement ChooseCard. DCHECK(!address_or_card_callback_);
std::move(callback).Run(""); address_or_card_callback_ = std::move(callback);
JNIEnv* env = AttachCurrentThread();
Java_AutofillAssistantUiController_onChooseCard(
env, java_autofill_assistant_ui_controller_);
} }
std::string UiControllerAndroid::GetApiKey() { std::string UiControllerAndroid::GetApiKey() {
......
...@@ -49,6 +49,13 @@ class UiControllerAndroid : public UiController, public Client { ...@@ -49,6 +49,13 @@ class UiControllerAndroid : public UiController, public Client {
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller, const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& jscript_path); const base::android::JavaParamRef<jstring>& jscript_path);
void OnAddressSelected(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& jaddress_guid);
void OnCardSelected(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& jcard_guid);
private: private:
// Java-side AutofillAssistantUiController object. // Java-side AutofillAssistantUiController object.
...@@ -57,6 +64,8 @@ class UiControllerAndroid : public UiController, public Client { ...@@ -57,6 +64,8 @@ class UiControllerAndroid : public UiController, public Client {
UiDelegate* ui_delegate_; UiDelegate* ui_delegate_;
base::OnceCallback<void(const std::string&)> address_or_card_callback_;
DISALLOW_COPY_AND_ASSIGN(UiControllerAndroid); DISALLOW_COPY_AND_ASSIGN(UiControllerAndroid);
}; };
......
...@@ -43,12 +43,16 @@ class UiController { ...@@ -43,12 +43,16 @@ class UiController {
// Show UI to ask user to choose an address in personal data manager. GUID of // Show UI to ask user to choose an address in personal data manager. GUID of
// the chosen address will be returned through callback, otherwise empty // the chosen address will be returned through callback, otherwise empty
// string if the user chose to continue manually. // string if the user chose to continue manually.
// TODO(806868): Return full address object instead of GUID (the GUID can
// change after synchronization with the server).
virtual void ChooseAddress( virtual void ChooseAddress(
base::OnceCallback<void(const std::string&)> callback) = 0; base::OnceCallback<void(const std::string&)> callback) = 0;
// Show UI to ask user to choose a card in personal data manager. GUID of the // Show UI to ask user to choose a card in personal data manager. GUID of the
// chosen card will be returned through callback, otherwise empty string if // chosen card will be returned through callback, otherwise empty string if
// the user chose to continue manually. // the user chose to continue manually.
// TODO(806868): Return full card object instead of GUID (the GUID can change
// after synchronization with the server).
virtual void ChooseCard( virtual void ChooseCard(
base::OnceCallback<void(const std::string&)> callback) = 0; base::OnceCallback<void(const std::string&)> callback) = 0;
......
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