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; 27 28/** 29 * Presents a menu as a modal dialog. 30 */ 31public class MenuDialogHelper implements MenuHelper, DialogInterface.OnKeyListener, 32 DialogInterface.OnClickListener, DialogInterface.OnDismissListener, 33 MenuPresenter.Callback { 34 private MenuBuilder mMenu; 35 private AlertDialog mDialog; 36 ListMenuPresenter mPresenter; 37 private MenuPresenter.Callback mPresenterCallback; 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 the builder for the dialog 53 final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext()); 54 55 mPresenter = new ListMenuPresenter(builder.getContext(), 56 com.android.internal.R.layout.list_menu_item_layout); 57 58 mPresenter.setCallback(this); 59 mMenu.addMenuPresenter(mPresenter); 60 builder.setAdapter(mPresenter.getAdapter(), this); 61 62 // Set the title 63 final View headerView = menu.getHeaderView(); 64 if (headerView != null) { 65 // Menu's client has given a custom header view, use it 66 builder.setCustomTitle(headerView); 67 } else { 68 // Otherwise use the (text) title and icon 69 builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle()); 70 } 71 72 // Set the key listener 73 builder.setOnKeyListener(this); 74 75 // Show the menu 76 mDialog = builder.create(); 77 mDialog.setOnDismissListener(this); 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 /* closeAllMenus */); 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 @Override 126 public void setPresenterCallback(MenuPresenter.Callback cb) { 127 mPresenterCallback = cb; 128 } 129 130 /** 131 * Dismisses the menu's dialog. 132 * 133 * @see Dialog#dismiss() 134 */ 135 @Override 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