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 static android.support.v17.leanback.widget.ObjectAdapter.NO_ID; 17 18/** 19 * A row in a RowsFragment. This is the base class for all rows. 20 * You will typically use a {@link ListRow}, but you may override this class 21 * for non-list Rows. 22 */ 23public class Row { 24 25 private static final int FLAG_ID_USE_MASK = 1; 26 private static final int FLAG_ID_USE_HEADER = 1; 27 private static final int FLAG_ID_USE_ID = 0; 28 29 private int mFlags = FLAG_ID_USE_HEADER; 30 private HeaderItem mHeaderItem; 31 private long mId = NO_ID; 32 33 /** 34 * Constructor for a Row. 35 * 36 * @param id The id of the row. 37 * @param headerItem The {@link HeaderItem} for this Row, or null if there 38 * is no header. 39 */ 40 public Row(long id, HeaderItem headerItem) { 41 setId(id); 42 setHeaderItem(headerItem); 43 } 44 45 /** 46 * Constructor for a Row. 47 * 48 * @param headerItem The {@link HeaderItem} for this Row, or null if there 49 * is no header. 50 */ 51 public Row(HeaderItem headerItem) { 52 setHeaderItem(headerItem); 53 } 54 55 /** 56 * Constructor for a Row. 57 */ 58 public Row() { 59 } 60 61 /** 62 * Get the {@link HeaderItem} that represents metadata for the row. 63 * 64 * @return The HeaderItem for this row, or null if unset. 65 */ 66 public final HeaderItem getHeaderItem() { 67 return mHeaderItem; 68 } 69 70 /** 71 * Set the {@link HeaderItem} that represents metadata for the row. 72 * 73 * @param headerItem The HeaderItem for this Row, or null if there is no 74 * header. 75 */ 76 public final void setHeaderItem(HeaderItem headerItem) { 77 mHeaderItem = headerItem; 78 } 79 80 /** 81 * Set the id for this row. 82 * 83 * @param id The id of the row. 84 */ 85 public final void setId(long id) { 86 mId = id; 87 setFlags(FLAG_ID_USE_ID, FLAG_ID_USE_MASK); 88 } 89 90 /** 91 * Returns a unique identifier for this row. This id can come from one of 92 * three places: 93 * <ul> 94 * <li>If {@link #setId(long)} is ever called on this row, it will return 95 * this id. 96 * <li>If {@link #setId(long)} has not been called but the header item is 97 * not null, the result of {@link HeaderItem#getId()} is returned. 98 * <li>Otherwise {@link ObjectAdapter#NO_ID NO_ID} is returned. 99 * </ul> 100 */ 101 public final long getId() { 102 if ( (mFlags & FLAG_ID_USE_MASK) == FLAG_ID_USE_HEADER) { 103 HeaderItem header = getHeaderItem(); 104 if (header != null) { 105 return header.getId(); 106 } 107 return NO_ID; 108 } else { 109 return mId; 110 } 111 } 112 113 final void setFlags(int flags, int mask) { 114 mFlags = (mFlags & ~mask) | (flags & mask); 115 } 116 117 final int getFlags() { 118 return mFlags; 119 } 120} 121