Commit 5c74c3e1 authored by Friedrich Horschig's avatar Friedrich Horschig Committed by Commit Bot

[Mfill Android] Add callbacks to AccessorySheetData

This CL adds callbacks to the AccessorySheetData java class. They are
needed for the rework of the UI.

Bug: 902425
Change-Id: Idaa321a21268b9b071d244633553a86a9733408c
Reviewed-on: https://chromium-review.googlesource.com/c/1348331Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Commit-Queue: Friedrich Horschig [CET] <fhorschig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611080}
parent c352b1d6
......@@ -343,30 +343,26 @@ public class KeyboardAccessoryData {
/**
* Represents an item (either selectable or not) presented on the UI, such as the username
* or a credit card number.
*
* TODO(crbug.com/902425): Add a callback to be invoked on user tap.
*/
public final static class Field {
private final String mDisplayText;
private final String mA11yDescription;
private final boolean mIsObfuscated;
// TODO(crbug.com/902425): Once the selection callback is added to this class, replace
// this with a check if the callback is not null.
private final boolean mSelectable;
private final Callback<Field> mCallback;
/**
* Creates a new Field.
* @param displayText The text to display. Plain text if |isObfuscated| is false.
* @param a11yDescription The description used for accessibility.
* @param isObfuscated If true, the displayed caption is transformed into stars.
* @param selectable If true, user can interact with the suggested field.
* @param callback Called when the user taps the suggestions.
*/
public Field(String displayText, String a11yDescription, boolean isObfuscated,
boolean selectable) {
Callback<Field> callback) {
mDisplayText = displayText;
mA11yDescription = a11yDescription;
mIsObfuscated = isObfuscated;
mSelectable = selectable;
mCallback = callback;
}
/**
......@@ -388,7 +384,7 @@ public class KeyboardAccessoryData {
* this is false if this is a password suggestion on a non-password input field.
*/
public boolean isSelectable() {
return mSelectable;
return mCallback != null;
}
/**
......@@ -398,6 +394,13 @@ public class KeyboardAccessoryData {
public boolean isObfuscated() {
return mIsObfuscated;
}
/**
* The delegate is called when the Item is selected by a user.
*/
public void triggerSelection() {
if (mCallback != null) mCallback.onResult(this);
}
}
public UserInfo() {}
......@@ -423,13 +426,16 @@ public class KeyboardAccessoryData {
*/
public final static class FooterCommand {
private final String mDisplayText;
private final Callback<FooterCommand> mCallback;
/**
* Creates a new FooterCommand.
* @param displayText The text to be displayed on the footer.
* @param callback Called when the user taps the suggestions.
*/
public FooterCommand(String displayText) {
public FooterCommand(String displayText, Callback<FooterCommand> callback) {
mDisplayText = displayText;
mCallback = callback;
}
/**
......@@ -439,6 +445,14 @@ public class KeyboardAccessoryData {
public String getDisplayText() {
return mDisplayText;
}
/**
* Returns the translated text to be shown on the UI for this footer command. This text is
* used for accessibility.
*/
public void execute() {
mCallback.onResult(this);
}
}
/**
......
......@@ -114,19 +114,33 @@ class ManualFillingBridge {
}
@CalledByNative
private static void addFieldToUserInfo(Object objUserInfo, String displayText,
String a11yDescription, boolean isObfuscated, boolean selectable) {
private void addFieldToUserInfo(Object objUserInfo, String displayText, String a11yDescription,
boolean isObfuscated, boolean selectable) {
Callback<UserInfo.Field> callback = null;
if (selectable) {
callback = (field) -> {
assert mNativeView != 0 : "Controller was destroyed but the bridge wasn't!";
KeyboardAccessoryMetricsRecorder.recordSuggestionSelected(
AccessoryTabType.PASSWORDS,
field.isObfuscated() ? AccessorySuggestionType.PASSWORD
: AccessorySuggestionType.USERNAME);
nativeOnFillingTriggered(mNativeView, field.isObfuscated(), field.getDisplayText());
};
}
((UserInfo) objUserInfo)
.getFields()
.add(new UserInfo.Field(displayText, a11yDescription, isObfuscated, selectable));
.add(new UserInfo.Field(displayText, a11yDescription, isObfuscated, callback));
}
@CalledByNative
private static void addFooterCommandToAccessorySheetData(
private void addFooterCommandToAccessorySheetData(
Object objAccessorySheetData, String displayText) {
((AccessorySheetData) objAccessorySheetData)
.getFooterCommands()
.add(new FooterCommand(displayText));
.add(new FooterCommand(displayText, (footerCommand) -> {
assert mNativeView != 0 : "Controller was destroyed but the bridge wasn't!";
nativeOnOptionSelected(mNativeView, footerCommand.getDisplayText());
}));
}
private Item[] convertToItems(AccessorySheetData accessorySheetData) {
......
......@@ -27,36 +27,6 @@ using autofill::UserInfo;
using base::android::ConvertUTF16ToJavaString;
using base::android::ScopedJavaLocalRef;
namespace {
ScopedJavaLocalRef<jobject> ConvertAccessorySheetDataToJavaObject(
JNIEnv* env,
const AccessorySheetData& data) {
ScopedJavaLocalRef<jobject> j_data =
Java_ManualFillingBridge_createAccessorySheetData(
env, ConvertUTF16ToJavaString(env, data.title()));
for (const UserInfo& user_info : data.user_info_list()) {
ScopedJavaLocalRef<jobject> j_user_info =
Java_ManualFillingBridge_addUserInfoToAccessorySheetData(env, j_data);
for (const UserInfo::Field& field : user_info.fields()) {
Java_ManualFillingBridge_addFieldToUserInfo(
env, j_user_info, ConvertUTF16ToJavaString(env, field.display_text()),
ConvertUTF16ToJavaString(env, field.a11y_description()),
field.is_obfuscated(), field.selectable());
}
}
for (const FooterCommand& footer_command : data.footer_commands()) {
Java_ManualFillingBridge_addFooterCommandToAccessorySheetData(
env, j_data,
ConvertUTF16ToJavaString(env, footer_command.display_text()));
}
return j_data;
}
} // namespace
ManualFillingViewAndroid::ManualFillingViewAndroid(
ManualFillingController* controller)
: controller_(controller) {
......@@ -159,6 +129,35 @@ void ManualFillingViewAndroid::OnImageFetched(
RunObjectCallbackAndroid(j_callback, j_bitmap);
}
ScopedJavaLocalRef<jobject>
ManualFillingViewAndroid::ConvertAccessorySheetDataToJavaObject(
JNIEnv* env,
const AccessorySheetData& tab_data) {
ScopedJavaLocalRef<jobject> j_tab_data =
Java_ManualFillingBridge_createAccessorySheetData(
env, ConvertUTF16ToJavaString(env, tab_data.title()));
for (const UserInfo& user_info : tab_data.user_info_list()) {
ScopedJavaLocalRef<jobject> j_user_info =
Java_ManualFillingBridge_addUserInfoToAccessorySheetData(env,
j_tab_data);
for (const UserInfo::Field& field : user_info.fields()) {
Java_ManualFillingBridge_addFieldToUserInfo(
env, java_object_, j_user_info,
ConvertUTF16ToJavaString(env, field.display_text()),
ConvertUTF16ToJavaString(env, field.a11y_description()),
field.is_obfuscated(), field.selectable());
}
}
for (const FooterCommand& footer_command : tab_data.footer_commands()) {
Java_ManualFillingBridge_addFooterCommandToAccessorySheetData(
env, java_object_, j_tab_data,
ConvertUTF16ToJavaString(env, footer_command.display_text()));
}
return j_tab_data;
}
// static
std::unique_ptr<ManualFillingViewInterface> ManualFillingViewInterface::Create(
ManualFillingController* controller) {
......
......@@ -56,6 +56,11 @@ class ManualFillingViewAndroid : public ManualFillingViewInterface {
const base::android::ScopedJavaGlobalRef<jobject>& j_callback,
const gfx::Image& image);
base::android::ScopedJavaLocalRef<jobject>
ConvertAccessorySheetDataToJavaObject(
JNIEnv* env,
const autofill::AccessorySheetData& tab_data);
// The controller provides data for this view and owns it.
ManualFillingController* controller_;
......
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