1// Copyright 2013 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
5package org.chromium.android_webview;
6
7import android.content.Context;
8import android.view.View;
9import android.view.ViewGroup;
10
11import org.chromium.base.CalledByNative;
12import org.chromium.base.JNINamespace;
13import org.chromium.content.browser.ContentViewCore;
14import org.chromium.ui.autofill.AutofillPopup;
15import org.chromium.ui.autofill.AutofillSuggestion;
16
17// Java counterpart to the AwAutofillManagerDelegate. This class is owned by
18// AwContents and has a weak reference from native side.
19@JNINamespace("android_webview")
20public class AwAutofillManagerDelegate {
21
22    private final int mNativeAwAutofillManagerDelegate;
23    private AutofillPopup mAutofillPopup;
24    private ViewGroup mContainerView;
25    private ContentViewCore mContentViewCore;
26
27    @CalledByNative
28    public static AwAutofillManagerDelegate create(int nativeDelegate) {
29        return new AwAutofillManagerDelegate(nativeDelegate);
30    }
31
32    private AwAutofillManagerDelegate(int nativeAwAutofillManagerDelegate) {
33        mNativeAwAutofillManagerDelegate = nativeAwAutofillManagerDelegate;
34    }
35
36    public void init(ContentViewCore contentViewCore) {
37        mContentViewCore = contentViewCore;
38        mContainerView = contentViewCore.getContainerView();
39    }
40
41    @CalledByNative
42    private void showAutofillPopup(float x, float y, float width, float height,
43            AutofillSuggestion[] suggestions) {
44
45        if (mContentViewCore == null) return;
46
47        if (mAutofillPopup == null) {
48            mAutofillPopup = new AutofillPopup(
49                mContentViewCore.getContext(),
50                mContentViewCore.getViewAndroidDelegate(),
51                new AutofillPopup.AutofillPopupDelegate() {
52                    public void requestHide() { }
53                    public void suggestionSelected(int listIndex) {
54                        nativeSuggestionSelected(mNativeAwAutofillManagerDelegate, listIndex);
55                    }
56                });
57        }
58        mAutofillPopup.setAnchorRect(x, y, width, height);
59        mAutofillPopup.show(suggestions);
60    }
61
62    @CalledByNative
63    public void hideAutofillPopup() {
64        if (mAutofillPopup == null)
65            return;
66        mAutofillPopup.hide();
67        mAutofillPopup = null;
68    }
69
70    @CalledByNative
71    private static AutofillSuggestion[] createAutofillSuggestionArray(int size) {
72        return new AutofillSuggestion[size];
73    }
74
75    /**
76     * @param array AutofillSuggestion array that should get a new suggestion added.
77     * @param index Index in the array where to place a new suggestion.
78     * @param name Name of the suggestion.
79     * @param label Label of the suggestion.
80     * @param uniqueId Unique suggestion id.
81     */
82    @CalledByNative
83    private static void addToAutofillSuggestionArray(AutofillSuggestion[] array, int index,
84            String name, String label, int uniqueId) {
85        array[index] = new AutofillSuggestion(name, label, uniqueId);
86    }
87
88    private native void nativeSuggestionSelected(int nativeAwAutofillManagerDelegate, int position);
89}
90