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 com.android.tv.common.customization.CustomAction;
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /** An adapter of options that can accepts customization data. */
25 public abstract class CustomizableOptionsRowAdapter extends OptionsRowAdapter {
26     private final List<CustomAction> mCustomActions;
27 
CustomizableOptionsRowAdapter(Context context, List<CustomAction> customActions)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.
createBaseActions()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}.
executeBaseAction(int type)41     protected abstract void executeBaseAction(int type);
42 
43     @Override
createActions()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
executeAction(int type)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 
getCustomActions()77     protected List<CustomAction> getCustomActions() {
78         return mCustomActions;
79     }
80 }
81