[go: nahoru, domu]

1/*
2 * Copyright (C) 2013 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 android.support.v7.view.menu;
18
19import android.app.Dialog;
20import android.content.DialogInterface;
21import android.os.IBinder;
22import android.support.v7.app.AlertDialog;
23import android.support.v7.appcompat.R;
24import android.view.KeyEvent;
25import android.view.View;
26import android.view.Window;
27import android.view.WindowManager;
28
29/**
30 * Helper for menus that appear as Dialogs (context and submenus).
31 */
32class MenuDialogHelper implements DialogInterface.OnKeyListener,
33        DialogInterface.OnClickListener,
34        DialogInterface.OnDismissListener,
35        MenuPresenter.Callback {
36    private MenuBuilder mMenu;
37    private AlertDialog mDialog;
38    ListMenuPresenter mPresenter;
39    private MenuPresenter.Callback mPresenterCallback;
40
41    public MenuDialogHelper(MenuBuilder menu) {
42        mMenu = menu;
43    }
44
45    /**
46     * Shows menu as a dialog.
47     *
48     * @param windowToken Optional token to assign to the window.
49     */
50    public void show(IBinder windowToken) {
51        // Many references to mMenu, create local reference
52        final MenuBuilder menu = mMenu;
53
54        // Get the builder for the dialog
55        final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext());
56
57        mPresenter = new ListMenuPresenter(builder.getContext(),
58                R.layout.abc_list_menu_item_layout);
59
60        mPresenter.setCallback(this);
61        mMenu.addMenuPresenter(mPresenter);
62        builder.setAdapter(mPresenter.getAdapter(), this);
63
64        // Set the title
65        final View headerView = menu.getHeaderView();
66        if (headerView != null) {
67            // Menu's client has given a custom header view, use it
68            builder.setCustomTitle(headerView);
69        } else {
70            // Otherwise use the (text) title and icon
71            builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle());
72        }
73
74        // Set the key listener
75        builder.setOnKeyListener(this);
76
77        // Show the menu
78        mDialog = builder.create();
79        mDialog.setOnDismissListener(this);
80
81        WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
82        lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
83        if (windowToken != null) {
84            lp.token = windowToken;
85        }
86        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
87
88        mDialog.show();
89    }
90
91    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
92        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
93            if (event.getAction() == KeyEvent.ACTION_DOWN
94                    && event.getRepeatCount() == 0) {
95                Window win = mDialog.getWindow();
96                if (win != null) {
97                    View decor = win.getDecorView();
98                    if (decor != null) {
99                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
100                        if (ds != null) {
101                            ds.startTracking(event, this);
102                            return true;
103                        }
104                    }
105                }
106            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
107                Window win = mDialog.getWindow();
108                if (win != null) {
109                    View decor = win.getDecorView();
110                    if (decor != null) {
111                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
112                        if (ds != null && ds.isTracking(event)) {
113                            mMenu.close(true);
114                            dialog.dismiss();
115                            return true;
116                        }
117                    }
118                }
119            }
120        }
121
122        // Menu shortcut matching
123        return mMenu.performShortcut(keyCode, event, 0);
124
125    }
126
127    public void setPresenterCallback(MenuPresenter.Callback cb) {
128        mPresenterCallback = cb;
129    }
130
131    /**
132     * Dismisses the menu's dialog.
133     *
134     * @see Dialog#dismiss()
135     */
136    public void dismiss() {
137        if (mDialog != null) {
138            mDialog.dismiss();
139        }
140    }
141
142    @Override
143    public void onDismiss(DialogInterface dialog) {
144        mPresenter.onCloseMenu(mMenu, true);
145    }
146
147    @Override
148    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
149        if (allMenusAreClosing || menu == mMenu) {
150            dismiss();
151        }
152        if (mPresenterCallback != null) {
153            mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
154        }
155    }
156
157    @Override
158    public boolean onOpenSubMenu(MenuBuilder subMenu) {
159        if (mPresenterCallback != null) {
160            return mPresenterCallback.onOpenSubMenu(subMenu);
161        }
162        return false;
163    }
164
165    public void onClick(DialogInterface dialog, int which) {
166        mMenu.performItemAction((MenuItemImpl) mPresenter.getAdapter().getItem(which), 0);
167    }
168}
169