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 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Adam Powell | 38639b1 | 2011-06-17 16:02:32 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 21 | import android.os.Parcelable; |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 22 | import android.view.Gravity; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 23 | import android.view.KeyEvent; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 25 | import android.view.MenuItem; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.View.MeasureSpec; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 29 | import android.view.ViewTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 30 | import android.widget.AdapterView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 31 | import android.widget.BaseAdapter; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 33 | import android.widget.ListAdapter; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 34 | import android.widget.ListPopupWindow; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 35 | import android.widget.PopupWindow; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 36 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 38 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 39 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 40 | * Presents a menu as a small, simple popup anchored to another view. |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 41 | * @hide |
| 42 | */ |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 43 | public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener, |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 44 | ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener, |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 45 | View.OnAttachStateChangeListener, MenuPresenter { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 46 | static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout; |
| 47 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 48 | private final Context mContext; |
| 49 | private final LayoutInflater mInflater; |
| 50 | private final MenuBuilder mMenu; |
| 51 | private final MenuAdapter mAdapter; |
| 52 | private final boolean mOverflowOnly; |
| 53 | private final int mPopupMaxWidth; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 54 | private final int mPopupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 55 | private final int mPopupStyleRes; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 56 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 57 | private View mAnchorView; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 58 | private ListPopupWindow mPopup; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 59 | private ViewTreeObserver mTreeObserver; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 60 | private Callback mPresenterCallback; |
Adam Powell | e2b03a6 | 2011-01-25 14:39:02 -0800 | [diff] [blame] | 61 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 62 | boolean mForceShowIcon; |
| 63 | |
| 64 | private ViewGroup mMeasureParent; |
| 65 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 66 | /** Whether the cached content width value is valid. */ |
| 67 | private boolean mHasContentWidth; |
| 68 | |
| 69 | /** Cached content width from {@link #measureContentWidth}. */ |
| 70 | private int mContentWidth; |
| 71 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 72 | private int mDropDownGravity = Gravity.NO_GRAVITY; |
| 73 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 74 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 75 | this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 79 | this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle, 0); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 82 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView, |
| 83 | boolean overflowOnly, int popupStyleAttr) { |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 84 | this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0); |
| 85 | } |
| 86 | |
| 87 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView, |
| 88 | boolean overflowOnly, int popupStyleAttr, int popupStyleRes) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 89 | mContext = context; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 90 | mInflater = LayoutInflater.from(context); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 91 | mMenu = menu; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 92 | mAdapter = new MenuAdapter(mMenu); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 93 | mOverflowOnly = overflowOnly; |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 94 | mPopupStyleAttr = popupStyleAttr; |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 95 | mPopupStyleRes = popupStyleRes; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 96 | |
Adam Powell | 38639b1 | 2011-06-17 16:02:32 -0700 | [diff] [blame] | 97 | final Resources res = context.getResources(); |
| 98 | mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2, |
| 99 | res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth)); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 100 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 101 | mAnchorView = anchorView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 102 | |
Alan Viverette | 3d0f21d | 2014-07-10 16:15:01 -0700 | [diff] [blame] | 103 | // Present the menu using our context, not the menu builder's context. |
| 104 | menu.addMenuPresenter(this, context); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 107 | public void setAnchorView(View anchor) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 108 | mAnchorView = anchor; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 111 | public void setForceShowIcon(boolean forceShow) { |
| 112 | mForceShowIcon = forceShow; |
| 113 | } |
| 114 | |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 115 | public void setGravity(int gravity) { |
| 116 | mDropDownGravity = gravity; |
| 117 | } |
| 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 | |
Alan Viverette | ca6a3611 | 2013-08-16 14:41:06 -0700 | [diff] [blame] | 129 | public ListPopupWindow getPopup() { |
| 130 | return mPopup; |
| 131 | } |
| 132 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame^] | 133 | /** |
| 134 | * Attempts to show the popup anchored to the view specified by |
| 135 | * {@link #setAnchorView(View)}. |
| 136 | * |
| 137 | * @return {@code true} if the popup was shown or was already showing prior |
| 138 | * to calling this method, {@code false} otherwise |
| 139 | */ |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 140 | public boolean tryShow() { |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame^] | 141 | if (isShowing()) { |
| 142 | return true; |
| 143 | } |
| 144 | |
Alan Viverette | 2963252 | 2014-10-15 17:19:30 -0700 | [diff] [blame] | 145 | mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 146 | mPopup.setOnDismissListener(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 147 | mPopup.setOnItemClickListener(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 148 | mPopup.setAdapter(mAdapter); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 149 | mPopup.setModal(true); |
| 150 | |
Alan Viverette | 75d8379 | 2015-01-07 15:51:54 -0800 | [diff] [blame] | 151 | final View anchor = mAnchorView; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 152 | if (anchor != null) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 153 | final boolean addGlobalListener = mTreeObserver == null; |
| 154 | mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest |
| 155 | if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this); |
| 156 | anchor.addOnAttachStateChangeListener(this); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 157 | mPopup.setAnchorView(anchor); |
Adam Powell | 54c94de | 2013-09-26 15:36:34 -0700 | [diff] [blame] | 158 | mPopup.setDropDownGravity(mDropDownGravity); |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 159 | } else { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 160 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 161 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 162 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 163 | if (!mHasContentWidth) { |
| 164 | mContentWidth = measureContentWidth(); |
| 165 | mHasContentWidth = true; |
| 166 | } |
| 167 | |
| 168 | mPopup.setContentWidth(mContentWidth); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 169 | mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 170 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 171 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 172 | return true; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 176 | if (isShowing()) { |
| 177 | mPopup.dismiss(); |
| 178 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame^] | 181 | @Override |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 182 | public void onDismiss() { |
| 183 | mPopup = null; |
Adam Powell | 42b91bb | 2011-06-21 18:32:26 -0700 | [diff] [blame] | 184 | mMenu.close(); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 185 | if (mTreeObserver != null) { |
| 186 | if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 187 | mTreeObserver.removeGlobalOnLayoutListener(this); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 188 | mTreeObserver = null; |
Adam Powell | ed8b403 | 2010-11-16 10:22:35 -0800 | [diff] [blame] | 189 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 190 | mAnchorView.removeOnAttachStateChangeListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 193 | public boolean isShowing() { |
| 194 | return mPopup != null && mPopup.isShowing(); |
| 195 | } |
| 196 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 197 | @Override |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 198 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 199 | MenuAdapter adapter = mAdapter; |
| 200 | adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alan Viverette | 8fd949e | 2015-03-11 12:21:30 -0700 | [diff] [blame^] | 203 | @Override |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 204 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 205 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 206 | dismiss(); |
| 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 212 | private int measureContentWidth() { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 213 | // Menus don't tend to be long, so this is more sane than it looks. |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 214 | int maxWidth = 0; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 215 | View itemView = null; |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 216 | int itemType = 0; |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 217 | |
| 218 | final ListAdapter adapter = mAdapter; |
| 219 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 220 | final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 221 | final int count = adapter.getCount(); |
| 222 | for (int i = 0; i < count; i++) { |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 223 | final int positionType = adapter.getItemViewType(i); |
| 224 | if (positionType != itemType) { |
| 225 | itemType = positionType; |
| 226 | itemView = null; |
| 227 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 228 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 229 | if (mMeasureParent == null) { |
| 230 | mMeasureParent = new FrameLayout(mContext); |
| 231 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 232 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 233 | itemView = adapter.getView(i, itemView, mMeasureParent); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 234 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 235 | |
| 236 | final int itemWidth = itemView.getMeasuredWidth(); |
| 237 | if (itemWidth >= mPopupMaxWidth) { |
| 238 | return mPopupMaxWidth; |
| 239 | } else if (itemWidth > maxWidth) { |
| 240 | maxWidth = itemWidth; |
| 241 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 242 | } |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 243 | |
| 244 | return maxWidth; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 245 | } |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 246 | |
| 247 | @Override |
| 248 | public void onGlobalLayout() { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 249 | if (isShowing()) { |
| 250 | final View anchor = mAnchorView; |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 251 | if (anchor == null || !anchor.isShown()) { |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 252 | dismiss(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 253 | } else if (isShowing()) { |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 254 | // Recompute window size and position |
| 255 | mPopup.show(); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 259 | |
| 260 | @Override |
| 261 | public void onViewAttachedToWindow(View v) { |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onViewDetachedFromWindow(View v) { |
| 266 | if (mTreeObserver != null) { |
| 267 | if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver(); |
| 268 | mTreeObserver.removeGlobalOnLayoutListener(this); |
| 269 | } |
| 270 | v.removeOnAttachStateChangeListener(this); |
| 271 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 272 | |
| 273 | @Override |
| 274 | public void initForMenu(Context context, MenuBuilder menu) { |
| 275 | // Don't need to do anything; we added as a presenter in the constructor. |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public MenuView getMenuView(ViewGroup root) { |
| 280 | throw new UnsupportedOperationException("MenuPopupHelpers manage their own views"); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void updateMenuView(boolean cleared) { |
Alan Viverette | 0bce6ab | 2013-06-26 17:46:16 -0700 | [diff] [blame] | 285 | mHasContentWidth = false; |
| 286 | |
| 287 | if (mAdapter != null) { |
| 288 | mAdapter.notifyDataSetChanged(); |
| 289 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void setCallback(Callback cb) { |
| 294 | mPresenterCallback = cb; |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 299 | if (subMenu.hasVisibleItems()) { |
Alan Viverette | 560f170 | 2014-05-05 14:40:07 -0700 | [diff] [blame] | 300 | MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 301 | subPopup.setCallback(mPresenterCallback); |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 302 | |
| 303 | boolean preserveIconSpacing = false; |
| 304 | final int count = subMenu.size(); |
| 305 | for (int i = 0; i < count; i++) { |
| 306 | MenuItem childItem = subMenu.getItem(i); |
| 307 | if (childItem.isVisible() && childItem.getIcon() != null) { |
| 308 | preserveIconSpacing = true; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | subPopup.setForceShowIcon(preserveIconSpacing); |
| 313 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 314 | if (subPopup.tryShow()) { |
| 315 | if (mPresenterCallback != null) { |
| 316 | mPresenterCallback.onOpenSubMenu(subMenu); |
| 317 | } |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 326 | // Only care about the (sub)menu we're presenting. |
| 327 | if (menu != mMenu) return; |
| 328 | |
| 329 | dismiss(); |
| 330 | if (mPresenterCallback != null) { |
| 331 | mPresenterCallback.onCloseMenu(menu, allMenusAreClosing); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public boolean flagActionItems() { |
| 337 | return false; |
| 338 | } |
| 339 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 340 | public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { |
| 345 | return false; |
| 346 | } |
| 347 | |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 348 | @Override |
| 349 | public int getId() { |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public Parcelable onSaveInstanceState() { |
| 355 | return null; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void onRestoreInstanceState(Parcelable state) { |
| 360 | } |
| 361 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 362 | private class MenuAdapter extends BaseAdapter { |
| 363 | private MenuBuilder mAdapterMenu; |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 364 | private int mExpandedIndex = -1; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 365 | |
| 366 | public MenuAdapter(MenuBuilder menu) { |
| 367 | mAdapterMenu = menu; |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 368 | findExpandedIndex(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | public int getCount() { |
| 372 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 373 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 374 | if (mExpandedIndex < 0) { |
| 375 | return items.size(); |
| 376 | } |
| 377 | return items.size() - 1; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | public MenuItemImpl getItem(int position) { |
| 381 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 382 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 383 | if (mExpandedIndex >= 0 && position >= mExpandedIndex) { |
| 384 | position++; |
| 385 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 386 | return items.get(position); |
| 387 | } |
| 388 | |
| 389 | public long getItemId(int position) { |
| 390 | // Since a menu item's ID is optional, we'll use the position as an |
| 391 | // ID for the item in the AdapterView |
| 392 | return position; |
| 393 | } |
| 394 | |
| 395 | public View getView(int position, View convertView, ViewGroup parent) { |
| 396 | if (convertView == null) { |
| 397 | convertView = mInflater.inflate(ITEM_LAYOUT, parent, false); |
| 398 | } |
| 399 | |
| 400 | MenuView.ItemView itemView = (MenuView.ItemView) convertView; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 401 | if (mForceShowIcon) { |
| 402 | ((ListMenuItemView) convertView).setForceShowIcon(true); |
| 403 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 404 | itemView.initialize(getItem(position), 0); |
| 405 | return convertView; |
| 406 | } |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 407 | |
| 408 | void findExpandedIndex() { |
| 409 | final MenuItemImpl expandedItem = mMenu.getExpandedItem(); |
| 410 | if (expandedItem != null) { |
| 411 | final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems(); |
| 412 | final int count = items.size(); |
| 413 | for (int i = 0; i < count; i++) { |
| 414 | final MenuItemImpl item = items.get(i); |
| 415 | if (item == expandedItem) { |
| 416 | mExpandedIndex = i; |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | mExpandedIndex = -1; |
| 422 | } |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 423 | |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 424 | @Override |
Adam Powell | 76889f3 | 2012-05-08 22:22:52 -0700 | [diff] [blame] | 425 | public void notifyDataSetChanged() { |
| 426 | findExpandedIndex(); |
| 427 | super.notifyDataSetChanged(); |
Adam Powell | 275702c | 2011-09-23 17:34:04 -0700 | [diff] [blame] | 428 | } |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 429 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 430 | } |