blob: 9913c00994062352a051de207b5c7c9310742c4c [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 Powell8028dd32010-07-15 10:16:33 -070079 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 }
Adam Powell42675342010-07-09 18:02:59 -070086
87 mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
88 mPopup.show();
Adam Powell8028dd32010-07-15 10:16:33 -070089 mPopup.getListView().setOnKeyListener(this);
Adam Powell42675342010-07-09 18:02:59 -070090 }
91
92 public void dismiss() {
93 mPopup.dismiss();
94 mPopup = null;
95 }
96
Adam Powell8028dd32010-07-15 10:16:33 -070097 public boolean isShowing() {
98 return mPopup != null && mPopup.isShowing();
99 }
100
Adam Powell42675342010-07-09 18:02:59 -0700101 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell8028dd32010-07-15 10:16:33 -0700102 MenuItem item = null;
103 if (mOverflowOnly) {
104 item = mMenu.getOverflowItem(position);
105 } else {
106 item = mMenu.getItem(position);
107 }
108 mMenu.performItemAction(item, 0);
Adam Powell42675342010-07-09 18:02:59 -0700109 mPopup.dismiss();
110 }
111
Adam Powell8028dd32010-07-15 10:16:33 -0700112 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
Adam Powell42675342010-07-09 18:02:59 -0700120 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}