MenuPopupHelper.java revision 8028dd32a4a04154050220dd0693583d5b750330
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 (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 } 86 87 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth)); 88 mPopup.show(); 89 mPopup.getListView().setOnKeyListener(this); 90 } 91 92 public void dismiss() { 93 mPopup.dismiss(); 94 mPopup = null; 95 } 96 97 public boolean isShowing() { 98 return mPopup != null && mPopup.isShowing(); 99 } 100 101 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 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); 109 mPopup.dismiss(); 110 } 111 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 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} 137