1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.autofill;
7 import android.app.Activity;
8 import android.os.Handler;
10 import org.chromium.base.CalledByNative;
11 import org.chromium.base.JNINamespace;
12 import org.chromium.ui.autofill.AutofillPopup;
13 import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate;
14 import org.chromium.ui.autofill.AutofillSuggestion;
15 import org.chromium.ui.base.ViewAndroid;
16 import org.chromium.ui.base.ViewAndroidDelegate;
17 import org.chromium.ui.base.WindowAndroid;
20 * JNI call glue for AutofillExternalDelagate C++ and Java objects.
22 @JNINamespace("autofill")
23 public class AutofillPopupBridge implements AutofillPopupDelegate{
24 private final long mNativeAutofillPopup;
25 private final AutofillPopup mAutofillPopup;
27 public AutofillPopupBridge(long nativeAutofillPopupViewAndroid, WindowAndroid windowAndroid,
28 ViewAndroidDelegate containerViewDelegate) {
29 mNativeAutofillPopup = nativeAutofillPopupViewAndroid;
30 Activity activity = windowAndroid.getActivity().get();
31 if (activity == null) {
32 mAutofillPopup = null;
33 // Clean up the native counterpart. This is posted to allow the native counterpart
34 // to fully finish the construction of this glue object before we attempt to delete it.
35 new Handler().post(new Runnable() {
42 mAutofillPopup = new AutofillPopup(activity, containerViewDelegate, this);
47 private static AutofillPopupBridge create(long nativeAutofillPopupViewAndroid,
48 WindowAndroid windowAndroid, ViewAndroid viewAndroid) {
49 return new AutofillPopupBridge(nativeAutofillPopupViewAndroid, windowAndroid,
50 viewAndroid.getViewAndroidDelegate());
54 public void requestHide() {
55 nativeRequestHide(mNativeAutofillPopup);
59 public void suggestionSelected(int listIndex) {
60 nativeSuggestionSelected(mNativeAutofillPopup, listIndex);
64 * Hides the Autofill Popup and removes its anchor from the ContainerView.
68 if (mAutofillPopup != null) mAutofillPopup.hide();
72 * Shows an Autofill popup with specified suggestions.
73 * @param suggestions Autofill suggestions to be displayed.
76 private void show(AutofillSuggestion[] suggestions) {
77 if (mAutofillPopup != null) mAutofillPopup.filterAndShow(suggestions);
81 * Sets the location and size of the Autofill popup anchor (input field).
82 * @param x X coordinate.
83 * @param y Y coordinate.
84 * @param width The width of the anchor.
85 * @param height The height of the anchor.
88 private void setAnchorRect(float x, float y, float width, float height) {
89 if (mAutofillPopup != null) mAutofillPopup.setAnchorRect(x, y, width, height);
92 // Helper methods for AutofillSuggestion
95 private static AutofillSuggestion[] createAutofillSuggestionArray(int size) {
96 return new AutofillSuggestion[size];
100 * @param array AutofillSuggestion array that should get a new suggestion added.
101 * @param index Index in the array where to place a new suggestion.
102 * @param label First line of the suggestion.
103 * @param sublabel Second line of the suggestion.
104 * @param uniqueId Unique suggestion id.
107 private static void addToAutofillSuggestionArray(AutofillSuggestion[] array, int index,
108 String label, String sublabel, int uniqueId) {
109 array[index] = new AutofillSuggestion(label, sublabel, uniqueId);
112 private native void nativeRequestHide(long nativeAutofillPopupViewAndroid);
113 private native void nativeSuggestionSelected(long nativeAutofillPopupViewAndroid,