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