1696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell/* 2696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Copyright (C) 2011 The Android Open Source Project 3696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 4696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * you may not use this file except in compliance with the License. 6696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * You may obtain a copy of the License at 7696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 8696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * http://www.apache.org/licenses/LICENSE-2.0 9696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 10696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Unless required by applicable law or agreed to in writing, software 11696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * See the License for the specific language governing permissions and 14696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * limitations under the License. 15696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 16696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 17696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellpackage com.android.internal.view.menu; 18696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 1928a8468995c71ba3fbba12557d143e7599db38d8Alan Viveretteimport android.annotation.NonNull; 2028a8468995c71ba3fbba12557d143e7599db38d8Alan Viveretteimport android.annotation.Nullable; 21696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellimport android.content.Context; 2211ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powellimport android.os.Parcelable; 23696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellimport android.view.ViewGroup; 24696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 25696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell/** 26696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * A MenuPresenter is responsible for building views for a Menu object. 27696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * It takes over some responsibility from the old style monolithic MenuBuilder class. 28696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 29696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellpublic interface MenuPresenter { 30696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 31696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called by menu implementation to notify another component of open/close events. 32696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 33696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public interface Callback { 34696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 35696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called when a menu is closing. 36696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param menu 37696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param allMenusAreClosing 38696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 39696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 40696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 41696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 42696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called when a submenu opens. Useful for notifying the application 43696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * of menu state so that it does not attempt to hide the action bar 44696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * while a submenu is open or similar. 45696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 46696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param subMenu Submenu currently being opened 47696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @return true if the Callback will handle presenting the submenu, false if 48696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * the presenter should attempt to do so. 49696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 50696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean onOpenSubMenu(MenuBuilder subMenu); 51696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell } 52696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 53696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 5428a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * Initializes this presenter for the given context and menu. 5528a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * <p> 5628a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * This method is called by MenuBuilder when a presenter is added. See 5728a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * {@link MenuBuilder#addMenuPresenter(MenuPresenter)}. 58696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 5928a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * @param context the context for this presenter; used for view creation 6028a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * and resource management, must be non-{@code null} 6128a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette * @param menu the menu to host, or {@code null} to clear the hosted menu 62696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 6328a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette public void initForMenu(@NonNull Context context, @Nullable MenuBuilder menu); 64696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 65696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 66696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Retrieve a MenuView to display the menu specified in 673d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette * {@link #initForMenu(Context, MenuBuilder)}. 68696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 69696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param root Intended parent of the MenuView. 70696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @return A freshly created MenuView. 71696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 72696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public MenuView getMenuView(ViewGroup root); 73696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 74696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 75696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Update the menu UI in response to a change. Called by 76696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * MenuBuilder during the normal course of operation. 77696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 78696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param cleared true if the menu was entirely cleared 79696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 80696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public void updateMenuView(boolean cleared); 81696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 82696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 83696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Set a callback object that will be notified of menu events 84696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * related to this specific presentation. 85696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param cb Callback that will be notified of future events 86696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 87696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public void setCallback(Callback cb); 88696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 89696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 90696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called by Menu implementations to indicate that a submenu item 91696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * has been selected. An active Callback should be notified, and 92696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * if applicable the presenter should present the submenu. 93696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 94696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @param subMenu SubMenu being opened 95696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @return true if the the event was handled, false otherwise. 96696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 97696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean onSubMenuSelected(SubMenuBuilder subMenu); 98696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 99696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 100696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called by Menu implementations to indicate that a menu or submenu is 101696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * closing. Presenter implementations should close the representation 102696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * of the menu indicated as necessary and notify a registered callback. 103696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * 10400aa5103e2f71ad3f29f53168e37ef7da8ca03f2Alan Viverette * @param menu the menu or submenu that is closing 10500aa5103e2f71ad3f29f53168e37ef7da8ca03f2Alan Viverette * @param allMenusAreClosing {@code true} if all displayed menus and 10600aa5103e2f71ad3f29f53168e37ef7da8ca03f2Alan Viverette * submenus are closing, {@code false} if only 10700aa5103e2f71ad3f29f53168e37ef7da8ca03f2Alan Viverette * the specified menu is closing 108696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 109696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 110696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 111696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell /** 112696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * Called by Menu implementations to flag items that will be shown as actions. 113696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * @return true if this presenter changed the action status of any items. 114696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */ 115696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean flagActionItems(); 1168d02deabac62c4a68a335a7b3141795466362b89Adam Powell 1178d02deabac62c4a68a335a7b3141795466362b89Adam Powell /** 1188d02deabac62c4a68a335a7b3141795466362b89Adam Powell * Called when a menu item with a collapsable action view should expand its action view. 1198d02deabac62c4a68a335a7b3141795466362b89Adam Powell * 1208d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @param menu Menu containing the item to be expanded 1218d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @param item Item to be expanded 1228d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @return true if this presenter expanded the action view, false otherwise. 1238d02deabac62c4a68a335a7b3141795466362b89Adam Powell */ 1248d02deabac62c4a68a335a7b3141795466362b89Adam Powell public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item); 1258d02deabac62c4a68a335a7b3141795466362b89Adam Powell 1268d02deabac62c4a68a335a7b3141795466362b89Adam Powell /** 1278d02deabac62c4a68a335a7b3141795466362b89Adam Powell * Called when a menu item with a collapsable action view should collapse its action view. 1288d02deabac62c4a68a335a7b3141795466362b89Adam Powell * 1298d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @param menu Menu containing the item to be collapsed 1308d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @param item Item to be collapsed 1318d02deabac62c4a68a335a7b3141795466362b89Adam Powell * @return true if this presenter collapsed the action view, false otherwise. 1328d02deabac62c4a68a335a7b3141795466362b89Adam Powell */ 1338d02deabac62c4a68a335a7b3141795466362b89Adam Powell public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item); 13411ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell 13511ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell /** 13611ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * Returns an ID for determining how to save/restore instance state. 13711ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * @return a valid ID value. 13811ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell */ 13911ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell public int getId(); 14011ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell 14111ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell /** 14211ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * Returns a Parcelable describing the current state of the presenter. 14311ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * It will be passed to the {@link #onRestoreInstanceState(Parcelable)} 14411ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * method of the presenter sharing the same ID later. 14511ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * @return The saved instance state 14611ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell */ 14711ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell public Parcelable onSaveInstanceState(); 14811ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell 14911ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell /** 15011ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * Supplies the previously saved instance state to be restored. 15111ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell * @param state The previously saved instance state 15211ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell */ 15311ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell public void onRestoreInstanceState(Parcelable state); 154696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell} 155