ChannelsPosterPrefetcher.java revision 816a4be1a0f34f6a48877c8afd3dbbca19eac435
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;
20import android.os.Handler;
21import android.os.Message;
22import android.util.Log;
23
24import com.android.tv.R;
25import com.android.tv.data.Channel;
26import com.android.tv.data.Program;
27import com.android.tv.data.ProgramDataManager;
28
29import java.util.List;
30
31/**
32 * A poster image prefetcher to show the program poster art in the Channels row faster.
33 */
34public class ChannelsPosterPrefetcher {
35    private static final String TAG = "PosterPrefetcher";
36    private static final boolean DEBUG = false;
37
38    private final ProgramDataManager mProgramDataManager;
39    private final ChannelsRowAdapter mChannelsAdapter;
40    private final int mPosterArtWidth;
41    private final int mPosterArtHeight;
42    private final Context mContext;
43
44    private static final int MSG_PREFETCH_IMAGE = 1000;
45
46    private static final int ONDEMAND_POSTER_PREFETCH_DELAY_MILLIS = 500; // 500 milliseconds
47
48    private final Handler mHandler = new Handler() {
49        @Override
50        public void handleMessage(Message msg) {
51            switch (msg.what) {
52                case MSG_PREFETCH_IMAGE:
53                    doPrefetchImages();
54                    break;
55            }
56        }
57    };
58
59    /**
60     * Create {@link ChannelsPosterPrefetcher} object with given parameters.
61     */
62    public ChannelsPosterPrefetcher(Context context, ProgramDataManager programDataManager,
63            ChannelsRowAdapter adapter) {
64        mProgramDataManager = programDataManager;
65        mChannelsAdapter = adapter;
66        mPosterArtWidth = context.getResources().getDimensionPixelSize(
67                R.dimen.card_image_layout_width);
68        mPosterArtHeight = context.getResources().getDimensionPixelSize(
69                R.dimen.card_image_layout_height);
70        mContext = context;
71    }
72
73    /**
74     * Start prefetching of program poster art of recommendation.
75     */
76    public void prefetch() {
77        if (DEBUG) {
78            Log.d(TAG, "startPrefetching()");
79        }
80        /*
81         * When a user browse channels, this method could be called many times. We don't need to
82         * prefetch the intermediate channels. So ignore previous schedule.
83         */
84        mHandler.removeMessages(MSG_PREFETCH_IMAGE);
85        mHandler.sendMessageDelayed(
86                mHandler.obtainMessage(MSG_PREFETCH_IMAGE), ONDEMAND_POSTER_PREFETCH_DELAY_MILLIS);
87    }
88
89    private void doPrefetchImages() {
90        if (DEBUG) {
91            Log.d(TAG, "doPrefetchImages()");
92        }
93
94        List<Channel> channelList = mChannelsAdapter.getItemList();
95        if (channelList != null) {
96            for (Channel channel : channelList) {
97                if (!Channel.isValid(channel)) {
98                    continue;
99                }
100                channel.prefetchImage(mContext, Channel.LOAD_IMAGE_TYPE_CHANNEL_LOGO,
101                        mPosterArtWidth, mPosterArtHeight);
102                Program program = mProgramDataManager.getCurrentProgram(channel.getId());
103                if (program != null) {
104                    program.prefetchPosterArt(mContext, mPosterArtWidth, mPosterArtHeight);
105                }
106            }
107        }
108    }
109}
110