1/*
2 * Copyright (C) 2016 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.content.Context;
17import android.graphics.Color;
18import android.support.v17.leanback.R;
19import android.view.ContextThemeWrapper;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25/**
26 * Abstract presenter class for rendering the header for a list of media items in a playlist.
27 * The presenter creates a {@link ViewHolder} for the TextView holding the header text.
28 * <p>
29 *    Subclasses of this class must override {@link
30 *    #onBindMediaListHeaderViewHolder(ViewHolder, Object)} in order to bind their header text to
31 *    the media list header view.
32 * </p>
33 * <p>
34 * {@link AbstractMediaItemPresenter} can be used in conjunction with this presenter in order to
35 * display a playlist with a header view.
36 * </p>
37 */
38public abstract class AbstractMediaListHeaderPresenter extends RowPresenter{
39
40    private final Context mContext;
41    private int mBackgroundColor = Color.TRANSPARENT;
42    private boolean mBackgroundColorSet;
43
44    /**
45     * The ViewHolder for the {@link AbstractMediaListHeaderPresenter}. It references the TextView
46     * that places the header text provided by the data binder.
47     */
48    public static class ViewHolder extends RowPresenter.ViewHolder {
49
50        private final TextView mHeaderView;
51
52        public ViewHolder(View view) {
53            super(view);
54            mHeaderView = (TextView) view.findViewById(R.id.mediaListHeader);
55        }
56
57        /**
58         *
59         * @return the header {@link TextView} responsible for rendering the playlist header text.
60         */
61        public TextView getHeaderView() {
62            return mHeaderView;
63        }
64    }
65
66    /**
67     * Constructor used for creating an abstract media-list header presenter of a given theme.
68     * @param context The context the user of this presenter is running in.
69     * @param mThemeResId The resource id of the desired theme used for styling of this presenter.
70     */
71    public AbstractMediaListHeaderPresenter(Context context, int mThemeResId) {
72        mContext = new ContextThemeWrapper(context.getApplicationContext(), mThemeResId);
73        setHeaderPresenter(null);
74    }
75
76    /**
77     * Constructor used for creating an abstract media-list header presenter.
78     * The styling for this presenter is extracted from Context of parent in
79     * {@link #createRowViewHolder(ViewGroup)}.
80     */
81    public AbstractMediaListHeaderPresenter() {
82        mContext = null;
83        setHeaderPresenter(null);
84    }
85
86    @Override
87    public boolean isUsingDefaultSelectEffect() {
88        return false;
89    }
90
91    @Override
92    protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
93        Context context = (mContext != null) ? mContext : parent.getContext();
94        View view = LayoutInflater.from(context).inflate(R.layout.lb_media_list_header,
95                parent, false);
96        view.setFocusable(false);
97        view.setFocusableInTouchMode(false);
98        ViewHolder vh = new ViewHolder(view);
99        if (mBackgroundColorSet) {
100            vh.view.setBackgroundColor(mBackgroundColor);
101        }
102        return vh;
103    }
104
105    @Override
106    protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) {
107        super.onBindRowViewHolder(vh, item);
108        onBindMediaListHeaderViewHolder((ViewHolder) vh, item);
109    }
110
111    /**
112     * Sets the background color for the row views within the playlist.
113     * If this is not set, a default color, defaultBrandColor, from theme is used.
114     * This defaultBrandColor defaults to android:attr/colorPrimary on v21, if it's specified.
115     * @param color The ARGB color used to set as the header text background color.
116     */
117    public void setBackgroundColor(int color) {
118        mBackgroundColorSet = true;
119        mBackgroundColor = color;
120    }
121
122    /**
123     * Binds the playlist header data model provided by the user to the {@link ViewHolder}
124     * provided by the {@link AbstractMediaListHeaderPresenter}.
125     * The subclasses of this presenter can access and bind the text view corresponding to the
126     * header by calling {@link ViewHolder#getHeaderView()}, on the
127     * {@link ViewHolder} provided as the argument {@code vh} by this presenter.
128     *
129     * @param vh The ViewHolder for this {@link AbstractMediaListHeaderPresenter}.
130     * @param item The header data object being presented.
131     */
132    protected abstract void onBindMediaListHeaderViewHolder(ViewHolder vh, Object item);
133
134}
135