MenuDialogHelper.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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.WindowManager;
26import android.widget.ListAdapter;
27
28/**
29 * Helper for menus that appear as Dialogs (context and submenus).
30 *
31 * @hide
32 */
33public class MenuDialogHelper implements DialogInterface.OnKeyListener, DialogInterface.OnClickListener {
34    private MenuBuilder mMenu;
35    private ListAdapter mAdapter;
36    private AlertDialog mDialog;
37
38    public MenuDialogHelper(MenuBuilder menu) {
39        mMenu = menu;
40    }
41
42    /**
43     * Shows menu as a dialog.
44     *
45     * @param windowToken Optional token to assign to the window.
46     */
47    public void show(IBinder windowToken) {
48        // Many references to mMenu, create local reference
49        final MenuBuilder menu = mMenu;
50
51        // Get an adapter for the menu item views
52        mAdapter = menu.getMenuAdapter(MenuBuilder.TYPE_DIALOG);
53
54        // Get the builder for the dialog
55        final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext())
56                .setAdapter(mAdapter, this);
57
58        // Set the title
59        final View headerView = menu.getHeaderView();
60        if (headerView != null) {
61            // Menu's client has given a custom header view, use it
62            builder.setCustomTitle(headerView);
63        } else {
64            // Otherwise use the (text) title and icon
65            builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle());
66        }
67
68        // Set the key listener
69        builder.setOnKeyListener(this);
70
71        // Show the menu
72        mDialog = builder.create();
73
74        WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
75        lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
76        if (windowToken != null) {
77            lp.token = windowToken;
78        }
79        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
80
81        mDialog.show();
82    }
83
84    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
85        /*
86         * Close menu on key down (more responsive, and there's no way to cancel
87         * a key press so no point having it on key up. Note: This is also
88         * needed because when a top-level menu item that shows a submenu is
89         * invoked by chording, this onKey method will be called with the menu
90         * up event.
91         */
92        if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_MENU)
93                || (keyCode == KeyEvent.KEYCODE_BACK)) {
94            mMenu.close(true);
95            dialog.dismiss();
96            return true;
97        }
98
99        // Menu shortcut matching
100        if (mMenu.performShortcut(keyCode, event, 0)) {
101            return true;
102        }
103
104        return false;
105    }
106
107    /**
108     * Dismisses the menu's dialog.
109     *
110     * @see Dialog#dismiss()
111     */
112    public void dismiss() {
113        if (mDialog != null) {
114            mDialog.dismiss();
115        }
116    }
117
118    public void onClick(DialogInterface dialog, int which) {
119        mMenu.performItemAction((MenuItemImpl) mAdapter.getItem(which), 0);
120    }
121
122}
123