AbstractMediaItemPresenter.java revision b6ea4fa686b06fdb91f10998bd1d4b14c14af0ce
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.util.Log;
21import android.util.TypedValue;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TextView;
26
27/**
28 * Abstract {@link Presenter} class for rendering media items in a playlist format.
29 * Media items in the playlist are arranged as a vertical list with each row holding each media's
30 * metadata which is provided by the user of this class.
31 * <p>
32 *     Subclasses must override {@link #onBindMediaViewHolder} to implement their media item model
33 *     data binding to each row view
34 * </p>
35 * <p>
36 *     {@link AbstractMediaListHeaderPresenter} can be used in conjunction with this presenter in
37 *     order to display a playlist with a header view..
38 * </p>
39 */
40public abstract class AbstractMediaItemPresenter extends RowPresenter {
41
42
43    private int mBackgroundColor = Color.TRANSPARENT;
44    private boolean mBackgroundColorSet;
45    private final Context mContext;
46
47    /**
48     * Constructor used for creating an abstract media item presenter of a given theme.
49     * @param context The context the user of this presenter is running in.
50     * @param mThemeResId The resource id of the desired theme used for styling of this presenter.
51     */
52    public AbstractMediaItemPresenter(Context context, int mThemeResId) {
53        mContext = new ContextThemeWrapper(context.getApplicationContext(), mThemeResId);
54        setHeaderPresenter(null);
55    }
56
57    /**
58     * Constructor used for creating an abstract media item presenter.
59     * The styling for this presenter is extracted from Context of parent in
60     * {@link #createRowViewHolder(ViewGroup)}.
61     */
62    public AbstractMediaItemPresenter() {
63        mContext = null;
64        setHeaderPresenter(null);
65    }
66
67    /**
68     * The ViewHolder for the {@link AbstractMediaItemPresenter}. It references different views
69     * that place different meta-data corresponding to a media item.
70     */
71    public static class ViewHolder extends RowPresenter.ViewHolder {
72
73        private final View mContainerView;
74        private final TextView mTrackNumberView;
75        private final TextView mTrackNameView;
76        private final TextView mTrackDurationView;
77
78        View.OnClickListener mOnClickListener = new View.OnClickListener() {
79            @Override
80            public void onClick(View v) {
81                if (getOnItemViewClickedListener() != null) {
82                    getOnItemViewClickedListener().onItemClicked(ViewHolder.this,
83                            ViewHolder.this.getRowObject(), ViewHolder.this,
84                            ViewHolder.this.getRowObject());
85                }
86            }
87        };
88
89        public ViewHolder(View view) {
90            super(view);
91            mContainerView  = view.findViewById(R.id.rowContainer);
92            mContainerView.setOnClickListener(mOnClickListener);
93            mTrackNumberView = (TextView) view.findViewById(R.id.trackNumber);
94            mTrackNameView = (TextView) view.findViewById(R.id.trackName);
95            mTrackDurationView = (TextView) view.findViewById(R.id.trackDuration);
96        }
97
98        /**
99         * @return The TextView responsible for rendering the track number
100         */
101        public TextView getTrackNumberView() {
102            return mTrackNumberView;
103        }
104
105        /**
106         * @return The TextView responsible for rendering the track name
107         */
108        public TextView getTrackNameView() {
109            return mTrackNameView;
110        }
111
112        /**
113         * @return The TextView responsible for rendering the track duration
114         */
115        public TextView getTrackDurationView() {
116            return mTrackDurationView;
117        }
118    }
119
120    @Override
121    protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
122        Context context = mContext != null ? mContext : parent.getContext();
123        View view = LayoutInflater.from(context).
124                inflate(R.layout.lb_row_media_item, parent, false);
125        ViewHolder vh = new ViewHolder(view);
126        if (mBackgroundColorSet) {
127            vh.mContainerView.setBackgroundColor(mBackgroundColor);
128        }
129        return new ViewHolder(view);
130    }
131
132    @Override
133    public boolean isUsingDefaultSelectEffect() {
134        return false;
135    }
136
137    @Override
138    protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) {
139        super.onBindRowViewHolder(vh, item);
140        onBindMediaViewHolder((ViewHolder) vh, item);
141    }
142
143    /**
144     * Sets the background color for the row views within the playlist.
145     * If this is not set, a default color, defaultBrandColor, from theme is used.
146     * This defaultBrandColor defaults to android:attr/colorPrimary on v21, if it's specified.
147     * @param color The ARGB color used to set as the media list background color.
148     */
149    public void setBackgroundColor(int color) {
150        mBackgroundColorSet = true;
151        mBackgroundColor = color;
152    }
153
154    /**
155     * Binds the media data model provided by the user to the {@link ViewHolder} provided by the
156     * {@link AbstractMediaItemPresenter}.
157     * The subclasses of this presenter can access and bind individual views for TrackNumber,
158     * TrackName, and TrackDuration, by calling {@link ViewHolder#getTrackNumberView},
159     * {@link ViewHolder#getTrackNameView}, and {@link ViewHolder#getTrackDurationView}, on the
160     * {@link ViewHolder} provided as the argument {@code vh} by this presenter.
161     *
162     * @param vh The ViewHolder for this {@link AbstractMediaItemPresenter}.
163     * @param item The media item data object being presented.
164     */
165    protected abstract void onBindMediaViewHolder(ViewHolder vh, Object item);
166
167}