blob: 04a059e720e84c4d785c9dfad8872351f3a37328 [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;
Adam Powelle2b03a62011-01-25 14:39:02 -080022import android.os.Handler;
Adam Powell42675342010-07-09 18:02:59 -070023import android.util.DisplayMetrics;
Adam Powell8028dd32010-07-15 10:16:33 -070024import android.view.KeyEvent;
25import android.view.MenuItem;
Adam Powell42675342010-07-09 18:02:59 -070026import android.view.View;
27import android.view.View.MeasureSpec;
Adam Powell04587962010-11-11 22:15:07 -080028import android.view.ViewTreeObserver;
Adam Powell42675342010-07-09 18:02:59 -070029import android.widget.AdapterView;
30import android.widget.ListPopupWindow;
Adam Powell6c6f5752010-08-20 18:34:46 -070031import android.widget.PopupWindow;
Adam Powell42675342010-07-09 18:02:59 -070032
Adam Powell8028dd32010-07-15 10:16:33 -070033import java.lang.ref.WeakReference;
34
Adam Powell42675342010-07-09 18:02:59 -070035/**
36 * @hide
37 */
Adam Powell04587962010-11-11 22:15:07 -080038public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
Adam Powell4afd62b2011-02-18 15:02:18 -080039 ViewTreeObserver.OnGlobalLayoutListener, PopupWindow.OnDismissListener,
40 View.OnAttachStateChangeListener {
Adam Powell42675342010-07-09 18:02:59 -070041 private static final String TAG = "MenuPopupHelper";
42
43 private Context mContext;
44 private ListPopupWindow mPopup;
Adam Powell8028dd32010-07-15 10:16:33 -070045 private MenuBuilder mMenu;
Adam Powell42675342010-07-09 18:02:59 -070046 private int mPopupMaxWidth;
Adam Powell4afd62b2011-02-18 15:02:18 -080047 private View mAnchorView;
Adam Powell8028dd32010-07-15 10:16:33 -070048 private boolean mOverflowOnly;
Adam Powell04587962010-11-11 22:15:07 -080049 private ViewTreeObserver mTreeObserver;
Adam Powell42675342010-07-09 18:02:59 -070050
Adam Powelle2b03a62011-01-25 14:39:02 -080051 private final Handler mHandler = new Handler();
52
Adam Powell8028dd32010-07-15 10:16:33 -070053 public MenuPopupHelper(Context context, MenuBuilder menu) {
54 this(context, menu, null, false);
55 }
56
57 public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
58 this(context, menu, anchorView, false);
59 }
60
61 public MenuPopupHelper(Context context, MenuBuilder menu,
62 View anchorView, boolean overflowOnly) {
Adam Powell42675342010-07-09 18:02:59 -070063 mContext = context;
Adam Powell8028dd32010-07-15 10:16:33 -070064 mMenu = menu;
65 mOverflowOnly = overflowOnly;
Adam Powell42675342010-07-09 18:02:59 -070066
67 final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
68 mPopupMaxWidth = metrics.widthPixels / 2;
Adam Powell8028dd32010-07-15 10:16:33 -070069
Adam Powell4afd62b2011-02-18 15:02:18 -080070 mAnchorView = anchorView;
Adam Powell42675342010-07-09 18:02:59 -070071 }
72
Adam Powellf0ad6e62011-01-10 17:14:06 -080073 public void setAnchorView(View anchor) {
Adam Powell4afd62b2011-02-18 15:02:18 -080074 mAnchorView = anchor;
Adam Powellf0ad6e62011-01-10 17:14:06 -080075 }
76
Adam Powell42675342010-07-09 18:02:59 -070077 public void show() {
Adam Powell5e3f2842011-01-07 17:16:56 -080078 if (!tryShow()) {
79 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
80 }
81 }
82
83 public boolean tryShow() {
Adam Powell0b2d3062010-09-14 16:15:02 -070084 mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
Adam Powell42675342010-07-09 18:02:59 -070085 mPopup.setOnItemClickListener(this);
Adam Powell8515ee82010-11-30 14:09:55 -080086 mPopup.setOnDismissListener(this);
Adam Powell42675342010-07-09 18:02:59 -070087
Adam Powell8028dd32010-07-15 10:16:33 -070088 final MenuAdapter adapter = mOverflowOnly ?
89 mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) :
90 mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP);
Adam Powell42675342010-07-09 18:02:59 -070091 mPopup.setAdapter(adapter);
92 mPopup.setModal(true);
93
Adam Powell4afd62b2011-02-18 15:02:18 -080094 View anchor = mAnchorView;
Adam Powell04587962010-11-11 22:15:07 -080095 if (anchor == null && mMenu instanceof SubMenuBuilder) {
Adam Powell8028dd32010-07-15 10:16:33 -070096 SubMenuBuilder subMenu = (SubMenuBuilder) mMenu;
97 final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem();
Adam Powell04587962010-11-11 22:15:07 -080098 anchor = itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null);
Adam Powell4afd62b2011-02-18 15:02:18 -080099 mAnchorView = anchor;
Adam Powell04587962010-11-11 22:15:07 -0800100 }
101
102 if (anchor != null) {
Adam Powell4afd62b2011-02-18 15:02:18 -0800103 final boolean addGlobalListener = mTreeObserver == null;
104 mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
105 if (addGlobalListener) mTreeObserver.addOnGlobalLayoutListener(this);
106 anchor.addOnAttachStateChangeListener(this);
Adam Powell04587962010-11-11 22:15:07 -0800107 mPopup.setAnchorView(anchor);
Adam Powell4be0d522010-08-03 17:53:14 -0700108 } else {
Adam Powell5e3f2842011-01-07 17:16:56 -0800109 return false;
Adam Powell8028dd32010-07-15 10:16:33 -0700110 }
Adam Powell42675342010-07-09 18:02:59 -0700111
112 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
Adam Powellaa0b92c2010-12-13 22:38:53 -0800113 mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
Adam Powell42675342010-07-09 18:02:59 -0700114 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -0700115 mPopup.getListView().setOnKeyListener(this);
Adam Powell5e3f2842011-01-07 17:16:56 -0800116 return true;
Adam Powell42675342010-07-09 18:02:59 -0700117 }
118
119 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -0700120 if (isShowing()) {
121 mPopup.dismiss();
122 }
Adam Powell8515ee82010-11-30 14:09:55 -0800123 }
124
125 public void onDismiss() {
126 mPopup = null;
Adam Powell4afd62b2011-02-18 15:02:18 -0800127 if (mTreeObserver != null) {
128 if (!mTreeObserver.isAlive()) mTreeObserver = mAnchorView.getViewTreeObserver();
Adam Powellca51e872011-02-14 19:54:29 -0800129 mTreeObserver.removeGlobalOnLayoutListener(this);
Adam Powell4afd62b2011-02-18 15:02:18 -0800130 mTreeObserver = null;
Adam Powelled8b4032010-11-16 10:22:35 -0800131 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800132 mAnchorView.removeOnAttachStateChangeListener(this);
Adam Powell42675342010-07-09 18:02:59 -0700133 }
134
Adam Powell8028dd32010-07-15 10:16:33 -0700135 public boolean isShowing() {
136 return mPopup != null && mPopup.isShowing();
137 }
138
Adam Powell42675342010-07-09 18:02:59 -0700139 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powellca51e872011-02-14 19:54:29 -0800140 if (!isShowing()) return;
141
Adam Powell8028dd32010-07-15 10:16:33 -0700142 MenuItem item = null;
143 if (mOverflowOnly) {
144 item = mMenu.getOverflowItem(position);
145 } else {
Adam Powell1700ae02010-12-19 17:11:07 -0800146 item = mMenu.getVisibleItems().get(position);
Adam Powell8028dd32010-07-15 10:16:33 -0700147 }
Adam Powell04587962010-11-11 22:15:07 -0800148 dismiss();
Adam Powelle2b03a62011-01-25 14:39:02 -0800149
150 final MenuItem performItem = item;
151 mHandler.post(new Runnable() {
152 public void run() {
153 mMenu.performItemAction(performItem, 0);
154 }
155 });
Adam Powell42675342010-07-09 18:02:59 -0700156 }
157
Adam Powell8028dd32010-07-15 10:16:33 -0700158 public boolean onKey(View v, int keyCode, KeyEvent event) {
159 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
160 dismiss();
161 return true;
162 }
163 return false;
164 }
165
Adam Powell42675342010-07-09 18:02:59 -0700166 private int measureContentWidth(MenuAdapter adapter) {
167 // Menus don't tend to be long, so this is more sane than it looks.
168 int width = 0;
169 View itemView = null;
Adam Powell50f784c2010-12-19 16:12:19 -0800170 int itemType = 0;
Adam Powell42675342010-07-09 18:02:59 -0700171 final int widthMeasureSpec =
172 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
173 final int heightMeasureSpec =
174 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
175 final int count = adapter.getCount();
176 for (int i = 0; i < count; i++) {
Adam Powell50f784c2010-12-19 16:12:19 -0800177 final int positionType = adapter.getItemViewType(i);
178 if (positionType != itemType) {
179 itemType = positionType;
180 itemView = null;
181 }
Adam Powell42675342010-07-09 18:02:59 -0700182 itemView = adapter.getView(i, itemView, null);
183 itemView.measure(widthMeasureSpec, heightMeasureSpec);
184 width = Math.max(width, itemView.getMeasuredWidth());
185 }
186 return width;
187 }
Adam Powell04587962010-11-11 22:15:07 -0800188
189 @Override
190 public void onGlobalLayout() {
Adam Powell4afd62b2011-02-18 15:02:18 -0800191 if (isShowing()) {
192 final View anchor = mAnchorView;
Adam Powellca51e872011-02-14 19:54:29 -0800193 if (anchor == null || !anchor.isShown()) {
Adam Powell04587962010-11-11 22:15:07 -0800194 dismiss();
Adam Powellca51e872011-02-14 19:54:29 -0800195 } else if (isShowing()) {
Adam Powellaa0b92c2010-12-13 22:38:53 -0800196 // Recompute window size and position
197 mPopup.show();
Adam Powell04587962010-11-11 22:15:07 -0800198 }
199 }
200 }
Adam Powell4afd62b2011-02-18 15:02:18 -0800201
202 @Override
203 public void onViewAttachedToWindow(View v) {
204 }
205
206 @Override
207 public void onViewDetachedFromWindow(View v) {
208 if (mTreeObserver != null) {
209 if (!mTreeObserver.isAlive()) mTreeObserver = v.getViewTreeObserver();
210 mTreeObserver.removeGlobalOnLayoutListener(this);
211 }
212 v.removeOnAttachStateChangeListener(this);
213 }
Adam Powell42675342010-07-09 18:02:59 -0700214}