| OLD | NEW |
| (Empty) |
| 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 android.content.Context; | |
| 8 import android.graphics.Paint; | |
| 9 import android.graphics.Rect; | |
| 10 import android.view.LayoutInflater; | |
| 11 import android.view.View; | |
| 12 import android.view.View.OnLayoutChangeListener; | |
| 13 import android.widget.AdapterView; | |
| 14 import android.widget.ListPopupWindow; | |
| 15 import android.widget.TextView; | |
| 16 | |
| 17 import java.util.ArrayList; | |
| 18 | |
| 19 import org.chromium.chrome.R; | |
| 20 import org.chromium.ui.ViewAndroidDelegate; | |
| 21 | |
| 22 /** | |
| 23 * The Autofill suggestion popup that lists relevant suggestions. | |
| 24 */ | |
| 25 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
ClickListener { | |
| 26 | |
| 27 /** | |
| 28 * Constants defining types of Autofill suggestion entries. | |
| 29 * Has to be kept in sync with enum in WebAutofillClient.h | |
| 30 * | |
| 31 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC
learForm, and | |
| 32 * MenuItemIDAutofillOptions. | |
| 33 */ | |
| 34 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; | |
| 35 private static final int ITEM_ID_PASSWORD_ENTRY = -2; | |
| 36 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; | |
| 37 | |
| 38 private static final int TEXT_PADDING_DP = 40; | |
| 39 | |
| 40 private final AutofillPopupDelegate mAutofillCallback; | |
| 41 private final Context mContext; | |
| 42 private final ViewAndroidDelegate mViewAndroidDelegate; | |
| 43 private View mAnchorView; | |
| 44 private float mAnchorWidth; | |
| 45 private float mAnchorHeight; | |
| 46 private float mAnchorX; | |
| 47 private float mAnchorY; | |
| 48 private Paint mNameViewPaint; | |
| 49 private Paint mLabelViewPaint; | |
| 50 private OnLayoutChangeListener mLayoutChangeListener; | |
| 51 | |
| 52 /** | |
| 53 * An interface to handle the touch interaction with an AutofillPopup object
. | |
| 54 */ | |
| 55 public interface AutofillPopupDelegate { | |
| 56 /** | |
| 57 * Requests the controller to hide AutofillPopup. | |
| 58 */ | |
| 59 public void requestHide(); | |
| 60 | |
| 61 /** | |
| 62 * Handles the selection of an Autofill suggestion from an AutofillPopup
. | |
| 63 * @param listIndex The index of the selected Autofill suggestion. | |
| 64 */ | |
| 65 public void suggestionSelected(int listIndex); | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Creates an AutofillWindow with specified parameters. | |
| 70 * @param context Application context. | |
| 71 * @param viewAndroidDelegate View delegate used to add and remove views. | |
| 72 * @param autofillCallback A object that handles the calls to the native Aut
ofillPopupView. | |
| 73 */ | |
| 74 public AutofillPopup(Context context, ViewAndroidDelegate viewAndroidDelegat
e, | |
| 75 AutofillPopupDelegate autofillCallback) { | |
| 76 super(context); | |
| 77 mContext = context; | |
| 78 mViewAndroidDelegate = viewAndroidDelegate ; | |
| 79 mAutofillCallback = autofillCallback; | |
| 80 | |
| 81 setOnItemClickListener(this); | |
| 82 | |
| 83 mAnchorView = mViewAndroidDelegate.acquireAnchorView(); | |
| 84 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mAncho
rY, mAnchorWidth, | |
| 85 mAnchorHeight); | |
| 86 | |
| 87 mLayoutChangeListener = new OnLayoutChangeListener() { | |
| 88 @Override | |
| 89 public void onLayoutChange(View v, int left, int top, int right, int
bottom, | |
| 90 int oldLeft, int oldTop, int oldRight, int oldBottom) { | |
| 91 if (v == mAnchorView) AutofillPopup.this.show(); | |
| 92 } | |
| 93 }; | |
| 94 | |
| 95 mAnchorView.addOnLayoutChangeListener(mLayoutChangeListener); | |
| 96 setAnchorView(mAnchorView); | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Sets the location and the size of the anchor view that the AutofillPopup
will use to attach | |
| 101 * itself. | |
| 102 * @param x X coordinate of the top left corner of the anchor view. | |
| 103 * @param y Y coordinate of the top left corner of the anchor view. | |
| 104 * @param width The width of the anchor view. | |
| 105 * @param height The height of the anchor view. | |
| 106 */ | |
| 107 public void setAnchorRect(float x, float y, float width, float height) { | |
| 108 mAnchorWidth = width; | |
| 109 mAnchorHeight = height; | |
| 110 mAnchorX = x; | |
| 111 mAnchorY = y; | |
| 112 if (mAnchorView != null) { | |
| 113 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mA
nchorY, | |
| 114 mAnchorWidth, mAnchorHeight); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Sets the Autofill suggestions to display in the popup and shows the popup
. | |
| 120 * @param suggestions Autofill suggestion data. | |
| 121 */ | |
| 122 public void show(AutofillSuggestion[] suggestions) { | |
| 123 // Remove the AutofillSuggestions with IDs that are not supported by And
roid | |
| 124 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges
tion>(); | |
| 125 for (int i = 0; i < suggestions.length; i++) { | |
| 126 int itemId = suggestions[i].mUniqueId; | |
| 127 if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY || | |
| 128 itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_L
IST_ENTRY) { | |
| 129 cleanedData.add(suggestions[i]); | |
| 130 } | |
| 131 } | |
| 132 setAdapter(new AutofillListAdapter(mContext, cleanedData)); | |
| 133 // Once the mAnchorRect is resized and placed correctly, it will show th
e Autofill popup. | |
| 134 mAnchorWidth = Math.max(getDesiredWidth(suggestions), mAnchorWidth); | |
| 135 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mAncho
rY, mAnchorWidth, | |
| 136 mAnchorHeight); | |
| 137 } | |
| 138 | |
| 139 /** | |
| 140 * Overrides the default dismiss behavior to request the controller to dismi
ss the view. | |
| 141 */ | |
| 142 @Override | |
| 143 public void dismiss() { | |
| 144 mAutofillCallback.requestHide(); | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Hides the popup and removes the anchor view from the ContainerView. | |
| 149 */ | |
| 150 public void hide() { | |
| 151 super.dismiss(); | |
| 152 mAnchorView.removeOnLayoutChangeListener(mLayoutChangeListener); | |
| 153 mViewAndroidDelegate.releaseAnchorView(mAnchorView); | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Get desired popup window width by calculating the maximum text length fro
m Autofill data. | |
| 158 * @param data Autofill suggestion data. | |
| 159 * @return The popup window width in DIP. | |
| 160 */ | |
| 161 private float getDesiredWidth(AutofillSuggestion[] data) { | |
| 162 if (mNameViewPaint == null || mLabelViewPaint == null) { | |
| 163 LayoutInflater inflater = | |
| 164 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_IN
FLATER_SERVICE); | |
| 165 View layout = inflater.inflate(R.layout.autofill_text, null); | |
| 166 TextView nameView = (TextView) layout.findViewById(R.id.autofill_nam
e); | |
| 167 mNameViewPaint = nameView.getPaint(); | |
| 168 TextView labelView = (TextView) layout.findViewById(R.id.autofill_la
bel); | |
| 169 mLabelViewPaint = labelView.getPaint(); | |
| 170 } | |
| 171 | |
| 172 float maxTextWidth = 0; | |
| 173 Rect bounds = new Rect(); | |
| 174 for (int i = 0; i < data.length; ++i) { | |
| 175 bounds.setEmpty(); | |
| 176 String name = data[i].mName; | |
| 177 float width = 0; | |
| 178 mNameViewPaint.getTextBounds(name, 0, name.length(), bounds); | |
| 179 width += bounds.width(); | |
| 180 | |
| 181 bounds.setEmpty(); | |
| 182 String label = data[i].mLabel; | |
| 183 mLabelViewPaint.getTextBounds(label, 0, label.length(), bounds); | |
| 184 width += bounds.width(); | |
| 185 maxTextWidth = Math.max(width, maxTextWidth); | |
| 186 } | |
| 187 // Scale it down to make it unscaled by screen density. | |
| 188 maxTextWidth = maxTextWidth / mContext.getResources().getDisplayMetrics(
).density; | |
| 189 // Adding padding. | |
| 190 return maxTextWidth + TEXT_PADDING_DP; | |
| 191 } | |
| 192 | |
| 193 @Override | |
| 194 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { | |
| 195 mAutofillCallback.suggestionSelected(position); | |
| 196 } | |
| 197 | |
| 198 } | |
| OLD | NEW |