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 * The base class for all rows.  A commonly used subclass is the {@link ListRow}.  Custom
20 * subclasses may define other types of rows.
21 */
22public class Row {
23
24    private static final int FLAG_ID_USE_MASK = 1;
25    private static final int FLAG_ID_USE_HEADER = 1;
26    private static final int FLAG_ID_USE_ID = 0;
27
28    private int mFlags = FLAG_ID_USE_HEADER;
29    private HeaderItem mHeaderItem;
30    private long mId = NO_ID;
31
32    /**
33     * Constructor for a Row.
34     *
35     * @param id The id of the row.
36     * @param headerItem The {@link HeaderItem} for this Row, or null if there
37     *        is no header.
38     */
39    public Row(long id, HeaderItem headerItem) {
40        setId(id);
41        setHeaderItem(headerItem);
42    }
43
44    /**
45     * Constructor for a Row.
46     *
47     * @param headerItem The {@link HeaderItem} for this Row, or null if there
48     *        is no header.
49     */
50    public Row(HeaderItem headerItem) {
51        setHeaderItem(headerItem);
52    }
53
54    /**
55     * Constructor for a Row.
56     */
57    public Row() {
58    }
59
60    /**
61     * Returns the {@link HeaderItem} that represents metadata for the row.
62     *
63     * @return The HeaderItem for this row, or null if unset.
64     */
65    public final HeaderItem getHeaderItem() {
66        return mHeaderItem;
67    }
68
69    /**
70     * Sets the {@link HeaderItem} that represents metadata for the row.
71     *
72     * @param headerItem The HeaderItem for this Row, or null if there is no
73     *        header.
74     */
75    public final void setHeaderItem(HeaderItem headerItem) {
76        mHeaderItem = headerItem;
77    }
78
79    /**
80     * Sets the id for this row.
81     *
82     * @param id The id of the row.
83     */
84    public final void setId(long id) {
85        mId = id;
86        setFlags(FLAG_ID_USE_ID, FLAG_ID_USE_MASK);
87    }
88
89    /**
90     * Returns a unique identifier for this row. This id can come from one of
91     * three places:
92     * <ul>
93     *   <li>If {@link #setId(long)} is ever called on this row, it will return
94     *   this id.
95     *   <li>If {@link #setId(long)} has not been called but the header item is
96     *   not null, the result of {@link HeaderItem#getId()} is returned.
97     *   <li>Otherwise {@link ObjectAdapter#NO_ID NO_ID} is returned.
98     * </ul>
99     */
100    public final long getId() {
101        if ( (mFlags & FLAG_ID_USE_MASK) == FLAG_ID_USE_HEADER) {
102            HeaderItem header = getHeaderItem();
103            if (header != null) {
104                return header.getId();
105            }
106            return NO_ID;
107        } else {
108            return mId;
109        }
110    }
111
112    final void setFlags(int flags, int mask) {
113        mFlags = (mFlags & ~mask) | (flags & mask);
114    }
115
116    final int getFlags() {
117        return mFlags;
118    }
119
120    /**
121     * Returns true if this Row can be rendered in a visible row view, false otherwise.  For example
122     * {@link ListRow} is rendered by {@link ListRowPresenter}.  {@link PageRow},
123     * {@link SectionRow}, {@link DividerRow} are rendered as invisible row views.
124     * @return True if this Row can be rendered in a visible row view, false otherwise.
125     */
126    public boolean isRenderedAsRowView() {
127        return true;
128    }
129}
130