ChannelCardView.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; 20import android.graphics.Bitmap; 21import android.support.annotation.Nullable; 22import android.text.TextUtils; 23import android.util.AttributeSet; 24import android.util.Log; 25import android.view.View; 26import android.widget.ImageView; 27import android.widget.ProgressBar; 28import android.widget.TextView; 29 30import com.android.tv.MainActivity; 31import com.android.tv.R; 32import com.android.tv.data.Channel; 33import com.android.tv.data.Program; 34import com.android.tv.parental.ParentalControlSettings; 35import com.android.tv.util.ImageLoader; 36 37import java.util.Objects; 38 39/** 40 * A view to render channel card. 41 */ 42public class ChannelCardView extends BaseCardView<ChannelsRowItem> { 43 private static final String TAG = MenuView.TAG; 44 private static final boolean DEBUG = MenuView.DEBUG; 45 46 private final int mCardImageWidth; 47 private final int mCardImageHeight; 48 49 private ImageView mImageView; 50 private TextView mChannelNumberNameView; 51 private ProgressBar mProgressBar; 52 private Channel mChannel; 53 private Program mProgram; 54 private String mPosterArtUri; 55 private final MainActivity mMainActivity; 56 57 public ChannelCardView(Context context) { 58 this(context, null); 59 } 60 61 public ChannelCardView(Context context, AttributeSet attrs) { 62 this(context, attrs, 0); 63 } 64 65 public ChannelCardView(Context context, AttributeSet attrs, int defStyle) { 66 super(context, attrs, defStyle); 67 mCardImageWidth = getResources().getDimensionPixelSize(R.dimen.card_image_layout_width); 68 mCardImageHeight = getResources().getDimensionPixelSize(R.dimen.card_image_layout_height); 69 mMainActivity = (MainActivity) context; 70 } 71 72 @Override 73 protected void onFinishInflate() { 74 super.onFinishInflate(); 75 mImageView = (ImageView) findViewById(R.id.image); 76 mImageView.setBackgroundResource(R.color.channel_card); 77 mChannelNumberNameView = (TextView) findViewById(R.id.channel_number_and_name); 78 mProgressBar = (ProgressBar) findViewById(R.id.progress); 79 } 80 81 @Override 82 public void onBind(ChannelsRowItem item, boolean selected) { 83 if (DEBUG) { 84 Log.d(TAG, "onBind(channelName=" + item.getChannel().getDisplayName() + ", selected=" 85 + selected + ")"); 86 } 87 updateChannel(item); 88 updateProgram(); 89 super.onBind(item, selected); 90 } 91 92 private void updateChannel(ChannelsRowItem item) { 93 if (!item.getChannel().equals(mChannel)) { 94 mChannel = item.getChannel(); 95 mChannelNumberNameView.setText(mChannel.getDisplayText()); 96 mChannelNumberNameView.setVisibility(VISIBLE); 97 } 98 } 99 100 private void updateProgram() { 101 ParentalControlSettings parental = mMainActivity.getParentalControlSettings(); 102 if (parental.isParentalControlsEnabled() && mChannel.isLocked()) { 103 setText(R.string.program_title_for_blocked_channel); 104 mProgram = null; 105 } else { 106 Program currentProgram = 107 mMainActivity.getProgramDataManager().getCurrentProgram(mChannel.getId()); 108 if (!Objects.equals(currentProgram, mProgram)) { 109 mProgram = currentProgram; 110 if (mProgram == null || TextUtils.isEmpty(mProgram.getTitle())) { 111 setTextViewEnabled(false); 112 setText(R.string.program_title_for_no_information); 113 } else { 114 setTextViewEnabled(true); 115 setText(mProgram.getTitle()); 116 } 117 } 118 } 119 if (mProgram == null) { 120 mProgressBar.setVisibility(GONE); 121 setPosterArt(null); 122 } else { 123 // Update progress. 124 mProgressBar.setVisibility(View.VISIBLE); 125 long startTime = mProgram.getStartTimeUtcMillis(); 126 long endTime = mProgram.getEndTimeUtcMillis(); 127 long currTime = System.currentTimeMillis(); 128 if (currTime <= startTime) { 129 mProgressBar.setProgress(0); 130 } else if (currTime >= endTime) { 131 mProgressBar.setProgress(100); 132 } else { 133 mProgressBar.setProgress( 134 (int) (100 * (currTime - startTime) / (endTime - startTime))); 135 } 136 // Update image. 137 if (!parental.isParentalControlsEnabled() 138 || !parental.isRatingBlocked(mProgram.getContentRatings())) { 139 setPosterArt(mProgram.getPosterArtUri()); 140 } 141 } 142 } 143 144 private static ImageLoader.ImageLoaderCallback<ChannelCardView> createProgramPosterArtCallback( 145 ChannelCardView cardView, final Program program) { 146 return new ImageLoader.ImageLoaderCallback<ChannelCardView>(cardView) { 147 @Override 148 public void onBitmapLoaded(ChannelCardView cardView, @Nullable Bitmap posterArt) { 149 if (posterArt == null || cardView.mProgram == null 150 || program.getChannelId() != cardView.mProgram.getChannelId() 151 || program.getChannelId() != cardView.mChannel.getId()) { 152 return; 153 } 154 cardView.updatePosterArt(posterArt); 155 } 156 }; 157 } 158 159 private void setPosterArt(String posterArtUri) { 160 if (!TextUtils.equals(mPosterArtUri, posterArtUri)) { 161 mPosterArtUri = posterArtUri; 162 if (posterArtUri == null 163 || !mProgram.loadPosterArt(getContext(), mCardImageWidth, mCardImageHeight, 164 createProgramPosterArtCallback(this, mProgram))) { 165 mImageView.setImageResource(R.drawable.ic_recent_thumbnail_default); 166 mImageView.setForeground(null); 167 } 168 } 169 } 170 171 private void updatePosterArt(Bitmap posterArt) { 172 mImageView.setImageBitmap(posterArt); 173 mImageView.setForeground(getContext().getDrawable(R.drawable.card_image_gradient)); 174 } 175}