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 android.os.Handler; 21 import android.os.Looper; 22 import android.os.Message; 23 import android.support.annotation.MainThread; 24 import android.support.annotation.NonNull; 25 import android.util.Log; 26 27 import com.android.tv.R; 28 import com.android.tv.common.SoftPreconditions; 29 import com.android.tv.common.WeakHandler; 30 import com.android.tv.data.ChannelImpl; 31 import com.android.tv.data.ProgramDataManager; 32 import com.android.tv.data.api.Channel; 33 import com.android.tv.data.api.Program; 34 35 import java.util.List; 36 37 /** A poster image prefetcher to show the program poster art in the Channels row faster. */ 38 public class ChannelsPosterPrefetcher { 39 private static final String TAG = "PosterPrefetcher"; 40 private static final boolean DEBUG = false; 41 private static final int MSG_PREFETCH_IMAGE = 1000; 42 private static final int ONDEMAND_POSTER_PREFETCH_DELAY_MILLIS = 500; // 500 milliseconds 43 44 private final ProgramDataManager mProgramDataManager; 45 private final ChannelsRowAdapter mChannelsAdapter; 46 private final int mPosterArtWidth; 47 private final int mPosterArtHeight; 48 private final Context mContext; 49 private final Handler mHandler = new PrefetchHandler(this); 50 51 private boolean isCanceled; 52 53 /** Create {@link ChannelsPosterPrefetcher} object with given parameters. */ ChannelsPosterPrefetcher( Context context, ProgramDataManager programDataManager, ChannelsRowAdapter adapter)54 public ChannelsPosterPrefetcher( 55 Context context, ProgramDataManager programDataManager, ChannelsRowAdapter adapter) { 56 mProgramDataManager = programDataManager; 57 mChannelsAdapter = adapter; 58 mPosterArtWidth = 59 context.getResources().getDimensionPixelSize(R.dimen.card_image_layout_width); 60 mPosterArtHeight = 61 context.getResources().getDimensionPixelSize(R.dimen.card_image_layout_height); 62 mContext = context.getApplicationContext(); 63 } 64 65 /** Start prefetching of program poster art of recommendation. */ prefetch()66 public void prefetch() { 67 SoftPreconditions.checkState(!isCanceled, TAG, "Prefetch called after cancel was called."); 68 if (isCanceled) { 69 return; 70 } 71 if (DEBUG) Log.d(TAG, "startPrefetching()"); 72 /* 73 * When a user browse channels, this method could be called many times. We don't need to 74 * prefetch the intermediate channels. So ignore previous schedule. 75 */ 76 mHandler.removeMessages(MSG_PREFETCH_IMAGE); 77 mHandler.sendMessageDelayed( 78 mHandler.obtainMessage(MSG_PREFETCH_IMAGE), ONDEMAND_POSTER_PREFETCH_DELAY_MILLIS); 79 } 80 81 /** Cancels pending and current prefetch requests. */ cancel()82 public void cancel() { 83 isCanceled = true; 84 mHandler.removeCallbacksAndMessages(null); 85 } 86 87 @MainThread // ProgramDataManager.getCurrentProgram must be called from the main thread doPrefetchImages()88 private void doPrefetchImages() { 89 if (DEBUG) Log.d(TAG, "doPrefetchImages() started"); 90 91 // This executes on the main thread, but since the item list is expected to be about 5 items 92 // and ImageLoader spawns an async task so this is fast enough. 1 ms in local testing. 93 List<ChannelsRowItem> items = mChannelsAdapter.getItemList(); 94 if (items != null) { 95 for (ChannelsRowItem item : items) { 96 if (isCanceled) { 97 return; 98 } 99 Channel channel = item.getChannel(); 100 if (!ChannelImpl.isValid(channel)) { 101 continue; 102 } 103 channel.prefetchImage( 104 mContext, 105 Channel.LOAD_IMAGE_TYPE_CHANNEL_LOGO, 106 mPosterArtWidth, 107 mPosterArtHeight); 108 Program program = mProgramDataManager.getCurrentProgram(channel.getId()); 109 if (program != null) { 110 program.prefetchPosterArt(mContext, mPosterArtWidth, mPosterArtHeight); 111 } 112 } 113 } 114 if (DEBUG) { 115 Log.d( 116 TAG, 117 "doPrefetchImages() finished. ImageLoader may still have async tasks for " 118 + "channels " 119 + items); 120 } 121 } 122 123 private static class PrefetchHandler extends WeakHandler<ChannelsPosterPrefetcher> { PrefetchHandler(ChannelsPosterPrefetcher ref)124 public PrefetchHandler(ChannelsPosterPrefetcher ref) { 125 // doPrefetchImages must be called from the main thread. 126 super(Looper.getMainLooper(), ref); 127 } 128 129 @Override 130 @MainThread handleMessage(Message msg, @NonNull ChannelsPosterPrefetcher prefetcher)131 public void handleMessage(Message msg, @NonNull ChannelsPosterPrefetcher prefetcher) { 132 switch (msg.what) { 133 case MSG_PREFETCH_IMAGE: 134 prefetcher.doPrefetchImages(); 135 break; 136 } 137 } 138 } 139 } 140