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 android.graphics.drawable.Drawable;
21import android.text.TextUtils;
22
23import com.android.tv.MainActivity;
24import com.android.tv.R;
25import com.android.tv.TvOptionsManager;
26
27/**
28 * A class to define possible actions from main menu.
29 */
30public class MenuAction {
31    // Actions in the TV option row.
32    public static final MenuAction SELECT_CLOSED_CAPTION_ACTION =
33            new MenuAction(R.string.options_item_closed_caption,
34                    TvOptionsManager.OPTION_CLOSED_CAPTIONS,
35                    R.drawable.ic_tvoption_cc);
36    public static final MenuAction SELECT_DISPLAY_MODE_ACTION =
37            new MenuAction(R.string.options_item_display_mode, TvOptionsManager.OPTION_DISPLAY_MODE,
38                    R.drawable.ic_tvoption_aspect);
39    public static final MenuAction PIP_IN_APP_ACTION =
40            new MenuAction(R.string.options_item_pip, TvOptionsManager.OPTION_IN_APP_PIP,
41                    R.drawable.ic_tvoption_pip);
42    public static final MenuAction SYSTEMWIDE_PIP_ACTION =
43            new MenuAction(R.string.options_item_pip, TvOptionsManager.OPTION_SYSTEMWIDE_PIP,
44                    R.drawable.ic_tvoption_pip);
45    public static final MenuAction SELECT_AUDIO_LANGUAGE_ACTION =
46            new MenuAction(R.string.options_item_multi_audio, TvOptionsManager.OPTION_MULTI_AUDIO,
47                    R.drawable.ic_tvoption_multi_track);
48    public static final MenuAction MORE_CHANNELS_ACTION =
49            new MenuAction(R.string.options_item_more_channels,
50                    TvOptionsManager.OPTION_MORE_CHANNELS, R.drawable.ic_store);
51    // TODO: Change the icon.
52    public static final MenuAction SETTINGS_ACTION =
53            new MenuAction(R.string.options_item_settings, TvOptionsManager.OPTION_SETTINGS,
54                    R.drawable.ic_settings);
55    // Actions in the PIP option row.
56    public static final MenuAction PIP_SELECT_INPUT_ACTION =
57            new MenuAction(R.string.pip_options_item_source, TvOptionsManager.OPTION_PIP_INPUT,
58                    R.drawable.ic_pip_option_input);
59    public static final MenuAction PIP_SWAP_ACTION =
60            new MenuAction(R.string.pip_options_item_swap, TvOptionsManager.OPTION_PIP_SWAP,
61                    R.drawable.ic_pip_option_swap);
62    public static final MenuAction PIP_SOUND_ACTION =
63            new MenuAction(R.string.pip_options_item_sound, TvOptionsManager.OPTION_PIP_SOUND,
64                    R.drawable.ic_pip_option_swap_audio);
65    public static final MenuAction PIP_LAYOUT_ACTION =
66            new MenuAction(R.string.pip_options_item_layout, TvOptionsManager.OPTION_PIP_LAYOUT,
67                    R.drawable.ic_pip_option_layout1);
68    public static final MenuAction PIP_SIZE_ACTION =
69            new MenuAction(R.string.pip_options_item_size, TvOptionsManager.OPTION_PIP_SIZE,
70                    R.drawable.ic_pip_option_size);
71
72    private final String mActionName;
73    private final int mActionNameResId;
74    private final int mType;
75    private Drawable mDrawable;
76    private int mDrawableResId;
77    private boolean mEnabled = true;
78
79    public MenuAction(int actionNameResId, int type, int drawableResId) {
80        mActionName = null;
81        mActionNameResId = actionNameResId;
82        mType = type;
83        mDrawable = null;
84        mDrawableResId = drawableResId;
85    }
86
87    public MenuAction(String actionName, int type, Drawable drawable) {
88        mActionName = actionName;
89        mActionNameResId = 0;
90        mType = type;
91        mDrawable = drawable;
92        mDrawableResId = 0;
93    }
94
95    public String getActionName(Context context) {
96        if (!TextUtils.isEmpty(mActionName)) {
97            return mActionName;
98        }
99        return context.getString(mActionNameResId);
100    }
101
102    public String getActionDescription(Context context) {
103        return ((MainActivity) context).getTvOptionsManager().getOptionString(mType);
104    }
105
106    public int getType() {
107        return mType;
108    }
109
110    /**
111     * Returns Drawable.
112     */
113    public Drawable getDrawable(Context context) {
114        if (mDrawable == null) {
115            mDrawable = context.getDrawable(mDrawableResId);
116        }
117        return mDrawable;
118    }
119
120    /**
121     * Sets drawable resource id.
122     *
123     * @return {@code true} if drawable is changed.
124     */
125    public boolean setDrawableResId(int resId) {
126        if (mDrawableResId == resId) {
127            return false;
128        }
129        mDrawable = null;
130        mDrawableResId = resId;
131        return true;
132    }
133
134    public boolean isEnabled() {
135        return mEnabled;
136    }
137
138    public void setEnabled(boolean enabled) {
139        mEnabled = enabled;
140    }
141
142    public int getActionNameResId() {
143        return mActionNameResId;
144    }
145}
146