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; |
| 20 | import android.util.DisplayMetrics; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 21 | import android.view.KeyEvent; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 22 | import android.view.LayoutInflater; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.View.MeasureSpec; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 26 | import android.view.ViewTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 27 | import android.widget.AdapterView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 28 | import android.widget.BaseAdapter; |
| 29 | import android.widget.ListAdapter; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 30 | import android.widget.ListPopupWindow; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 31 | import android.widget.PopupWindow; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 32 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 34 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 35 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 36 | * Presents a menu as a small, simple popup anchored to another view. |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 37 | * @hide |
| 38 | */ |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 39 | public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener, |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 40 | ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener, |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 41 | View.OnAttachStateChangeListener, MenuPresenter { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 42 | private static final String TAG = "MenuPopupHelper"; |
| 43 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 44 | static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout; |
| 45 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 46 | private Context mContext; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 47 | private LayoutInflater mInflater; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 48 | private ListPopupWindow mPopup; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 49 | private MenuBuilder mMenu; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 50 | private int mPopupMaxWidth; |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 51 | private View mAnchorView; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 52 | private boolean mOverflowOnly; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 53 | private ViewTreeObserver mTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 54 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 55 | private MenuAdapter mAdapter; |
| 56 | |
| 57 | private Callback mPresenterCallback; |
Adam Powell | e2b03a6 | 2011-01-25 14:39:02 -0800 | [diff] [blame] | 58 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 59 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
| 60 | this(context, menu, null, false); |
| 61 | } |
| 62 | |
| 63 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
| 64 | this(context, menu, anchorView, false); |
| 65 | } |
| 66 | |
| 67 | public MenuPopupHelper(Context context, MenuBuilder menu, |
| 68 | View anchorView, boolean overflowOnly) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 69 | mContext = context; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 70 | mInflater = LayoutInflater.from(context); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 71 | mMenu = menu; |
| 72 | mOverflowOnly = overflowOnly; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 73 | |
| 74 | final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 75 | mPopupMaxWidth = metrics.widthPixels / 2; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 76 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 77 | mAnchorView = anchorView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 78 | |
| 79 | menu.addMenuPresenter(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 82 | public void setAnchorView(View anchor) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 83 | mAnchorView = anchor; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 86 | public void show() { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 87 | if (!tryShow()) { |
| 88 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public boolean tryShow() { |
Adam Powell | 0b2d306 | 2010-09-14 16:15:02 -0700 | [diff] [blame] | 93 | mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 94 | mPopup.setOnDismissListener(this); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 95 | mPopup.setOnItemClickListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 96 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 97 | mAdapter = new MenuAdapter(mMenu); |
| 98 | mPopup.setAdapter(mAdapter); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 99 | mPopup.setModal(true); |
| 100 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 101 | View anchor = mAnchorView; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 102 | if (anchor != null) { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 103 | final boolean addGlobalListener = mTreeObserver == null; |
| 104 | mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest |
| 105 | if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this); |
| 106 | anchor.addOnAttachStateChangeListener(this); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 107 | mPopup.setAnchorView(anchor); |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 108 | } else { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 109 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 110 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 111 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 112 | mPopup.setContentWidth(Math.min(measureContentWidth(mAdapter), mPopupMaxWidth)); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 113 | mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 114 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 115 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 116 | return true; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 120 | if (isShowing()) { |
| 121 | mPopup.dismiss(); |
| 122 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public void onDismiss() { |
| 126 | mPopup = null; |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 127 | if (mTreeObserver != null) { |
| 128 | if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 129 | mTreeObserver.removeGlobalOnLayoutListener(this); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 130 | mTreeObserver = null; |
Adam Powell | ed8b403 | 2010-11-16 10:22:35 -0800 | [diff] [blame] | 131 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 132 | mAnchorView.removeOnAttachStateChangeListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 135 | public boolean isShowing() { |
| 136 | return mPopup != null && mPopup.isShowing(); |
| 137 | } |
| 138 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 139 | @Override |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 140 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 141 | MenuAdapter adapter = mAdapter; |
| 142 | adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 145 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 146 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 147 | dismiss(); |
| 148 | return true; |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 153 | private int measureContentWidth(ListAdapter adapter) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 154 | // Menus don't tend to be long, so this is more sane than it looks. |
| 155 | int width = 0; |
| 156 | View itemView = null; |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 157 | int itemType = 0; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 158 | final int widthMeasureSpec = |
| 159 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 160 | final int heightMeasureSpec = |
| 161 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 162 | final int count = adapter.getCount(); |
| 163 | for (int i = 0; i < count; i++) { |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 164 | final int positionType = adapter.getItemViewType(i); |
| 165 | if (positionType != itemType) { |
| 166 | itemType = positionType; |
| 167 | itemView = null; |
| 168 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 169 | itemView = adapter.getView(i, itemView, null); |
| 170 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
| 171 | width = Math.max(width, itemView.getMeasuredWidth()); |
| 172 | } |
| 173 | return width; |
| 174 | } |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 175 | |
| 176 | @Override |
| 177 | public void onGlobalLayout() { |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 178 | if (isShowing()) { |
| 179 | final View anchor = mAnchorView; |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 180 | if (anchor == null || !anchor.isShown()) { |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 181 | dismiss(); |
Adam Powell | ca51e87 | 2011-02-14 19:54:29 -0800 | [diff] [blame] | 182 | } else if (isShowing()) { |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 183 | // Recompute window size and position |
| 184 | mPopup.show(); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 188 | |
| 189 | @Override |
| 190 | public void onViewAttachedToWindow(View v) { |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onViewDetachedFromWindow(View v) { |
| 195 | if (mTreeObserver != null) { |
| 196 | if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver(); |
| 197 | mTreeObserver.removeGlobalOnLayoutListener(this); |
| 198 | } |
| 199 | v.removeOnAttachStateChangeListener(this); |
| 200 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public void initForMenu(Context context, MenuBuilder menu) { |
| 204 | // Don't need to do anything; we added as a presenter in the constructor. |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public MenuView getMenuView(ViewGroup root) { |
| 209 | throw new UnsupportedOperationException("MenuPopupHelpers manage their own views"); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void updateMenuView(boolean cleared) { |
| 214 | if (mAdapter != null) mAdapter.notifyDataSetChanged(); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void setCallback(Callback cb) { |
| 219 | mPresenterCallback = cb; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 224 | if (subMenu.hasVisibleItems()) { |
| 225 | MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView, false); |
| 226 | subPopup.setCallback(mPresenterCallback); |
| 227 | if (subPopup.tryShow()) { |
| 228 | if (mPresenterCallback != null) { |
| 229 | mPresenterCallback.onOpenSubMenu(subMenu); |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 239 | // Only care about the (sub)menu we're presenting. |
| 240 | if (menu != mMenu) return; |
| 241 | |
| 242 | dismiss(); |
| 243 | if (mPresenterCallback != null) { |
| 244 | mPresenterCallback.onCloseMenu(menu, allMenusAreClosing); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public boolean flagActionItems() { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | private class MenuAdapter extends BaseAdapter { |
| 254 | private MenuBuilder mAdapterMenu; |
| 255 | |
| 256 | public MenuAdapter(MenuBuilder menu) { |
| 257 | mAdapterMenu = menu; |
| 258 | } |
| 259 | |
| 260 | public int getCount() { |
| 261 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 262 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
| 263 | return items.size(); |
| 264 | } |
| 265 | |
| 266 | public MenuItemImpl getItem(int position) { |
| 267 | ArrayList<MenuItemImpl> items = mOverflowOnly ? |
| 268 | mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems(); |
| 269 | return items.get(position); |
| 270 | } |
| 271 | |
| 272 | public long getItemId(int position) { |
| 273 | // Since a menu item's ID is optional, we'll use the position as an |
| 274 | // ID for the item in the AdapterView |
| 275 | return position; |
| 276 | } |
| 277 | |
| 278 | public View getView(int position, View convertView, ViewGroup parent) { |
| 279 | if (convertView == null) { |
| 280 | convertView = mInflater.inflate(ITEM_LAYOUT, parent, false); |
| 281 | } |
| 282 | |
| 283 | MenuView.ItemView itemView = (MenuView.ItemView) convertView; |
| 284 | itemView.initialize(getItem(position), 0); |
| 285 | return convertView; |
| 286 | } |
| 287 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 288 | } |