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