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