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