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.menu;
18 
19 import android.content.Context;
20 import com.android.tv.R;
21 import com.android.tv.data.ProgramDataManager;
22 import com.android.tv.recommendation.RecentChannelEvaluator;
23 import com.android.tv.recommendation.Recommender;
24 
25 public class ChannelsRow extends ItemListRow {
26     public static final String ID = ChannelsRow.class.getName();
27 
28     /** Minimum count for recent channels. */
29     public static final int MIN_COUNT_FOR_RECENT_CHANNELS = 5;
30     /** Maximum count for recent channels. */
31     public static final int MAX_COUNT_FOR_RECENT_CHANNELS = 10;
32 
33     private Recommender mTvRecommendation;
34     private ChannelsRowAdapter mChannelsAdapter;
35     private ChannelsPosterPrefetcher mChannelsPosterPrefetcher;
36 
ChannelsRow(Context context, Menu menu, ProgramDataManager programDataManager)37     public ChannelsRow(Context context, Menu menu, ProgramDataManager programDataManager) {
38         super(context, menu, R.string.menu_title_channels, R.dimen.card_layout_height, null);
39         mTvRecommendation =
40                 new Recommender(
41                         getContext(),
42                         new Recommender.Listener() {
43                             @Override
44                             public void onRecommenderReady() {
45                                 mChannelsAdapter.update();
46                                 mChannelsPosterPrefetcher.prefetch();
47                             }
48 
49                             @Override
50                             public void onRecommendationChanged() {
51                                 mChannelsAdapter.update();
52                                 mChannelsPosterPrefetcher.prefetch();
53                             }
54                         },
55                         true);
56         mTvRecommendation.registerEvaluator(new RecentChannelEvaluator());
57         mChannelsAdapter =
58                 new ChannelsRowAdapter(
59                         context,
60                         mTvRecommendation,
61                         MIN_COUNT_FOR_RECENT_CHANNELS,
62                         MAX_COUNT_FOR_RECENT_CHANNELS);
63         setAdapter(mChannelsAdapter);
64         mChannelsPosterPrefetcher =
65                 new ChannelsPosterPrefetcher(context, programDataManager, mChannelsAdapter);
66     }
67 
68     @Override
release()69     public void release() {
70         super.release();
71         if (mTvRecommendation != null) {
72             mTvRecommendation.release();
73             mTvRecommendation = null;
74         }
75         mChannelsPosterPrefetcher.cancel();
76         mChannelsAdapter.release();
77     }
78 
79     /** Handle the update event of the recent channel. */
80     @Override
onRecentChannelsChanged()81     public void onRecentChannelsChanged() {
82         mChannelsPosterPrefetcher.prefetch();
83     }
84 
85     @Override
getId()86     public String getId() {
87         return ID;
88     }
89 }
90