Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Unified Diff: ui/android/java/src/org/chromium/ui/autofill/AutofillKeyboardAccessory.java

Issue 1082183002: Android - Introduce "keyboard accessory" for Autofill suggestions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more dtrainor review Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/android/java/src/org/chromium/ui/autofill/AutofillKeyboardAccessory.java
diff --git a/ui/android/java/src/org/chromium/ui/autofill/AutofillKeyboardAccessory.java b/ui/android/java/src/org/chromium/ui/autofill/AutofillKeyboardAccessory.java
new file mode 100644
index 0000000000000000000000000000000000000000..51cc4999db6640d3c5eee2f8253674229e7c34a2
--- /dev/null
+++ b/ui/android/java/src/org/chromium/ui/autofill/AutofillKeyboardAccessory.java
@@ -0,0 +1,137 @@
+// Copyright 2015 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.ui.autofill;
+
+import android.annotation.SuppressLint;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.accessibility.AccessibilityEvent;
+import android.widget.AdapterView;
+import android.widget.ListAdapter;
+import android.widget.ListView;
+
+import org.chromium.base.ApiCompatibilityUtils;
+import org.chromium.ui.DropdownAdapter;
+import org.chromium.ui.DropdownItem;
+import org.chromium.ui.R;
+import org.chromium.ui.base.WindowAndroid;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+/**
+ * The Autofill suggestion view that lists relevant suggestions.
newt (away) 2015/04/24 17:45:12 I'd mention where this view appears ("above the ke
Evan Stade 2015/04/24 21:19:18 Done.
+ */
+public class AutofillKeyboardAccessory extends ListView implements AdapterView.OnItemClickListener {
+ /**
+ * The constant used to specify a separator in a list of Autofill suggestions.
+ * Has to be kept in sync with enum in components/autofill/core/browser/popup_item_ids.h.
+ */
+ private static final int ITEM_ID_SEPARATOR_ENTRY = -3;
newt (away) 2015/04/24 17:45:12 Could you filter out separators in code inside com
Evan Stade 2015/04/24 21:19:18 this leads to some ugly ifdefs but I guess it make
+
+ private final WindowAndroid mWindowAndroid;
+ private final AutofillKeyboardAccessoryDelegate mAutofillCallback;
+ private List<AutofillSuggestion> mSuggestions;
+
+ /**
+ * An interface to handle the touch interaction with an AutofillKeyboardAccessory object.
+ */
+ public interface AutofillKeyboardAccessoryDelegate {
+ /**
+ * Informs the controller the AutofillKeyboardAccessory was hidden.
+ */
+ public void dismissed();
+
+ /**
+ * Handles the selection of an Autofill suggestion from an AutofillKeyboardAccessory.
+ * @param listIndex The index of the selected Autofill suggestion.
+ */
+ public void suggestionSelected(int listIndex);
+ }
+
+ /**
+ * Creates an AutofillKeyboardAccessory with specified parameters.
+ * @param windowAndroid The owning WindowAndroid.
+ * @param autofillCallback A object that handles the calls to the native
+ * AutofillKeyboardAccessoryView.
+ */
+ public AutofillKeyboardAccessory(
+ WindowAndroid windowAndroid, AutofillKeyboardAccessoryDelegate autofillCallback) {
+ super(windowAndroid.getActivity().get());
+ assert autofillCallback != null;
+ mWindowAndroid = windowAndroid;
+ mAutofillCallback = autofillCallback;
+
+ setBackgroundResource(R.drawable.autofill_accessory_view_border);
+ setOnItemClickListener(this);
+ setContentDescription(windowAndroid.getActivity().get().getString(
+ R.string.autofill_popup_content_description));
+ }
+
+ /**
+ * Filters the Autofill suggestions to the ones that we support and shows the popup.
+ * @param suggestions Autofill suggestion data.
+ * @param isRtl Gives the layout direction for the <input> field.
+ */
+ @SuppressLint("InlinedApi")
+ public void filterAndShow(AutofillSuggestion[] suggestions, boolean isRtl) {
+ mSuggestions = new ArrayList<AutofillSuggestion>(Arrays.asList(suggestions));
+ // Remove the AutofillSuggestions with IDs that are not supported by Android
+ ArrayList<DropdownItem> cleanedData = new ArrayList<DropdownItem>();
+ for (int i = 0; i < suggestions.length; i++) {
newt (away) 2015/04/24 17:45:12 you can use an enhanced for loop here: for (A
Evan Stade 2015/04/24 21:19:18 this code is removed
+ int itemId = suggestions[i].getSuggestionId();
+ if (itemId != ITEM_ID_SEPARATOR_ENTRY) {
+ cleanedData.add(suggestions[i]);
+ }
+ }
+
+ setAdapter(new DropdownAdapter(
+ mWindowAndroid.getActivity().get(), cleanedData, new HashSet<Integer>()));
+ ApiCompatibilityUtils.setLayoutDirection(
+ this, isRtl ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
+
+ int height = ViewGroup.LayoutParams.WRAP_CONTENT;
+ // Limit the visible number of suggestions (others are accessible via scrolling).
+ final int suggestionLimit = 2;
+ ListAdapter listAdapter = getAdapter();
+ if (listAdapter.getCount() > suggestionLimit) {
+ height = 0;
+ for (int i = 0; i < suggestionLimit; i++) {
+ View listItem = listAdapter.getView(i, null, this);
+ listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
+ MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
+ height += listItem.getMeasuredHeight();
+ }
+ }
+
+ setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
+
+ if (getParent() == null) {
+ ViewGroup container = mWindowAndroid.getKeyboardAccessoryView();
+ container.addView(this);
+ container.setVisibility(View.VISIBLE);
+ sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
+ }
+ }
+
+ /**
+ * Called to hide the suggestion view.
+ */
+ public void dismiss() {
+ ViewGroup container = mWindowAndroid.getKeyboardAccessoryView();
+ container.removeView(this);
+ container.setVisibility(View.GONE);
+ }
+
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ DropdownAdapter adapter = (DropdownAdapter) parent.getAdapter();
+ int listIndex = mSuggestions.indexOf(adapter.getItem(position));
+ assert listIndex > -1;
+ mAutofillCallback.suggestionSelected(listIndex);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698