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