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 */
14package android.support.v17.leanback.widget;
15
16import android.graphics.drawable.Drawable;
17import android.text.TextUtils;
18import android.view.KeyEvent;
19
20import java.util.ArrayList;
21
22/**
23 * An action contains one or two lines of text, an optional image and an optional id. It may also
24 * be invoked by one or more keycodes.
25 */
26public class Action {
27
28    /** Indicates that an id has not been set. */
29    public static final long NO_ID = -1;
30
31    private long mId = NO_ID;
32    private Drawable mIcon;
33    private CharSequence mLabel1;
34    private CharSequence mLabel2;
35    private ArrayList mKeyCodes = new ArrayList();
36
37    /**
38     * Constructor for an Action.
39     *
40     * @param id The id of the Action.
41     */
42    public Action(long id) {
43        this(id, "");
44    }
45
46    /**
47     * Constructor for an Action.
48     *
49     * @param id The id of the Action.
50     * @param label The label to display for the Action.
51     */
52    public Action(long id, CharSequence label) {
53        this(id, label, null);
54    }
55
56    /**
57     * Constructor for an Action.
58     *
59     * @param id The id of the Action.
60     * @param label1 The label to display on the first line of the Action.
61     * @param label2 The label to display on the second line of the Action.
62     */
63    public Action(long id, CharSequence label1, CharSequence label2) {
64        this(id, label1, label2, null);
65    }
66
67    /**
68     * Constructor for an Action.
69     *
70     * @param id The id of the Action.
71     * @param label1 The label to display on the first line of the Action.
72     * @param label2 The label to display on the second line of the Action.
73     * @param icon The icon to display for the Action.
74     */
75    public Action(long id, CharSequence label1, CharSequence label2, Drawable icon) {
76        setId(id);
77        setLabel1(label1);
78        setLabel2(label2);
79        setIcon(icon);
80    }
81
82    /**
83     * Sets the id for this Action.
84     */
85    public final void setId(long id) {
86        mId = id;
87    }
88
89    /**
90     * Returns the id for this Action.
91     */
92    public final long getId() {
93        return mId;
94    }
95
96    /**
97     * Sets the first line label for this Action.
98     */
99    public final void setLabel1(CharSequence label) {
100        mLabel1 = label;
101    }
102
103    /**
104     * Returns the first line label for this Action.
105     */
106    public final CharSequence getLabel1() {
107        return mLabel1;
108    }
109
110    /**
111     * Sets the second line label for this Action.
112     */
113    public final void setLabel2(CharSequence label) {
114        mLabel2 = label;
115    }
116
117    /**
118     * Returns the second line label for this Action.
119     */
120    public final CharSequence getLabel2() {
121        return mLabel2;
122    }
123
124    /**
125     * Sets the icon drawable for this Action.
126     */
127    public final void setIcon(Drawable icon) {
128        mIcon = icon;
129    }
130
131    /**
132     * Returns the icon drawable for this Action.
133     */
134    public final Drawable getIcon() {
135        return mIcon;
136    }
137
138    /**
139     * Adds a keycode used to invoke this Action.
140     */
141    public final void addKeyCode(int keyCode) {
142        mKeyCodes.add(keyCode);
143    }
144
145    /**
146     * Removes a keycode used to invoke this Action.
147     */
148    public final void removeKeyCode(int keyCode) {
149        mKeyCodes.remove(keyCode);
150    }
151
152    /**
153     * Returns true if the Action should respond to the given keycode.
154     */
155    public final boolean respondsToKeyCode(int keyCode) {
156        return mKeyCodes.contains(keyCode);
157    }
158
159    @Override
160    public String toString(){
161        StringBuilder sb = new StringBuilder();
162        if (!TextUtils.isEmpty(mLabel1)) {
163            sb.append(mLabel1);
164        }
165        if (!TextUtils.isEmpty(mLabel2)) {
166            if (!TextUtils.isEmpty(mLabel1)) {
167                sb.append(" ");
168            }
169            sb.append(mLabel2);
170        }
171        if (mIcon != null && sb.length() == 0) {
172            sb.append("(action icon)");
173        }
174        return sb.toString();
175    }
176}
177