• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.tv.R;
22 import com.android.tv.TvSingletons;
23 import com.android.tv.analytics.Tracker;
24 import java.util.List;
25 
26 /*
27  * An adapter of options.
28  */
29 public abstract class OptionsRowAdapter extends ItemListRowView.ItemListAdapter<MenuAction> {
30     private static final String CUSTOM_ACTION_LABEL = "custom action";
31     protected final Tracker mTracker;
32     private List<MenuAction> mActionList;
33 
34     private final View.OnClickListener mMenuActionOnClickListener =
35             new View.OnClickListener() {
36                 @Override
37                 public void onClick(View view) {
38                     final MenuAction action = (MenuAction) view.getTag();
39                     view.post(
40                             () -> {
41                                 int resId = action.getActionNameResId();
42                                 if (resId == 0) {
43                                     mTracker.sendMenuClicked(CUSTOM_ACTION_LABEL);
44                                 } else {
45                                     mTracker.sendMenuClicked(resId);
46                                 }
47                                 executeAction(action.getType());
48                             });
49                 }
50             };
51 
OptionsRowAdapter(Context context)52     public OptionsRowAdapter(Context context) {
53         super(context);
54         mTracker = TvSingletons.getSingletons(context).getTracker();
55     }
56 
57     /** Update action list and its content. */
58     @Override
update()59     public void update() {
60         if (mActionList == null) {
61             mActionList = createActions();
62             setItemList(mActionList);
63         } else {
64             updateActions();
65         }
66     }
67 
68     @Override
getLayoutResId(int viewType)69     protected int getLayoutResId(int viewType) {
70         return R.layout.menu_card_action;
71     }
72 
createActions()73     protected abstract List<MenuAction> createActions();
74 
updateActions()75     protected abstract void updateActions();
76 
executeAction(int type)77     protected abstract void executeAction(int type);
78 
79     /**
80      * Gets the action at the given position. Note that action at the position may differ from
81      * returned by {@link #createActions}. See {@link CustomizableOptionsRowAdapter}
82      */
getAction(int position)83     protected MenuAction getAction(int position) {
84         return mActionList.get(position);
85     }
86 
87     @Override
onBindViewHolder(MyViewHolder viewHolder, int position)88     public void onBindViewHolder(MyViewHolder viewHolder, int position) {
89         super.onBindViewHolder(viewHolder, position);
90 
91         viewHolder.itemView.setTag(getItemList().get(position));
92         viewHolder.itemView.setOnClickListener(mMenuActionOnClickListener);
93     }
94 
95     @Override
getItemViewType(int position)96     public int getItemViewType(int position) {
97         // This makes 1:1 mapping from MenuAction to ActionCardView. That is, an ActionCardView will
98         // not be used(recycled) by other type of MenuAction. So the selection state of the view can
99         // be preserved.
100         return mActionList.get(position).getType();
101     }
102 }
103