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