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 java.util.List;
20 
21 public class FavoriteChannelEvaluator extends Recommender.Evaluator {
22     private static final long MIN_WATCH_PERIOD_MS = 1000 * 60 * 60 * 24; // 1 day
23     // When there is no watch history, use the current time as a default value.
24     private long mEarliestWatchStartTimeMs = System.currentTimeMillis();
25 
26     @Override
onChannelRecordListChanged(List<ChannelRecord> channelRecords)27     protected void onChannelRecordListChanged(List<ChannelRecord> channelRecords) {
28         for (ChannelRecord cr : channelRecords) {
29             WatchedProgram[] watchedPrograms = cr.getWatchHistory();
30             if (watchedPrograms.length > 0
31                     && mEarliestWatchStartTimeMs > watchedPrograms[0].getWatchStartTimeMs()) {
32                 mEarliestWatchStartTimeMs = watchedPrograms[0].getWatchStartTimeMs();
33             }
34         }
35     }
36 
37     @Override
evaluateChannel(long channelId)38     public double evaluateChannel(long channelId) {
39         ChannelRecord cr = getRecommender().getChannelRecord(channelId);
40         if (cr == null) {
41             return NOT_RECOMMENDED;
42         }
43 
44         if (cr.getTotalWatchDurationMs() == 0) {
45             return NOT_RECOMMENDED;
46         }
47 
48         long watchPeriodMs = System.currentTimeMillis() - mEarliestWatchStartTimeMs;
49         return (double) cr.getTotalWatchDurationMs() / Math.max(watchPeriodMs, MIN_WATCH_PERIOD_MS);
50     }
51 }
52