Home | History | Annotate | Download | only in actionbarmenu
      1 /*
      2  * Copyright (C) 2016 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.deskclock.actionbarmenu;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.view.Menu;
     22 import android.view.MenuItem;
     23 
     24 import java.util.ArrayList;
     25 import java.util.Collections;
     26 import java.util.List;
     27 
     28 /**
     29  * Activity scoped singleton that manages action bar menus. Each menu item is controlled by a
     30  * {@link MenuItemController} instance.
     31  */
     32 public final class OptionsMenuManager {
     33 
     34     private final List<MenuItemController> mControllers = new ArrayList<>();
     35 
     36     /**
     37      * Add one or more {@link MenuItemController} to the actionbar menu.
     38      * <p/>
     39      * This should be called in {@link Activity#onCreate(Bundle)}.
     40      */
     41     public OptionsMenuManager addMenuItemController(MenuItemController... controllers) {
     42         Collections.addAll(mControllers, controllers);
     43         return this;
     44     }
     45 
     46     /**
     47      * Inflates {@link Menu} for the activity.
     48      * <p/>
     49      * This method should be called during {@link Activity#onCreateOptionsMenu(Menu)}.
     50      */
     51     public void onCreateOptionsMenu(Menu menu) {
     52         for (MenuItemController controller : mControllers) {
     53             controller.onCreateOptionsItem(menu);
     54         }
     55     }
     56 
     57     /**
     58      * Prepares the popup to displays all required menu items.
     59      * <p/>
     60      * This method should be called during {@link Activity#onPrepareOptionsMenu(Menu)} (Menu)}.
     61      */
     62     public void onPrepareOptionsMenu(Menu menu) {
     63         for (MenuItemController controller : mControllers) {
     64             final MenuItem menuItem = menu.findItem(controller.getId());
     65             if (menuItem != null) {
     66                 controller.onPrepareOptionsItem(menuItem);
     67             }
     68         }
     69     }
     70 
     71     /**
     72      * Handles click action for a menu item.
     73      * <p/>
     74      * This method should be called during {@link Activity#onOptionsItemSelected(MenuItem)}.
     75      */
     76     public boolean onOptionsItemSelected(MenuItem item) {
     77         final int itemId = item.getItemId();
     78         for (MenuItemController controller : mControllers) {
     79             if (controller.getId() == itemId
     80                     && controller.onOptionsItemSelected(item)) {
     81                 return true;
     82             }
     83         }
     84         return false;
     85     }
     86 }
     87