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