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