MenuPopupHelper.java revision 0458796f1401732b38660794148f4c5e5602f432
1/*
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;
23import android.view.KeyEvent;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.View.MeasureSpec;
27import android.view.ViewTreeObserver;
28import android.widget.AdapterView;
29import android.widget.ListPopupWindow;
30import android.widget.PopupWindow;
31
32import java.lang.ref.WeakReference;
33
34/**
35 * @hide
36 */
37public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.OnKeyListener,
38        ViewTreeObserver.OnGlobalLayoutListener {
39    private static final String TAG = "MenuPopupHelper";
40
41    private Context mContext;
42    private ListPopupWindow mPopup;
43    private MenuBuilder mMenu;
44    private int mPopupMaxWidth;
45    private WeakReference<View> mAnchorView;
46    private boolean mOverflowOnly;
47    private ViewTreeObserver mTreeObserver;
48
49    private PopupWindow.OnDismissListener mDismissListener = new PopupWindow.OnDismissListener() {
50        public void onDismiss() {
51            mPopup = null;
52        }
53    };
54
55    public MenuPopupHelper(Context context, MenuBuilder menu) {
56        this(context, menu, null, false);
57    }
58
59    public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView) {
60        this(context, menu, anchorView, false);
61    }
62
63    public MenuPopupHelper(Context context, MenuBuilder menu,
64            View anchorView, boolean overflowOnly) {
65        mContext = context;
66        mMenu = menu;
67        mOverflowOnly = overflowOnly;
68
69        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
70        mPopupMaxWidth = metrics.widthPixels / 2;
71
72        if (anchorView != null) {
73            mAnchorView = new WeakReference<View>(anchorView);
74        }
75    }
76
77    public void show() {
78        mPopup = new ListPopupWindow(mContext, null, com.android.internal.R.attr.popupMenuStyle);
79        mPopup.setOnItemClickListener(this);
80        mPopup.setOnDismissListener(mDismissListener);
81
82        final MenuAdapter adapter = mOverflowOnly ?
83                mMenu.getOverflowMenuAdapter(MenuBuilder.TYPE_POPUP) :
84                mMenu.getMenuAdapter(MenuBuilder.TYPE_POPUP);
85        mPopup.setAdapter(adapter);
86        mPopup.setModal(true);
87
88        View anchor = mAnchorView != null ? mAnchorView.get() : null;
89        if (anchor == null && mMenu instanceof SubMenuBuilder) {
90            SubMenuBuilder subMenu = (SubMenuBuilder) mMenu;
91            final MenuItemImpl itemImpl = (MenuItemImpl) subMenu.getItem();
92            anchor = itemImpl.getItemView(MenuBuilder.TYPE_ACTION_BUTTON, null);
93            mAnchorView = new WeakReference<View>(anchor);
94        }
95
96        if (anchor != null) {
97            mTreeObserver = anchor.getViewTreeObserver();
98            mTreeObserver.addOnGlobalLayoutListener(this);
99            mPopup.setAnchorView(anchor);
100        } else {
101            throw new IllegalStateException("MenuPopupHelper cannot be used without an anchor");
102        }
103
104        mPopup.setContentWidth(Math.min(measureContentWidth(adapter), mPopupMaxWidth));
105        mPopup.show();
106        mPopup.getListView().setOnKeyListener(this);
107    }
108
109    public void dismiss() {
110        if (isShowing()) {
111            mPopup.dismiss();
112        }
113        mTreeObserver.removeGlobalOnLayoutListener(this);
114        mTreeObserver = null;
115    }
116
117    public boolean isShowing() {
118        return mPopup != null && mPopup.isShowing();
119    }
120
121    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
122        MenuItem item = null;
123        if (mOverflowOnly) {
124            item = mMenu.getOverflowItem(position);
125        } else {
126            item = mMenu.getItem(position);
127        }
128        mMenu.performItemAction(item, 0);
129        dismiss();
130    }
131
132    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
140    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    }
156
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    }
169}
170