• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.graphics.drawable.Drawable;
21 import android.text.TextUtils;
22 
23 import com.android.tv.R;
24 import com.android.tv.TvOptionsManager;
25 import com.android.tv.TvOptionsManager.OptionType;
26 
27 /**
28  * A class to define possible actions from main menu.
29  */
30 public 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 SYSTEMWIDE_PIP_ACTION =
40             new MenuAction(R.string.options_item_pip, TvOptionsManager.OPTION_SYSTEMWIDE_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     public static final MenuAction DEV_ACTION =
49             new MenuAction(R.string.options_item_developer,
50                     TvOptionsManager.OPTION_DEVELOPER, R.drawable.ic_developer_mode_tv_white_48dp);
51     public static final MenuAction SETTINGS_ACTION =
52             new MenuAction(R.string.options_item_settings, TvOptionsManager.OPTION_SETTINGS,
53                     R.drawable.ic_settings);
54 
55     private final String mActionName;
56     private final int mActionNameResId;
57     @OptionType private final int mType;
58     private String mActionDescription;
59     private Drawable mDrawable;
60     private int mDrawableResId;
61     private boolean mEnabled = true;
62 
63     /**
64      * Sets the action description. Returns {@code trye} if the description is changed.
65      */
setActionDescription(MenuAction action, String actionDescription)66     public static boolean setActionDescription(MenuAction action, String actionDescription) {
67         String oldDescription = action.mActionDescription;
68         action.mActionDescription = actionDescription;
69         return !TextUtils.equals(action.mActionDescription, oldDescription);
70     }
71 
72     /**
73      * Enables or disables the action. Returns {@code true} if the value is changed.
74      */
setEnabled(MenuAction action, boolean enabled)75     public static boolean setEnabled(MenuAction action, boolean enabled) {
76         boolean changed = action.mEnabled != enabled;
77         action.mEnabled = enabled;
78         return changed;
79     }
80 
MenuAction(int actionNameResId, int type, int drawableResId)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 
MenuAction(String actionName, int type, Drawable drawable)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 
getActionName(Context context)97     public String getActionName(Context context) {
98         if (!TextUtils.isEmpty(mActionName)) {
99             return mActionName;
100         }
101         return context.getString(mActionNameResId);
102     }
103 
getActionDescription()104     public String getActionDescription() {
105         return mActionDescription;
106     }
107 
getType()108     @OptionType public int getType() {
109         return mType;
110     }
111 
112     /**
113      * Returns Drawable.
114      */
getDrawable(Context context)115     public Drawable getDrawable(Context context) {
116         if (mDrawable == null) {
117             mDrawable = context.getDrawable(mDrawableResId);
118         }
119         return mDrawable;
120     }
121 
isEnabled()122     public boolean isEnabled() {
123         return mEnabled;
124     }
125 
getActionNameResId()126     public int getActionNameResId() {
127         return mActionNameResId;
128     }
129 }
130