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