PreviewProgramContent.java revision 38fef3bf253578f518d1bc727da4afb263194398
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tv.data;
18
19import android.content.Context;
20import android.media.tv.TvContract;
21import android.net.Uri;
22import android.text.TextUtils;
23import android.util.Pair;
24import com.android.tv.TvApplication;
25import com.android.tv.dvr.data.RecordedProgram;
26import java.util.Objects;
27
28/** A class to store the content of preview programs. */
29public class PreviewProgramContent {
30    private static final String PARAM_INPUT = "input";
31
32    private long mId;
33    private long mPreviewChannelId;
34    private int mType;
35    private boolean mLive;
36    private String mTitle;
37    private String mDescription;
38    private Uri mPosterArtUri;
39    private Uri mIntentUri;
40    private Uri mPreviewVideoUri;
41
42    /** Create preview program content from {@link Program} */
43    public static PreviewProgramContent createFromProgram(
44            Context context, long previewChannelId, Program program) {
45        Channel channel =
46                TvApplication.getSingletons(context)
47                        .getChannelDataManager()
48                        .getChannel(program.getChannelId());
49        if (channel == null) {
50            return null;
51        }
52        String channelDisplayName = channel.getDisplayName();
53        return new PreviewProgramContent.Builder()
54                .setId(program.getId())
55                .setPreviewChannelId(previewChannelId)
56                .setType(TvContract.PreviewPrograms.TYPE_CHANNEL)
57                .setLive(true)
58                .setTitle(program.getTitle())
59                .setDescription(
60                        !TextUtils.isEmpty(channelDisplayName)
61                                ? channelDisplayName
62                                : channel.getDisplayNumber())
63                .setPosterArtUri(Uri.parse(program.getPosterArtUri()))
64                .setIntentUri(channel.getUri())
65                .setPreviewVideoUri(
66                        PreviewDataManager.PreviewDataUtils.addQueryParamToUri(
67                                channel.getUri(), new Pair<>(PARAM_INPUT, channel.getInputId())))
68                .build();
69    }
70
71    /** Create preview program content from {@link RecordedProgram} */
72    public static PreviewProgramContent createFromRecordedProgram(
73            Context context, long previewChannelId, RecordedProgram recordedProgram) {
74        Channel channel =
75                TvApplication.getSingletons(context)
76                        .getChannelDataManager()
77                        .getChannel(recordedProgram.getChannelId());
78        String channelDisplayName = null;
79        if (channel != null) {
80            channelDisplayName = channel.getDisplayName();
81        }
82        Uri recordedProgramUri = TvContract.buildRecordedProgramUri(recordedProgram.getId());
83        return new PreviewProgramContent.Builder()
84                .setId(recordedProgram.getId())
85                .setPreviewChannelId(previewChannelId)
86                .setType(TvContract.PreviewPrograms.TYPE_CLIP)
87                .setTitle(recordedProgram.getTitle())
88                .setDescription(channelDisplayName != null ? channelDisplayName : "")
89                .setPosterArtUri(Uri.parse(recordedProgram.getPosterArtUri()))
90                .setIntentUri(recordedProgramUri)
91                .setPreviewVideoUri(
92                        PreviewDataManager.PreviewDataUtils.addQueryParamToUri(
93                                recordedProgramUri,
94                                new Pair<>(PARAM_INPUT, recordedProgram.getInputId())))
95                .build();
96    }
97
98    private PreviewProgramContent() {}
99
100    public void copyFrom(PreviewProgramContent other) {
101        if (this == other) {
102            return;
103        }
104        mId = other.mId;
105        mPreviewChannelId = other.mPreviewChannelId;
106        mType = other.mType;
107        mLive = other.mLive;
108        mTitle = other.mTitle;
109        mDescription = other.mDescription;
110        mPosterArtUri = other.mPosterArtUri;
111        mIntentUri = other.mIntentUri;
112        mPreviewVideoUri = other.mPreviewVideoUri;
113    }
114
115    /**
116     * Returns the id, which is an identification. It usually comes from the original data which
117     * create the {@PreviewProgramContent}.
118     */
119    public long getId() {
120        return mId;
121    }
122
123    /** Returns the preview channel id which the preview program belongs to. */
124    public long getPreviewChannelId() {
125        return mPreviewChannelId;
126    }
127
128    /** Returns the type of the preview program. */
129    public int getType() {
130        return mType;
131    }
132
133    /** Returns whether the preview program is live or not. */
134    public boolean getLive() {
135        return mLive;
136    }
137
138    /** Returns the title of the preview program. */
139    public String getTitle() {
140        return mTitle;
141    }
142
143    /** Returns the description of the preview program. */
144    public String getDescription() {
145        return mDescription;
146    }
147
148    /** Returns the poster art uri of the preview program. */
149    public Uri getPosterArtUri() {
150        return mPosterArtUri;
151    }
152
153    /** Returns the intent uri of the preview program. */
154    public Uri getIntentUri() {
155        return mIntentUri;
156    }
157
158    /** Returns the preview video uri of the preview program. */
159    public Uri getPreviewVideoUri() {
160        return mPreviewVideoUri;
161    }
162
163    @Override
164    public boolean equals(Object other) {
165        if (!(other instanceof PreviewProgramContent)) {
166            return false;
167        }
168        PreviewProgramContent previewProgramContent = (PreviewProgramContent) other;
169        return previewProgramContent.mId == mId
170                && previewProgramContent.mPreviewChannelId == mPreviewChannelId
171                && previewProgramContent.mType == mType
172                && previewProgramContent.mLive == mLive
173                && Objects.equals(previewProgramContent.mTitle, mTitle)
174                && Objects.equals(previewProgramContent.mDescription, mDescription)
175                && Objects.equals(previewProgramContent.mPosterArtUri, mPosterArtUri)
176                && Objects.equals(previewProgramContent.mIntentUri, mIntentUri)
177                && Objects.equals(previewProgramContent.mPreviewVideoUri, mPreviewVideoUri);
178    }
179
180    @Override
181    public int hashCode() {
182        return Objects.hash(
183                mId,
184                mPreviewChannelId,
185                mType,
186                mLive,
187                mTitle,
188                mDescription,
189                mPosterArtUri,
190                mIntentUri,
191                mPreviewVideoUri);
192    }
193
194    public static final class Builder {
195        private final PreviewProgramContent mPreviewProgramContent;
196
197        public Builder() {
198            mPreviewProgramContent = new PreviewProgramContent();
199        }
200
201        public Builder setId(long id) {
202            mPreviewProgramContent.mId = id;
203            return this;
204        }
205
206        public Builder setPreviewChannelId(long previewChannelId) {
207            mPreviewProgramContent.mPreviewChannelId = previewChannelId;
208            return this;
209        }
210
211        public Builder setType(int type) {
212            mPreviewProgramContent.mType = type;
213            return this;
214        }
215
216        public Builder setLive(boolean live) {
217            mPreviewProgramContent.mLive = live;
218            return this;
219        }
220
221        public Builder setTitle(String title) {
222            mPreviewProgramContent.mTitle = title;
223            return this;
224        }
225
226        public Builder setDescription(String description) {
227            mPreviewProgramContent.mDescription = description;
228            return this;
229        }
230
231        public Builder setPosterArtUri(Uri posterArtUri) {
232            mPreviewProgramContent.mPosterArtUri = posterArtUri;
233            return this;
234        }
235
236        public Builder setIntentUri(Uri intentUri) {
237            mPreviewProgramContent.mIntentUri = intentUri;
238            return this;
239        }
240
241        public Builder setPreviewVideoUri(Uri previewVideoUri) {
242            mPreviewProgramContent.mPreviewVideoUri = previewVideoUri;
243            return this;
244        }
245
246        public PreviewProgramContent build() {
247            PreviewProgramContent previewProgramContent = new PreviewProgramContent();
248            previewProgramContent.copyFrom(mPreviewProgramContent);
249            return previewProgramContent;
250        }
251    }
252}
253