blob: 99bb1ac9c6ca481f89538d7b095acc63de55e177 [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 Viverette29632522014-10-15 17:19:30 -070057 private final int mPopupStyleRes;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070058
Adam Powell4afd62b2011-02-18 15:02:18 -080059 private View mAnchorView;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070060 private ListPopupWindow mPopup;
Adam Powell04587962010-11-11 22:15:07 -080061 private ViewTreeObserver mTreeObserver;
Adam Powell696cba52011-03-29 10:38:16 -070062 private Callback mPresenterCallback;
Adam Powelle2b03a62011-01-25 14:39:02 -080063
Adam Powell91511032011-07-13 10:24:06 -070064 boolean mForceShowIcon;
65
66 private ViewGroup mMeasureParent;
67
Alan Viverette0bce6ab2013-06-26 17:46:16 -070068 /** Whether the cached content width value is valid. */
69 private boolean mHasContentWidth;
70
71 /** Cached content width from {@link #measureContentWidth}. */
72 private int mContentWidth;
73
Adam Powell54c94de2013-09-26 15:36:34 -070074 private int mDropDownGravity = Gravity.NO_GRAVITY;
75
Adam Powell8028dd32010-07-15 10:16:33 -070076 public MenuPopupHelper(Context context, MenuBuilder menu) {
Alan Viverette29632522014-10-15 17:19:30 -070077 this(context, menu, null, false, com.android.internal.R.attr.popupMenuStyle, 0);
Adam Powell8028dd32010-07-15 10:16:33 -070078 }
79
80 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
Alan Viverette29632522014-10-15 17:19:30 -070081 this(context, menu, anchorView, false, com.android.internal.R.attr.popupMenuStyle, 0);
Adam Powell8028dd32010-07-15 10:16:33 -070082 }
83
Alan Viverette560f1702014-05-05 14:40:07 -070084 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
85 boolean overflowOnly, int popupStyleAttr) {
Alan Viverette29632522014-10-15 17:19:30 -070086 this(context, menu, anchorView, overflowOnly, popupStyleAttr, 0);
87 }
88
89 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView,
90 boolean overflowOnly, int popupStyleAttr, int popupStyleRes) {
Adam Powell42675342010-07-09 18:02:59 -070091 mContext = context;
Adam Powell696cba52011-03-29 10:38:16 -070092 mInflater = LayoutInflater.from(context);
Adam Powell8028dd32010-07-15 10:16:33 -070093 mMenu = menu;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070094 mAdapter = new MenuAdapter(mMenu);
Adam Powell8028dd32010-07-15 10:16:33 -070095 mOverflowOnly = overflowOnly;
Alan Viverette560f1702014-05-05 14:40:07 -070096 mPopupStyleAttr = popupStyleAttr;
Alan Viverette29632522014-10-15 17:19:30 -070097 mPopupStyleRes = popupStyleRes;
Adam Powell42675342010-07-09 18:02:59 -070098
Adam Powell38639b12011-06-17 16:02:32 -070099 final Resources res = context.getResources();
100 mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
101 res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
Adam Powell8028dd32010-07-15 10:16:33 -0700102
Adam Powell4afd62b2011-02-18 15:02:18 -0800103 mAnchorView = anchorView;
Adam Powell696cba52011-03-29 10:38:16 -0700104
Alan Viverette3d0f21d2014-07-10 16:15:01 -0700105 // Present the menu using our context, not the menu builder's context.
106 menu.addMenuPresenter(this, context);
Adam Powell42675342010-07-09 18:02:59 -0700107 }
108
Adam Powellf0ad6e62011-01-10 17:14:06 -0800109 public void setAnchorView(View anchor) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800110 mAnchorView = anchor;
Adam Powellf0ad6e62011-01-10 17:14:06 -0800111 }
112
Adam Powell91511032011-07-13 10:24:06 -0700113 public void setForceShowIcon(boolean forceShow) {
114 mForceShowIcon = forceShow;
115 }
116
Adam Powell54c94de2013-09-26 15:36:34 -0700117 public void setGravity(int gravity) {
118 mDropDownGravity = gravity;
119 }
120
Adam Powell42675342010-07-09 18:02:59 -0700121 public void show() {
Adam Powell5e3f2842011-01-07 17:16:56 -0800122 if (!tryShow()) {
123 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
124 }
125 }
126
Alan Viveretteca6a36112013-08-16 14:41:06 -0700127 public ListPopupWindow getPopup() {
128 return mPopup;
129 }
130
Adam Powell5e3f2842011-01-07 17:16:56 -0800131 public boolean tryShow() {
Alan Viverette29632522014-10-15 17:19:30 -0700132 mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
Adam Powell8515ee82010-11-30 14:09:55 -0800133 mPopup.setOnDismissListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700134 mPopup.setOnItemClickListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700135 mPopup.setAdapter(mAdapter);
Adam Powell42675342010-07-09 18:02:59 -0700136 mPopup.setModal(true);
137
Adam Powell4afd62b2011-02-18 15:02:18 -0800138 View anchor = mAnchorView;
Adam Powell04587962010-11-11 22:15:07 -0800139 if (anchor != null) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800140 final boolean addGlobalListener = mTreeObserver == null;
141 mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
142 if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
143 anchor.addOnAttachStateChangeListener(this);
Adam Powell04587962010-11-11 22:15:07 -0800144 mPopup.setAnchorView(anchor);
Adam Powell54c94de2013-09-26 15:36:34 -0700145 mPopup.setDropDownGravity(mDropDownGravity);
Adam Powell4be0d522010-08-03 17:53:14 -0700146 } else {
Adam Powell5e3f2842011-01-07 17:16:56 -0800147 return false;
Adam Powell8028dd32010-07-15 10:16:33 -0700148 }
Adam Powell42675342010-07-09 18:02:59 -0700149
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700150 if (!mHasContentWidth) {
151 mContentWidth = measureContentWidth();
152 mHasContentWidth = true;
153 }
154
155 mPopup.setContentWidth(mContentWidth);
Adam Powellaa0b92c2010-12-13 22:38:53 -0800156 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Adam Powell42675342010-07-09 18:02:59 -0700157 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700158 mPopup.getListView().setOnKeyListener(this);
Adam Powell5e3f2842011-01-07 17:16:56 -0800159 return true;
Adam Powell42675342010-07-09 18:02:59 -0700160 }
161
162 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700163 if (isShowing()) {
164 mPopup.dismiss();
165 }
Adam Powell8515ee82010-11-30 14:09:55 -0800166 }
167
168 public void onDismiss() {
169 mPopup = null;
Adam Powell42b91bb2011-06-21 18:32:26 -0700170 mMenu.close();
Adam Powell4afd62b2011-02-18 15:02:18 -0800171 if (mTreeObserver != null) {
172 if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
Adam Powellca51e872011-02-14 19:54:29 -0800173 mTreeObserver.removeGlobalOnLayoutListener(this);
Adam Powell4afd62b2011-02-18 15:02:18 -0800174 mTreeObserver = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800175 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800176 mAnchorView.removeOnAttachStateChangeListener(this);
Adam Powell42675342010-07-09 18:02:59 -0700177 }
178
Adam Powell8028dd32010-07-15 10:16:33 -0700179 public boolean isShowing() {
180 return mPopup != null && mPopup.isShowing();
181 }
182
Adam Powell696cba52011-03-29 10:38:16 -0700183 @Override
Adam Powell42675342010-07-09 18:02:59 -0700184 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell696cba52011-03-29 10:38:16 -0700185 MenuAdapter adapter = mAdapter;
186 adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
Adam Powell42675342010-07-09 18:02:59 -0700187 }
188
Adam Powell8028dd32010-07-15 10:16:33 -0700189 public boolean onKey(View v, int keyCode, KeyEvent event) {
190 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
191 dismiss();
192 return true;
193 }
194 return false;
195 }
196
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700197 private int measureContentWidth() {
Adam Powell42675342010-07-09 18:02:59 -0700198 // Menus don't tend to be long, so this is more sane than it looks.
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700199 int maxWidth = 0;
Adam Powell42675342010-07-09 18:02:59 -0700200 View itemView = null;
Adam Powell50f784c2010-12-19 16:12:19 -0800201 int itemType = 0;
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700202
203 final ListAdapter adapter = mAdapter;
204 final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
205 final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Adam Powell42675342010-07-09 18:02:59 -0700206 final int count = adapter.getCount();
207 for (int i = 0; i < count; i++) {
Adam Powell50f784c2010-12-19 16:12:19 -0800208 final int positionType = adapter.getItemViewType(i);
209 if (positionType != itemType) {
210 itemType = positionType;
211 itemView = null;
212 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700213
Adam Powell91511032011-07-13 10:24:06 -0700214 if (mMeasureParent == null) {
215 mMeasureParent = new FrameLayout(mContext);
216 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700217
Adam Powell91511032011-07-13 10:24:06 -0700218 itemView = adapter.getView(i, itemView, mMeasureParent);
Adam Powell42675342010-07-09 18:02:59 -0700219 itemView.measure(widthMeasureSpec, heightMeasureSpec);
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700220
221 final int itemWidth = itemView.getMeasuredWidth();
222 if (itemWidth >= mPopupMaxWidth) {
223 return mPopupMaxWidth;
224 } else if (itemWidth > maxWidth) {
225 maxWidth = itemWidth;
226 }
Adam Powell42675342010-07-09 18:02:59 -0700227 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700228
229 return maxWidth;
Adam Powell42675342010-07-09 18:02:59 -0700230 }
Adam Powell04587962010-11-11 22:15:07 -0800231
232 @Override
233 public void onGlobalLayout() {
Adam Powell4afd62b2011-02-18 15:02:18 -0800234 if (isShowing()) {
235 final View anchor = mAnchorView;
Adam Powellca51e872011-02-14 19:54:29 -0800236 if (anchor == null || !anchor.isShown()) {
Adam Powell04587962010-11-11 22:15:07 -0800237 dismiss();
Adam Powellca51e872011-02-14 19:54:29 -0800238 } else if (isShowing()) {
Adam Powellaa0b92c2010-12-13 22:38:53 -0800239 // Recompute window size and position
240 mPopup.show();
Adam Powell04587962010-11-11 22:15:07 -0800241 }
242 }
243 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800244
245 @Override
246 public void onViewAttachedToWindow(View v) {
247 }
248
249 @Override
250 public void onViewDetachedFromWindow(View v) {
251 if (mTreeObserver != null) {
252 if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver();
253 mTreeObserver.removeGlobalOnLayoutListener(this);
254 }
255 v.removeOnAttachStateChangeListener(this);
256 }
Adam Powell696cba52011-03-29 10:38:16 -0700257
258 @Override
259 public void initForMenu(Context context, MenuBuilder menu) {
260 // Don't need to do anything; we added as a presenter in the constructor.
261 }
262
263 @Override
264 public MenuView getMenuView(ViewGroup root) {
265 throw new UnsupportedOperationException("MenuPopupHelpers manage their own views");
266 }
267
268 @Override
269 public void updateMenuView(boolean cleared) {
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700270 mHasContentWidth = false;
271
272 if (mAdapter != null) {
273 mAdapter.notifyDataSetChanged();
274 }
Adam Powell696cba52011-03-29 10:38:16 -0700275 }
276
277 @Override
278 public void setCallback(Callback cb) {
279 mPresenterCallback = cb;
280 }
281
282 @Override
283 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
284 if (subMenu.hasVisibleItems()) {
Alan Viverette560f1702014-05-05 14:40:07 -0700285 MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView);
Adam Powell696cba52011-03-29 10:38:16 -0700286 subPopup.setCallback(mPresenterCallback);
Adam Powell91511032011-07-13 10:24:06 -0700287
288 boolean preserveIconSpacing = false;
289 final int count = subMenu.size();
290 for (int i = 0; i < count; i++) {
291 MenuItem childItem = subMenu.getItem(i);
292 if (childItem.isVisible() && childItem.getIcon() != null) {
293 preserveIconSpacing = true;
294 break;
295 }
296 }
297 subPopup.setForceShowIcon(preserveIconSpacing);
298
Adam Powell696cba52011-03-29 10:38:16 -0700299 if (subPopup.tryShow()) {
300 if (mPresenterCallback != null) {
301 mPresenterCallback.onOpenSubMenu(subMenu);
302 }
303 return true;
304 }
305 }
306 return false;
307 }
308
309 @Override
310 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
311 // Only care about the (sub)menu we're presenting.
312 if (menu != mMenu) return;
313
314 dismiss();
315 if (mPresenterCallback != null) {
316 mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
317 }
318 }
319
320 @Override
321 public boolean flagActionItems() {
322 return false;
323 }
324
Adam Powell8d02dea2011-05-31 21:35:13 -0700325 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
326 return false;
327 }
328
329 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
330 return false;
331 }
332
Adam Powell275702c2011-09-23 17:34:04 -0700333 @Override
334 public int getId() {
335 return 0;
336 }
337
338 @Override
339 public Parcelable onSaveInstanceState() {
340 return null;
341 }
342
343 @Override
344 public void onRestoreInstanceState(Parcelable state) {
345 }
346
Adam Powell696cba52011-03-29 10:38:16 -0700347 private class MenuAdapter extends BaseAdapter {
348 private MenuBuilder mAdapterMenu;
Adam Powell275702c2011-09-23 17:34:04 -0700349 private int mExpandedIndex = -1;
Adam Powell696cba52011-03-29 10:38:16 -0700350
351 public MenuAdapter(MenuBuilder menu) {
352 mAdapterMenu = menu;
Adam Powell275702c2011-09-23 17:34:04 -0700353 findExpandedIndex();
Adam Powell696cba52011-03-29 10:38:16 -0700354 }
355
356 public int getCount() {
357 ArrayList<MenuItemImpl> items = mOverflowOnly ?
358 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700359 if (mExpandedIndex < 0) {
360 return items.size();
361 }
362 return items.size() - 1;
Adam Powell696cba52011-03-29 10:38:16 -0700363 }
364
365 public MenuItemImpl getItem(int position) {
366 ArrayList<MenuItemImpl> items = mOverflowOnly ?
367 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700368 if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
369 position++;
370 }
Adam Powell696cba52011-03-29 10:38:16 -0700371 return items.get(position);
372 }
373
374 public long getItemId(int position) {
375 // Since a menu item's ID is optional, we'll use the position as an
376 // ID for the item in the AdapterView
377 return position;
378 }
379
380 public View getView(int position, View convertView, ViewGroup parent) {
381 if (convertView == null) {
382 convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
383 }
384
385 MenuView.ItemView itemView = (MenuView.ItemView) convertView;
Adam Powell91511032011-07-13 10:24:06 -0700386 if (mForceShowIcon) {
387 ((ListMenuItemView) convertView).setForceShowIcon(true);
388 }
Adam Powell696cba52011-03-29 10:38:16 -0700389 itemView.initialize(getItem(position), 0);
390 return convertView;
391 }
Adam Powell275702c2011-09-23 17:34:04 -0700392
393 void findExpandedIndex() {
394 final MenuItemImpl expandedItem = mMenu.getExpandedItem();
395 if (expandedItem != null) {
396 final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
397 final int count = items.size();
398 for (int i = 0; i < count; i++) {
399 final MenuItemImpl item = items.get(i);
400 if (item == expandedItem) {
401 mExpandedIndex = i;
402 return;
403 }
404 }
405 }
406 mExpandedIndex = -1;
407 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700408
Adam Powell275702c2011-09-23 17:34:04 -0700409 @Override
Adam Powell76889f32012-05-08 22:22:52 -0700410 public void notifyDataSetChanged() {
411 findExpandedIndex();
412 super.notifyDataSetChanged();
Adam Powell275702c2011-09-23 17:34:04 -0700413 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700414 }
Adam Powell42675342010-07-09 18:02:59 -0700415}