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