blob: 2cb78a58c49bd8d24d52927dcbaaeed0e4006612 [file] [log] [blame]
Adam Powell42675342010-07-09 18:02:59 -07001/*
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;
Adam Powell8028dd32010-07-15 10:16:33 -070023import android.view.KeyEvent;
24import android.view.MenuItem;
Adam Powell42675342010-07-09 18:02:59 -070025import android.view.View;
26import android.view.View.MeasureSpec;
Adam Powell04587962010-11-11 22:15:07 -080027import android.view.ViewTreeObserver;
Adam Powell42675342010-07-09 18:02:59 -070028import android.widget.AdapterView;
29import android.widget.ListPopupWindow;
Adam Powell6c6f5752010-08-20 18:34:46 -070030import android.widget.PopupWindow;
Adam Powell42675342010-07-09 18:02:59 -070031
Adam Powell8028dd32010-07-15 10:16:33 -070032import java.lang.ref.WeakReference;
33
Adam Powell42675342010-07-09 18:02:59 -070034/**
35 * @hide
36 */
Adam Powell04587962010-11-11 22:15:07 -080037public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
Adam Powell8515ee82010-11-30 14:09:55 -080038 ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener {
Adam Powell42675342010-07-09 18:02:59 -070039 private static final String TAG = "MenuPopupHelper";
40
41 private Context mContext;
42 private ListPopupWindow mPopup;
Adam Powell8028dd32010-07-15 10:16:33 -070043 private MenuBuilder mMenu;
Adam Powell42675342010-07-09 18:02:59 -070044 private int mPopupMaxWidth;
Adam Powell8028dd32010-07-15 10:16:33 -070045 private WeakReference<View> mAnchorView;
46 private boolean mOverflowOnly;
Adam Powell04587962010-11-11 22:15:07 -080047 private ViewTreeObserver mTreeObserver;
Adam Powell42675342010-07-09 18:02:59 -070048
Adam Powell8028dd32010-07-15 10:16:33 -070049 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 Powell42675342010-07-09 18:02:59 -070059 mContext = context;
Adam Powell8028dd32010-07-15 10:16:33 -070060 mMenu = menu;
61 mOverflowOnly = overflowOnly;
Adam Powell42675342010-07-09 18:02:59 -070062
63 final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
64 mPopupMaxWidth = metrics.widthPixels / 2;
Adam Powell8028dd32010-07-15 10:16:33 -070065
66 if (anchorView != null) {
67 mAnchorView = new WeakReference<View>(anchorView);
68 }
Adam Powell42675342010-07-09 18:02:59 -070069 }
70
71 public void show() {
Adam Powell0b2d3062010-09-14 16:15:02 -070072 mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
Adam Powell42675342010-07-09 18:02:59 -070073 mPopup.setOnItemClickListener(this);
Adam Powell8515ee82010-11-30 14:09:55 -080074 mPopup.setOnDismissListener(this);
Adam Powell42675342010-07-09 18:02:59 -070075
Adam Powell8028dd32010-07-15 10:16:33 -070076 final MenuAdapter adapter = mOverflowOnly ?
77 mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) :
78 mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP);
Adam Powell42675342010-07-09 18:02:59 -070079 mPopup.setAdapter(adapter);
80 mPopup.setModal(true);
81
Adam Powell04587962010-11-11 22:15:07 -080082 View anchor = mAnchorView != null ? mAnchorView.get() : null;
83 if (anchor == null && mMenu instanceof SubMenuBuilder) {
Adam Powell8028dd32010-07-15 10:16:33 -070084 SubMenuBuilder subMenu = (SubMenuBuilder) mMenu;
85 final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem();
Adam Powell04587962010-11-11 22:15:07 -080086 anchor = itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null);
87 mAnchorView = new WeakReference<View>(anchor);
88 }
89
90 if (anchor != null) {
91 mTreeObserver = anchor.getViewTreeObserver();
92 mTreeObserver.addOnGlobalLayoutListener(this);
93 mPopup.setAnchorView(anchor);
Adam Powell4be0d522010-08-03 17:53:14 -070094 } else {
95 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
Adam Powell8028dd32010-07-15 10:16:33 -070096 }
Adam Powell42675342010-07-09 18:02:59 -070097
98 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
99 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700100 mPopup.getListView().setOnKeyListener(this);
Adam Powell42675342010-07-09 18:02:59 -0700101 }
102
103 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700104 if (isShowing()) {
105 mPopup.dismiss();
106 }
Adam Powell8515ee82010-11-30 14:09:55 -0800107 }
108
109 public void onDismiss() {
110 mPopup = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800111 if (mTreeObserver != null) {
Adam Powell8515ee82010-11-30 14:09:55 -0800112 mTreeObserver.removeGlobalOnLayoutListener(MenuPopupHelper.this);
Adam Powelled8b4032010-11-16 10:22:35 -0800113 mTreeObserver = null;
114 }
Adam Powell42675342010-07-09 18:02:59 -0700115 }
116
Adam Powell8028dd32010-07-15 10:16:33 -0700117 public boolean isShowing() {
118 return mPopup != null && mPopup.isShowing();
119 }
120
Adam Powell42675342010-07-09 18:02:59 -0700121 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell8028dd32010-07-15 10:16:33 -0700122 MenuItem item = null;
123 if (mOverflowOnly) {
124 item = mMenu.getOverflowItem(position);
125 } else {
126 item = mMenu.getItem(position);
127 }
128 mMenu.performItemAction(item, 0);
Adam Powell04587962010-11-11 22:15:07 -0800129 dismiss();
Adam Powell42675342010-07-09 18:02:59 -0700130 }
131
Adam Powell8028dd32010-07-15 10:16:33 -0700132 public boolean onKey(View v, int keyCode, KeyEvent event) {
133 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
134 dismiss();
135 return true;
136 }
137 return false;
138 }
139
Adam Powell42675342010-07-09 18:02:59 -0700140 private int measureContentWidth(MenuAdapter adapter) {
141 // Menus don't tend to be long, so this is more sane than it looks.
142 int width = 0;
143 View itemView = null;
144 final int widthMeasureSpec =
145 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
146 final int heightMeasureSpec =
147 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
148 final int count = adapter.getCount();
149 for (int i = 0; i < count; i++) {
150 itemView = adapter.getView(i, itemView, null);
151 itemView.measure(widthMeasureSpec, heightMeasureSpec);
152 width = Math.max(width, itemView.getMeasuredWidth());
153 }
154 return width;
155 }
Adam Powell04587962010-11-11 22:15:07 -0800156
157 @Override
158 public void onGlobalLayout() {
159 if (!isShowing()) {
160 mTreeObserver.removeGlobalOnLayoutListener(this);
161 mTreeObserver = null;
162 } else {
163 final View anchor = mAnchorView != null ? mAnchorView.get() : null;
164 if (anchor != null && !anchor.isShown()) {
165 dismiss();
166 }
167 }
168 }
Adam Powell42675342010-07-09 18:02:59 -0700169}