Action.java revision 70c858a5ca5b7ed9862e2edfa43912faecf42f96
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 public Action(long id) { 33 this(id, ""); 34 } 35 36 public Action(long id, CharSequence label) { 37 this(id, label, null); 38 } 39 40 public Action(long id, CharSequence label1, CharSequence label2) { 41 this(id, label1, label2, null); 42 } 43 44 public Action(long id, CharSequence label1, CharSequence label2, Drawable icon) { 45 setId(id); 46 setLabel1(label1); 47 setLabel2(label2); 48 setIcon(icon); 49 } 50 51 /** 52 * Set id for this action. 53 */ 54 public final void setId(long id) { 55 mId = id; 56 } 57 58 /** 59 * Returns the id for this action. 60 */ 61 public final long getId() { 62 return mId; 63 } 64 65 /** 66 * Set the first line label for this action. 67 */ 68 public final void setLabel1(CharSequence label) { 69 mLabel1 = label; 70 } 71 72 /** 73 * Returns the first line label for this action. 74 */ 75 public final CharSequence getLabel1() { 76 return mLabel1; 77 } 78 79 /** 80 * Set the second line label for this action. 81 */ 82 public final void setLabel2(CharSequence label) { 83 mLabel2 = label; 84 } 85 86 /** 87 * Returns the second line label for this action. 88 */ 89 public final CharSequence getLabel2() { 90 return mLabel2; 91 } 92 93 /** 94 * Set the icon drawable for this action. 95 */ 96 public final void setIcon(Drawable icon) { 97 mIcon = icon; 98 } 99 100 /** 101 * Returns the icon drawable for this action. 102 */ 103 public final Drawable getIcon() { 104 return mIcon; 105 } 106 107 @Override 108 public String toString(){ 109 StringBuilder sb = new StringBuilder(); 110 if (!TextUtils.isEmpty(mLabel1)) { 111 sb.append(mLabel1); 112 } 113 if (!TextUtils.isEmpty(mLabel2)) { 114 if (!TextUtils.isEmpty(mLabel1)) { 115 sb.append(" "); 116 } 117 sb.append(mLabel2); 118 } 119 if (mIcon != null && sb.length() == 0) { 120 sb.append("(action icon)"); 121 } 122 return sb.toString(); 123 } 124} 125