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