Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / autofill / AutofillPopupBridge.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.chrome.browser.autofill;
6
7 import android.app.Activity;
8 import android.os.Handler;
9
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;
18
19 /**
20 * JNI call glue for AutofillExternalDelagate C++ and Java objects.
21 */
22 @JNINamespace("autofill")
23 public class AutofillPopupBridge implements AutofillPopupDelegate{
24     private final long mNativeAutofillPopup;
25     private final AutofillPopup mAutofillPopup;
26
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() {
36                 @Override
37                 public void run() {
38                     requestHide();
39                 }
40             });
41         } else {
42             mAutofillPopup = new AutofillPopup(activity, containerViewDelegate, this);
43         }
44     }
45
46     @CalledByNative
47     private static AutofillPopupBridge create(long nativeAutofillPopupViewAndroid,
48             WindowAndroid windowAndroid, ViewAndroid viewAndroid) {
49         return new AutofillPopupBridge(nativeAutofillPopupViewAndroid, windowAndroid,
50                 viewAndroid.getViewAndroidDelegate());
51     }
52
53     @Override
54     public void requestHide() {
55         nativeRequestHide(mNativeAutofillPopup);
56     }
57
58     @Override
59     public void suggestionSelected(int listIndex) {
60         nativeSuggestionSelected(mNativeAutofillPopup, listIndex);
61     }
62
63     /**
64      * Hides the Autofill Popup and removes its anchor from the ContainerView.
65      */
66     @CalledByNative
67     private void hide() {
68         if (mAutofillPopup != null) mAutofillPopup.hide();
69     }
70
71     /**
72      * Shows an Autofill popup with specified suggestions.
73      * @param suggestions Autofill suggestions to be displayed.
74      */
75     @CalledByNative
76     private void show(AutofillSuggestion[] suggestions) {
77         if (mAutofillPopup != null) mAutofillPopup.filterAndShow(suggestions);
78     }
79
80     /**
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.
86      */
87     @CalledByNative
88     private void setAnchorRect(float x, float y, float width, float height) {
89         if (mAutofillPopup != null) mAutofillPopup.setAnchorRect(x, y, width, height);
90     }
91
92     // Helper methods for AutofillSuggestion
93
94     @CalledByNative
95     private static AutofillSuggestion[] createAutofillSuggestionArray(int size) {
96         return new AutofillSuggestion[size];
97     }
98
99     /**
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.
105      */
106     @CalledByNative
107     private static void addToAutofillSuggestionArray(AutofillSuggestion[] array, int index,
108             String label, String sublabel, int uniqueId) {
109         array[index] = new AutofillSuggestion(label, sublabel, uniqueId);
110     }
111
112     private native void nativeRequestHide(long nativeAutofillPopupViewAndroid);
113     private native void nativeSuggestionSelected(long nativeAutofillPopupViewAndroid,
114             int listIndex);
115 }