blob: b93fac44f9fc30e8d345c1b21d3eeded7b1da7f7 [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
Adam Powellf0ad6e62011-01-10 17:14:06 -080071 public void setAnchorView(View anchor) {
72 mAnchorView = new WeakReference<View>(anchor);
73 }
74
Adam Powell42675342010-07-09 18:02:59 -070075 public void show() {
Adam Powell5e3f2842011-01-07 17:16:56 -080076 if (!tryShow()) {
77 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
78 }
79 }
80
81 public boolean tryShow() {
Adam Powell0b2d3062010-09-14 16:15:02 -070082 mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
Adam Powell42675342010-07-09 18:02:59 -070083 mPopup.setOnItemClickListener(this);
Adam Powell8515ee82010-11-30 14:09:55 -080084 mPopup.setOnDismissListener(this);
Adam Powell42675342010-07-09 18:02:59 -070085
Adam Powell8028dd32010-07-15 10:16:33 -070086 final MenuAdapter adapter = mOverflowOnly ?
87 mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) :
88 mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP);
Adam Powell42675342010-07-09 18:02:59 -070089 mPopup.setAdapter(adapter);
90 mPopup.setModal(true);
91
Adam Powell04587962010-11-11 22:15:07 -080092 View anchor = mAnchorView != null ? mAnchorView.get() : null;
93 if (anchor == null && mMenu instanceof SubMenuBuilder) {
Adam Powell8028dd32010-07-15 10:16:33 -070094 SubMenuBuilder subMenu = (SubMenuBuilder) mMenu;
95 final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem();
Adam Powell04587962010-11-11 22:15:07 -080096 anchor = itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null);
97 mAnchorView = new WeakReference<View>(anchor);
98 }
99
100 if (anchor != null) {
101 mTreeObserver = anchor.getViewTreeObserver();
102 mTreeObserver.addOnGlobalLayoutListener(this);
103 mPopup.setAnchorView(anchor);
Adam Powell4be0d522010-08-03 17:53:14 -0700104 } else {
Adam Powell5e3f2842011-01-07 17:16:56 -0800105 return false;
Adam Powell8028dd32010-07-15 10:16:33 -0700106 }
Adam Powell42675342010-07-09 18:02:59 -0700107
108 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
Adam Powellaa0b92c2010-12-13 22:38:53 -0800109 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Adam Powell42675342010-07-09 18:02:59 -0700110 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700111 mPopup.getListView().setOnKeyListener(this);
Adam Powell5e3f2842011-01-07 17:16:56 -0800112 return true;
Adam Powell42675342010-07-09 18:02:59 -0700113 }
114
115 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700116 if (isShowing()) {
117 mPopup.dismiss();
118 }
Adam Powell8515ee82010-11-30 14:09:55 -0800119 }
120
121 public void onDismiss() {
122 mPopup = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800123 if (mTreeObserver != null) {
Adam Powell8515ee82010-11-30 14:09:55 -0800124 mTreeObserver.removeGlobalOnLayoutListener(MenuPopupHelper.this);
Adam Powelled8b4032010-11-16 10:22:35 -0800125 mTreeObserver = null;
126 }
Adam Powell42675342010-07-09 18:02:59 -0700127 }
128
Adam Powell8028dd32010-07-15 10:16:33 -0700129 public boolean isShowing() {
130 return mPopup != null && mPopup.isShowing();
131 }
132
Adam Powell42675342010-07-09 18:02:59 -0700133 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell8028dd32010-07-15 10:16:33 -0700134 MenuItem item = null;
135 if (mOverflowOnly) {
136 item = mMenu.getOverflowItem(position);
137 } else {
Adam Powell1700ae02010-12-19 17:11:07 -0800138 item = mMenu.getVisibleItems().get(position);
Adam Powell8028dd32010-07-15 10:16:33 -0700139 }
140 mMenu.performItemAction(item, 0);
Adam Powell04587962010-11-11 22:15:07 -0800141 dismiss();
Adam Powell42675342010-07-09 18:02:59 -0700142 }
143
Adam Powell8028dd32010-07-15 10:16:33 -0700144 public boolean onKey(View v, int keyCode, KeyEvent event) {
145 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
146 dismiss();
147 return true;
148 }
149 return false;
150 }
151
Adam Powell42675342010-07-09 18:02:59 -0700152 private int measureContentWidth(MenuAdapter adapter) {
153 // Menus don't tend to be long, so this is more sane than it looks.
154 int width = 0;
155 View itemView = null;
Adam Powell50f784c2010-12-19 16:12:19 -0800156 int itemType = 0;
Adam Powell42675342010-07-09 18:02:59 -0700157 final int widthMeasureSpec =
158 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
159 final int heightMeasureSpec =
160 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
161 final int count = adapter.getCount();
162 for (int i = 0; i < count; i++) {
Adam Powell50f784c2010-12-19 16:12:19 -0800163 final int positionType = adapter.getItemViewType(i);
164 if (positionType != itemType) {
165 itemType = positionType;
166 itemView = null;
167 }
Adam Powell42675342010-07-09 18:02:59 -0700168 itemView = adapter.getView(i, itemView, null);
169 itemView.measure(widthMeasureSpec, heightMeasureSpec);
170 width = Math.max(width, itemView.getMeasuredWidth());
171 }
172 return width;
173 }
Adam Powell04587962010-11-11 22:15:07 -0800174
175 @Override
176 public void onGlobalLayout() {
177 if (!isShowing()) {
178 mTreeObserver.removeGlobalOnLayoutListener(this);
179 mTreeObserver = null;
180 } else {
181 final View anchor = mAnchorView != null ? mAnchorView.get() : null;
182 if (anchor != null && !anchor.isShown()) {
183 dismiss();
Adam Powellaa0b92c2010-12-13 22:38:53 -0800184 } else {
185 // Recompute window size and position
186 mPopup.show();
Adam Powell04587962010-11-11 22:15:07 -0800187 }
188 }
189 }
Adam Powell42675342010-07-09 18:02:59 -0700190}