Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.view.menu; |
| 18 | |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame] | 19 | import com.android.internal.view.menu.MenuPresenter.Callback; |
| 20 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 21 | import android.annotation.AttrRes; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
| 24 | import android.annotation.StyleRes; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Alan Viverette | 9109857 | 2016-01-19 14:07:31 -0500 | [diff] [blame^] | 26 | import android.graphics.Rect; |
| 27 | import android.util.DisplayMetrics; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 28 | import android.view.Gravity; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 29 | import android.view.View; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 30 | import android.widget.PopupWindow.OnDismissListener; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 33 | * Presents a menu as a small, simple popup anchored to another view. |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 34 | */ |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 35 | public class MenuPopupHelper implements MenuHelper { |
Alan Viverette | 9109857 | 2016-01-19 14:07:31 -0500 | [diff] [blame^] | 36 | private static final int TOUCH_EPICENTER_SIZE_DP = 48; |
| 37 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 38 | private final Context mContext; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 39 | |
| 40 | // Immutable cached popup menu properties. |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 41 | private final MenuBuilder mMenu; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 42 | private final boolean mOverflowOnly; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 43 | private final int mPopupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 44 | private final int mPopupStyleRes; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 45 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 46 | // Mutable cached popup menu properties. |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 47 | private View mAnchorView; |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 48 | private int mDropDownGravity = Gravity.START; |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 49 | private boolean mForceShowIcon; |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame] | 50 | private Callback mPresenterCallback; |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 51 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 52 | private MenuPopup mPopup; |
| 53 | private OnDismissListener mOnDismissListener; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 54 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 55 | public MenuPopupHelper(@NonNull Context context, @NonNull MenuBuilder menu) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 56 | this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 59 | public MenuPopupHelper(@NonNull Context context, @NonNull MenuBuilder menu, |
| 60 | @NonNull View anchorView) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 61 | this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 64 | public MenuPopupHelper(@NonNull Context context, @NonNull MenuBuilder menu, |
| 65 | @NonNull View anchorView, |
| 66 | boolean overflowOnly, @AttrRes int popupStyleAttr) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 67 | this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0); |
| 68 | } |
| 69 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 70 | public MenuPopupHelper(@NonNull Context context, @NonNull MenuBuilder menu, |
| 71 | @NonNull View anchorView, boolean overflowOnly, @AttrRes int popupStyleAttr, |
| 72 | @StyleRes int popupStyleRes) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 73 | mContext = context; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 74 | mMenu = menu; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 75 | mAnchorView = anchorView; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 76 | mOverflowOnly = overflowOnly; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 77 | mPopupStyleAttr = popupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 78 | mPopupStyleRes = popupStyleRes; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 79 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 80 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 81 | public void setOnDismissListener(@Nullable OnDismissListener listener) { |
| 82 | mOnDismissListener = listener; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 85 | /** |
| 86 | * Sets the view to which the popup window is anchored. |
| 87 | * <p> |
| 88 | * Changes take effect on the next call to show(). |
| 89 | * |
| 90 | * @param anchor the view to which the popup window should be anchored |
| 91 | */ |
| 92 | public void setAnchorView(@NonNull View anchor) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 93 | mAnchorView = anchor; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 96 | /** |
| 97 | * Sets whether the popup menu's adapter is forced to show icons in the |
| 98 | * menu item views. |
| 99 | * <p> |
| 100 | * Changes take effect on the next call to show(). |
| 101 | * |
| 102 | * @param forceShowIcon {@code true} to force icons to be shown, or |
| 103 | * {@code false} for icons to be optionally shown |
| 104 | */ |
| 105 | public void setForceShowIcon(boolean forceShowIcon) { |
| 106 | mForceShowIcon = forceShowIcon; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 109 | /** |
| 110 | * Sets the alignment of the popup window relative to the anchor view. |
| 111 | * <p> |
| 112 | * Changes take effect on the next call to show(). |
| 113 | * |
| 114 | * @param gravity alignment of the popup relative to the anchor |
| 115 | */ |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 116 | public void setGravity(int gravity) { |
| 117 | mDropDownGravity = gravity; |
| 118 | } |
| 119 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 120 | /** |
| 121 | * @return alignment of the popup relative to the anchor |
| 122 | */ |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 123 | public int getGravity() { |
| 124 | return mDropDownGravity; |
| 125 | } |
| 126 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 127 | public void show() { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 128 | if (!tryShow()) { |
| 129 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 130 | } |
| 131 | } |
| 132 | |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 133 | public void show(int x, int y) { |
| 134 | if (!tryShow(x, y)) { |
| 135 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 136 | } |
| 137 | } |
| 138 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 139 | @NonNull |
| 140 | public MenuPopup getPopup() { |
| 141 | if (mPopup == null) { |
| 142 | mPopup = createPopup(); |
| 143 | } |
Alan Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 144 | return mPopup; |
| 145 | } |
| 146 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 147 | /** |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 148 | * Attempts to show the popup anchored to the view specified by {@link #setAnchorView(View)}. |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 149 | * |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 150 | * @return {@code true} if the popup was shown or was already showing prior to calling this |
| 151 | * method, {@code false} otherwise |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 152 | */ |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 153 | public boolean tryShow() { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 154 | if (isShowing()) { |
| 155 | return true; |
| 156 | } |
| 157 | |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame] | 158 | if (mAnchorView == null) { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 159 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 160 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 161 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 162 | showPopup(0, 0, false, false); |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 166 | /** |
| 167 | * Shows the popup menu and makes a best-effort to anchor it to the |
| 168 | * specified (x,y) coordinate relative to the anchor view. |
| 169 | * <p> |
Alan Viverette | 9109857 | 2016-01-19 14:07:31 -0500 | [diff] [blame^] | 170 | * Additionally, the popup's transition epicenter (see |
| 171 | * {@link android.widget.PopupWindow#setEpicenterBounds(Rect)} will be |
| 172 | * centered on the specified coordinate, rather than using the bounds of |
| 173 | * the anchor view. |
| 174 | * <p> |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 175 | * If the popup's resolved gravity is {@link Gravity#LEFT}, this will |
| 176 | * display the popup with its top-left corner at (x,y) relative to the |
| 177 | * anchor view. If the resolved gravity is {@link Gravity#RIGHT}, the |
| 178 | * popup's top-right corner will be at (x,y). |
| 179 | * <p> |
| 180 | * If the popup cannot be displayed fully on-screen, this method will |
| 181 | * attempt to scroll the anchor view's ancestors and/or offset the popup |
| 182 | * such that it may be displayed fully on-screen. |
| 183 | * |
| 184 | * @param x x coordinate relative to the anchor view |
| 185 | * @param y y coordinate relative to the anchor view |
| 186 | * @return {@code true} if the popup was shown or was already showing prior |
| 187 | * to calling this method, {@code false} otherwise |
| 188 | */ |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 189 | public boolean tryShow(int x, int y) { |
| 190 | if (isShowing()) { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | if (mAnchorView == null) { |
| 195 | return false; |
| 196 | } |
| 197 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 198 | showPopup(x, y, true, true); |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 202 | /** |
| 203 | * Creates the popup and assigns cached properties. |
| 204 | * |
| 205 | * @return an initialized popup |
| 206 | */ |
| 207 | @NonNull |
| 208 | private MenuPopup createPopup() { |
| 209 | final boolean enableCascadingSubmenus = mContext.getResources().getBoolean( |
| 210 | com.android.internal.R.bool.config_enableCascadingSubmenus); |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 211 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 212 | final MenuPopup popup; |
| 213 | if (enableCascadingSubmenus) { |
| 214 | popup = new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr, |
| 215 | mPopupStyleRes, mOverflowOnly); |
| 216 | } else { |
| 217 | popup = new StandardMenuPopup(mContext, mMenu, mAnchorView, mPopupStyleAttr, |
| 218 | mPopupStyleRes, mOverflowOnly); |
| 219 | } |
| 220 | |
| 221 | // Assign immutable properties. |
| 222 | popup.addMenu(mMenu); |
| 223 | popup.setOnDismissListener(mInternalOnDismissListener); |
| 224 | |
| 225 | // Assign mutable properties. These may be reassigned later. |
| 226 | popup.setAnchorView(mAnchorView); |
| 227 | popup.setCallback(mPresenterCallback); |
| 228 | popup.setForceShowIcon(mForceShowIcon); |
| 229 | popup.setGravity(mDropDownGravity); |
| 230 | |
| 231 | return popup; |
| 232 | } |
| 233 | |
Alan Viverette | 9109857 | 2016-01-19 14:07:31 -0500 | [diff] [blame^] | 234 | private void showPopup(int xOffset, int yOffset, boolean useOffsets, boolean showTitle) { |
| 235 | final MenuPopup popup = getPopup(); |
| 236 | popup.setShowTitle(showTitle); |
| 237 | |
| 238 | if (useOffsets) { |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 239 | // If the resolved drop-down gravity is RIGHT, the popup's right |
| 240 | // edge will be aligned with the anchor view. Adjust by the anchor |
| 241 | // width such that the top-right corner is at the X offset. |
| 242 | final int hgrav = Gravity.getAbsoluteGravity(mDropDownGravity, |
| 243 | mAnchorView.getLayoutDirection()) & Gravity.HORIZONTAL_GRAVITY_MASK; |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 244 | if (hgrav == Gravity.RIGHT) { |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 245 | xOffset -= mAnchorView.getWidth(); |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 246 | } |
Alan Viverette | 9109857 | 2016-01-19 14:07:31 -0500 | [diff] [blame^] | 247 | |
| 248 | popup.setHorizontalOffset(xOffset); |
| 249 | popup.setVerticalOffset(yOffset); |
| 250 | |
| 251 | // Set the transition epicenter to be roughly finger (or mouse |
| 252 | // cursor) sized and centered around the offset position. This |
| 253 | // will give the appearance that the window is emerging from |
| 254 | // the touch point. |
| 255 | final float density = mContext.getResources().getDisplayMetrics().density; |
| 256 | final int halfSize = (int) (TOUCH_EPICENTER_SIZE_DP * density / 2); |
| 257 | final Rect epicenter = new Rect(xOffset - halfSize, yOffset - halfSize, |
| 258 | xOffset + halfSize, yOffset + halfSize); |
| 259 | popup.setEpicenterBounds(epicenter); |
Alan Viverette | d6443f6 | 2015-11-20 13:57:15 -0500 | [diff] [blame] | 260 | } |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame] | 261 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 262 | popup.show(); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 265 | /** |
| 266 | * Dismisses the popup, if showing. |
| 267 | */ |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 268 | @Override |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 269 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 270 | if (isShowing()) { |
| 271 | mPopup.dismiss(); |
| 272 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 275 | /** |
| 276 | * Called after the popup has been dismissed. |
| 277 | * <p> |
| 278 | * <strong>Note:</strong> Subclasses should call the super implementation |
| 279 | * last to ensure that any necessary tear down has occurred before the |
| 280 | * listener specified by {@link #setOnDismissListener(OnDismissListener)} |
| 281 | * is called. |
| 282 | */ |
| 283 | protected void onDismiss() { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 284 | mPopup = null; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 285 | |
| 286 | if (mOnDismissListener != null) { |
| 287 | mOnDismissListener.onDismiss(); |
| 288 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 291 | public boolean isShowing() { |
| 292 | return mPopup != null && mPopup.isShowing(); |
| 293 | } |
| 294 | |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 295 | @Override |
| 296 | public void setPresenterCallback(@Nullable MenuPresenter.Callback cb) { |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame] | 297 | mPresenterCallback = cb; |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 298 | if (mPopup != null) { |
| 299 | mPopup.setCallback(cb); |
| 300 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 301 | } |
Alan Viverette | 708aa9d | 2015-11-20 15:21:30 -0500 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * Listener used to proxy dismiss callbacks to the helper's owner. |
| 305 | */ |
| 306 | private final OnDismissListener mInternalOnDismissListener = new OnDismissListener() { |
| 307 | @Override |
| 308 | public void onDismiss() { |
| 309 | MenuPopupHelper.this.onDismiss(); |
| 310 | } |
| 311 | }; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 312 | } |