MenuDialogHelper.java revision 870a2b015645ef5086af6a5de31dc482e7193214
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 // Since this is for a menu, disable the recycling of views 72 // This is done by the menu framework anyway 73 builder.setRecycleOnMeasureEnabled(false); 74 75 // Show the menu 76 mDialog = builder.create(); 77 78 WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes(); 79 lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; 80 if (windowToken != null) { 81 lp.token = windowToken; 82 } 83 lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 84 85 mDialog.show(); 86 } 87 88 public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 89 /* 90 * Close menu on key down (more responsive, and there's no way to cancel 91 * a key press so no point having it on key up. Note: This is also 92 * needed because when a top-level menu item that shows a submenu is 93 * invoked by chording, this onKey method will be called with the menu 94 * up event. 95 */ 96 if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_MENU) 97 || (keyCode == KeyEvent.KEYCODE_BACK)) { 98 mMenu.close(true); 99 dialog.dismiss(); 100 return true; 101 } 102 103 // Menu shortcut matching 104 return mMenu.performShortcut(keyCode, event, 0); 105 106 } 107 108 /** 109 * Dismisses the menu's dialog. 110 * 111 * @see Dialog#dismiss() 112 */ 113 public void dismiss() { 114 if (mDialog != null) { 115 mDialog.dismiss(); 116 } 117 } 118 119 public void onClick(DialogInterface dialog, int which) { 120 mMenu.performItemAction((MenuItemImpl) mAdapter.getItem(which), 0); 121 } 122 123} 124