blob: dbb78c28675b66adc1ca3cc56e7b0c2672b3a0e5 [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 Powell8028dd32010-07-15 10:16:33 -070022import android.view.KeyEvent;
Adam Powell696cba52011-03-29 10:38:16 -070023import android.view.LayoutInflater;
Adam Powell91511032011-07-13 10:24:06 -070024import android.view.MenuItem;
Alan Viverette80e72702013-07-29 11:12:59 -070025import android.view.MotionEvent;
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;
56
Adam Powell4afd62b2011-02-18 15:02:18 -080057 private View mAnchorView;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070058 private ListPopupWindow mPopup;
Adam Powell04587962010-11-11 22:15:07 -080059 private ViewTreeObserver mTreeObserver;
Adam Powell696cba52011-03-29 10:38:16 -070060 private Callback mPresenterCallback;
Adam Powelle2b03a62011-01-25 14:39:02 -080061
Adam Powell91511032011-07-13 10:24:06 -070062 boolean mForceShowIcon;
63
64 private ViewGroup mMeasureParent;
65
Alan Viverette0bce6ab2013-06-26 17:46:16 -070066 /** 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 Powell8028dd32010-07-15 10:16:33 -070072 public MenuPopupHelper(Context context, MenuBuilder menu) {
73 this(context, menu, null, false);
74 }
75
76 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
77 this(context, menu, anchorView, false);
78 }
79
80 public MenuPopupHelper(Context context, MenuBuilder menu,
81 View anchorView, boolean overflowOnly) {
Adam Powell42675342010-07-09 18:02:59 -070082 mContext = context;
Adam Powell696cba52011-03-29 10:38:16 -070083 mInflater = LayoutInflater.from(context);
Adam Powell8028dd32010-07-15 10:16:33 -070084 mMenu = menu;
Alan Viverette0bce6ab2013-06-26 17:46:16 -070085 mAdapter = new MenuAdapter(mMenu);
Adam Powell8028dd32010-07-15 10:16:33 -070086 mOverflowOnly = overflowOnly;
Adam Powell42675342010-07-09 18:02:59 -070087
Adam Powell38639b12011-06-17 16:02:32 -070088 final Resources res = context.getResources();
89 mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
90 res.getDimensionPixelSize(com.android.internal.R.dimen.config_prefDialogWidth));
Adam Powell8028dd32010-07-15 10:16:33 -070091
Adam Powell4afd62b2011-02-18 15:02:18 -080092 mAnchorView = anchorView;
Adam Powell696cba52011-03-29 10:38:16 -070093
94 menu.addMenuPresenter(this);
Adam Powell42675342010-07-09 18:02:59 -070095 }
96
Adam Powellf0ad6e62011-01-10 17:14:06 -080097 public void setAnchorView(View anchor) {
Adam Powell4afd62b2011-02-18 15:02:18 -080098 mAnchorView = anchor;
Adam Powellf0ad6e62011-01-10 17:14:06 -080099 }
100
Adam Powell91511032011-07-13 10:24:06 -0700101 public void setForceShowIcon(boolean forceShow) {
102 mForceShowIcon = forceShow;
103 }
104
Adam Powell42675342010-07-09 18:02:59 -0700105 public void show() {
Adam Powell5e3f2842011-01-07 17:16:56 -0800106 if (!tryShow()) {
107 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
108 }
109 }
110
Alan Viveretteca6a36112013-08-16 14:41:06 -0700111 public ListPopupWindow getPopup() {
112 return mPopup;
113 }
114
Adam Powell5e3f2842011-01-07 17:16:56 -0800115 public boolean tryShow() {
Adam Powell0b2d3062010-09-14 16:15:02 -0700116 mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
Adam Powell8515ee82010-11-30 14:09:55 -0800117 mPopup.setOnDismissListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700118 mPopup.setOnItemClickListener(this);
Adam Powell696cba52011-03-29 10:38:16 -0700119 mPopup.setAdapter(mAdapter);
Adam Powell42675342010-07-09 18:02:59 -0700120 mPopup.setModal(true);
121
Adam Powell4afd62b2011-02-18 15:02:18 -0800122 View anchor = mAnchorView;
Adam Powell04587962010-11-11 22:15:07 -0800123 if (anchor != null) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800124 final boolean addGlobalListener = mTreeObserver == null;
125 mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
126 if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
127 anchor.addOnAttachStateChangeListener(this);
Adam Powell04587962010-11-11 22:15:07 -0800128 mPopup.setAnchorView(anchor);
Adam Powell4be0d522010-08-03 17:53:14 -0700129 } else {
Adam Powell5e3f2842011-01-07 17:16:56 -0800130 return false;
Adam Powell8028dd32010-07-15 10:16:33 -0700131 }
Adam Powell42675342010-07-09 18:02:59 -0700132
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700133 if (!mHasContentWidth) {
134 mContentWidth = measureContentWidth();
135 mHasContentWidth = true;
136 }
137
138 mPopup.setContentWidth(mContentWidth);
Adam Powellaa0b92c2010-12-13 22:38:53 -0800139 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Adam Powell42675342010-07-09 18:02:59 -0700140 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700141 mPopup.getListView().setOnKeyListener(this);
Adam Powell5e3f2842011-01-07 17:16:56 -0800142 return true;
Adam Powell42675342010-07-09 18:02:59 -0700143 }
144
145 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700146 if (isShowing()) {
147 mPopup.dismiss();
148 }
Adam Powell8515ee82010-11-30 14:09:55 -0800149 }
150
151 public void onDismiss() {
152 mPopup = null;
Adam Powell42b91bb2011-06-21 18:32:26 -0700153 mMenu.close();
Adam Powell4afd62b2011-02-18 15:02:18 -0800154 if (mTreeObserver != null) {
155 if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
Adam Powellca51e872011-02-14 19:54:29 -0800156 mTreeObserver.removeGlobalOnLayoutListener(this);
Adam Powell4afd62b2011-02-18 15:02:18 -0800157 mTreeObserver = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800158 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800159 mAnchorView.removeOnAttachStateChangeListener(this);
Adam Powell42675342010-07-09 18:02:59 -0700160 }
161
Adam Powell8028dd32010-07-15 10:16:33 -0700162 public boolean isShowing() {
163 return mPopup != null && mPopup.isShowing();
164 }
165
Adam Powell696cba52011-03-29 10:38:16 -0700166 @Override
Adam Powell42675342010-07-09 18:02:59 -0700167 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell696cba52011-03-29 10:38:16 -0700168 MenuAdapter adapter = mAdapter;
169 adapter.mAdapterMenu.performItemAction(adapter.getItem(position), 0);
Adam Powell42675342010-07-09 18:02:59 -0700170 }
171
Adam Powell8028dd32010-07-15 10:16:33 -0700172 public boolean onKey(View v, int keyCode, KeyEvent event) {
173 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
174 dismiss();
175 return true;
176 }
177 return false;
178 }
179
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700180 private int measureContentWidth() {
Adam Powell42675342010-07-09 18:02:59 -0700181 // Menus don't tend to be long, so this is more sane than it looks.
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700182 int maxWidth = 0;
Adam Powell42675342010-07-09 18:02:59 -0700183 View itemView = null;
Adam Powell50f784c2010-12-19 16:12:19 -0800184 int itemType = 0;
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700185
186 final ListAdapter adapter = mAdapter;
187 final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
188 final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Adam Powell42675342010-07-09 18:02:59 -0700189 final int count = adapter.getCount();
190 for (int i = 0; i < count; i++) {
Adam Powell50f784c2010-12-19 16:12:19 -0800191 final int positionType = adapter.getItemViewType(i);
192 if (positionType != itemType) {
193 itemType = positionType;
194 itemView = null;
195 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700196
Adam Powell91511032011-07-13 10:24:06 -0700197 if (mMeasureParent == null) {
198 mMeasureParent = new FrameLayout(mContext);
199 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700200
Adam Powell91511032011-07-13 10:24:06 -0700201 itemView = adapter.getView(i, itemView, mMeasureParent);
Adam Powell42675342010-07-09 18:02:59 -0700202 itemView.measure(widthMeasureSpec, heightMeasureSpec);
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700203
204 final int itemWidth = itemView.getMeasuredWidth();
205 if (itemWidth >= mPopupMaxWidth) {
206 return mPopupMaxWidth;
207 } else if (itemWidth > maxWidth) {
208 maxWidth = itemWidth;
209 }
Adam Powell42675342010-07-09 18:02:59 -0700210 }
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700211
212 return maxWidth;
Adam Powell42675342010-07-09 18:02:59 -0700213 }
Adam Powell04587962010-11-11 22:15:07 -0800214
215 @Override
216 public void onGlobalLayout() {
Adam Powell4afd62b2011-02-18 15:02:18 -0800217 if (isShowing()) {
218 final View anchor = mAnchorView;
Adam Powellca51e872011-02-14 19:54:29 -0800219 if (anchor == null || !anchor.isShown()) {
Adam Powell04587962010-11-11 22:15:07 -0800220 dismiss();
Adam Powellca51e872011-02-14 19:54:29 -0800221 } else if (isShowing()) {
Adam Powellaa0b92c2010-12-13 22:38:53 -0800222 // Recompute window size and position
223 mPopup.show();
Adam Powell04587962010-11-11 22:15:07 -0800224 }
225 }
226 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800227
228 @Override
229 public void onViewAttachedToWindow(View v) {
230 }
231
232 @Override
233 public void onViewDetachedFromWindow(View v) {
234 if (mTreeObserver != null) {
235 if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver();
236 mTreeObserver.removeGlobalOnLayoutListener(this);
237 }
238 v.removeOnAttachStateChangeListener(this);
239 }
Adam Powell696cba52011-03-29 10:38:16 -0700240
241 @Override
242 public void initForMenu(Context context, MenuBuilder menu) {
243 // Don't need to do anything; we added as a presenter in the constructor.
244 }
245
246 @Override
247 public MenuView getMenuView(ViewGroup root) {
248 throw new UnsupportedOperationException("MenuPopupHelpers manage their own views");
249 }
250
251 @Override
252 public void updateMenuView(boolean cleared) {
Alan Viverette0bce6ab2013-06-26 17:46:16 -0700253 mHasContentWidth = false;
254
255 if (mAdapter != null) {
256 mAdapter.notifyDataSetChanged();
257 }
Adam Powell696cba52011-03-29 10:38:16 -0700258 }
259
260 @Override
261 public void setCallback(Callback cb) {
262 mPresenterCallback = cb;
263 }
264
265 @Override
266 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
267 if (subMenu.hasVisibleItems()) {
268 MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView, false);
269 subPopup.setCallback(mPresenterCallback);
Adam Powell91511032011-07-13 10:24:06 -0700270
271 boolean preserveIconSpacing = false;
272 final int count = subMenu.size();
273 for (int i = 0; i < count; i++) {
274 MenuItem childItem = subMenu.getItem(i);
275 if (childItem.isVisible() && childItem.getIcon() != null) {
276 preserveIconSpacing = true;
277 break;
278 }
279 }
280 subPopup.setForceShowIcon(preserveIconSpacing);
281
Adam Powell696cba52011-03-29 10:38:16 -0700282 if (subPopup.tryShow()) {
283 if (mPresenterCallback != null) {
284 mPresenterCallback.onOpenSubMenu(subMenu);
285 }
286 return true;
287 }
288 }
289 return false;
290 }
291
292 @Override
293 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
294 // Only care about the (sub)menu we're presenting.
295 if (menu != mMenu) return;
296
297 dismiss();
298 if (mPresenterCallback != null) {
299 mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
300 }
301 }
302
303 @Override
304 public boolean flagActionItems() {
305 return false;
306 }
307
Adam Powell8d02dea2011-05-31 21:35:13 -0700308 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
309 return false;
310 }
311
312 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
313 return false;
314 }
315
Adam Powell275702c2011-09-23 17:34:04 -0700316 @Override
317 public int getId() {
318 return 0;
319 }
320
321 @Override
322 public Parcelable onSaveInstanceState() {
323 return null;
324 }
325
326 @Override
327 public void onRestoreInstanceState(Parcelable state) {
328 }
329
Adam Powell696cba52011-03-29 10:38:16 -0700330 private class MenuAdapter extends BaseAdapter {
331 private MenuBuilder mAdapterMenu;
Adam Powell275702c2011-09-23 17:34:04 -0700332 private int mExpandedIndex = -1;
Adam Powell696cba52011-03-29 10:38:16 -0700333
334 public MenuAdapter(MenuBuilder menu) {
335 mAdapterMenu = menu;
Adam Powell275702c2011-09-23 17:34:04 -0700336 findExpandedIndex();
Adam Powell696cba52011-03-29 10:38:16 -0700337 }
338
339 public int getCount() {
340 ArrayList<MenuItemImpl> items = mOverflowOnly ?
341 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700342 if (mExpandedIndex < 0) {
343 return items.size();
344 }
345 return items.size() - 1;
Adam Powell696cba52011-03-29 10:38:16 -0700346 }
347
348 public MenuItemImpl getItem(int position) {
349 ArrayList<MenuItemImpl> items = mOverflowOnly ?
350 mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
Adam Powell275702c2011-09-23 17:34:04 -0700351 if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
352 position++;
353 }
Adam Powell696cba52011-03-29 10:38:16 -0700354 return items.get(position);
355 }
356
357 public long getItemId(int position) {
358 // Since a menu item's ID is optional, we'll use the position as an
359 // ID for the item in the AdapterView
360 return position;
361 }
362
363 public View getView(int position, View convertView, ViewGroup parent) {
364 if (convertView == null) {
365 convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
366 }
367
368 MenuView.ItemView itemView = (MenuView.ItemView) convertView;
Adam Powell91511032011-07-13 10:24:06 -0700369 if (mForceShowIcon) {
370 ((ListMenuItemView) convertView).setForceShowIcon(true);
371 }
Adam Powell696cba52011-03-29 10:38:16 -0700372 itemView.initialize(getItem(position), 0);
373 return convertView;
374 }
Adam Powell275702c2011-09-23 17:34:04 -0700375
376 void findExpandedIndex() {
377 final MenuItemImpl expandedItem = mMenu.getExpandedItem();
378 if (expandedItem != null) {
379 final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
380 final int count = items.size();
381 for (int i = 0; i < count; i++) {
382 final MenuItemImpl item = items.get(i);
383 if (item == expandedItem) {
384 mExpandedIndex = i;
385 return;
386 }
387 }
388 }
389 mExpandedIndex = -1;
390 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700391
Adam Powell275702c2011-09-23 17:34:04 -0700392 @Override
Adam Powell76889f32012-05-08 22:22:52 -0700393 public void notifyDataSetChanged() {
394 findExpandedIndex();
395 super.notifyDataSetChanged();
Adam Powell275702c2011-09-23 17:34:04 -0700396 }
Adam Powell11ed1d62011-07-11 21:19:59 -0700397 }
Adam Powell42675342010-07-09 18:02:59 -0700398}