| 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.view.LayoutInflater; | 13 import android.view.LayoutInflater; |
| 14 import android.view.View; | 14 import android.view.View; |
| 15 import android.view.WindowManager; | 15 import android.view.WindowManager; |
| 16 import android.widget.AdapterView; | 16 import android.widget.AdapterView; |
| 17 import android.widget.FrameLayout; | 17 import android.widget.FrameLayout; |
| 18 import android.widget.ListPopupWindow; | 18 import android.widget.ListPopupWindow; |
| 19 import android.widget.TextView; | 19 import android.widget.TextView; |
| 20 | 20 |
| 21 import java.util.ArrayList; | 21 import java.util.ArrayList; |
| 22 | 22 |
| 23 import org.chromium.chrome.R; | 23 import org.chromium.chrome.R; |
| 24 import org.chromium.content.browser.ContainerViewDelegate; | |
| 25 import org.chromium.ui.gfx.DeviceDisplayInfo; | 24 import org.chromium.ui.gfx.DeviceDisplayInfo; |
| 25 import org.chromium.ui.ViewAndroidDelegate; |
| 26 import org.chromium.ui.WindowAndroid; | 26 import org.chromium.ui.WindowAndroid; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * The Autofill suggestion popup that lists relevant suggestions. | 29 * The Autofill suggestion popup that lists relevant suggestions. |
| 30 */ | 30 */ |
| 31 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
ClickListener { | 31 public class AutofillPopup extends ListPopupWindow implements AdapterView.OnItem
ClickListener { |
| 32 | 32 |
| 33 /** | 33 /** |
| 34 * Constants defining types of Autofill suggestion entries. | 34 * Constants defining types of Autofill suggestion entries. |
| 35 * Has to be kept in sync with enum in WebAutofillClient.h | 35 * Has to be kept in sync with enum in WebAutofillClient.h |
| 36 * | 36 * |
| 37 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC
learForm, and | 37 * Not supported: MenuItemIDWarningMessage, MenuItemIDSeparator, MenuItemIDC
learForm, and |
| 38 * MenuItemIDAutofillOptions. | 38 * MenuItemIDAutofillOptions. |
| 39 */ | 39 */ |
| 40 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; | 40 private static final int ITEM_ID_AUTOCOMPLETE_ENTRY = 0; |
| 41 private static final int ITEM_ID_PASSWORD_ENTRY = -2; | 41 private static final int ITEM_ID_PASSWORD_ENTRY = -2; |
| 42 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; | 42 private static final int ITEM_ID_DATA_LIST_ENTRY = -6; |
| 43 | 43 |
| 44 private static final int TEXT_PADDING_DP = 30; | 44 private static final int TEXT_PADDING_DP = 30; |
| 45 | 45 |
| 46 private final AutofillPopupDelegate mAutofillCallback; | 46 private final AutofillPopupDelegate mAutofillCallback; |
| 47 private final Context mContext; | 47 private final Context mContext; |
| 48 private final ContainerViewDelegate mContainerViewDelegate; | 48 private final ViewAndroidDelegate mViewAndroidDelegate; |
| 49 private AnchorView mAnchorView; | 49 private AnchorView mAnchorView; |
| 50 private Rect mAnchorRect; | 50 private Rect mAnchorRect; |
| 51 private Paint mNameViewPaint; | 51 private Paint mNameViewPaint; |
| 52 private Paint mLabelViewPaint; | 52 private Paint mLabelViewPaint; |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * An interface to handle the touch interaction with an AutofillPopup object
. | 55 * An interface to handle the touch interaction with an AutofillPopup object
. |
| 56 */ | 56 */ |
| 57 public interface AutofillPopupDelegate { | 57 public interface AutofillPopupDelegate { |
| 58 /** | 58 /** |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, he
ight); | 116 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, he
ight); |
| 117 lp.leftMargin = r.left - paddingRect.left; | 117 lp.leftMargin = r.left - paddingRect.left; |
| 118 lp.topMargin = r.top; | 118 lp.topMargin = r.top; |
| 119 setLayoutParams(lp); | 119 setLayoutParams(lp); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 /** | 123 /** |
| 124 * Creates an AutofillWindow with specified parameters. | 124 * Creates an AutofillWindow with specified parameters. |
| 125 * @param context Application context. | 125 * @param context Application context. |
| 126 * @param containerViewDelegate View delegate used to add and remove views. | 126 * @param viewAndroidDelegate View delegate used to add and remove views. |
| 127 * @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. |
| 128 */ | 128 */ |
| 129 public AutofillPopup(Context context, ContainerViewDelegate containerViewDel
egate, | 129 public AutofillPopup(Context context, ViewAndroidDelegate viewAndroidDelegat
e, |
| 130 AutofillPopupDelegate autofillCallback) { | 130 AutofillPopupDelegate autofillCallback) { |
| 131 super(context); | 131 super(context); |
| 132 mContext = context; | 132 mContext = context; |
| 133 mContainerViewDelegate = containerViewDelegate; | 133 mViewAndroidDelegate = viewAndroidDelegate ; |
| 134 mAutofillCallback = autofillCallback; | 134 mAutofillCallback = autofillCallback; |
| 135 | 135 |
| 136 setOnItemClickListener(this); | 136 setOnItemClickListener(this); |
| 137 | 137 |
| 138 mAnchorView = new AnchorView(context, this); | 138 mAnchorView = new AnchorView(context, this); |
| 139 mContainerViewDelegate.addViewToContainerView(mAnchorView); | 139 mViewAndroidDelegate.addViewToContainerView(mAnchorView); |
| 140 setAnchorView(mAnchorView); | 140 setAnchorView(mAnchorView); |
| 141 } | 141 } |
| 142 | 142 |
| 143 /** | 143 /** |
| 144 * 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 |
| 145 * itself. | 145 * itself. |
| 146 * @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. |
| 147 * @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. |
| 148 * @param width The width of the anchor view. | 148 * @param width The width of the anchor view. |
| 149 * @param height The height of the anchor view. | 149 * @param height The height of the anchor view. |
| 150 */ | 150 */ |
| 151 public void setAnchorRect(float x, float y, float width, float height) { | 151 public void setAnchorRect(float x, float y, float width, float height) { |
| 152 float scale = (float) DeviceDisplayInfo.create(mContext).getDIPScale(); | 152 float scale = (float) DeviceDisplayInfo.create(mContext).getDIPScale(); |
| 153 mAnchorRect = new Rect(Math.round(x * scale), Math.round(y * scale), | 153 mAnchorRect = new Rect(Math.round(x * scale), Math.round(y * scale), |
| 154 Math.round((x + width) * scale), Math.round((y + height) * scale
)); | 154 Math.round((x + width) * scale), Math.round((y + height) * scale
)); |
| 155 mAnchorRect.offset(0, mContainerViewDelegate.getChildViewOffsetYPix()); | 155 mAnchorRect.offset(0, mViewAndroidDelegate.getChildViewOffsetYPix()); |
| 156 } | 156 } |
| 157 | 157 |
| 158 /** | 158 /** |
| 159 * 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
. |
| 160 * @param suggestions Autofill suggestion data. | 160 * @param suggestions Autofill suggestion data. |
| 161 */ | 161 */ |
| 162 public void show(AutofillSuggestion[] suggestions) { | 162 public void show(AutofillSuggestion[] suggestions) { |
| 163 // 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 |
| 164 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges
tion>(); | 164 ArrayList<AutofillSuggestion> cleanedData = new ArrayList<AutofillSugges
tion>(); |
| 165 for (int i = 0; i < suggestions.length; i++) { | 165 for (int i = 0; i < suggestions.length; i++) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 181 @Override | 181 @Override |
| 182 public void dismiss() { | 182 public void dismiss() { |
| 183 mAutofillCallback.requestHide(); | 183 mAutofillCallback.requestHide(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 /** | 186 /** |
| 187 * Hides the popup and removes the anchor view from the ContainerView. | 187 * Hides the popup and removes the anchor view from the ContainerView. |
| 188 */ | 188 */ |
| 189 public void hide() { | 189 public void hide() { |
| 190 super.dismiss(); | 190 super.dismiss(); |
| 191 mContainerViewDelegate.removeViewFromContainerView(mAnchorView); | 191 mViewAndroidDelegate.removeViewFromContainerView(mAnchorView); |
| 192 } | 192 } |
| 193 | 193 |
| 194 /** | 194 /** |
| 195 * 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. |
| 196 * @param data Autofill suggestion data. | 196 * @param data Autofill suggestion data. |
| 197 * @return The popup window width. | 197 * @return The popup window width. |
| 198 */ | 198 */ |
| 199 private int getDesiredWidth(AutofillSuggestion[] data) { | 199 private int getDesiredWidth(AutofillSuggestion[] data) { |
| 200 if (mNameViewPaint == null || mLabelViewPaint == null) { | 200 if (mNameViewPaint == null || mLabelViewPaint == null) { |
| 201 LayoutInflater inflater = | 201 LayoutInflater inflater = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 226 return maxTextWidth + (int) (TEXT_PADDING_DP * | 226 return maxTextWidth + (int) (TEXT_PADDING_DP * |
| 227 mContext.getResources().getDisplayMetrics().density); | 227 mContext.getResources().getDisplayMetrics().density); |
| 228 } | 228 } |
| 229 | 229 |
| 230 @Override | 230 @Override |
| 231 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { | 231 public void onItemClick(AdapterView<?> parent, View view, int position, long
id) { |
| 232 mAutofillCallback.suggestionSelected(position); | 232 mAutofillCallback.suggestionSelected(position); |
| 233 } | 233 } |
| 234 | 234 |
| 235 } | 235 } |
| OLD | NEW |