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 | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame^] | 79 | if (mMenu instanceof SubMenuBuilder) { |
| 80 | SubMenuBuilder subMenu = (SubMenuBuilder) mMenu; |
| 81 | final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem(); |
| 82 | mPopup.setAnchorView(itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null)); |
| 83 | } else if (mAnchorView != null) { |
| 84 | mPopup.setAnchorView(mAnchorView.get()); |
| 85 | } |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 86 | |
| 87 | mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth)); |
| 88 | mPopup.show(); |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame^] | 89 | mPopup.getListView().setOnKeyListener(this); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public void dismiss() { |
| 93 | mPopup.dismiss(); |
| 94 | mPopup = null; |
| 95 | } |
| 96 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame^] | 97 | public boolean isShowing() { |
| 98 | return mPopup != null && mPopup.isShowing(); |
| 99 | } |
| 100 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 101 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame^] | 102 | MenuItem item = null; |
| 103 | if (mOverflowOnly) { |
| 104 | item = mMenu.getOverflowItem(position); |
| 105 | } else { |
| 106 | item = mMenu.getItem(position); |
| 107 | } |
| 108 | mMenu.performItemAction(item, 0); |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 109 | mPopup.dismiss(); |
| 110 | } |
| 111 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame^] | 112 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 113 | if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) { |
| 114 | dismiss(); |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 120 | private int measureContentWidth(MenuAdapter adapter) { |
| 121 | // Menus don't tend to be long, so this is more sane than it looks. |
| 122 | int width = 0; |
| 123 | View itemView = null; |
| 124 | final int widthMeasureSpec = |
| 125 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 126 | final int heightMeasureSpec = |
| 127 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 128 | final int count = adapter.getCount(); |
| 129 | for (int i = 0; i < count; i++) { |
| 130 | itemView = adapter.getView(i, itemView, null); |
| 131 | itemView.measure(widthMeasureSpec, heightMeasureSpec); |
| 132 | width = Math.max(width, itemView.getMeasuredWidth()); |
| 133 | } |
| 134 | return width; |
| 135 | } |
| 136 | } |