1 | // Copyright (c) 2012 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. |
4 | |
5 | package org.chromium.chrome.browser.autofill; |
6 | |
7 | import org.chromium.base.CalledByNative; |
8 | import org.chromium.base.JNINamespace; |
9 | import org.chromium.ui.ViewAndroid; |
10 | import org.chromium.ui.ViewAndroidDelegate; |
11 | import org.chromium.ui.WindowAndroid; |
12 | import org.chromium.ui.autofill.AutofillPopup; |
13 | import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate; |
14 | import org.chromium.ui.autofill.AutofillSuggestion; |
15 | |
16 | /** |
17 | * JNI call glue for AutofillExternalDelagate C++ and Java objects. |
18 | */ |
19 | @JNINamespace("autofill") |
20 | public class AutofillPopupGlue implements AutofillPopupDelegate{ |
21 | private final int mNativeAutofillPopup; |
22 | private final AutofillPopup mAutofillPopup; |
23 | |
24 | public AutofillPopupGlue(int nativeAutofillPopupViewAndroid, WindowAndroid windowAndroid, |
25 | ViewAndroidDelegate containerViewDelegate) { |
26 | mNativeAutofillPopup = nativeAutofillPopupViewAndroid; |
27 | mAutofillPopup = new AutofillPopup(windowAndroid.getContext(), containerViewDelegate, this); |
28 | } |
29 | |
30 | @CalledByNative |
31 | private static AutofillPopupGlue create(int nativeAutofillPopupViewAndroid, |
32 | WindowAndroid windowAndroid, ViewAndroid viewAndroid) { |
33 | return new AutofillPopupGlue(nativeAutofillPopupViewAndroid, windowAndroid, |
34 | viewAndroid.getViewAndroidDelegate()); |
35 | } |
36 | |
37 | @Override |
38 | public void requestHide() { |
39 | nativeRequestHide(mNativeAutofillPopup); |
40 | } |
41 | |
42 | @Override |
43 | public void suggestionSelected(int listIndex) { |
44 | nativeSuggestionSelected(mNativeAutofillPopup, listIndex); |
45 | } |
46 | |
47 | /** |
48 | * Hides the Autofill Popup and removes its anchor from the ContainerView. |
49 | */ |
50 | @CalledByNative |
51 | private void hide() { |
52 | mAutofillPopup.hide(); |
53 | } |
54 | |
55 | /** |
56 | * Shows an Autofill popup with specified suggestions. |
57 | * @param suggestions Autofill suggestions to be displayed. |
58 | */ |
59 | @CalledByNative |
60 | private void show(AutofillSuggestion[] suggestions) { |
61 | mAutofillPopup.show(suggestions); |
62 | } |
63 | |
64 | /** |
65 | * Sets the location and size of the Autofill popup anchor (input field). |
66 | * @param x X coordinate. |
67 | * @param y Y coordinate. |
68 | * @param width The width of the anchor. |
69 | * @param height The height of the anchor. |
70 | */ |
71 | @CalledByNative |
72 | private void setAnchorRect(float x, float y, float width, float height) { |
73 | mAutofillPopup.setAnchorRect(x, y, width, height); |
74 | } |
75 | |
76 | // Helper methods for AutofillSuggestion |
77 | |
78 | @CalledByNative |
79 | private static AutofillSuggestion[] createAutofillSuggestionArray(int size) { |
80 | return new AutofillSuggestion[size]; |
81 | } |
82 | |
83 | /** |
84 | * @param array AutofillSuggestion array that should get a new suggestion added. |
85 | * @param index Index in the array where to place a new suggestion. |
86 | * @param label First line of the suggestion. |
87 | * @param sublabel Second line of the suggestion. |
88 | * @param uniqueId Unique suggestion id. |
89 | */ |
90 | @CalledByNative |
91 | private static void addToAutofillSuggestionArray(AutofillSuggestion[] array, int index, |
92 | String label, String sublabel, int uniqueId) { |
93 | array[index] = new AutofillSuggestion(label, sublabel, uniqueId); |
94 | } |
95 | |
96 | private native void nativeRequestHide(int nativeAutofillPopupViewAndroid); |
97 | private native void nativeSuggestionSelected(int nativeAutofillPopupViewAndroid, |
98 | int listIndex); |
99 | } |