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    private static final int MIN_COUNT_FOR_RECENT_CHANNELS = 5;
30    private static final int MAX_COUNT_FOR_RECENT_CHANNELS = 10;
31
32    private Recommender mTvRecommendation;
33    private ChannelsRowAdapter mChannelsAdapter;
34    private ChannelsPosterPrefetcher mChannelsPosterPrefetcher;
35
36    public ChannelsRow(Context context, Menu menu, ProgramDataManager programDataManager) {
37        super(context, menu, R.string.menu_title_channels, R.dimen.card_layout_height, null);
38        mTvRecommendation = new Recommender(getContext(), new Recommender.Listener() {
39            @Override
40            public void onRecommenderReady() {
41                mChannelsAdapter.update();
42                mChannelsPosterPrefetcher.prefetch();
43            }
44
45            @Override
46            public void onRecommendationChanged() {
47                mChannelsAdapter.update();
48                mChannelsPosterPrefetcher.prefetch();
49            }
50        }, true);
51        mTvRecommendation.registerEvaluator(new RecentChannelEvaluator());
52        mChannelsAdapter = new ChannelsRowAdapter(context, mTvRecommendation,
53                MIN_COUNT_FOR_RECENT_CHANNELS, MAX_COUNT_FOR_RECENT_CHANNELS);
54        setAdapter(mChannelsAdapter);
55        mChannelsPosterPrefetcher = new ChannelsPosterPrefetcher(context, programDataManager,
56                mChannelsAdapter);
57    }
58
59    @Override
60    public void release() {
61        super.release();
62        if (mTvRecommendation != null) {
63            mTvRecommendation.release();
64            mTvRecommendation = null;
65        }
66        mChannelsPosterPrefetcher.cancel();
67    }
68
69    /**
70     * Handle the update event of the recent channel.
71     */
72    @Override
73    public void onRecentChannelsChanged() {
74        mChannelsPosterPrefetcher.prefetch();
75    }
76
77    @Override
78    public String getId() {
79        return ID;
80    }
81}
82