aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/ChannelBannerView.java
blob: e7822754e89c7f1ecd3485a0547db53ebfc8b946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.ui;

import android.content.Context;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.tv.TvContract;
import android.media.tv.TvContentRating;
import android.media.tv.TvInputInfo;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.LruCache;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tv.R;
import com.android.tv.data.Channel;
import com.android.tv.data.ChannelMap;
import com.android.tv.data.Program;
import com.android.tv.data.StreamInfo;
import com.android.tv.util.Utils;

/**
 * A view to render channel banner.
 */
public class ChannelBannerView extends RelativeLayout implements Channel.LoadLogoCallback {
    private static final int CACHE_SIZE = 10;
    // TODO: Need to get UX design for how to show ratings in the channel banner.
    private TextView mRatingTextView;
    private TextView mClosedCaptionTextView;
    private TextView mResolutionTextView;
    private TextView mAspectRatioTextView;
    private TextView mAudioChannelTextView;
    private ProgressBar mRemainingTimeView;
    private TextView mProgrameDescriptionTextView;
    private TextView mChannelTextView;
    private TextView mChannelNameTextView;
    private ImageView mTvInputLogoImageView;
    private ImageView mChannelLogoImageView;
    private TextView mProgramTextView;
    private TextView mProgramTimeTextView;
    private View mAnchorView;
    private Uri mCurrentChannelUri;
    private final LruCache<TvInputInfo, Drawable> mChannelInfoLogoCache =
            new LruCache<TvInputInfo, Drawable> (CACHE_SIZE) {
                @Override
                protected Drawable create(TvInputInfo info) {
                    return info.loadIcon(getContext());
                }
            };

    private final ContentObserver mProgramUpdateObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            // TODO: This {@code uri} argument may be a program which is not related to this
            // channel. Consider adding channel id as a parameter of program URI to avoid
            // unnecessary update.
            post(mProgramUpdateRunnable);
        }
    };

    private final Runnable mProgramUpdateRunnable = new Runnable() {
        @Override
        public void run() {
            removeCallbacks(this);
            updateProgramInfo();
        }
    };

    public ChannelBannerView(Context context) {
        this(context, null);
    }

    public ChannelBannerView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public ChannelBannerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        getContext().getContentResolver().registerContentObserver(TvContract.Programs.CONTENT_URI,
                true, mProgramUpdateObserver);
    }

    @Override
    protected void onDetachedFromWindow() {
        getContext().getContentResolver().unregisterContentObserver(mProgramUpdateObserver);
        super.onDetachedFromWindow();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mRatingTextView = (TextView) findViewById(R.id.rating);
        mClosedCaptionTextView = (TextView) findViewById(R.id.closed_caption);
        mResolutionTextView = (TextView) findViewById(R.id.resolution);
        mAspectRatioTextView = (TextView) findViewById(R.id.aspect_ratio);
        mAudioChannelTextView = (TextView) findViewById(R.id.audio_channel);
        mRemainingTimeView = (ProgressBar) findViewById(R.id.remaining_time);
        mChannelTextView = (TextView) findViewById(R.id.channel_text);
        mChannelNameTextView = (TextView) findViewById(R.id.channel_name);
        mTvInputLogoImageView = (ImageView) findViewById(R.id.tvinput_logo);
        mChannelLogoImageView = (ImageView) findViewById(R.id.channel_logo);
        mProgramTimeTextView = (TextView) findViewById(R.id.program_time_text);
        mProgrameDescriptionTextView = (TextView) findViewById(R.id.program_description);
        mProgramTextView = (TextView) findViewById(R.id.program_text);
        mAnchorView = findViewById(R.id.anchor);
    }

    public void updateViews(ChannelMap channelMap, StreamInfo info) {
        if (channelMap == null || !channelMap.isLoadFinished()) {
            return;
        }

        TvInputInfo inputInfo = (info == null) ? null : info.getCurrentTvInputInfo();
        Drawable tvInputLogo = (inputInfo == null) ? null : mChannelInfoLogoCache.get(inputInfo);
        if (tvInputLogo != null) {
            mTvInputLogoImageView.setVisibility(View.VISIBLE);
            mTvInputLogoImageView.setImageDrawable(tvInputLogo);
        } else {
            mTvInputLogoImageView.setVisibility(View.GONE);
        }

        if (info.hasClosedCaption()) {
            mClosedCaptionTextView.setVisibility(View.VISIBLE);
            mClosedCaptionTextView.setText("CC");
            mClosedCaptionTextView.setVisibility(View.VISIBLE);
        } else {
            mClosedCaptionTextView.setVisibility(View.GONE);
        }
        if (info.getVideoDefinitionLevel() != StreamInfo.VIDEO_DEFINITION_LEVEL_UNKNOWN) {
            mResolutionTextView.setVisibility(View.VISIBLE);
            mResolutionTextView.setText(Utils.getVideoDefinitionLevelString(
                    info.getVideoDefinitionLevel()));
            mResolutionTextView.setVisibility(View.VISIBLE);
        } else {
            mResolutionTextView.setVisibility(View.GONE);
        }

        String aspectRatio =
                Utils.getAspectRatioString(info.getVideoWidth(), info.getVideoHeight());
        if (!TextUtils.isEmpty(aspectRatio)) {
            mAspectRatioTextView.setVisibility(View.VISIBLE);
            mAspectRatioTextView.setText(aspectRatio);
        } else {
            mAspectRatioTextView.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(Utils.getAudioChannelString(info.getAudioChannelCount()))) {
            mAudioChannelTextView.setVisibility(View.VISIBLE);
            mAudioChannelTextView.setText(Utils.getAudioChannelString(info.getAudioChannelCount()));
        } else {
            mAudioChannelTextView.setVisibility(View.GONE);
        }

        String displayNumber = channelMap.getCurrentDisplayNumber();
        if (displayNumber == null) {
            displayNumber = "";
        }

        if (displayNumber.length() <= 3) {
            updateTextView(
                    mChannelTextView,
                    R.dimen.channel_banner_title_large_text_size,
                    R.dimen.channel_banner_title_large_margin_top);
        } else if (displayNumber.length() <= 4) {
            updateTextView(
                    mChannelTextView,
                    R.dimen.channel_banner_title_medium_text_size,
                    R.dimen.channel_banner_title_medium_margin_top);
        } else {
            updateTextView(
                    mChannelTextView,
                    R.dimen.channel_banner_title_small_text_size,
                    R.dimen.channel_banner_title_small_margin_top);
        }
        mChannelTextView.setText(displayNumber);

        String displayName = channelMap.getCurrentDisplayName();
        if (displayName == null) {
            displayName = "";
        }
        mChannelNameTextView.setText(displayName);

        mCurrentChannelUri = channelMap.getCurrentChannelUri();
        if (channelMap.getCurrentChannel() != null) {
            channelMap.getCurrentChannel().loadLogo(getContext(), this);
        }

        updateProgramInfo();
    }

    private void updateTextView(TextView textView, int sizeRes, int marginTopRes) {
        float textSize = getContext().getResources().getDimension(sizeRes);
        if (textView.getTextSize() != textSize) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }
        updateTopMargin(textView, marginTopRes);
    }

    private void updateTopMargin(View view, int marginTopRes) {
        LayoutParams lp = (LayoutParams) view.getLayoutParams();
        int topMargin = (int) getContext().getResources().getDimension(marginTopRes);
        if (lp.topMargin != topMargin) {
            lp.topMargin = topMargin;
            view.setLayoutParams(lp);
        }
    }

    @Override
    public void onLoadLogoFinished(Channel channel, Bitmap logo) {
        if (logo == null) {
            mChannelLogoImageView.setVisibility(View.GONE);
        } else {
            mChannelLogoImageView.setImageBitmap(logo);
            mChannelLogoImageView.setVisibility(View.VISIBLE);
        }
    }

    private String getFormattedTimeString(long time) {
        return DateFormat.format(
                getContext().getString(R.string.channel_banner_time_format), time).toString();
    }

    private void updateProgramInfo() {
        if (mCurrentChannelUri == null) {
            handleNoProgramInformation();
            return;
        }

        Program program = Utils.getCurrentProgram(getContext(), mCurrentChannelUri);
        if (program == null) {
            handleNoProgramInformation();
            return;
        }

        String title = program.getTitle();
        if (!TextUtils.isEmpty(title)) {
            int width = mProgramTextView.getWidth();
            if (width == 0) {
                post(mProgramUpdateRunnable);
            }
            float largeTextSize = getContext().getResources().getDimension(
                    R.dimen.channel_banner_program_large_text_size);
            Typeface font = mProgramTextView.getTypeface();
            int estimatedLineCount = estimateLineCount(title, font, largeTextSize, width);
            boolean oneline = true;
            if (estimatedLineCount > 1) {
                updateTextView(
                        mProgramTextView,
                        R.dimen.channel_banner_program_medium_text_size,
                        R.dimen.channel_banner_program_medium_margin_top);
                float mediumTextSize = getContext().getResources().getDimension(
                        R.dimen.channel_banner_program_medium_text_size);
                if (estimateLineCount(title, font, mediumTextSize, width) > 1) {
                    oneline = false;
                }
            } else {
                updateTextView(
                        mProgramTextView,
                        R.dimen.channel_banner_program_large_text_size,
                        R.dimen.channel_banner_program_large_margin_top);
            }
            updateTopMargin(mAnchorView, oneline
                    ? R.dimen.channel_banner_anchor_one_line_y
                    : R.dimen.channel_banner_anchor_two_line_y);
            mProgramTextView.setText(title);

            long startTime = program.getStartTimeUtcMillis();
            long endTime = program.getEndTimeUtcMillis();
            if (startTime > 0 && endTime > 0) {
                mProgramTimeTextView.setVisibility(View.VISIBLE);
                mRemainingTimeView.setVisibility(View.VISIBLE);

                String startTimeText = getFormattedTimeString(startTime);
                String endTimeText = getFormattedTimeString(endTime);

                mProgramTimeTextView.setText(getContext().getString(
                        R.string.channel_banner_program_time_format, startTimeText, endTimeText));

                long currTime = System.currentTimeMillis();
                if (currTime <= startTime) {
                    mRemainingTimeView.setProgress(0);
                } else if (currTime >= endTime) {
                    mRemainingTimeView.setProgress(100);
                } else {
                    mRemainingTimeView.setProgress(
                            (int) (100 *(currTime - startTime) / (endTime - startTime)));
                }
            } else {
                mProgramTimeTextView.setVisibility(View.GONE);
                mRemainingTimeView.setVisibility(View.GONE);
            }
        } else {
            mProgramTextView.setText(getContext().getString(R.string.channel_banner_no_title));
            mProgramTimeTextView.setVisibility(View.GONE);
            mRemainingTimeView.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(program.getDescription())) {
            mProgrameDescriptionTextView.setVisibility(View.VISIBLE);
            mProgrameDescriptionTextView.setText(program.getDescription());
        } else {
            mProgrameDescriptionTextView.setVisibility(View.GONE);
        }
        if (program.getContentRatings() != null) {
            mRatingTextView.setText(Utils.contentRatingsToString(program.getContentRatings()));
            mRatingTextView.setVisibility(View.VISIBLE);
        } else {
            mRatingTextView.setVisibility(View.GONE);
        }
    }

    private void handleNoProgramInformation() {
        mProgramTextView.setText(getContext().getString(R.string.channel_banner_no_title));
        mProgramTimeTextView.setVisibility(View.GONE);
        mRemainingTimeView.setVisibility(View.GONE);
        mProgrameDescriptionTextView.setVisibility(View.GONE);
    }

    private int estimateLineCount(String str, Typeface font, float textSize, int width) {
        if (width == 0) {
            return -1;
        }
        Paint paint = new Paint();
        paint.setTypeface(font);
        paint.setTextSize(textSize);
        // Add +1 to measured size, because number of lines becomes 2
        // when measured size equals width.
        return divideRoundUp((int) paint.measureText(str) + 1, width);
    }

    private int divideRoundUp(int x, int y) {
        return (x + y - 1) / y;
    }
}