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;
18
19import static android.support.v17.leanback.widget.ObjectAdapter.NO_ID;
20
21/**
22 * An action that can be shown on a details page. It contains one or two lines
23 * of text and an optional image.
24 */
25public class Action {
26
27    private long mId = NO_ID;
28    private Drawable mIcon;
29    private CharSequence mLabel1;
30    private CharSequence mLabel2;
31
32    /**
33     * Constructor for an Action.
34     *
35     * @param id The id of the Action.
36     */
37    public Action(long id) {
38        this(id, "");
39    }
40
41    /**
42     * Constructor for an Action.
43     *
44     * @param id The id of the Action.
45     * @param label The label to display for the Action.
46     */
47    public Action(long id, CharSequence label) {
48        this(id, label, null);
49    }
50
51    /**
52     * Constructor for an Action.
53     *
54     * @param id The id of the Action.
55     * @param label1 The label to display on the first line of the Action.
56     * @param label2 The label to display on the second line of the Action.
57     */
58    public Action(long id, CharSequence label1, CharSequence label2) {
59        this(id, label1, label2, null);
60    }
61
62    /**
63     * Constructor for an Action.
64     *
65     * @param id The id of the Action.
66     * @param label1 The label to display on the first line of the Action.
67     * @param label2 The label to display on the second line of the Action.
68     * @param icon The icon to display for the Action.
69     */
70    public Action(long id, CharSequence label1, CharSequence label2, Drawable icon) {
71        setId(id);
72        setLabel1(label1);
73        setLabel2(label2);
74        setIcon(icon);
75    }
76
77    /**
78     * Set id for this Action.
79     */
80    public final void setId(long id) {
81        mId = id;
82    }
83
84    /**
85     * Returns the id for this Action.
86     */
87    public final long getId() {
88        return mId;
89    }
90
91    /**
92     * Set the first line label for this Action.
93     */
94    public final void setLabel1(CharSequence label) {
95        mLabel1 = label;
96    }
97
98    /**
99     * Returns the first line label for this Action.
100     */
101    public final CharSequence getLabel1() {
102        return mLabel1;
103    }
104
105    /**
106     * Set the second line label for this Action.
107     */
108    public final void setLabel2(CharSequence label) {
109        mLabel2 = label;
110    }
111
112    /**
113     * Returns the second line label for this Action.
114     */
115    public final CharSequence getLabel2() {
116        return mLabel2;
117    }
118
119    /**
120     * Set the icon drawable for this Action.
121     */
122    public final void setIcon(Drawable icon) {
123        mIcon = icon;
124    }
125
126    /**
127     * Returns the icon drawable for this Action.
128     */
129    public final Drawable getIcon() {
130        return mIcon;
131    }
132
133    @Override
134    public String toString(){
135        StringBuilder sb = new StringBuilder();
136        if (!TextUtils.isEmpty(mLabel1)) {
137            sb.append(mLabel1);
138        }
139        if (!TextUtils.isEmpty(mLabel2)) {
140            if (!TextUtils.isEmpty(mLabel1)) {
141                sb.append(" ");
142            }
143            sb.append(mLabel2);
144        }
145        if (mIcon != null && sb.length() == 0) {
146            sb.append("(action icon)");
147        }
148        return sb.toString();
149    }
150}
151