1 /* 2 * Copyright (C) 2015 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.recommendation; 18 19 import android.content.Context; 20 import android.support.annotation.GuardedBy; 21 import android.support.annotation.VisibleForTesting; 22 23 import com.android.tv.TvSingletons; 24 import com.android.tv.data.ProgramDataManager; 25 import com.android.tv.data.api.Channel; 26 import com.android.tv.data.api.Program; 27 28 import java.util.ArrayDeque; 29 import java.util.Deque; 30 31 public class ChannelRecord { 32 // TODO: decide the value for max history size. 33 @VisibleForTesting static final int MAX_HISTORY_SIZE = 100; 34 private final Context mContext; 35 36 @GuardedBy("this") 37 private final Deque<WatchedProgram> mWatchHistory; 38 39 private Program mCurrentProgram; 40 private Channel mChannel; 41 private long mTotalWatchDurationMs; 42 private boolean mInputRemoved; 43 ChannelRecord(Context context, Channel channel, boolean inputRemoved)44 public ChannelRecord(Context context, Channel channel, boolean inputRemoved) { 45 mContext = context; 46 mChannel = channel; 47 mWatchHistory = new ArrayDeque<>(); 48 mInputRemoved = inputRemoved; 49 } 50 getChannel()51 public Channel getChannel() { 52 return mChannel; 53 } 54 setChannel(Channel channel, boolean inputRemoved)55 public void setChannel(Channel channel, boolean inputRemoved) { 56 mChannel = channel; 57 mInputRemoved = inputRemoved; 58 } 59 isInputRemoved()60 public boolean isInputRemoved() { 61 return mInputRemoved; 62 } 63 setInputRemoved(boolean removed)64 public void setInputRemoved(boolean removed) { 65 mInputRemoved = removed; 66 } 67 getLastWatchEndTimeMs()68 public synchronized long getLastWatchEndTimeMs() { 69 WatchedProgram p = mWatchHistory.peekLast(); 70 return (p == null) ? 0 : p.getWatchEndTimeMs(); 71 } 72 getCurrentProgram()73 public Program getCurrentProgram() { 74 long time = System.currentTimeMillis(); 75 if (mCurrentProgram == null || mCurrentProgram.getEndTimeUtcMillis() < time) { 76 ProgramDataManager manager = 77 TvSingletons.getSingletons(mContext).getProgramDataManager(); 78 mCurrentProgram = manager.getCurrentProgram(mChannel.getId()); 79 } 80 return mCurrentProgram; 81 } 82 getTotalWatchDurationMs()83 public long getTotalWatchDurationMs() { 84 return mTotalWatchDurationMs; 85 } 86 getWatchHistory()87 public final synchronized WatchedProgram[] getWatchHistory() { 88 return mWatchHistory.toArray(new WatchedProgram[mWatchHistory.size()]); 89 } 90 logWatchHistory(WatchedProgram p)91 public synchronized void logWatchHistory(WatchedProgram p) { 92 mWatchHistory.offer(p); 93 mTotalWatchDurationMs += p.getWatchedDurationMs(); 94 if (mWatchHistory.size() > MAX_HISTORY_SIZE) { 95 WatchedProgram program = mWatchHistory.poll(); 96 mTotalWatchDurationMs -= program.getWatchedDurationMs(); 97 } 98 } 99 } 100