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