Row.java revision 8b068ddbbf22a246eab49ec25a2f7c3abfbdca51
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 the RowContainerFragment.  This is the basic class for all Rows.
20 * Developer usually overrides {@link ListRow}, but may override this class
21 * for non-list Row (e.g. a HtmlRow).
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    public Row(long id, HeaderItem headerItem) {
34        setId(id);
35        setHeaderItem(headerItem);
36    }
37
38    public Row(HeaderItem headerItem) {
39        setHeaderItem(headerItem);
40    }
41
42    public Row() {
43    }
44
45    /**
46     * Get optional {@link HeaderItem} that represents metadata for the row.
47     */
48    public final HeaderItem getHeaderItem() {
49        return mHeaderItem;
50    }
51
52    /**
53     * Set the {@link HeaderItem} that represents metadata for the row.
54     */
55    public final void setHeaderItem(HeaderItem headerItem) {
56        mHeaderItem = headerItem;
57    }
58
59    /**
60     * Set id for this row.
61     */
62    public final void setId(long id) {
63        mId = id;
64        setFlags(FLAG_ID_USE_ID, FLAG_ID_USE_MASK);
65    }
66
67    /**
68     * Returns a unique identifier for this row.  If {@link #setId(long)}
69     * is ever called, it will return this id; else returns {@link HeaderItem#getId()}
70     * if header item is null; otherwise returns NO_ID.
71     */
72    public final long getId() {
73        if ( (mFlags & FLAG_ID_USE_MASK) == FLAG_ID_USE_HEADER) {
74            HeaderItem header = getHeaderItem();
75            if (header != null) {
76                return header.getId();
77            }
78            return NO_ID;
79        } else {
80            return mId;
81        }
82    }
83
84    final void setFlags(int flags, int mask) {
85        mFlags = (mFlags & ~mask) | (flags & mask);
86    }
87
88    final int getFlags() {
89        return mFlags;
90    }
91}
92