blob: f52c93cf3537146205cc9f3ece90a4cabb350ed0 [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;
27import android.widget.AdapterView;
28import android.widget.ListPopupWindow;
29
Adam Powell8028dd32010-07-15 10:16:33 -070030import java.lang.ref.WeakReference;
31
Adam Powell42675342010-07-09 18:02:59 -070032/**
33 * @hide
34 */
Adam Powell8028dd32010-07-15 10:16:33 -070035public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener {
Adam Powell42675342010-07-09 18:02:59 -070036 private static final String TAG = "MenuPopupHelper";
37
38 private Context mContext;
39 private ListPopupWindow mPopup;
Adam Powell8028dd32010-07-15 10:16:33 -070040 private MenuBuilder mMenu;
Adam Powell42675342010-07-09 18:02:59 -070041 private int mPopupMaxWidth;
Adam Powell8028dd32010-07-15 10:16:33 -070042 private WeakReference<View> mAnchorView;
43 private boolean mOverflowOnly;
Adam Powell42675342010-07-09 18:02:59 -070044
Adam Powell8028dd32010-07-15 10:16:33 -070045 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) {
Adam Powell42675342010-07-09 18:02:59 -070055 mContext = context;
Adam Powell8028dd32010-07-15 10:16:33 -070056 mMenu = menu;
57 mOverflowOnly = overflowOnly;
Adam Powell42675342010-07-09 18:02:59 -070058
59 final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
60 mPopupMaxWidth = metrics.widthPixels / 2;
Adam Powell8028dd32010-07-15 10:16:33 -070061
62 if (anchorView != null) {
63 mAnchorView = new WeakReference<View>(anchorView);
64 }
Adam Powell42675342010-07-09 18:02:59 -070065 }
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
Adam Powell8028dd32010-07-15 10:16:33 -070073 final MenuAdapter adapter = mOverflowOnly ?
74 mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) :
75 mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP);
Adam Powell42675342010-07-09 18:02:59 -070076 mPopup.setAdapter(adapter);
77 mPopup.setModal(true);
78
Adam Powell4be0d522010-08-03 17:53:14 -070079 if (mAnchorView != null) {
80 mPopup.setAnchorView(mAnchorView.get());
81 } else if (mMenu instanceof SubMenuBuilder) {
Adam Powell8028dd32010-07-15 10:16:33 -070082 SubMenuBuilder subMenu = (SubMenuBuilder) mMenu;
83 final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem();
84 mPopup.setAnchorView(itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null));
Adam Powell4be0d522010-08-03 17:53:14 -070085 } else {
86 throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
Adam Powell8028dd32010-07-15 10:16:33 -070087 }
Adam Powell42675342010-07-09 18:02:59 -070088
89 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
90 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -070091 mPopup.getListView().setOnKeyListener(this);
Adam Powell42675342010-07-09 18:02:59 -070092 }
93
94 public void dismiss() {
Adam Powell3d3da272010-08-11 18:06:17 -070095 if (isShowing()) {
96 mPopup.dismiss();
97 }
Adam Powell42675342010-07-09 18:02:59 -070098 mPopup = null;
99 }
100
Adam Powell8028dd32010-07-15 10:16:33 -0700101 public boolean isShowing() {
102 return mPopup != null && mPopup.isShowing();
103 }
104
Adam Powell42675342010-07-09 18:02:59 -0700105 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell8028dd32010-07-15 10:16:33 -0700106 MenuItem item = null;
107 if (mOverflowOnly) {
108 item = mMenu.getOverflowItem(position);
109 } else {
110 item = mMenu.getItem(position);
111 }
112 mMenu.performItemAction(item, 0);
Adam Powell42675342010-07-09 18:02:59 -0700113 mPopup.dismiss();
114 }
115
Adam Powell8028dd32010-07-15 10:16:33 -0700116 public boolean onKey(View v, int keyCode, KeyEvent event) {
117 if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
118 dismiss();
119 return true;
120 }
121 return false;
122 }
123
Adam Powell42675342010-07-09 18:02:59 -0700124 private int measureContentWidth(MenuAdapter adapter) {
125 // Menus don't tend to be long, so this is more sane than it looks.
126 int width = 0;
127 View itemView = null;
128 final int widthMeasureSpec =
129 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
130 final int heightMeasureSpec =
131 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
132 final int count = adapter.getCount();
133 for (int i = 0; i < count; i++) {
134 itemView = adapter.getView(i, itemView, null);
135 itemView.measure(widthMeasureSpec, heightMeasureSpec);
136 width = Math.max(width, itemView.getMeasuredWidth());
137 }
138 return width;
139 }
140}