OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.autofill; | 5 package org.chromium.ui.autofill; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Paint; | 8 import android.graphics.Paint; |
9 import android.graphics.Rect; | 9 import android.graphics.Rect; |
10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
11 import android.view.LayoutInflater; | 11 import android.view.LayoutInflater; |
12 import android.view.View; | 12 import android.view.View; |
13 import android.view.View.OnLayoutChangeListener; | 13 import android.view.View.OnLayoutChangeListener; |
14 import android.widget.AdapterView; | 14 import android.widget.AdapterView; |
15 import android.widget.ListPopupWindow; | 15 import android.widget.ListPopupWindow; |
16 import android.widget.TextView; | 16 import android.widget.TextView; |
17 | 17 |
18 import java.util.ArrayList; | 18 import java.util.ArrayList; |
19 import java.util.Arrays; | |
20 import java.util.HashSet; | |
21 import java.util.List; | |
22 | 19 |
23 import org.chromium.ui.R; | 20 import org.chromium.ui.R; |
24 import org.chromium.ui.ViewAndroidDelegate; | 21 import org.chromium.ui.ViewAndroidDelegate; |
25 | 22 |
26 /** | 23 /** |
27 * The Autofill suggestion popup that lists relevant suggestions. | 24 * The Autofill suggestion popup that lists relevant suggestions. |
28 */ | 25 */ |
29 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
ClickListener { | 26 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
ClickListener { |
30 | 27 |
31 /** | 28 /** |
32 * Constants defining types of Autofill suggestion entries. | 29 * Constants defining types of Autofill suggestion entries. |
33 * Has to be kept in sync with enum in WebAutofillClient.h | 30 * Has to be kept in sync with enum in WebAutofillClient.h |
34 * | 31 * |
35 * Not supported: MenuItemIDWarningMessage, MenuItemIDClearForm, and | 32 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC
learForm, and |
36 * MenuItemIDAutofillOptions. | 33 * MenuItemIDAutofillOptions. |
37 */ | 34 */ |
38 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; | 35 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; |
39 private static final int ITEM_ID_PASSWORD_ENTRY = -2; | 36 private static final int ITEM_ID_PASSWORD_ENTRY = -2; |
40 private static final int ITEM_ID_SEPARATOR_ENTRY = -3; | |
41 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; | 37 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; |
42 | 38 |
43 private static final int TEXT_PADDING_DP = 30; | 39 private static final int TEXT_PADDING_DP = 30; |
44 | 40 |
45 private final AutofillPopupDelegate mAutofillCallback; | 41 private final AutofillPopupDelegate mAutofillCallback; |
46 private final Context mContext; | 42 private final Context mContext; |
47 private final ViewAndroidDelegate mViewAndroidDelegate; | 43 private final ViewAndroidDelegate mViewAndroidDelegate; |
48 private View mAnchorView; | 44 private View mAnchorView; |
49 private float mAnchorWidth; | 45 private float mAnchorWidth; |
50 private float mAnchorHeight; | 46 private float mAnchorHeight; |
51 private float mAnchorX; | 47 private float mAnchorX; |
52 private float mAnchorY; | 48 private float mAnchorY; |
53 private Paint mLabelViewPaint; | 49 private Paint mLabelViewPaint; |
54 private Paint mSublabelViewPaint; | 50 private Paint mSublabelViewPaint; |
55 private OnLayoutChangeListener mLayoutChangeListener; | 51 private OnLayoutChangeListener mLayoutChangeListener; |
56 private List<AutofillSuggestion> mSuggestions; | |
57 | 52 |
58 /** | 53 /** |
59 * An interface to handle the touch interaction with an AutofillPopup object
. | 54 * An interface to handle the touch interaction with an AutofillPopup object
. |
60 */ | 55 */ |
61 public interface AutofillPopupDelegate { | 56 public interface AutofillPopupDelegate { |
62 /** | 57 /** |
63 * Requests the controller to hide AutofillPopup. | 58 * Requests the controller to hide AutofillPopup. |
64 */ | 59 */ |
65 public void requestHide(); | 60 public void requestHide(); |
66 | 61 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 98 |
104 mAnchorView.addOnLayoutChangeListener(mLayoutChangeListener); | 99 mAnchorView.addOnLayoutChangeListener(mLayoutChangeListener); |
105 setAnchorView(mAnchorView); | 100 setAnchorView(mAnchorView); |
106 } | 101 } |
107 | 102 |
108 @Override | 103 @Override |
109 public void show() { | 104 public void show() { |
110 // An ugly hack to keep the popup from expanding on top of the keyboard. | 105 // An ugly hack to keep the popup from expanding on top of the keyboard. |
111 setInputMethodMode(INPUT_METHOD_NEEDED); | 106 setInputMethodMode(INPUT_METHOD_NEEDED); |
112 super.show(); | 107 super.show(); |
113 getListView().setDividerHeight(0); | |
114 } | 108 } |
115 | 109 |
116 /** | 110 /** |
117 * Sets the location and the size of the anchor view that the AutofillPopup
will use to attach | 111 * Sets the location and the size of the anchor view that the AutofillPopup
will use to attach |
118 * itself. | 112 * itself. |
119 * @param x X coordinate of the top left corner of the anchor view. | 113 * @param x X coordinate of the top left corner of the anchor view. |
120 * @param y Y coordinate of the top left corner of the anchor view. | 114 * @param y Y coordinate of the top left corner of the anchor view. |
121 * @param width The width of the anchor view. | 115 * @param width The width of the anchor view. |
122 * @param height The height of the anchor view. | 116 * @param height The height of the anchor view. |
123 */ | 117 */ |
124 public void setAnchorRect(float x, float y, float width, float height) { | 118 public void setAnchorRect(float x, float y, float width, float height) { |
125 mAnchorWidth = width; | 119 mAnchorWidth = width; |
126 mAnchorHeight = height; | 120 mAnchorHeight = height; |
127 mAnchorX = x; | 121 mAnchorX = x; |
128 mAnchorY = y; | 122 mAnchorY = y; |
129 if (mAnchorView != null) { | 123 if (mAnchorView != null) { |
130 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mA
nchorY, | 124 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mA
nchorY, |
131 mAnchorWidth, mAnchorHeight); | 125 mAnchorWidth, mAnchorHeight); |
132 } | 126 } |
133 } | 127 } |
134 | 128 |
135 /** | 129 /** |
136 * Sets the Autofill suggestions to display in the popup and shows the popup
. | 130 * Sets the Autofill suggestions to display in the popup and shows the popup
. |
137 * @param suggestions Autofill suggestion data. | 131 * @param suggestions Autofill suggestion data. |
138 */ | 132 */ |
139 public void show(AutofillSuggestion[] suggestions) { | 133 public void show(AutofillSuggestion[] suggestions) { |
140 mSuggestions = new ArrayList<AutofillSuggestion>(Arrays.asList(suggestio
ns)); | |
141 // Remove the AutofillSuggestions with IDs that are not supported by And
roid | 134 // Remove the AutofillSuggestions with IDs that are not supported by And
roid |
142 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges
tion>(); | 135 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges
tion>(); |
143 HashSet<Integer> separators = new HashSet<Integer>(); | |
144 for (int i = 0; i < suggestions.length; i++) { | 136 for (int i = 0; i < suggestions.length; i++) { |
145 int itemId = suggestions[i].mUniqueId; | 137 int itemId = suggestions[i].mUniqueId; |
146 if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY || | 138 if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY || |
147 itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_L
IST_ENTRY) { | 139 itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_L
IST_ENTRY) { |
148 cleanedData.add(suggestions[i]); | 140 cleanedData.add(suggestions[i]); |
149 } else if (itemId == ITEM_ID_SEPARATOR_ENTRY) { | |
150 separators.add(cleanedData.size()); | |
151 } | 141 } |
152 } | 142 } |
153 setAdapter(new AutofillListAdapter(mContext, cleanedData, separators)); | 143 setAdapter(new AutofillListAdapter(mContext, cleanedData)); |
154 // Once the mAnchorRect is resized and placed correctly, it will show th
e Autofill popup. | 144 // Once the mAnchorRect is resized and placed correctly, it will show th
e Autofill popup. |
155 mAnchorWidth = Math.max(getDesiredWidth(cleanedData), mAnchorWidth); | 145 mAnchorWidth = Math.max(getDesiredWidth(cleanedData), mAnchorWidth); |
156 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mAncho
rY, mAnchorWidth, | 146 mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, mAnchorX, mAncho
rY, mAnchorWidth, |
157 mAnchorHeight); | 147 mAnchorHeight); |
158 } | 148 } |
159 | 149 |
160 /** | 150 /** |
161 * Overrides the default dismiss behavior to request the controller to dismi
ss the view. | 151 * Overrides the default dismiss behavior to request the controller to dismi
ss the view. |
162 */ | 152 */ |
163 @Override | 153 @Override |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 maxTextWidth = Math.max(maxTextWidth, localMax); | 201 maxTextWidth = Math.max(maxTextWidth, localMax); |
212 } | 202 } |
213 // Scale it down to make it unscaled by screen density. | 203 // Scale it down to make it unscaled by screen density. |
214 maxTextWidth = maxTextWidth / mContext.getResources().getDisplayMetrics(
).density; | 204 maxTextWidth = maxTextWidth / mContext.getResources().getDisplayMetrics(
).density; |
215 // Adding padding. | 205 // Adding padding. |
216 return maxTextWidth + TEXT_PADDING_DP; | 206 return maxTextWidth + TEXT_PADDING_DP; |
217 } | 207 } |
218 | 208 |
219 @Override | 209 @Override |
220 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { | 210 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { |
221 AutofillListAdapter adapter = (AutofillListAdapter) parent.getAdapter(); | 211 mAutofillCallback.suggestionSelected(position); |
222 int listIndex = mSuggestions.indexOf(adapter.getItem(position)); | |
223 assert listIndex > -1; | |
224 mAutofillCallback.suggestionSelected(listIndex); | |
225 } | 212 } |
226 | 213 |
227 } | 214 } |
OLD | NEW |