MenuDialogHelper.java revision 6024ca5c90203b6c33acc6adfbf7e4e613549a0a
1/*
2 * Copyright (C) 2006 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 android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.DialogInterface;
22import android.os.IBinder;
23import android.view.KeyEvent;
24import android.view.View;
25import android.view.Window;
26import android.view.WindowManager;
27import android.widget.ListAdapter;
28
29/**
30 * Helper for menus that appear as Dialogs (context and submenus).
31 *
32 * @hide
33 */
34public class MenuDialogHelper implements DialogInterface.OnKeyListener, DialogInterface.OnClickListener {
35    private MenuBuilder mMenu;
36    private ListAdapter mAdapter;
37    private AlertDialog mDialog;
38
39    public MenuDialogHelper(MenuBuilder menu) {
40        mMenu = menu;
41    }
42
43    /**
44     * Shows menu as a dialog.
45     *
46     * @param windowToken Optional token to assign to the window.
47     */
48    public void show(IBinder windowToken) {
49        // Many references to mMenu, create local reference
50        final MenuBuilder menu = mMenu;
51
52        // Get an adapter for the menu item views
53        mAdapter = menu.getMenuAdapter(MenuBuilder.TYPE_DIALOG);
54
55        // Get the builder for the dialog
56        final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext())
57                .setAdapter(mAdapter, this);
58
59        // Set the title
60        final View headerView = menu.getHeaderView();
61        if (headerView != null) {
62            // Menu's client has given a custom header view, use it
63            builder.setCustomTitle(headerView);
64        } else {
65            // Otherwise use the (text) title and icon
66            builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle());
67        }
68
69        // Set the key listener
70        builder.setOnKeyListener(this);
71
72        // Since this is for a menu, disable the recycling of views
73        // This is done by the menu framework anyway
74        builder.setRecycleOnMeasureEnabled(false);
75
76        // Show the menu
77        mDialog = builder.create();
78
79        WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
80        lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
81        if (windowToken != null) {
82            lp.token = windowToken;
83        }
84        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
85
86        mDialog.show();
87    }
88
89    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
90        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
91            if (event.getAction() == KeyEvent.ACTION_DOWN
92                    && event.getRepeatCount() == 0) {
93                Window win = mDialog.getWindow();
94                if (win != null) {
95                    View decor = win.getDecorView();
96                    if (decor != null) {
97                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
98                        if (ds != null) {
99                            ds.startTracking(event, this);
100                            return true;
101                        }
102                    }
103                }
104            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
105                Window win = mDialog.getWindow();
106                if (win != null) {
107                    View decor = win.getDecorView();
108                    if (decor != null) {
109                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
110                        if (ds != null && ds.isTracking(event)) {
111                            mMenu.close(true);
112                            dialog.dismiss();
113                            return true;
114                        }
115                    }
116                }
117            }
118        }
119
120        // Menu shortcut matching
121        return mMenu.performShortcut(keyCode, event, 0);
122
123    }
124
125    /**
126     * Dismisses the menu's dialog.
127     *
128     * @see Dialog#dismiss()
129     */
130    public void dismiss() {
131        if (mDialog != null) {
132            mDialog.dismiss();
133        }
134    }
135
136    public void onClick(DialogInterface dialog, int which) {
137        mMenu.performItemAction((MenuItemImpl) mAdapter.getItem(which), 0);
138    }
139
140}
141