1 /*
2  * Copyright (C) 2015 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 package com.android.deskclock.actionbarmenu;
17 
18 import android.app.Activity;
19 import android.util.ArrayMap;
20 import android.view.Menu;
21 import android.view.MenuInflater;
22 import android.view.MenuItem;
23 
24 import com.android.deskclock.R;
25 
26 /**
27  * Activity scoped singleton that manages action bar menus. Each menu item is controlled by a
28  * {@link MenuItemController} instance.
29  * <p/>
30  * This class needs to be instantiated before or during activity's onCreate event.
31  */
32 public final class ActionBarMenuManager {
33     // A map of all menu item controllers, keyed by menu item id.
34     private final ArrayMap<Integer, MenuItemController> mControllers;
35 
ActionBarMenuManager(Activity activity)36     public ActionBarMenuManager(Activity activity) {
37         mControllers = new ArrayMap<>();
38     }
39 
40     /**
41      * Add one or more {@link MenuItemController} to the actionbar menu.
42      * <p/>
43      * This should be called before activity's onPrepareOptionsMenu event.
44      */
addMenuItemController(MenuItemController... menuItemControllers)45     public ActionBarMenuManager addMenuItemController(MenuItemController... menuItemControllers) {
46         if (menuItemControllers != null) {
47             for (MenuItemController controller : menuItemControllers) {
48                 mControllers.put(controller.getId(), controller);
49             }
50         }
51         return this;
52     }
53 
54     /**
55      * Inflates {@link Menu} for the activity.
56      * <p/>
57      * This method should be called during activity's onCreateOptionsMenu method.
58      */
createOptionsMenu(Menu menu, MenuInflater inflater)59     public void createOptionsMenu(Menu menu, MenuInflater inflater) {
60         if (menu.size() > 0) {
61             throw new IllegalStateException("Menu has already been inflated.");
62         }
63         inflater.inflate(R.menu.desk_clock_menu, menu);
64 
65         final int controllerSize = mControllers.size();
66         for (int i = 0; i < controllerSize; i++) {
67             final MenuItemController controller = mControllers.valueAt(i);
68             if (controller.isEnabled()) {
69                 controller.setInitialState(menu);
70             }
71         }
72     }
73 
74     /**
75      * Prepares the popup to displays all required menu items.
76      * <p/>
77      * This method should be called during activity's onPrepareOptionsMenu method.
78      */
prepareShowMenu(Menu menu)79     public void prepareShowMenu(Menu menu) {
80         final int menuSize = menu.size();
81         for (int i = 0; i < menuSize; i++) {
82             menu.getItem(i).setVisible(false);
83         }
84         final int controllerSize = mControllers.size();
85         for (int i = 0; i < controllerSize; i++) {
86             final MenuItemController controller = mControllers.valueAt(i);
87             if (controller.isEnabled()) {
88                 controller.showMenuItem(menu);
89             }
90         }
91     }
92 
93     /**
94      * Handles click action for a menu item.
95      * <p/>
96      * This method should be called during activity's onOptionsItemSelected method.
97      */
handleMenuItemClick(MenuItem item)98     public boolean handleMenuItemClick(MenuItem item) {
99         final int itemId = item.getItemId();
100         return mControllers.get(itemId).handleMenuItemClick(item);
101     }
102 }
103