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 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 22 | import android.view.Gravity; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 23 | import android.view.View; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 24 | import android.widget.PopupWindow; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 27 | * Presents a menu as a small, simple popup anchored to another view. |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 28 | * |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 29 | * @hide |
| 30 | */ |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame^] | 31 | public class MenuPopupHelper implements PopupWindow.OnDismissListener { |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 32 | private final Context mContext; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 33 | private final MenuBuilder mMenu; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 34 | private final boolean mOverflowOnly; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 35 | private final int mPopupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 36 | private final int mPopupStyleRes; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 37 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 38 | private View mAnchorView; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 39 | private MenuPopup mPopup; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 40 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 41 | private int mDropDownGravity = Gravity.NO_GRAVITY; |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame^] | 42 | private Callback mPresenterCallback; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 43 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 44 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 45 | this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 49 | this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 52 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView, |
| 53 | boolean overflowOnly, int popupStyleAttr) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 54 | this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0); |
| 55 | } |
| 56 | |
| 57 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView, |
| 58 | boolean overflowOnly, int popupStyleAttr, int popupStyleRes) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 59 | mContext = context; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 60 | mMenu = menu; |
| 61 | mOverflowOnly = overflowOnly; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 62 | mPopupStyleAttr = popupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 63 | mPopupStyleRes = popupStyleRes; |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 64 | mAnchorView = anchorView; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 65 | mPopup = createMenuPopup(); |
| 66 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 67 | |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 68 | private MenuPopup createMenuPopup() { |
| 69 | if (mContext.getResources().getBoolean( |
| 70 | com.android.internal.R.bool.config_enableCascadingSubmenus)) { |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 71 | return new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr, mPopupStyleRes, |
| 72 | mOverflowOnly); |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 73 | } |
| 74 | return new StandardMenuPopup( |
| 75 | mContext, mMenu, mAnchorView, mPopupStyleAttr, mPopupStyleRes, mOverflowOnly); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 78 | public void setAnchorView(View anchor) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 79 | mAnchorView = anchor; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 80 | mPopup.setAnchorView(anchor); |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 83 | public void setForceShowIcon(boolean forceShow) { |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 84 | mPopup.setForceShowIcon(forceShow); |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 87 | public void setGravity(int gravity) { |
| 88 | mDropDownGravity = gravity; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 89 | mPopup.setGravity(gravity); |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 92 | public int getGravity() { |
| 93 | return mDropDownGravity; |
| 94 | } |
| 95 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 96 | public void show() { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 97 | if (!tryShow()) { |
| 98 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 99 | } |
| 100 | } |
| 101 | |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 102 | public ShowableListMenu getPopup() { |
Alan Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 103 | return mPopup; |
| 104 | } |
| 105 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 106 | /** |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 107 | * 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] | 108 | * |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 109 | * @return {@code true} if the popup was shown or was already showing prior to calling this |
| 110 | * method, {@code false} otherwise |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 111 | */ |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 112 | public boolean tryShow() { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 113 | if (isShowing()) { |
| 114 | return true; |
| 115 | } |
| 116 | |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame^] | 117 | if (mAnchorView == null) { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 118 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 119 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 120 | |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame^] | 121 | mPopup = createMenuPopup(); |
| 122 | mPopup.setAnchorView(mAnchorView); |
| 123 | mPopup.setGravity(mDropDownGravity); |
| 124 | mPopup.setCallback(mPresenterCallback); |
| 125 | |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 126 | // In order for subclasses of MenuPopupHelper to satisfy the OnDismissedListener interface, |
| 127 | // we must set the listener to this outer Helper rather than to the inner MenuPopup. |
| 128 | // Not to worry -- the inner MenuPopup will call our own #onDismiss method after it's done |
| 129 | // its own handling. |
| 130 | mPopup.setOnDismissListener(this); |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 131 | |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 132 | mPopup.addMenu(mMenu); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 133 | mPopup.show(); |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 134 | return true; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 138 | if (isShowing()) { |
| 139 | mPopup.dismiss(); |
| 140 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame] | 143 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 144 | public void onDismiss() { |
| 145 | mPopup = null; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 148 | public boolean isShowing() { |
| 149 | return mPopup != null && mPopup.isShowing(); |
| 150 | } |
| 151 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 152 | |
Oren Blasberg | 8e12f8d | 2015-09-02 14:25:56 -0700 | [diff] [blame] | 153 | public void setCallback(MenuPresenter.Callback cb) { |
Oren Blasberg | 9916282 | 2015-09-10 14:37:26 -0700 | [diff] [blame^] | 154 | mPresenterCallback = cb; |
Oren Blasberg | b23976e | 2015-09-01 14:55:42 -0700 | [diff] [blame] | 155 | mPopup.setCallback(cb); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 156 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 157 | } |