blob: 5a12893812f03fb8ef8e96699333ec8fbf96a3ff [file] [log] [blame]
Adam Powell42675342010-07-09 18:02:59 -07001/*
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
17package com.android.internal.view.menu;
18
Adam Powell42675342010-07-09 18:02:59 -070019import android.content.Context;
Adam Powell38639b12011-06-17 16:02:32 -070020import android.content.res.Resources;
Adam Powell11ed1d62011-07-11 21:19:59 -070021import android.os.Parcelable;
Adam Powell54c94de2013-09-26 15:36:34 -070022import android.view.Gravity;
Adam Powell8028dd32010-07-15 10:16:33 -070023import android.view.KeyEvent;
Adam Powell696cba52011-03-29 10:38:16 -070024import android.view.LayoutInflater;
Adam Powell91511032011-07-13 10:24:06 -070025import android.view.MenuItem;
Adam Powell42675342010-07-09 18:02:59 -070026import android.view.View;
27import android.view.View.MeasureSpec;
Adam Powell696cba52011-03-29 10:38:16 -070028import android.view.ViewGroup;
Adam Powell04587962010-11-11 22:15:07 -080029import android.view.ViewTreeObserver;
Adam Powell42675342010-07-09 18:02:59 -070030import android.widget.AdapterView;
Adam Powell696cba52011-03-29 10:38:16 -070031import android.widget.BaseAdapter;
Adam Powell91511032011-07-13 10:24:06 -070032import android.widget.FrameLayout;
Adam Powell696cba52011-03-29 10:38:16 -070033import android.widget.ListAdapter;
Adam Powell42675342010-07-09 18:02:59 -070034import android.widget.ListPopupWindow;
Adam Powell6c6f5752010-08-20 18:34:46 -070035import android.widget.PopupWindow;
Adam Powell42675342010-07-09 18:02:59 -070036
Adam Powell696cba52011-03-29 10:38:16 -070037import java.util.ArrayList;
Adam Powell8028dd32010-07-15 10:16:33 -070038
Adam Powell42675342010-07-09 18:02:59 -070039/**
Adam Powell696cba52011-03-29 10:38:16 -070040 * Presents a menu as a small, simple popup anchored to another view.
Adam Powell42675342010-07-09 18:02:59 -070041 * @hide
42 */
Adam Powell04587962010-11-11 22:15:07 -080043public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
Adam Powell4afd62b2011-02-18 15:02:18 -080044 ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener,
Adam Powell696cba52011-03-29 10:38:16 -070045 View.OnAttachStateChangeListener, MenuPresenter {
Adam Powell42675342010-07-09 18:02:59 -070046 private static final String TAG = "MenuPopupHelper";
47
Adam Powell696cba52011-03-29 10:38:16 -070048 static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout;
49
Alan Viverette0bce6ab2013-06-26 17:46:16 -070050 private final Context mContext;
51 private final LayoutInflater mInflater;
52 private final MenuBuilder mMenu;
53 private final MenuAdapter mAdapter;
54 private final boolean mOverflowOnly;
55 private final int mPopupMaxWidth;
Alan Viverette560f1702014-05-05 14:40:07 -070056 private final int mPopupStyleAttr;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070057
Adam Powell4afd62b2011-02-18 15:02:18 -080058 private View mAnchorView;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070059 private ListPopupWindow mPopup;
Adam Powell04587962010-11-11 22:15:07 -080060 private ViewTreeObserver mTreeObserver;
Adam Powell696cba52011-03-29 10:38:16 -070061 private Callback mPresenterCallback;
Adam Powelle2b03a62011-01-25 14:39:02 -080062
Adam Powell91511032011-07-13 10:24:06 -070063 boolean mForceShowIcon;
64
65 private ViewGroup mMeasureParent;
66
Alan Viverette0bce6ab2013-06-26 17:46:16 -070067 /** 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 Powell54c94de2013-09-26 15:36:34 -070073 private int mDropDownGravity = Gravity.NO_GRAVITY;
74
Adam Powell8028dd32010-07-15 10:16:33 -070075 public MenuPopupHelper(Context context, MenuBuilder menu) {
Alan Viverette560f1702014-05-05 14:40:07 -070076 this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle);
Adam Powell8028dd32010-07-15 10:16:33 -070077 }
78
79 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
Alan Viverette560f1702014-05-05 14:40:07 -070080 this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle);
Adam Powell8028dd32010-07-15 10:16:33 -070081 }
82
Alan Viverette560f1702014-05-05 14:40:07 -070083 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
84 boolean overflowOnly, int popupStyleAttr) {
Adam Powell42675342010-07-09 18:02:59 -070085 mContext = context;
Adam Powell696cba52011-03-29 10:38:16 -070086 mInflater = LayoutInflater.from(context);
Adam Powell8028dd32010-07-15 10:16:33 -070087 mMenu = menu;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070088 mAdapter = new MenuAdapter(mMenu);
Adam Powell8028dd32010-07-15 10:16:33 -070089 mOverflowOnly = overflowOnly;
Alan Viverette560f1702014-05-05 14:40:07 -070090 mPopupStyleAttr = popupStyleAttr;
Adam Powell42675342010-07-09 18:02:59 -070091
Adam Powell38639b12011-06-17 16:02:32 -070092 final Resources res = context.getResources();
93 mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
94 res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
Adam Powell8028dd32010-07-15 10:16:33 -070095
Adam Powell4afd62b2011-02-18 15:02:18 -080096 mAnchorView = anchorView;
Adam Powell696cba52011-03-29 10:38:16 -070097
98 menu.addMenuPresenter(this);
Adam Powell42675342010-07-09 18:02:59 -070099 }
100
Adam Powellf0ad6e62011-01-10 17:14:06 -0800101 public void setAnchorView(View anchor) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800102 mAnchorView = anchor;
Adam Powellf0ad6e62011-01-10 17:14:06 -0800103 }
104
Adam Powell91511032011-07-13 10:24:06 -0700105 public void setForceShowIcon(boolean forceShow) {
106 mForceShowIcon = forceShow;
107 }
108
Adam Powell54c94de2013-09-26 15:36:34 -0700109 public void setGravity(int gravity) {
110 mDropDownGravity = gravity;
111 }
112
Adam Powell42675342010-07-09 18:02:59 -0700113 public void show() {
Adam Powell5e3f2842011-01-07 17:16:56 -0800114 if (!tryShow()) {
115 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
116 }
117 }
118
Alan Viveretteca6a36112013-08-16 14:41:06 -0700119 public ListPopupWindow getPopup() {
120 return mPopup;
121 }
122
Adam Powell5e3f2842011-01-07 17:16:56 -0800123 public boolean tryShow() {
Alan Viverette560f1702014-05-05 14:40:07 -0700124 mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr);
Adam Powell8515ee82010-11-30 14:09:55 -0800125 mPopup.setOnDismissListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700126 mPopup.setOnItemClickListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700127 mPopup.setAdapter(mAdapter);
Adam Powell42675342010-07-09 18:02:59 -0700128 mPopup.setModal(true);
129
Adam Powell4afd62b2011-02-18 15:02:18 -0800130 View anchor = mAnchorView;
Adam Powell04587962010-11-11 22:15:07 -0800131 if (anchor != null) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800132 final boolean addGlobalListener = mTreeObserver == null;
133 mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
134 if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
135 anchor.addOnAttachStateChangeListener(this);
Adam Powell04587962010-11-11 22:15:07 -0800136 mPopup.setAnchorView(anchor);
Adam Powell54c94de2013-09-26 15:36:34 -0700137 mPopup.setDropDownGravity(mDropDownGravity);
Adam Powell4be0d522010-08-03 17:53:14 -0700138 } else {
Adam Powell5e3f2842011-01-07 17:16:56 -0800139 return false;
Adam Powell8028dd32010-07-15 10:16:33 -0700140 }
Adam Powell42675342010-07-09 18:02:59 -0700141
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700142 if (!mHasContentWidth) {
143 mContentWidth = measureContentWidth();
144 mHasContentWidth = true;
145 }
146
147 mPopup.setContentWidth(mContentWidth);
Adam Powellaa0b92c2010-12-13 22:38:53 -0800148 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Adam Powell42675342010-07-09 18:02:59 -0700149 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700150 mPopup.getListView().setOnKeyListener(this);
Adam Powell5e3f2842011-01-07 17:16:56 -0800151 return true;
Adam Powell42675342010-07-09 18:02:59 -0700152 }
153
154 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700155 if (isShowing()) {
156 mPopup.dismiss();
157 }
Adam Powell8515ee82010-11-30 14:09:55 -0800158 }
159
160 public void onDismiss() {
161 mPopup = null;
Adam Powell42b91bb2011-06-21 18:32:26 -0700162 mMenu.close();
Adam Powell4afd62b2011-02-18 15:02:18 -0800163 if (mTreeObserver != null) {
164 if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
Adam Powellca51e872011-02-14 19:54:29 -0800165 mTreeObserver.removeGlobalOnLayoutListener(this);
Adam Powell4afd62b2011-02-18 15:02:18 -0800166 mTreeObserver = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800167 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800168 mAnchorView.removeOnAttachStateChangeListener(this);
Adam Powell42675342010-07-09 18:02:59 -0700169 }
170
Adam Powell8028dd32010-07-15 10:16:33 -0700171 public boolean isShowing() {
172 return mPopup != null && mPopup.isShowing();
173 }
174
Adam Powell696cba52011-03-29 10:38:16 -0700175 @Override
Adam Powell42675342010-07-09 18:02:59 -0700176 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell696cba52011-03-29 10:38:16 -0700177 MenuAdapter adapter = mAdapter;
178 adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
Adam Powell42675342010-07-09 18:02:59 -0700179 }
180
Adam Powell8028dd32010-07-15 10:16:33 -0700181 public boolean onKey(View v, int keyCode, KeyEvent event) {
182 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
183 dismiss();
184 return true;
185 }
186 return false;
187 }
188
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700189 private int measureContentWidth() {
Adam Powell42675342010-07-09 18:02:59 -0700190 // Menus don't tend to be long, so this is more sane than it looks.
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700191 int maxWidth = 0;
Adam Powell42675342010-07-09 18:02:59 -0700192 View itemView = null;
Adam Powell50f784c2010-12-19 16:12:19 -0800193 int itemType = 0;
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700194
195 final ListAdapter adapter = mAdapter;
196 final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
197 final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Adam Powell42675342010-07-09 18:02:59 -0700198 final int count = adapter.getCount();
199 for (int i = 0; i < count; i++) {
Adam Powell50f784c2010-12-19 16:12:19 -0800200 final int positionType = adapter.getItemViewType(i);
201 if (positionType != itemType) {
202 itemType = positionType;
203 itemView = null;
204 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700205
Adam Powell91511032011-07-13 10:24:06 -0700206 if (mMeasureParent == null) {
207 mMeasureParent = new FrameLayout(mContext);
208 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700209
Adam Powell91511032011-07-13 10:24:06 -0700210 itemView = adapter.getView(i, itemView, mMeasureParent);
Adam Powell42675342010-07-09 18:02:59 -0700211 itemView.measure(widthMeasureSpec, heightMeasureSpec);
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700212
213 final int itemWidth = itemView.getMeasuredWidth();
214 if (itemWidth >= mPopupMaxWidth) {
215 return mPopupMaxWidth;
216 } else if (itemWidth > maxWidth) {
217 maxWidth = itemWidth;
218 }
Adam Powell42675342010-07-09 18:02:59 -0700219 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700220
221 return maxWidth;
Adam Powell42675342010-07-09 18:02:59 -0700222 }
Adam Powell04587962010-11-11 22:15:07 -0800223
224 @Override
225 public void onGlobalLayout() {
Adam Powell4afd62b2011-02-18 15:02:18 -0800226 if (isShowing()) {
227 final View anchor = mAnchorView;
Adam Powellca51e872011-02-14 19:54:29 -0800228 if (anchor == null || !anchor.isShown()) {
Adam Powell04587962010-11-11 22:15:07 -0800229 dismiss();
Adam Powellca51e872011-02-14 19:54:29 -0800230 } else if (isShowing()) {
Adam Powellaa0b92c2010-12-13 22:38:53 -0800231 // Recompute window size and position
232 mPopup.show();
Adam Powell04587962010-11-11 22:15:07 -0800233 }
234 }
235 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800236
237 @Override
238 public void onViewAttachedToWindow(View v) {
239 }
240
241 @Override
242 public void onViewDetachedFromWindow(View v) {
243 if (mTreeObserver != null) {
244 if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver();
245 mTreeObserver.removeGlobalOnLayoutListener(this);
246 }
247 v.removeOnAttachStateChangeListener(this);
248 }
Adam Powell696cba52011-03-29 10:38:16 -0700249
250 @Override
251 public void initForMenu(Context context, MenuBuilder menu) {
252 // Don't need to do anything; we added as a presenter in the constructor.
253 }
254
255 @Override
256 public MenuView getMenuView(ViewGroup root) {
257 throw new UnsupportedOperationException("MenuPopupHelpers manage their own views");
258 }
259
260 @Override
261 public void updateMenuView(boolean cleared) {
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700262 mHasContentWidth = false;
263
264 if (mAdapter != null) {
265 mAdapter.notifyDataSetChanged();
266 }
Adam Powell696cba52011-03-29 10:38:16 -0700267 }
268
269 @Override
270 public void setCallback(Callback cb) {
271 mPresenterCallback = cb;
272 }
273
274 @Override
275 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
276 if (subMenu.hasVisibleItems()) {
Alan Viverette560f1702014-05-05 14:40:07 -0700277 MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView);
Adam Powell696cba52011-03-29 10:38:16 -0700278 subPopup.setCallback(mPresenterCallback);
Adam Powell91511032011-07-13 10:24:06 -0700279
280 boolean preserveIconSpacing = false;
281 final int count = subMenu.size();
282 for (int i = 0; i < count; i++) {
283 MenuItem childItem = subMenu.getItem(i);
284 if (childItem.isVisible() && childItem.getIcon() != null) {
285 preserveIconSpacing = true;
286 break;
287 }
288 }
289 subPopup.setForceShowIcon(preserveIconSpacing);
290
Adam Powell696cba52011-03-29 10:38:16 -0700291 if (subPopup.tryShow()) {
292 if (mPresenterCallback != null) {
293 mPresenterCallback.onOpenSubMenu(subMenu);
294 }
295 return true;
296 }
297 }
298 return false;
299 }
300
301 @Override
302 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
303 // Only care about the (sub)menu we're presenting.
304 if (menu != mMenu) return;
305
306 dismiss();
307 if (mPresenterCallback != null) {
308 mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
309 }
310 }
311
312 @Override
313 public boolean flagActionItems() {
314 return false;
315 }
316
Adam Powell8d02dea2011-05-31 21:35:13 -0700317 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
318 return false;
319 }
320
321 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
322 return false;
323 }
324
Adam Powell275702c2011-09-23 17:34:04 -0700325 @Override
326 public int getId() {
327 return 0;
328 }
329
330 @Override
331 public Parcelable onSaveInstanceState() {
332 return null;
333 }
334
335 @Override
336 public void onRestoreInstanceState(Parcelable state) {
337 }
338
Adam Powell696cba52011-03-29 10:38:16 -0700339 private class MenuAdapter extends BaseAdapter {
340 private MenuBuilder mAdapterMenu;
Adam Powell275702c2011-09-23 17:34:04 -0700341 private int mExpandedIndex = -1;
Adam Powell696cba52011-03-29 10:38:16 -0700342
343 public MenuAdapter(MenuBuilder menu) {
344 mAdapterMenu = menu;
Adam Powell275702c2011-09-23 17:34:04 -0700345 findExpandedIndex();
Adam Powell696cba52011-03-29 10:38:16 -0700346 }
347
348 public int getCount() {
349 ArrayList<MenuItemImpl> items = mOverflowOnly ?
350 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700351 if (mExpandedIndex < 0) {
352 return items.size();
353 }
354 return items.size() - 1;
Adam Powell696cba52011-03-29 10:38:16 -0700355 }
356
357 public MenuItemImpl getItem(int position) {
358 ArrayList<MenuItemImpl> items = mOverflowOnly ?
359 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700360 if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
361 position++;
362 }
Adam Powell696cba52011-03-29 10:38:16 -0700363 return items.get(position);
364 }
365
366 public long getItemId(int position) {
367 // Since a menu item's ID is optional, we'll use the position as an
368 // ID for the item in the AdapterView
369 return position;
370 }
371
372 public View getView(int position, View convertView, ViewGroup parent) {
373 if (convertView == null) {
374 convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
375 }
376
377 MenuView.ItemView itemView = (MenuView.ItemView) convertView;
Adam Powell91511032011-07-13 10:24:06 -0700378 if (mForceShowIcon) {
379 ((ListMenuItemView) convertView).setForceShowIcon(true);
380 }
Adam Powell696cba52011-03-29 10:38:16 -0700381 itemView.initialize(getItem(position), 0);
382 return convertView;
383 }
Adam Powell275702c2011-09-23 17:34:04 -0700384
385 void findExpandedIndex() {
386 final MenuItemImpl expandedItem = mMenu.getExpandedItem();
387 if (expandedItem != null) {
388 final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
389 final int count = items.size();
390 for (int i = 0; i < count; i++) {
391 final MenuItemImpl item = items.get(i);
392 if (item == expandedItem) {
393 mExpandedIndex = i;
394 return;
395 }
396 }
397 }
398 mExpandedIndex = -1;
399 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700400
Adam Powell275702c2011-09-23 17:34:04 -0700401 @Override
Adam Powell76889f32012-05-08 22:22:52 -0700402 public void notifyDataSetChanged() {
403 findExpandedIndex();
404 super.notifyDataSetChanged();
Adam Powell275702c2011-09-23 17:34:04 -0700405 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700406 }
Adam Powell42675342010-07-09 18:02:59 -0700407}