OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.chrome.browser.autofill; | 5 package org.chromium.chrome.browser.autofill; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Configuration; | 8 import android.content.res.Configuration; |
9 import android.graphics.Paint; | 9 import android.graphics.Paint; |
10 import android.graphics.Point; | 10 import android.graphics.Point; |
11 import android.graphics.Rect; | 11 import android.graphics.Rect; |
12 import android.graphics.drawable.Drawable; | 12 import android.graphics.drawable.Drawable; |
13 import android.util.Log; | |
14 import android.view.LayoutInflater; | 13 import android.view.LayoutInflater; |
15 import android.view.View; | 14 import android.view.View; |
16 import android.view.WindowManager; | 15 import android.view.WindowManager; |
17 import android.widget.AdapterView; | 16 import android.widget.AdapterView; |
18 import android.widget.FrameLayout; | 17 import android.widget.FrameLayout; |
19 import android.widget.ListAdapter; | |
20 import android.widget.ListPopupWindow; | 18 import android.widget.ListPopupWindow; |
21 import android.widget.TextView; | 19 import android.widget.TextView; |
22 | 20 |
23 import java.util.ArrayList; | 21 import java.util.ArrayList; |
24 | 22 |
25 import org.chromium.chrome.R; | 23 import org.chromium.chrome.R; |
26 import org.chromium.content.browser.ContainerViewDelegate; | 24 import org.chromium.content.browser.ContainerViewDelegate; |
27 import org.chromium.ui.gfx.DeviceDisplayInfo; | 25 import org.chromium.ui.gfx.DeviceDisplayInfo; |
28 import org.chromium.ui.gfx.NativeWindow; | 26 import org.chromium.ui.WindowAndroid; |
29 | 27 |
30 /** | 28 /** |
31 * The Autofill suggestion popup that lists relevant suggestions. | 29 * The Autofill suggestion popup that lists relevant suggestions. |
32 */ | 30 */ |
33 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem ClickListener { | 31 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem ClickListener { |
34 | 32 |
35 /** | 33 /** |
36 * Constants defining types of Autofill suggestion entries. | 34 * Constants defining types of Autofill suggestion entries. |
37 * Has to be kept in sync with enum in WebAutofillClient.h | 35 * Has to be kept in sync with enum in WebAutofillClient.h |
38 * | 36 * |
39 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC learForm, and | 37 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC learForm, and |
40 * MenuItemIDAutofillOptions. | 38 * MenuItemIDAutofillOptions. |
41 */ | 39 */ |
42 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; | 40 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; |
43 private static final int ITEM_ID_PASSWORD_ENTRY = -2; | 41 private static final int ITEM_ID_PASSWORD_ENTRY = -2; |
44 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; | 42 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; |
45 | 43 |
46 private static final int TEXT_PADDING_DP = 30; | 44 private static final int TEXT_PADDING_DP = 30; |
47 | 45 |
48 private final AutofillPopupDelegate mAutofillCallback; | 46 private final AutofillPopupDelegate mAutofillCallback; |
49 private final NativeWindow mNativeWindow; | 47 private final WindowAndroid mWindowAndroid; |
50 private final ContainerViewDelegate mContainerViewDelegate; | 48 private final ContainerViewDelegate mContainerViewDelegate; |
51 private AnchorView mAnchorView; | 49 private AnchorView mAnchorView; |
52 private Rect mAnchorRect; | 50 private Rect mAnchorRect; |
53 private Paint mNameViewPaint; | 51 private Paint mNameViewPaint; |
54 private Paint mLabelViewPaint; | 52 private Paint mLabelViewPaint; |
55 | 53 |
56 /** | 54 /** |
57 * An interface to handle the touch interaction with an AutofillPopup object . | 55 * An interface to handle the touch interaction with an AutofillPopup object . |
58 */ | 56 */ |
59 public interface AutofillPopupDelegate { | 57 public interface AutofillPopupDelegate { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 | 115 |
118 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, he ight); | 116 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, he ight); |
119 lp.leftMargin = r.left - paddingRect.left; | 117 lp.leftMargin = r.left - paddingRect.left; |
120 lp.topMargin = r.top; | 118 lp.topMargin = r.top; |
121 setLayoutParams(lp); | 119 setLayoutParams(lp); |
122 } | 120 } |
123 } | 121 } |
124 | 122 |
125 /** | 123 /** |
126 * Creates an AutofillWindow with specified parameters. | 124 * Creates an AutofillWindow with specified parameters. |
127 * @param nativeWindow NativeWindow used to get application context. | 125 * @param windowAndroid WindowAndroid used to get application context. |
nilesh
2013/04/18 16:50:06
It says it needs the application context but getti
aurimas (slooooooooow)
2013/04/18 20:46:16
It was a mistake in the comment. I wrote this comm
| |
128 * @param containerViewDelegate View delegate used to add and remove views. | 126 * @param containerViewDelegate View delegate used to add and remove views. |
129 * @param autofillCallback A object that handles the calls to the native Aut ofillPopupView. | 127 * @param autofillCallback A object that handles the calls to the native Aut ofillPopupView. |
130 */ | 128 */ |
131 public AutofillPopup(NativeWindow nativeWindow, ContainerViewDelegate contai nerViewDelegate, | 129 public AutofillPopup(WindowAndroid windowAndroid, ContainerViewDelegate cont ainerViewDelegate, |
132 AutofillPopupDelegate autofillCallback) { | 130 AutofillPopupDelegate autofillCallback) { |
133 super(nativeWindow.getContext()); | 131 super(windowAndroid.getContext()); |
134 mNativeWindow = nativeWindow; | 132 mWindowAndroid = windowAndroid; |
nilesh
2013/04/18 16:50:06
Why store a ref to window, when only application c
aurimas (slooooooooow)
2013/04/18 20:46:16
Done.
| |
135 mContainerViewDelegate = containerViewDelegate; | 133 mContainerViewDelegate = containerViewDelegate; |
136 mAutofillCallback = autofillCallback; | 134 mAutofillCallback = autofillCallback; |
137 | 135 |
138 setOnItemClickListener(this); | 136 setOnItemClickListener(this); |
139 | 137 |
140 mAnchorView = new AnchorView(mNativeWindow.getContext(), this); | 138 mAnchorView = new AnchorView(mWindowAndroid.getContext(), this); |
141 mContainerViewDelegate.addViewToContainerView(mAnchorView); | 139 mContainerViewDelegate.addViewToContainerView(mAnchorView); |
142 setAnchorView(mAnchorView); | 140 setAnchorView(mAnchorView); |
143 } | 141 } |
144 | 142 |
145 /** | 143 /** |
146 * Sets the location and the size of the anchor view that the AutofillPopup will use to attach | 144 * Sets the location and the size of the anchor view that the AutofillPopup will use to attach |
147 * itself. | 145 * itself. |
148 * @param x X coordinate of the top left corner of the anchor view. | 146 * @param x X coordinate of the top left corner of the anchor view. |
149 * @param y Y coordinate of the top left corner of the anchor view. | 147 * @param y Y coordinate of the top left corner of the anchor view. |
150 * @param width The width of the anchor view. | 148 * @param width The width of the anchor view. |
151 * @param height The height of the anchor view. | 149 * @param height The height of the anchor view. |
152 */ | 150 */ |
153 public void setAnchorRect(float x, float y, float width, float height) { | 151 public void setAnchorRect(float x, float y, float width, float height) { |
154 float scale = (float) DeviceDisplayInfo.create(mNativeWindow.getContext( )).getDIPScale(); | 152 float scale = (float) DeviceDisplayInfo.create(mWindowAndroid.getContext ()).getDIPScale(); |
155 mAnchorRect = new Rect(Math.round(x * scale), Math.round(y * scale), | 153 mAnchorRect = new Rect(Math.round(x * scale), Math.round(y * scale), |
156 Math.round((x + width) * scale), Math.round((y + height) * scale )); | 154 Math.round((x + width) * scale), Math.round((y + height) * scale )); |
157 mAnchorRect.offset(0, mContainerViewDelegate.getChildViewOffsetYPix()); | 155 mAnchorRect.offset(0, mContainerViewDelegate.getChildViewOffsetYPix()); |
158 } | 156 } |
159 | 157 |
160 /** | 158 /** |
161 * Sets the Autofill suggestions to display in the popup and shows the popup . | 159 * Sets the Autofill suggestions to display in the popup and shows the popup . |
162 * @param suggestions Autofill suggestion data. | 160 * @param suggestions Autofill suggestion data. |
163 */ | 161 */ |
164 public void show(AutofillSuggestion[] suggestions) { | 162 public void show(AutofillSuggestion[] suggestions) { |
165 // Remove the AutofillSuggestions with IDs that are not supported by And roid | 163 // Remove the AutofillSuggestions with IDs that are not supported by And roid |
166 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges tion>(); | 164 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges tion>(); |
167 for (int i = 0; i < suggestions.length; i++) { | 165 for (int i = 0; i < suggestions.length; i++) { |
168 int itemId = suggestions[i].mUniqueId; | 166 int itemId = suggestions[i].mUniqueId; |
169 if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY || | 167 if (itemId > 0 || itemId == ITEM_ID_AUTOCOMPLETE_ENTRY || |
170 itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_L IST_ENTRY) { | 168 itemId == ITEM_ID_PASSWORD_ENTRY || itemId == ITEM_ID_DATA_L IST_ENTRY) { |
171 cleanedData.add(suggestions[i]); | 169 cleanedData.add(suggestions[i]); |
172 } | 170 } |
173 } | 171 } |
174 setAdapter(new AutofillListAdapter(mNativeWindow.getContext(), cleanedDa ta)); | 172 setAdapter(new AutofillListAdapter(mWindowAndroid.getContext(), cleanedD ata)); |
175 // Once the mAnchorRect is resized and placed correctly, it will show th e Autofill popup. | 173 // Once the mAnchorRect is resized and placed correctly, it will show th e Autofill popup. |
176 mAnchorView.setSize(mAnchorRect, getDesiredWidth(suggestions)); | 174 mAnchorView.setSize(mAnchorRect, getDesiredWidth(suggestions)); |
177 } | 175 } |
178 | 176 |
179 | 177 |
180 /** | 178 /** |
181 * Overrides the default dismiss behavior to request the controller to dismi ss the view. | 179 * Overrides the default dismiss behavior to request the controller to dismi ss the view. |
182 */ | 180 */ |
183 @Override | 181 @Override |
184 public void dismiss() { | 182 public void dismiss() { |
185 mAutofillCallback.requestHide(); | 183 mAutofillCallback.requestHide(); |
186 } | 184 } |
187 | 185 |
188 /** | 186 /** |
189 * Hides the popup and removes the anchor view from the ContainerView. | 187 * Hides the popup and removes the anchor view from the ContainerView. |
190 */ | 188 */ |
191 public void hide() { | 189 public void hide() { |
192 super.dismiss(); | 190 super.dismiss(); |
193 mContainerViewDelegate.removeViewFromContainerView(mAnchorView); | 191 mContainerViewDelegate.removeViewFromContainerView(mAnchorView); |
194 } | 192 } |
195 | 193 |
196 /** | 194 /** |
197 * Get desired popup window width by calculating the maximum text length fro m Autofill data. | 195 * Get desired popup window width by calculating the maximum text length fro m Autofill data. |
198 * @param data Autofill suggestion data. | 196 * @param data Autofill suggestion data. |
199 * @return The popup window width. | 197 * @return The popup window width. |
200 */ | 198 */ |
201 private int getDesiredWidth(AutofillSuggestion[] data) { | 199 private int getDesiredWidth(AutofillSuggestion[] data) { |
202 if (mNameViewPaint == null || mLabelViewPaint == null) { | 200 if (mNameViewPaint == null || mLabelViewPaint == null) { |
203 LayoutInflater inflater = | 201 LayoutInflater inflater = |
204 (LayoutInflater) mNativeWindow.getContext().getSystemService ( | 202 (LayoutInflater) mWindowAndroid.getContext().getSystemServic e( |
205 Context.LAYOUT_INFLATER_SERVICE); | 203 Context.LAYOUT_INFLATER_SERVICE); |
206 View layout = inflater.inflate(R.layout.autofill_text, null); | 204 View layout = inflater.inflate(R.layout.autofill_text, null); |
207 TextView nameView = (TextView) layout.findViewById(R.id.autofill_nam e); | 205 TextView nameView = (TextView) layout.findViewById(R.id.autofill_nam e); |
208 mNameViewPaint = nameView.getPaint(); | 206 mNameViewPaint = nameView.getPaint(); |
209 TextView labelView = (TextView) layout.findViewById(R.id.autofill_la bel); | 207 TextView labelView = (TextView) layout.findViewById(R.id.autofill_la bel); |
210 mLabelViewPaint = labelView.getPaint(); | 208 mLabelViewPaint = labelView.getPaint(); |
211 } | 209 } |
212 | 210 |
213 int maxTextWidth = 0; | 211 int maxTextWidth = 0; |
214 Rect bounds = new Rect(); | 212 Rect bounds = new Rect(); |
215 for (int i = 0; i < data.length; ++i) { | 213 for (int i = 0; i < data.length; ++i) { |
216 bounds.setEmpty(); | 214 bounds.setEmpty(); |
217 String name = data[i].mName; | 215 String name = data[i].mName; |
218 int width = 0; | 216 int width = 0; |
219 mNameViewPaint.getTextBounds(name, 0, name.length(), bounds); | 217 mNameViewPaint.getTextBounds(name, 0, name.length(), bounds); |
220 width += bounds.width(); | 218 width += bounds.width(); |
221 | 219 |
222 bounds.setEmpty(); | 220 bounds.setEmpty(); |
223 String label = data[i].mLabel; | 221 String label = data[i].mLabel; |
224 mLabelViewPaint.getTextBounds(label, 0, label.length(), bounds); | 222 mLabelViewPaint.getTextBounds(label, 0, label.length(), bounds); |
225 width += bounds.width(); | 223 width += bounds.width(); |
226 maxTextWidth = Math.max(width, maxTextWidth); | 224 maxTextWidth = Math.max(width, maxTextWidth); |
227 } | 225 } |
228 // Adding padding. | 226 // Adding padding. |
229 return maxTextWidth + (int) (TEXT_PADDING_DP * | 227 return maxTextWidth + (int) (TEXT_PADDING_DP * |
230 mNativeWindow.getContext().getResources().getDisplayMetrics().de nsity); | 228 mWindowAndroid.getContext().getResources().getDisplayMetrics().d ensity); |
231 } | 229 } |
232 | 230 |
233 @Override | 231 @Override |
234 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | 232 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
235 mAutofillCallback.suggestionSelected(position); | 233 mAutofillCallback.suggestionSelected(position); |
236 } | 234 } |
237 | 235 |
238 } | 236 } |
OLD | NEW |