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; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 27 | import android.view.ViewTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 28 | import android.widget.AdapterView; |
| 29 | import android.widget.ListPopupWindow; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 30 | import android.widget.PopupWindow; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 31 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 32 | import java.lang.ref.WeakReference; |
| 33 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 34 | /** |
| 35 | * @hide |
| 36 | */ |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 37 | public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener, |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 38 | ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 39 | private static final String TAG = "MenuPopupHelper"; |
| 40 | |
| 41 | private Context mContext; |
| 42 | private ListPopupWindow mPopup; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 43 | private MenuBuilder mMenu; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 44 | private int mPopupMaxWidth; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 45 | private WeakReference<View> mAnchorView; |
| 46 | private boolean mOverflowOnly; |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 47 | private ViewTreeObserver mTreeObserver; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 48 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 49 | public MenuPopupHelper(Context context, MenuBuilder menu) { |
| 50 | this(context, menu, null, false); |
| 51 | } |
| 52 | |
| 53 | public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) { |
| 54 | this(context, menu, anchorView, false); |
| 55 | } |
| 56 | |
| 57 | public MenuPopupHelper(Context context, MenuBuilder menu, |
| 58 | View anchorView, boolean overflowOnly) { |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 59 | mContext = context; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 60 | mMenu = menu; |
| 61 | mOverflowOnly = overflowOnly; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 62 | |
| 63 | final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 64 | mPopupMaxWidth = metrics.widthPixels / 2; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 65 | |
| 66 | if (anchorView != null) { |
| 67 | mAnchorView = new WeakReference<View>(anchorView); |
| 68 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame^] | 71 | public void setAnchorView(View anchor) { |
| 72 | mAnchorView = new WeakReference<View>(anchor); |
| 73 | } |
| 74 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 75 | public void show() { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 76 | if (!tryShow()) { |
| 77 | throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public boolean tryShow() { |
Adam Powell | 0b2d306 | 2010-09-14 16:15:02 -0700 | [diff] [blame] | 82 | mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 83 | mPopup.setOnItemClickListener(this); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 84 | mPopup.setOnDismissListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 85 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 86 | final MenuAdapter adapter = mOverflowOnly ? |
| 87 | mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) : |
| 88 | mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 89 | mPopup.setAdapter(adapter); |
| 90 | mPopup.setModal(true); |
| 91 | |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 92 | View anchor = mAnchorView != null ? mAnchorView.get() : null; |
| 93 | if (anchor == null && mMenu instanceof SubMenuBuilder) { |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 94 | SubMenuBuilder subMenu = (SubMenuBuilder) mMenu; |
| 95 | final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem(); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 96 | anchor = itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null); |
| 97 | mAnchorView = new WeakReference<View>(anchor); |
| 98 | } |
| 99 | |
| 100 | if (anchor != null) { |
| 101 | mTreeObserver = anchor.getViewTreeObserver(); |
| 102 | mTreeObserver.addOnGlobalLayoutListener(this); |
| 103 | mPopup.setAnchorView(anchor); |
Adam Powell | 4be0d52 | 2010-08-03 17:53:14 -0700 | [diff] [blame] | 104 | } else { |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 105 | return false; |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 106 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 107 | |
| 108 | mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth)); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 109 | mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 110 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 111 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 5e3f284 | 2011-01-07 17:16:56 -0800 | [diff] [blame] | 112 | return true; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public void dismiss() { |
Adam Powell | 3d3da27 | 2010-08-11 18:06:17 -0700 | [diff] [blame] | 116 | if (isShowing()) { |
| 117 | mPopup.dismiss(); |
| 118 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | public void onDismiss() { |
| 122 | mPopup = null; |
Adam Powell | ed8b403 | 2010-11-16 10:22:35 -0800 | [diff] [blame] | 123 | if (mTreeObserver != null) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 124 | mTreeObserver.removeGlobalOnLayoutListener(MenuPopupHelper.this); |
Adam Powell | ed8b403 | 2010-11-16 10:22:35 -0800 | [diff] [blame] | 125 | mTreeObserver = null; |
| 126 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 129 | public boolean isShowing() { |
| 130 | return mPopup != null && mPopup.isShowing(); |
| 131 | } |
| 132 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 133 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 134 | MenuItem item = null; |
| 135 | if (mOverflowOnly) { |
| 136 | item = mMenu.getOverflowItem(position); |
| 137 | } else { |
Adam Powell | 1700ae0 | 2010-12-19 17:11:07 -0800 | [diff] [blame] | 138 | item = mMenu.getVisibleItems().get(position); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 139 | } |
| 140 | mMenu.performItemAction(item, 0); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 141 | dismiss(); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 144 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 145 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 146 | dismiss(); |
| 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 152 | private int measureContentWidth(MenuAdapter adapter) { |
| 153 | // Menus don't tend to be long, so this is more sane than it looks. |
| 154 | int width = 0; |
| 155 | View itemView = null; |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 156 | int itemType = 0; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 157 | final int widthMeasureSpec = |
| 158 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 159 | final int heightMeasureSpec = |
| 160 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 161 | final int count = adapter.getCount(); |
| 162 | for (int i = 0; i < count; i++) { |
Adam Powell | 50f784c | 2010-12-19 16:12:19 -0800 | [diff] [blame] | 163 | final int positionType = adapter.getItemViewType(i); |
| 164 | if (positionType != itemType) { |
| 165 | itemType = positionType; |
| 166 | itemView = null; |
| 167 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 168 | itemView = adapter.getView(i, itemView, null); |
| 169 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
| 170 | width = Math.max(width, itemView.getMeasuredWidth()); |
| 171 | } |
| 172 | return width; |
| 173 | } |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public void onGlobalLayout() { |
| 177 | if (!isShowing()) { |
| 178 | mTreeObserver.removeGlobalOnLayoutListener(this); |
| 179 | mTreeObserver = null; |
| 180 | } else { |
| 181 | final View anchor = mAnchorView != null ? mAnchorView.get() : null; |
| 182 | if (anchor != null && !anchor.isShown()) { |
| 183 | dismiss(); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 184 | } else { |
| 185 | // Recompute window size and position |
| 186 | mPopup.show(); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 190 | } |