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