1/* 2 * Copyright (C) 2011 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.internal.view.menu; 18 19import android.content.Context; 20import android.os.Parcelable; 21import android.view.ViewGroup; 22 23/** 24 * A MenuPresenter is responsible for building views for a Menu object. It takes over some 25 * responsibility from the old style monolithic MenuBuilder class. 26 * 27 * @hide 28 */ 29public interface MenuPresenter { 30 31 /** 32 * Called by menu implementation to notify another component of open/close events. 33 * 34 * @hide 35 */ 36 public interface Callback { 37 /** 38 * Called when a menu is closing. 39 * @param menu 40 * @param allMenusAreClosing 41 */ 42 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 43 44 /** 45 * Called when a submenu opens. Useful for notifying the application 46 * of menu state so that it does not attempt to hide the action bar 47 * while a submenu is open or similar. 48 * 49 * @param subMenu Submenu currently being opened 50 * @return true if the Callback will handle presenting the submenu, false if 51 * the presenter should attempt to do so. 52 */ 53 public boolean onOpenSubMenu(MenuBuilder subMenu); 54 } 55 56 /** 57 * Initialize this presenter for the given context and menu. 58 * This method is called by MenuBuilder when a presenter is 59 * added. See {@link MenuBuilder#addMenuPresenter(MenuPresenter)} 60 * 61 * @param context Context for this presenter; used for view creation and resource management 62 * @param menu Menu to host 63 */ 64 public void initForMenu(Context context, MenuBuilder menu); 65 66 /** 67 * Retrieve a MenuView to display the menu specified in 68 * {@link #initForMenu(Context, MenuBuilder)}. 69 * 70 * @param root Intended parent of the MenuView. 71 * @return A freshly created MenuView. 72 */ 73 public MenuView getMenuView(ViewGroup root); 74 75 /** 76 * Update the menu UI in response to a change. Called by 77 * MenuBuilder during the normal course of operation. 78 * 79 * @param cleared true if the menu was entirely cleared 80 */ 81 public void updateMenuView(boolean cleared); 82 83 /** 84 * Set a callback object that will be notified of menu events 85 * related to this specific presentation. 86 * @param cb Callback that will be notified of future events 87 */ 88 public void setCallback(Callback cb); 89 90 /** 91 * Called by Menu implementations to indicate that a submenu item 92 * has been selected. An active Callback should be notified, and 93 * if applicable the presenter should present the submenu. 94 * 95 * @param subMenu SubMenu being opened 96 * @return true if the the event was handled, false otherwise. 97 */ 98 public boolean onSubMenuSelected(SubMenuBuilder subMenu); 99 100 /** 101 * Called by Menu implementations to indicate that a menu or submenu is 102 * closing. Presenter implementations should close the representation 103 * of the menu indicated as necessary and notify a registered callback. 104 * 105 * @param menu Menu or submenu that is closing. 106 * @param allMenusAreClosing True if all associated menus are closing. 107 */ 108 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 109 110 /** 111 * Called by Menu implementations to flag items that will be shown as actions. 112 * @return true if this presenter changed the action status of any items. 113 */ 114 public boolean flagActionItems(); 115 116 /** 117 * Called when a menu item with a collapsable action view should expand its action view. 118 * 119 * @param menu Menu containing the item to be expanded 120 * @param item Item to be expanded 121 * @return true if this presenter expanded the action view, false otherwise. 122 */ 123 public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item); 124 125 /** 126 * Called when a menu item with a collapsable action view should collapse its action view. 127 * 128 * @param menu Menu containing the item to be collapsed 129 * @param item Item to be collapsed 130 * @return true if this presenter collapsed the action view, false otherwise. 131 */ 132 public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item); 133 134 /** 135 * Returns an ID for determining how to save/restore instance state. 136 * @return a valid ID value. 137 */ 138 public int getId(); 139 140 /** 141 * Returns a Parcelable describing the current state of the presenter. 142 * It will be passed to the {@link #onRestoreInstanceState(Parcelable)} 143 * method of the presenter sharing the same ID later. 144 * @return The saved instance state 145 */ 146 public Parcelable onSaveInstanceState(); 147 148 /** 149 * Supplies the previously saved instance state to be restored. 150 * @param state The previously saved instance state 151 */ 152 public void onRestoreInstanceState(Parcelable state); 153} 154