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 new Runnable() { 41 @Override 42 public void run() { 43 int resId = action.getActionNameResId(); 44 if (resId == 0) { 45 mTracker.sendMenuClicked(CUSTOM_ACTION_LABEL); 46 } else { 47 mTracker.sendMenuClicked(resId); 48 } 49 executeAction(action.getType()); 50 } 51 }); 52 } 53 }; 54 OptionsRowAdapter(Context context)55 public OptionsRowAdapter(Context context) { 56 super(context); 57 mTracker = TvSingletons.getSingletons(context).getTracker(); 58 } 59 60 /** Update action list and its content. */ 61 @Override update()62 public void update() { 63 if (mActionList == null) { 64 mActionList = createActions(); 65 setItemList(mActionList); 66 } else { 67 updateActions(); 68 } 69 } 70 71 @Override getLayoutResId(int viewType)72 protected int getLayoutResId(int viewType) { 73 return R.layout.menu_card_action; 74 } 75 createActions()76 protected abstract List<MenuAction> createActions(); 77 updateActions()78 protected abstract void updateActions(); 79 executeAction(int type)80 protected abstract void executeAction(int type); 81 82 /** 83 * Gets the action at the given position. Note that action at the position may differ from 84 * returned by {@link #createActions}. See {@link CustomizableOptionsRowAdapter} 85 */ getAction(int position)86 protected MenuAction getAction(int position) { 87 return mActionList.get(position); 88 } 89 90 @Override onBindViewHolder(MyViewHolder viewHolder, int position)91 public void onBindViewHolder(MyViewHolder viewHolder, int position) { 92 super.onBindViewHolder(viewHolder, position); 93 94 viewHolder.itemView.setTag(getItemList().get(position)); 95 viewHolder.itemView.setOnClickListener(mMenuActionOnClickListener); 96 } 97 98 @Override getItemViewType(int position)99 public int getItemViewType(int position) { 100 // This makes 1:1 mapping from MenuAction to ActionCardView. That is, an ActionCardView will 101 // not be used(recycled) by other type of MenuAction. So the selection state of the view can 102 // be preserved. 103 return mActionList.get(position).getType(); 104 } 105 } 106