CustomizableOptionsRowAdapter.java revision 95961816a768da387f0b5523cf4363ace2044089
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 17package com.android.tv.menu; 18 19import android.content.Context; 20import com.android.tv.customization.CustomAction; 21import java.util.ArrayList; 22import java.util.List; 23 24/** An adapter of options that can accepts customization data. */ 25public abstract class CustomizableOptionsRowAdapter extends OptionsRowAdapter { 26 private final List<CustomAction> mCustomActions; 27 28 public CustomizableOptionsRowAdapter(Context context, List<CustomAction> customActions) { 29 super(context); 30 mCustomActions = customActions; 31 } 32 33 // Subclass should implement this and return list of {@link MenuAction}. 34 // Custom actions will be added at the first or the last position in addition. 35 // Note that {@link MenuAction} should have non-negative type 36 // because negative types are reserved for custom actions. 37 protected abstract List<MenuAction> createBaseActions(); 38 39 // Subclass should implement this to perform proper action 40 // for {@link MenuAction} with the given type returned by {@link createBaseActions}. 41 protected abstract void executeBaseAction(int type); 42 43 @Override 44 protected List<MenuAction> createActions() { 45 List<MenuAction> actions = new ArrayList<>(createBaseActions()); 46 47 if (mCustomActions != null) { 48 int position = 0; 49 for (int i = 0; i < mCustomActions.size(); i++) { 50 // Type of MenuAction should be unique in the Adapter. 51 int type = -(i + 1); 52 CustomAction customAction = mCustomActions.get(i); 53 MenuAction action = 54 new MenuAction( 55 customAction.getTitle(), type, customAction.getIconDrawable()); 56 57 if (customAction.isFront()) { 58 actions.add(position++, action); 59 } else { 60 actions.add(action); 61 } 62 } 63 } 64 return actions; 65 } 66 67 @Override 68 protected void executeAction(int type) { 69 if (type < 0) { 70 int position = -(type + 1); 71 getMainActivity().startActivitySafe(mCustomActions.get(position).getIntent()); 72 } else { 73 executeBaseAction(type); 74 } 75 } 76 77 protected List<CustomAction> getCustomActions() { 78 return mCustomActions; 79 } 80} 81