1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package androidx.leanback.widget;
15 
16 import android.graphics.drawable.Drawable;
17 import android.text.TextUtils;
18 
19 import org.jspecify.annotations.NonNull;
20 import org.jspecify.annotations.Nullable;
21 
22 import java.util.ArrayList;
23 
24 /**
25  * An action contains one or two lines of text, an optional image and an optional id. It may also
26  * be invoked by one or more keycodes.
27  */
28 public class Action {
29 
30     /** Indicates that an id has not been set. */
31     public static final long NO_ID = -1;
32 
33     private long mId = NO_ID;
34     private Drawable mIcon;
35     private CharSequence mLabel1;
36     private CharSequence mLabel2;
37     private ArrayList<Integer> mKeyCodes = new ArrayList<>();
38 
39     /**
40      * Constructor for an Action.
41      *
42      * @param id The id of the Action.
43      */
Action(long id)44     public Action(long id) {
45         this(id, "");
46     }
47 
48     /**
49      * Constructor for an Action.
50      *
51      * @param id The id of the Action.
52      * @param label The label to display for the Action.
53      */
Action(long id, @Nullable CharSequence label)54     public Action(long id, @Nullable CharSequence label) {
55         this(id, label, null);
56     }
57 
58     /**
59      * Constructor for an Action.
60      *
61      * @param id The id of the Action.
62      * @param label1 The label to display on the first line of the Action.
63      * @param label2 The label to display on the second line of the Action.
64      */
Action(long id, @Nullable CharSequence label1, @Nullable CharSequence label2)65     public Action(long id, @Nullable CharSequence label1, @Nullable CharSequence label2) {
66         this(id, label1, label2, null);
67     }
68 
69     /**
70      * Constructor for an Action.
71      *
72      * @param id The id of the Action.
73      * @param label1 The label to display on the first line of the Action.
74      * @param label2 The label to display on the second line of the Action.
75      * @param icon The icon to display for the Action.
76      */
Action( long id, @Nullable CharSequence label1, @Nullable CharSequence label2, @Nullable Drawable icon )77     public Action(
78             long id,
79             @Nullable CharSequence label1,
80             @Nullable CharSequence label2,
81             @Nullable Drawable icon
82     ) {
83         setId(id);
84         setLabel1(label1);
85         setLabel2(label2);
86         setIcon(icon);
87     }
88 
89     /**
90      * Sets the id for this Action.
91      */
setId(long id)92     public final void setId(long id) {
93         mId = id;
94     }
95 
96     /**
97      * Returns the id for this Action.
98      */
getId()99     public final long getId() {
100         return mId;
101     }
102 
103     /**
104      * Sets the first line label for this Action.
105      */
setLabel1(@ullable CharSequence label)106     public final void setLabel1(@Nullable CharSequence label) {
107         mLabel1 = label;
108     }
109 
110     /**
111      * Returns the first line label for this Action.
112      */
getLabel1()113     public final @Nullable CharSequence getLabel1() {
114         return mLabel1;
115     }
116 
117     /**
118      * Sets the second line label for this Action.
119      */
setLabel2(@ullable CharSequence label)120     public final void setLabel2(@Nullable CharSequence label) {
121         mLabel2 = label;
122     }
123 
124     /**
125      * Returns the second line label for this Action.
126      */
getLabel2()127     public final @Nullable CharSequence getLabel2() {
128         return mLabel2;
129     }
130 
131     /**
132      * Sets the icon drawable for this Action.
133      */
setIcon(@ullable Drawable icon)134     public final void setIcon(@Nullable Drawable icon) {
135         mIcon = icon;
136     }
137 
138     /**
139      * Returns the icon drawable for this Action.
140      */
getIcon()141     public final @Nullable Drawable getIcon() {
142         return mIcon;
143     }
144 
145     /**
146      * Adds a keycode used to invoke this Action.
147      */
addKeyCode(int keyCode)148     public final void addKeyCode(int keyCode) {
149         mKeyCodes.add(keyCode);
150     }
151 
152     /**
153      * Removes a keycode used to invoke this Action.
154      */
removeKeyCode(int keyCode)155     public final void removeKeyCode(int keyCode) {
156         mKeyCodes.remove(keyCode);
157     }
158 
159     /**
160      * Returns true if the Action should respond to the given keycode.
161      */
respondsToKeyCode(int keyCode)162     public final boolean respondsToKeyCode(int keyCode) {
163         return mKeyCodes.contains(keyCode);
164     }
165 
166     @Override
toString()167     public @NonNull String toString() {
168         StringBuilder sb = new StringBuilder();
169         if (!TextUtils.isEmpty(mLabel1)) {
170             sb.append(mLabel1);
171         }
172         if (!TextUtils.isEmpty(mLabel2)) {
173             if (!TextUtils.isEmpty(mLabel1)) {
174                 sb.append(" ");
175             }
176             sb.append(mLabel2);
177         }
178         if (mIcon != null && sb.length() == 0) {
179             sb.append("(action icon)");
180         }
181         return sb.toString();
182     }
183 }
184