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 | |
| 19 | import com.android.internal.view.menu.MenuBuilder.MenuAdapter; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.util.DisplayMetrics; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 23 | import android.view.KeyEvent; |
| 24 | import android.view.MenuItem; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.View.MeasureSpec; |
| 27 | import android.widget.AdapterView; |
| 28 | import android.widget.ListPopupWindow; |
| 29 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 30 | import java.lang.ref.WeakReference; |
| 31 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 32 | /** |
| 33 | * @hide |
| 34 | */ |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 35 | public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 36 | private static final String TAG = "MenuPopupHelper"; |
| 37 | |
| 38 | private Context mContext; |
| 39 | private ListPopupWindow mPopup; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 40 | private MenuBuilder mMenu; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 41 | private int mPopupMaxWidth; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 42 | private WeakReference<View> mAnchorView; |
| 43 | private boolean mOverflowOnly; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 44 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 45 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
| 46 | this(context, menu, null, false); |
| 47 | } |
| 48 | |
| 49 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
| 50 | this(context, menu, anchorView, false); |
| 51 | } |
| 52 | |
| 53 | public MenuPopupHelper(Context context, MenuBuilder menu, |
| 54 | View anchorView, boolean overflowOnly) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 55 | mContext = context; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 56 | mMenu = menu; |
| 57 | mOverflowOnly = overflowOnly; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 58 | |
| 59 | final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 60 | mPopupMaxWidth = metrics.widthPixels / 2; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 61 | |
| 62 | if (anchorView != null) { |
| 63 | mAnchorView = new WeakReference<View>(anchorView); |
| 64 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public void show() { |
| 68 | // TODO Use a style from the theme here |
| 69 | mPopup = new ListPopupWindow(mContext, null, 0, |
| 70 | com.android.internal.R.style.Widget_Spinner); |
| 71 | mPopup.setOnItemClickListener(this); |
| 72 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 73 | final MenuAdapter adapter = mOverflowOnly ? |
| 74 | mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) : |
| 75 | mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 76 | mPopup.setAdapter(adapter); |
| 77 | mPopup.setModal(true); |
| 78 | |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 79 | if (mAnchorView != null) { |
| 80 | mPopup.setAnchorView(mAnchorView.get()); |
| 81 | } else if (mMenu instanceof SubMenuBuilder) { |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 82 | SubMenuBuilder subMenu = (SubMenuBuilder) mMenu; |
| 83 | final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem(); |
| 84 | mPopup.setAnchorView(itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null)); |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 85 | } else { |
| 86 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 87 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 88 | |
| 89 | mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth)); |
| 90 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 91 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame^] | 95 | if (isShowing()) { |
| 96 | mPopup.dismiss(); |
| 97 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 98 | mPopup = null; |
| 99 | } |
| 100 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 101 | public boolean isShowing() { |
| 102 | return mPopup != null && mPopup.isShowing(); |
| 103 | } |
| 104 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 105 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 106 | MenuItem item = null; |
| 107 | if (mOverflowOnly) { |
| 108 | item = mMenu.getOverflowItem(position); |
| 109 | } else { |
| 110 | item = mMenu.getItem(position); |
| 111 | } |
| 112 | mMenu.performItemAction(item, 0); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 113 | mPopup.dismiss(); |
| 114 | } |
| 115 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 116 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 117 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 118 | dismiss(); |
| 119 | return true; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 124 | private int measureContentWidth(MenuAdapter adapter) { |
| 125 | // Menus don't tend to be long, so this is more sane than it looks. |
| 126 | int width = 0; |
| 127 | View itemView = null; |
| 128 | final int widthMeasureSpec = |
| 129 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 130 | final int heightMeasureSpec = |
| 131 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 132 | final int count = adapter.getCount(); |
| 133 | for (int i = 0; i < count; i++) { |
| 134 | itemView = adapter.getView(i, itemView, null); |
| 135 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
| 136 | width = Math.max(width, itemView.getMeasuredWidth()); |
| 137 | } |
| 138 | return width; |
| 139 | } |
| 140 | } |