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