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