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.view.View;
21 
22 import com.android.tv.R;
23 import com.android.tv.TvApplication;
24 import com.android.tv.TvOptionsManager;
25 import com.android.tv.TvOptionsManager.OptionChangedListener;
26 import com.android.tv.analytics.Tracker;
27 
28 import java.util.List;
29 
30 /*
31  * An adapter of options.
32  */
33 public abstract class OptionsRowAdapter extends ItemListRowView.ItemListAdapter<MenuAction> {
34     private static final String CUSTOM_ACTION_LABEL = "custom action";
35     protected final Tracker mTracker;
36     private List<MenuAction> mActionList;
37 
38     private final View.OnClickListener mMenuActionOnClickListener = new View.OnClickListener() {
39         @Override
40         public void onClick(View view) {
41             final MenuAction action = (MenuAction) view.getTag();
42             view.post(new Runnable() {
43                 @Override
44                 public void run() {
45                     int resId = action.getActionNameResId();
46                     if (resId == 0) {
47                         mTracker.sendMenuClicked(CUSTOM_ACTION_LABEL);
48                     } else {
49                         mTracker.sendMenuClicked(resId);
50                     }
51                     executeAction(action.getType());
52                 }
53             });
54         }
55     };
56 
OptionsRowAdapter(Context context)57     public OptionsRowAdapter(Context context) {
58         super(context);
59         mTracker = TvApplication.getSingletons(context).getTracker();
60     }
61 
62     /**
63      * Update action list and its content.
64      */
65     @Override
update()66     public void update() {
67         if (mActionList == null) {
68             mActionList = createActions();
69             updateActions();
70             setItemList(mActionList);
71         } else {
72             if (updateActions()) {
73                 setItemList(mActionList);
74             }
75         }
76     }
77 
78     @Override
getLayoutResId(int viewType)79     protected int getLayoutResId(int viewType) {
80         return R.layout.menu_card_action;
81     }
82 
createActions()83     protected abstract List<MenuAction> createActions();
updateActions()84     protected abstract boolean updateActions();
executeAction(int type)85     protected abstract void executeAction(int type);
86 
87     /**
88      * Gets the action at the given position.
89      * Note that action at the position may differ from returned by {@link #createActions}.
90      * See {@link CustomizableOptionsRowAdapter}
91      */
getAction(int position)92     protected MenuAction getAction(int position) {
93         return mActionList.get(position);
94     }
95 
96     /**
97      * Sets the action at the given position.
98      * Note that action at the position may differ from returned by {@link #createActions}.
99      * See {@link CustomizableOptionsRowAdapter}
100      */
setAction(int position, MenuAction action)101     protected void setAction(int position, MenuAction action) {
102         mActionList.set(position, action);
103     }
104 
105     /**
106      * Adds an action to the given position.
107      * Note that action at the position may differ from returned by {@link #createActions}.
108      * See {@link CustomizableOptionsRowAdapter}
109      */
addAction(int position, MenuAction action)110     protected void addAction(int position, MenuAction action) {
111         mActionList.add(position, action);
112     }
113 
114     /**
115      * Removes an action at the given position.
116      * Note that action at the position may differ from returned by {@link #createActions}.
117      * See {@link CustomizableOptionsRowAdapter}
118      */
removeAction(int position)119     protected void removeAction(int position) {
120         mActionList.remove(position);
121     }
122 
getActionSize()123     protected int getActionSize() {
124         return mActionList.size();
125     }
126 
127     @Override
onBindViewHolder(MyViewHolder viewHolder, int position)128     public void onBindViewHolder(MyViewHolder viewHolder, int position) {
129         super.onBindViewHolder(viewHolder, position);
130 
131         viewHolder.itemView.setTag(getItemList().get(position));
132         viewHolder.itemView.setOnClickListener(mMenuActionOnClickListener);
133     }
134 
135     @Override
getItemViewType(int position)136     public int getItemViewType(int position) {
137         // This makes 1:1 mapping from MenuAction to ActionCardView. That is, an ActionCardView will
138         // not be used(recycled) by other type of MenuAction. So the selection state of the view can
139         // be preserved.
140         return mActionList.get(position).getType();
141     }
142 
setOptionChangedListener(final MenuAction action)143     protected void setOptionChangedListener(final MenuAction action) {
144         TvOptionsManager om = getMainActivity().getTvOptionsManager();
145         om.setOptionChangedListener(action.getType(), new OptionChangedListener() {
146             @Override
147             public void onOptionChanged(String newOption) {
148                 setItemList(mActionList);
149             }
150         });
151     }
152 }
153