FileDocCategorySizeDatePackage
MenuDialogHelper.javaAPI DocAndroid 5.1 API5640Thu Mar 12 22:22:10 GMT 2015com.android.internal.view.menu

MenuDialogHelper

public class MenuDialogHelper extends Object implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener, MenuPresenter.Callback
Helper for menus that appear as Dialogs (context and submenus).
hide

Fields Summary
private MenuBuilder
mMenu
private android.app.AlertDialog
mDialog
ListMenuPresenter
mPresenter
private MenuPresenter.Callback
mPresenterCallback
Constructors Summary
public MenuDialogHelper(MenuBuilder menu)

        mMenu = menu;
    
Methods Summary
public voiddismiss()
Dismisses the menu's dialog.

see
Dialog#dismiss()

        if (mDialog != null) {
            mDialog.dismiss();
        }
    
public voidonClick(android.content.DialogInterface dialog, int which)

        mMenu.performItemAction((MenuItemImpl) mPresenter.getAdapter().getItem(which), 0);
    
public voidonCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

        if (allMenusAreClosing || menu == mMenu) {
            dismiss();
        }
        if (mPresenterCallback != null) {
            mPresenterCallback.onCloseMenu(menu, allMenusAreClosing);
        }
    
public voidonDismiss(android.content.DialogInterface dialog)

        mPresenter.onCloseMenu(mMenu, true);
    
public booleanonKey(android.content.DialogInterface dialog, int keyCode, android.view.KeyEvent event)

        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                Window win = mDialog.getWindow();
                if (win != null) {
                    View decor = win.getDecorView();
                    if (decor != null) {
                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
                        if (ds != null) {
                            ds.startTracking(event, this);
                            return true;
                        }
                    }
                }
            } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
                Window win = mDialog.getWindow();
                if (win != null) {
                    View decor = win.getDecorView();
                    if (decor != null) {
                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
                        if (ds != null && ds.isTracking(event)) {
                            mMenu.close(true);
                            dialog.dismiss();
                            return true;
                        }
                    }
                }
            }
        }

        // Menu shortcut matching
        return mMenu.performShortcut(keyCode, event, 0);

    
public booleanonOpenSubMenu(MenuBuilder subMenu)

        if (mPresenterCallback != null) {
            return mPresenterCallback.onOpenSubMenu(subMenu);
        }
        return false;
    
public voidsetPresenterCallback(MenuPresenter.Callback cb)

        mPresenterCallback = cb;
    
public voidshow(android.os.IBinder windowToken)
Shows menu as a dialog.

param
windowToken Optional token to assign to the window.

        // Many references to mMenu, create local reference
        final MenuBuilder menu = mMenu;
        
        // Get the builder for the dialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext());

        mPresenter = new ListMenuPresenter(builder.getContext(),
                com.android.internal.R.layout.list_menu_item_layout);

        mPresenter.setCallback(this);
        mMenu.addMenuPresenter(mPresenter);
        builder.setAdapter(mPresenter.getAdapter(), this);

        // Set the title
        final View headerView = menu.getHeaderView();
        if (headerView != null) {
            // Menu's client has given a custom header view, use it
            builder.setCustomTitle(headerView);
        } else {
            // Otherwise use the (text) title and icon
            builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle());
        }
        
        // Set the key listener
        builder.setOnKeyListener(this);
        
        // Show the menu
        mDialog = builder.create();
        mDialog.setOnDismissListener(this);
        
        WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
        lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
        if (windowToken != null) {
            lp.token = windowToken;
        }
        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        
        mDialog.show();