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