1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/* 2bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Copyright (C) 2011 The Android Open Source Project 3bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * 4bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 5bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * you may not use this file except in compliance with the License. 6bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * You may obtain a copy of the License at 7bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * 8bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 9bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * 10bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Unless required by applicable law or agreed to in writing, software 11bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 12bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * See the License for the specific language governing permissions and 14bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * limitations under the License. 15bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */ 16bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 1766698bb15ba0f873aa1c2290cc50d6bb839a474aChris Banespackage android.support.v7.view.menu; 18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas 21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context; 22bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.os.Parcelable; 23c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo; 24bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.ViewGroup; 25bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 26bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/** 2720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * A MenuPresenter is responsible for building views for a Menu object. It takes over some 2820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * responsibility from the old style monolithic MenuBuilder class. 2989208232f3b5d1451408d787872504a190bc7ee0Chris Banes * 3089208232f3b5d1451408d787872504a190bc7ee0Chris Banes * @hide 31bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */ 328e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas@RestrictTo(LIBRARY_GROUP) 33bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic interface MenuPresenter { 3420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 3520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 3620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * Called by menu implementation to notify another component of open/close events. 3720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 38c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette interface Callback { 3920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 4020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * Called when a menu is closing. 4149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @param menu 4249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @param allMenusAreClosing 4320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 44c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 4520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 4620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 4749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Called when a submenu opens. Useful for notifying the application 4849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * of menu state so that it does not attempt to hide the action bar 4949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * while a submenu is open or similar. 5020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 5120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param subMenu Submenu currently being opened 5249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @return true if the Callback will handle presenting the submenu, false if 5349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * the presenter should attempt to do so. 5420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 55c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette boolean onOpenSubMenu(MenuBuilder subMenu); 5620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns } 5720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 5820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 59722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * Initializes this presenter for the given context and menu. 60722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * <p> 61722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * This method is called by MenuBuilder when a presenter is added. See 62722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * {@link MenuBuilder#addMenuPresenter(MenuPresenter)}. 6320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 64722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * @param context the context for this presenter; used for view creation 65722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * and resource management, must be non-{@code null} 66722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * @param menu the menu to host, or {@code null} to clear the hosted menu 6720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 68c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void initForMenu(Context context, MenuBuilder menu); 6920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 70bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell /** 7130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * Retrieve a MenuView to display the menu specified in 7230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * {@link #initForMenu(Context, MenuBuilder)}. 7320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 7420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param root Intended parent of the MenuView. 7520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return A freshly created MenuView. 7620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 77c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette MenuView getMenuView(ViewGroup root); 7820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 7920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 8049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Update the menu UI in response to a change. Called by 8149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * MenuBuilder during the normal course of operation. 8220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 8320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param cleared true if the menu was entirely cleared 8420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 85c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void updateMenuView(boolean cleared); 8620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 8720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 8849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Set a callback object that will be notified of menu events 8949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * related to this specific presentation. 9020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param cb Callback that will be notified of future events 9120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 92c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void setCallback(Callback cb); 9320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 9420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 9549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Called by Menu implementations to indicate that a submenu item 9649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * has been selected. An active Callback should be notified, and 9749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * if applicable the presenter should present the submenu. 9820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 9920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param subMenu SubMenu being opened 10020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return true if the the event was handled, false otherwise. 10120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 102c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette boolean onSubMenuSelected(SubMenuBuilder subMenu); 10320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 10420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 10549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Called by Menu implementations to indicate that a menu or submenu is 10649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * closing. Presenter implementations should close the representation 10749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * of the menu indicated as necessary and notify a registered callback. 10820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 109722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * @param menu the menu or submenu that is closing 110722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * @param allMenusAreClosing {@code true} if all displayed menus and 111722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * submenus are closing, {@code false} if only 112722327e27579c196b92883c07f4b47e9efada8adKirill Grouchnikov * the specified menu is closing 113bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */ 114c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing); 115bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 116bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell /** 11720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * Called by Menu implementations to flag items that will be shown as actions. 11820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return true if this presenter changed the action status of any items. 11920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 120c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette boolean flagActionItems(); 12120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 12220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 123c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas * Called when a menu item with a collapsible action view should expand its action view. 12420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 12520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param menu Menu containing the item to be expanded 12620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param item Item to be expanded 12720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return true if this presenter expanded the action view, false otherwise. 12820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 129c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item); 13020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 13120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 132c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas * Called when a menu item with a collapsible action view should collapse its action view. 13320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * 13420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param menu Menu containing the item to be collapsed 13520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param item Item to be collapsed 13620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return true if this presenter collapsed the action view, false otherwise. 13720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 138c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item); 13920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 14020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 14120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * Returns an ID for determining how to save/restore instance state. 14220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return a valid ID value. 14320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 144c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette int getId(); 14520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 14620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 14749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Returns a Parcelable describing the current state of the presenter. 14849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * It will be passed to the {@link #onRestoreInstanceState(Parcelable)} 14949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * method of the presenter sharing the same ID later. 15020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @return The saved instance state 15120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns */ 152c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette Parcelable onSaveInstanceState(); 15320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns 15420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns /** 15520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * Supplies the previously saved instance state to be restored. 15620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * @param state The previously saved instance state 157bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */ 158c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette void onRestoreInstanceState(Parcelable state); 159bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell} 160