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 17 package com.android.tv.data; 18 19 import android.content.Context; 20 import android.net.Uri; 21 import android.support.annotation.VisibleForTesting; 22 import android.support.media.tv.TvContractCompat; 23 import android.text.TextUtils; 24 import android.util.Pair; 25 import com.android.tv.TvSingletons; 26 import com.android.tv.data.api.Channel; 27 import com.android.tv.dvr.data.RecordedProgram; 28 import java.util.Objects; 29 30 /** A class to store the content of preview programs. */ 31 public class PreviewProgramContent { 32 @VisibleForTesting static final String PARAM_INPUT = "input"; 33 34 private long mId; 35 private long mPreviewChannelId; 36 private int mType; 37 private boolean mLive; 38 private String mTitle; 39 private String mDescription; 40 private Uri mPosterArtUri; 41 private Uri mIntentUri; 42 private Uri mPreviewVideoUri; 43 44 /** Create preview program content from {@link Program} */ 45 public static PreviewProgramContent createFromProgram( 46 Context context, long previewChannelId, Program program) { 47 Channel channel = 48 TvSingletons.getSingletons(context) 49 .getChannelDataManager() 50 .getChannel(program.getChannelId()); 51 return channel == null ? null : createFromProgram(previewChannelId, program, channel); 52 } 53 54 /** Create preview program content from {@link RecordedProgram} */ 55 public static PreviewProgramContent createFromRecordedProgram( 56 Context context, long previewChannelId, RecordedProgram recordedProgram) { 57 Channel channel = 58 TvSingletons.getSingletons(context) 59 .getChannelDataManager() 60 .getChannel(recordedProgram.getChannelId()); 61 return createFromRecordedProgram(previewChannelId, recordedProgram, channel); 62 } 63 64 @VisibleForTesting 65 static PreviewProgramContent createFromProgram( 66 long previewChannelId, Program program, Channel channel) { 67 String channelDisplayName = channel.getDisplayName(); 68 return new PreviewProgramContent.Builder() 69 .setId(program.getId()) 70 .setPreviewChannelId(previewChannelId) 71 .setType(TvContractCompat.PreviewPrograms.TYPE_CHANNEL) 72 .setLive(true) 73 .setTitle(program.getTitle()) 74 .setDescription( 75 !TextUtils.isEmpty(channelDisplayName) 76 ? channelDisplayName 77 : channel.getDisplayNumber()) 78 .setPosterArtUri(Uri.parse(program.getPosterArtUri())) 79 .setIntentUri(channel.getUri()) 80 .setPreviewVideoUri( 81 PreviewDataManager.PreviewDataUtils.addQueryParamToUri( 82 channel.getUri(), new Pair<>(PARAM_INPUT, channel.getInputId()))) 83 .build(); 84 } 85 86 @VisibleForTesting 87 static PreviewProgramContent createFromRecordedProgram( 88 long previewChannelId, RecordedProgram recordedProgram, Channel channel) { 89 String channelDisplayName = channel == null ? null : channel.getDisplayName(); 90 Uri recordedProgramUri = TvContractCompat.buildRecordedProgramUri(recordedProgram.getId()); 91 return new PreviewProgramContent.Builder() 92 .setId(recordedProgram.getId()) 93 .setPreviewChannelId(previewChannelId) 94 .setType(TvContractCompat.PreviewPrograms.TYPE_CLIP) 95 .setTitle(recordedProgram.getTitle()) 96 .setDescription(channelDisplayName != null ? channelDisplayName : "") 97 .setPosterArtUri(Uri.parse(recordedProgram.getPosterArtUri())) 98 .setIntentUri(recordedProgramUri) 99 .setPreviewVideoUri( 100 PreviewDataManager.PreviewDataUtils.addQueryParamToUri( 101 recordedProgramUri, 102 new Pair<>(PARAM_INPUT, recordedProgram.getInputId()))) 103 .build(); 104 } 105 106 private PreviewProgramContent() {} 107 108 @SuppressWarnings("ReferenceEquality") 109 public void copyFrom(PreviewProgramContent other) { 110 if (this == other) { 111 return; 112 } 113 mId = other.mId; 114 mPreviewChannelId = other.mPreviewChannelId; 115 mType = other.mType; 116 mLive = other.mLive; 117 mTitle = other.mTitle; 118 mDescription = other.mDescription; 119 mPosterArtUri = other.mPosterArtUri; 120 mIntentUri = other.mIntentUri; 121 mPreviewVideoUri = other.mPreviewVideoUri; 122 } 123 124 /** 125 * Returns the id, which is an identification. It usually comes from the original data which 126 * create the {@PreviewProgramContent}. 127 */ 128 public long getId() { 129 return mId; 130 } 131 132 /** Returns the preview channel id which the preview program belongs to. */ 133 public long getPreviewChannelId() { 134 return mPreviewChannelId; 135 } 136 137 /** Returns the type of the preview program. */ 138 public int getType() { 139 return mType; 140 } 141 142 /** Returns whether the preview program is live or not. */ 143 public boolean getLive() { 144 return mLive; 145 } 146 147 /** Returns the title of the preview program. */ 148 public String getTitle() { 149 return mTitle; 150 } 151 152 /** Returns the description of the preview program. */ 153 public String getDescription() { 154 return mDescription; 155 } 156 157 /** Returns the poster art uri of the preview program. */ 158 public Uri getPosterArtUri() { 159 return mPosterArtUri; 160 } 161 162 /** Returns the intent uri of the preview program. */ 163 public Uri getIntentUri() { 164 return mIntentUri; 165 } 166 167 /** Returns the preview video uri of the preview program. */ 168 public Uri getPreviewVideoUri() { 169 return mPreviewVideoUri; 170 } 171 172 @Override 173 public boolean equals(Object other) { 174 if (!(other instanceof PreviewProgramContent)) { 175 return false; 176 } 177 PreviewProgramContent previewProgramContent = (PreviewProgramContent) other; 178 return previewProgramContent.mId == mId 179 && previewProgramContent.mPreviewChannelId == mPreviewChannelId 180 && previewProgramContent.mType == mType 181 && previewProgramContent.mLive == mLive 182 && Objects.equals(previewProgramContent.mTitle, mTitle) 183 && Objects.equals(previewProgramContent.mDescription, mDescription) 184 && Objects.equals(previewProgramContent.mPosterArtUri, mPosterArtUri) 185 && Objects.equals(previewProgramContent.mIntentUri, mIntentUri) 186 && Objects.equals(previewProgramContent.mPreviewVideoUri, mPreviewVideoUri); 187 } 188 189 @Override 190 public int hashCode() { 191 return Objects.hash( 192 mId, 193 mPreviewChannelId, 194 mType, 195 mLive, 196 mTitle, 197 mDescription, 198 mPosterArtUri, 199 mIntentUri, 200 mPreviewVideoUri); 201 } 202 203 public static final class Builder { 204 private final PreviewProgramContent mPreviewProgramContent; 205 206 public Builder() { 207 mPreviewProgramContent = new PreviewProgramContent(); 208 } 209 210 public Builder setId(long id) { 211 mPreviewProgramContent.mId = id; 212 return this; 213 } 214 215 public Builder setPreviewChannelId(long previewChannelId) { 216 mPreviewProgramContent.mPreviewChannelId = previewChannelId; 217 return this; 218 } 219 220 public Builder setType(int type) { 221 mPreviewProgramContent.mType = type; 222 return this; 223 } 224 225 public Builder setLive(boolean live) { 226 mPreviewProgramContent.mLive = live; 227 return this; 228 } 229 230 public Builder setTitle(String title) { 231 mPreviewProgramContent.mTitle = title; 232 return this; 233 } 234 235 public Builder setDescription(String description) { 236 mPreviewProgramContent.mDescription = description; 237 return this; 238 } 239 240 public Builder setPosterArtUri(Uri posterArtUri) { 241 mPreviewProgramContent.mPosterArtUri = posterArtUri; 242 return this; 243 } 244 245 public Builder setIntentUri(Uri intentUri) { 246 mPreviewProgramContent.mIntentUri = intentUri; 247 return this; 248 } 249 250 public Builder setPreviewVideoUri(Uri previewVideoUri) { 251 mPreviewProgramContent.mPreviewVideoUri = previewVideoUri; 252 return this; 253 } 254 255 public PreviewProgramContent build() { 256 PreviewProgramContent previewProgramContent = new PreviewProgramContent(); 257 previewProgramContent.copyFrom(mPreviewProgramContent); 258 return previewProgramContent; 259 } 260 } 261 } 262