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
19696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellimport android.content.Context;
2011ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powellimport android.os.Parcelable;
21696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellimport android.view.ViewGroup;
22696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
23696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell/**
24696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * A MenuPresenter is responsible for building views for a Menu object.
25696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell * It takes over some responsibility from the old style monolithic MenuBuilder class.
26696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell */
27696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powellpublic interface MenuPresenter {
28696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
29696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Called by menu implementation to notify another component of open/close events.
30696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
31696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public interface Callback {
32696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        /**
33696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * Called when a menu is closing.
34696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * @param menu
35696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * @param allMenusAreClosing
36696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         */
37696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing);
38696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
39696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        /**
40696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * Called when a submenu opens. Useful for notifying the application
41696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * of menu state so that it does not attempt to hide the action bar
42696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * while a submenu is open or similar.
43696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         *
44696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * @param subMenu Submenu currently being opened
45696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         * @return true if the Callback will handle presenting the submenu, false if
46696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         *         the presenter should attempt to do so.
47696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell         */
48696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell        public boolean onOpenSubMenu(MenuBuilder subMenu);
49696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    }
50696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
51696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
52696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Initialize this presenter for the given context and menu.
53696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * This method is called by MenuBuilder when a presenter is
54696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * added. See {@link MenuBuilder#addMenuPresenter(MenuPresenter)}
55696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     *
56696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param context Context for this presenter; used for view creation and resource management
57696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param menu Menu to host
58696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
59696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public void initForMenu(Context context, MenuBuilder menu);
60696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
61696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
62696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Retrieve a MenuView to display the menu specified in
633d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * {@link #initForMenu(Context, MenuBuilder)}.
64696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     *
65696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param root Intended parent of the MenuView.
66696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @return A freshly created MenuView.
67696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
68696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public MenuView getMenuView(ViewGroup root);
69696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
70696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
71696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Update the menu UI in response to a change. Called by
72696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * MenuBuilder during the normal course of operation.
73696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     *
74696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param cleared true if the menu was entirely cleared
75696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
76696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public void updateMenuView(boolean cleared);
77696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
78696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
79696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Set a callback object that will be notified of menu events
80696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * related to this specific presentation.
81696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param cb Callback that will be notified of future events
82696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
83696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public void setCallback(Callback cb);
84696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
85696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
86696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Called by Menu implementations to indicate that a submenu item
87696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * has been selected. An active Callback should be notified, and
88696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * if applicable the presenter should present the submenu.
89696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     *
90696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param subMenu SubMenu being opened
91696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @return true if the the event was handled, false otherwise.
92696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
93696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public boolean onSubMenuSelected(SubMenuBuilder subMenu);
94696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
95696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
96696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Called by Menu implementations to indicate that a menu or submenu is
97696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * closing. Presenter implementations should close the representation
98696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * of the menu indicated as necessary and notify a registered callback.
99696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     *
100696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param menu Menu or submenu that is closing.
101696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @param allMenusAreClosing True if all associated menus are closing.
102696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
103696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing);
104696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell
105696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    /**
106696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * Called by Menu implementations to flag items that will be shown as actions.
107696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     * @return true if this presenter changed the action status of any items.
108696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell     */
109696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell    public boolean flagActionItems();
1108d02deabac62c4a68a335a7b3141795466362b89Adam Powell
1118d02deabac62c4a68a335a7b3141795466362b89Adam Powell    /**
1128d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * Called when a menu item with a collapsable action view should expand its action view.
1138d02deabac62c4a68a335a7b3141795466362b89Adam Powell     *
1148d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @param menu Menu containing the item to be expanded
1158d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @param item Item to be expanded
1168d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @return true if this presenter expanded the action view, false otherwise.
1178d02deabac62c4a68a335a7b3141795466362b89Adam Powell     */
1188d02deabac62c4a68a335a7b3141795466362b89Adam Powell    public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item);
1198d02deabac62c4a68a335a7b3141795466362b89Adam Powell
1208d02deabac62c4a68a335a7b3141795466362b89Adam Powell    /**
1218d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * Called when a menu item with a collapsable action view should collapse its action view.
1228d02deabac62c4a68a335a7b3141795466362b89Adam Powell     *
1238d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @param menu Menu containing the item to be collapsed
1248d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @param item Item to be collapsed
1258d02deabac62c4a68a335a7b3141795466362b89Adam Powell     * @return true if this presenter collapsed the action view, false otherwise.
1268d02deabac62c4a68a335a7b3141795466362b89Adam Powell     */
1278d02deabac62c4a68a335a7b3141795466362b89Adam Powell    public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item);
12811ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell
12911ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    /**
13011ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * Returns an ID for determining how to save/restore instance state.
13111ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * @return a valid ID value.
13211ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     */
13311ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    public int getId();
13411ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell
13511ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    /**
13611ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * Returns a Parcelable describing the current state of the presenter.
13711ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * It will be passed to the {@link #onRestoreInstanceState(Parcelable)}
13811ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * method of the presenter sharing the same ID later.
13911ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * @return The saved instance state
14011ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     */
14111ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    public Parcelable onSaveInstanceState();
14211ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell
14311ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    /**
14411ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * Supplies the previously saved instance state to be restored.
14511ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     * @param state The previously saved instance state
14611ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell     */
14711ed1d6cae9214335c92ac38498a4e6c7d1c8324Adam Powell    public void onRestoreInstanceState(Parcelable state);
148696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell}
149