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