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.customization.CustomAction;
26 import com.android.tv.customization.TvCustomizationManager;
27 import com.android.tv.ui.TunableTvView;
28 
29 import java.util.List;
30 
31 /**
32  * A factory class to create menu rows.
33  */
34 public class MenuRowFactory {
35     private final MainActivity mMainActivity;
36     private final TunableTvView mTvView;
37     private final TvCustomizationManager mTvCustomizationManager;
38 
39     /**
40      * A constructor.
41      */
MenuRowFactory(MainActivity mainActivity, TunableTvView tvView)42     public MenuRowFactory(MainActivity mainActivity, TunableTvView tvView) {
43         mMainActivity = mainActivity;
44         mTvView = tvView;
45         mTvCustomizationManager = new TvCustomizationManager(mainActivity);
46         mTvCustomizationManager.initialize();
47     }
48 
49     /**
50      * Creates an object corresponding to the given {@code key}.
51      */
52     @Nullable
createMenuRow(Menu menu, Class<?> key)53     public MenuRow createMenuRow(Menu menu, Class<?> key) {
54         if (PlayControlsRow.class.equals(key)) {
55             return new PlayControlsRow(mMainActivity, mTvView, menu,
56                     mMainActivity.getTimeShiftManager());
57         } else if (ChannelsRow.class.equals(key)) {
58             return new ChannelsRow(mMainActivity, menu, mMainActivity.getProgramDataManager());
59         } else if (PartnerRow.class.equals(key)) {
60             List<CustomAction> customActions = mTvCustomizationManager.getCustomActions(
61                     TvCustomizationManager.ID_PARTNER_ROW);
62             String title = mTvCustomizationManager.getPartnerRowTitle();
63             if (customActions != null && !TextUtils.isEmpty(title)) {
64                 return new PartnerRow(mMainActivity, menu, title, customActions);
65             }
66             return null;
67         } else if (TvOptionsRow.class.equals(key)) {
68             return new TvOptionsRow(mMainActivity, menu, mTvCustomizationManager
69                     .getCustomActions(TvCustomizationManager.ID_OPTIONS_ROW));
70         } else if (PipOptionsRow.class.equals(key)) {
71             return new PipOptionsRow(mMainActivity, menu);
72         }
73         return null;
74     }
75 
76     /**
77      * A menu row which represents the TV options row.
78      */
79     public static class TvOptionsRow extends ItemListRow {
TvOptionsRow(Context context, Menu menu, List<CustomAction> customActions)80         private TvOptionsRow(Context context, Menu menu, List<CustomAction> customActions) {
81             super(context, menu, R.string.menu_title_options, R.dimen.action_card_height,
82                     new TvOptionsRowAdapter(context, customActions));
83         }
84 
85         @Override
onStreamInfoChanged()86         public void onStreamInfoChanged() {
87             if (getMenu().isActive()) {
88                 update();
89             }
90         }
91     }
92 
93     /**
94      * A menu row which represents the PIP options row.
95      */
96     public static class PipOptionsRow extends ItemListRow {
97         private final MainActivity mMainActivity;
98 
PipOptionsRow(Context context, Menu menu)99         private PipOptionsRow(Context context, Menu menu) {
100             super(context, menu, R.string.menu_title_pip_options, R.dimen.action_card_height,
101                     new PipOptionsRowAdapter(context));
102             mMainActivity = (MainActivity) context;
103         }
104 
105         @Override
isVisible()106         public boolean isVisible() {
107             // TODO: Remove the dependency on MainActivity.
108             return super.isVisible() && mMainActivity.isPipEnabled();
109         }
110     }
111 
112     /**
113      * A menu row which represents the partner row.
114      */
115     public static class PartnerRow extends ItemListRow {
PartnerRow(Context context, Menu menu, String title, List<CustomAction> customActions)116         private PartnerRow(Context context, Menu menu, String title,
117                            List<CustomAction> customActions) {
118             super(context, menu, title, R.dimen.action_card_height,
119                     new PartnerOptionsRowAdapter(context, customActions));
120         }
121     }
122 }
123