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.tv.menu;
18 
19 import android.content.Context;
20 import android.support.annotation.Nullable;
21 import android.text.TextUtils;
22 
23 import com.android.tv.MainActivity;
24 import com.android.tv.R;
25 import com.android.tv.common.customization.CustomAction;
26 import com.android.tv.common.customization.CustomizationManager;
27 import com.android.tv.menu.MenuRowFactory.Factory;
28 import com.android.tv.ui.TunableTvView;
29 
30 import com.google.auto.factory.AutoFactory;
31 import com.google.auto.factory.Provided;
32 
33 import java.util.List;
34 
35 /** A factory class to create menu rows. */
36 @AutoFactory(implementing = Factory.class)
37 public class MenuRowFactory {
38 
39     /** Factory for a {@link MenuRowFactory}. */
40     public interface Factory {
41         /** Creates a {@link MenuRowFactory} */
create(MainActivity mainActivity, TunableTvView tvView)42         MenuRowFactory create(MainActivity mainActivity, TunableTvView tvView);
43     }
44 
45     private final MainActivity mMainActivity;
46     private final TunableTvView mTvView;
47     private final CustomizationManager mCustomizationManager;
48     private final TvOptionsRowAdapter.Factory mTvOptionsRowAdapterFactory;
49 
50     /** A constructor. */
MenuRowFactory( MainActivity mainActivity, TunableTvView tvView, @Provided TvOptionsRowAdapter.Factory tvOptionsRowAdapterFactory)51     public MenuRowFactory(
52             MainActivity mainActivity,
53             TunableTvView tvView,
54             @Provided TvOptionsRowAdapter.Factory tvOptionsRowAdapterFactory) {
55         mMainActivity = mainActivity;
56         mTvView = tvView;
57         mCustomizationManager = new CustomizationManager(mainActivity);
58         mTvOptionsRowAdapterFactory = tvOptionsRowAdapterFactory;
59         mCustomizationManager.initialize();
60     }
61 
62     /** Creates an object corresponding to the given {@code key}. */
63     @Nullable
createMenuRow(Menu menu, Class<?> key)64     public MenuRow createMenuRow(Menu menu, Class<?> key) {
65         if (PlayControlsRow.class.equals(key)) {
66             return new PlayControlsRow(
67                     mMainActivity, mTvView, menu, mMainActivity.getTimeShiftManager());
68         } else if (ChannelsRow.class.equals(key)) {
69             return new ChannelsRow(mMainActivity, menu, mMainActivity.getProgramDataManager());
70         } else if (PartnerRow.class.equals(key)) {
71             List<CustomAction> customActions =
72                     mCustomizationManager.getCustomActions(CustomizationManager.ID_PARTNER_ROW);
73             String title = mCustomizationManager.getPartnerRowTitle();
74             if (customActions != null && !TextUtils.isEmpty(title)) {
75                 return new PartnerRow(mMainActivity, menu, title, customActions);
76             }
77             return null;
78         } else if (TvOptionsRow.class.equals(key)) {
79             return new TvOptionsRow(
80                     mMainActivity,
81                     menu,
82                     mCustomizationManager.getCustomActions(CustomizationManager.ID_OPTIONS_ROW),
83                     mTvOptionsRowAdapterFactory);
84         }
85         return null;
86     }
87 
88     /** A menu row which represents the TV options row. */
89     public static class TvOptionsRow extends ItemListRow {
90         /** The ID of the row. */
91         public static final String ID = TvOptionsRow.class.getName();
92 
TvOptionsRow( Context context, Menu menu, @Nullable List<CustomAction> customActions, TvOptionsRowAdapter.Factory tvOptionsRowAdapterFactory)93         private TvOptionsRow(
94                 Context context,
95                 Menu menu,
96                 @Nullable List<CustomAction> customActions,
97                 TvOptionsRowAdapter.Factory tvOptionsRowAdapterFactory) {
98             super(
99                     context,
100                     menu,
101                     R.string.menu_title_options,
102                     R.dimen.action_card_height,
103                     tvOptionsRowAdapterFactory.create(context, customActions));
104         }
105     }
106 
107     /** A menu row which represents the partner row. */
108     public static class PartnerRow extends ItemListRow {
PartnerRow( Context context, Menu menu, String title, List<CustomAction> customActions)109         private PartnerRow(
110                 Context context, Menu menu, String title, List<CustomAction> customActions) {
111             super(
112                     context,
113                     menu,
114                     title,
115                     R.dimen.action_card_height,
116                     new PartnerOptionsRowAdapter(context, customActions));
117         }
118     }
119 }
120