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 17 package com.android.deskclock.actionbarmenu; 18 19 import android.app.Activity; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 24 /** 25 * Factory that builds optional {@link MenuItemController} instances. 26 */ 27 public final class MenuItemControllerFactory { 28 29 private static final MenuItemControllerFactory INSTANCE = new MenuItemControllerFactory(); 30 getInstance()31 public static MenuItemControllerFactory getInstance() { 32 return INSTANCE; 33 } 34 35 private final List<MenuItemProvider> mMenuItemProviders; 36 MenuItemControllerFactory()37 private MenuItemControllerFactory() { 38 mMenuItemProviders = new ArrayList<>(); 39 } 40 addMenuItemProvider(MenuItemProvider provider)41 public MenuItemControllerFactory addMenuItemProvider(MenuItemProvider provider) { 42 mMenuItemProviders.add(provider); 43 return this; 44 } 45 buildMenuItemControllers(Activity activity)46 public MenuItemController[] buildMenuItemControllers(Activity activity) { 47 final int providerSize = mMenuItemProviders.size(); 48 final MenuItemController[] controllers = new MenuItemController[providerSize]; 49 for (int i = 0; i < providerSize; i++) { 50 controllers[i] = mMenuItemProviders.get(i).provide(activity); 51 } 52 return controllers; 53 } 54 } 55