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