Song.java revision 86163d539e1aeb9ae7612def7aaf7dbfc5b88f28
1/*
2 * Copyright (C) 2015 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 *
14 */
15
16package android.support.v17.leanback.supportleanbackshowcase.cards.models;
17
18import android.content.Context;
19import android.support.v17.leanback.supportleanbackshowcase.R;
20import android.support.v17.leanback.widget.Row;
21import android.support.v17.leanback.widget.RowPresenter;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TextView;
26
27import com.google.gson.annotations.SerializedName;
28
29public class Song extends Row {
30
31    @SerializedName("title") private String mTitle = "";
32    @SerializedName("description") private String mDescription = "";
33    @SerializedName("text") private String mText = "";
34    @SerializedName("image") private String mImage = null;
35    @SerializedName("file") private String mFile = null;
36    @SerializedName("duration") private String mDuration = null;
37    @SerializedName("number") private int mNumber = 0;
38
39
40    public String getDuration() {
41        return mDuration;
42    }
43
44    public int getNumber() {
45        return mNumber;
46    }
47
48    public String getText() {
49        return mText;
50    }
51
52    public String getDescription() {
53        return mDescription;
54    }
55
56    public String getTitle() {
57        return mTitle;
58    }
59
60    public int getFileResource(Context context) {
61        return context.getResources()
62                      .getIdentifier(mFile, "raw", context.getPackageName());
63    }
64
65    public int getImageResource(Context context) {
66        return context.getResources()
67                      .getIdentifier(mImage, "drawable", context.getPackageName());
68    }
69
70    public interface OnSongRowClickListener {
71
72        void onSongRowClicked(Song song);
73
74    }
75
76    public static class Presenter extends RowPresenter implements View.OnClickListener {
77
78        private final Context mContext;
79        private OnSongRowClickListener mClickListener;
80
81
82        public Presenter(Context context) {
83            mContext = context;
84            setHeaderPresenter(null);
85        }
86
87        public void setOnClickListener(OnSongRowClickListener listener) {
88            mClickListener = listener;
89        }
90
91        @Override protected ViewHolder createRowViewHolder(ViewGroup parent) {
92            View view = LayoutInflater.from(mContext).inflate(R.layout.row_song, parent, false);
93            view.findViewById(R.id.rowContainer).setOnClickListener(this);
94            return new ViewHolder(view);
95        }
96
97        @Override public boolean isUsingDefaultSelectEffect() {
98            return false;
99        }
100
101        @Override protected void onBindRowViewHolder(ViewHolder vh, Object item) {
102            super.onBindRowViewHolder(vh, item);
103            Song song = (Song) item;
104            ((TextView) vh.view.findViewById(R.id.trackNumber)).setText("" + song.getNumber());
105            ((TextView) vh.view.findViewById(R.id.trackDuration)).setText(song.getDuration());
106            String text = song.getTitle() + " / " + song.getDescription();
107            ((TextView) vh.view.findViewById(R.id.trackName)).setText(text);
108            vh.view.findViewById(R.id.rowContainer).setTag(item);
109        }
110
111        @Override public void onClick(View v) {
112            if (mClickListener == null) return;
113            mClickListener.onSongRowClicked((Song) v.getTag());
114        }
115    }
116}
117