ChannelBannerView.java revision f533911c31daa05e2ad955434c55b4d412673656
1// Copyright 2014 Google Inc. All Rights Reserved.
2
3package com.android.tv.ui;
4
5import com.android.tv.ChannelMap;
6import com.android.tv.Program;
7import com.android.tv.R;
8import com.android.tv.Utils;
9
10import android.content.Context;
11import android.database.ContentObserver;
12import android.net.Uri;
13import android.os.Handler;
14import android.provider.TvContract;
15import android.text.Html;
16import android.text.TextUtils;
17import android.text.format.DateFormat;
18import android.tv.TvInputInfo;
19import android.util.AttributeSet;
20import android.view.View;
21import android.widget.LinearLayout;
22import android.widget.ProgressBar;
23import android.widget.TextView;
24
25/**
26 * A view to render channel banner.
27 */
28public class ChannelBannerView extends LinearLayout {
29
30    private static final String TAG = "ChannelBannerView";
31
32    private TextView mResolutionTextView;
33    private TextView mAspectRatioTextView;
34    private ProgressBar mRemainingTimeView;
35    private LinearLayout mProgramInfoContainer;
36    private TextView mProgrameDescriptionTextView;
37    private TextView mChannelTextView;
38    private TextView mProgramTextView;
39    private TextView mProgramTimeTextView;
40    private Uri mCurrentChannelUri;
41
42    private final ContentObserver mProgramUpdateObserver = new ContentObserver(new Handler()) {
43        @Override
44        public void onChange(boolean selfChange, Uri uri) {
45            updateProgramInfo();
46        }
47    };
48
49    public ChannelBannerView(Context context) {
50        super(context);
51        mContext = context;
52    }
53
54    public ChannelBannerView(Context context, AttributeSet attrs) {
55        super(context, attrs);
56        mContext = context;
57    }
58
59    public ChannelBannerView(Context context, AttributeSet attrs, int defStyle) {
60        super(context, attrs, defStyle);
61        mContext = context;
62    }
63
64    @Override
65    protected void onAttachedToWindow() {
66        super.onAttachedToWindow();
67        getContext().getContentResolver().registerContentObserver(TvContract.Programs.CONTENT_URI,
68                true, mProgramUpdateObserver);
69    }
70
71    @Override
72    protected void onDetachedFromWindow() {
73        getContext().getContentResolver().unregisterContentObserver(mProgramUpdateObserver);
74        super.onDetachedFromWindow();
75    }
76
77    @Override
78    protected void onFinishInflate() {
79        super.onFinishInflate();
80
81        mResolutionTextView = (TextView) findViewById(R.id.resolution);
82        mAspectRatioTextView = (TextView) findViewById(R.id.aspect_ratio);
83        mRemainingTimeView = (ProgressBar) findViewById(R.id.remaining_time);
84        mChannelTextView = (TextView) findViewById(R.id.channel_text);
85        mProgramTimeTextView = (TextView) findViewById(R.id.program_time_text);
86        mProgramInfoContainer = (LinearLayout) findViewById(R.id.program_info);
87        mProgrameDescriptionTextView = (TextView) findViewById(R.id.program_description);
88        mProgramTextView = (TextView) findViewById(R.id.program_text);
89    }
90
91    public void updateViews(ChannelMap channelMap) {
92        if (channelMap == null || !channelMap.isLoadFinished()) {
93            return;
94        }
95
96        mResolutionTextView.setVisibility(View.GONE);
97        mAspectRatioTextView.setVisibility(View.GONE);
98        mProgrameDescriptionTextView.setVisibility(View.GONE);
99
100        String displayNumber = channelMap.getCurrentDisplayNumber();
101        String displayName = channelMap.getCurrentDisplayName();
102        if (displayNumber == null) {
103            displayNumber = "";
104        }
105        if (displayName == null) {
106            displayName = "";
107        }
108        mChannelTextView.setText(Html.fromHtml(mContext.getString(
109                R.string.channel_banner_channel_title, displayNumber, displayName)));
110        TvInputInfo info = channelMap.getTvInputInfo();
111
112        mCurrentChannelUri = channelMap.getCurrentChannelUri();
113        updateProgramInfo();
114    }
115
116    private String getFormattedTimeString(long time) {
117        return DateFormat.format(
118                getContext().getString(R.string.channel_banner_time_format), time).toString();
119    }
120
121    public void updateProgramInfo() {
122        if (mCurrentChannelUri == null) {
123            hideProgramInformation();
124            return;
125        }
126
127        Program program = Utils.getCurrentProgram(mContext, mCurrentChannelUri);
128        if (program == null) {
129            hideProgramInformation();
130            return;
131        }
132        if (!TextUtils.isEmpty(program.getTitle())) {
133            mProgramInfoContainer.setVisibility(View.VISIBLE);
134            mProgramTextView.setText(program.getTitle());
135
136            long startTime = program.getStartTimeUtcMillis();
137            long endTime = program.getEndTimeUtcMillis();
138            if (startTime > 0 && endTime > 0) {
139                mProgramTimeTextView.setVisibility(View.VISIBLE);
140                mRemainingTimeView.setVisibility(View.VISIBLE);
141
142                String startTimeText = getFormattedTimeString(startTime);
143                String endTimeText = getFormattedTimeString(endTime);
144
145                mProgramTimeTextView.setText(mContext.getString(
146                        R.string.channel_banner_program_time_format, startTimeText, endTimeText));
147
148                long currTime = System.currentTimeMillis();
149                if (currTime <= startTime) {
150                    mRemainingTimeView.setProgress(0);
151                } else if (currTime >= endTime) {
152                    mRemainingTimeView.setProgress(100);
153                } else {
154                    mRemainingTimeView.setProgress(
155                            (int) (100 *(currTime - startTime) / (endTime - startTime)));
156                }
157            } else {
158                mProgramTimeTextView.setVisibility(View.INVISIBLE);
159                mRemainingTimeView.setVisibility(View.INVISIBLE);
160            }
161        } else {
162            hideProgramInformation();
163        }
164    }
165
166    private void hideProgramInformation() {
167        mProgramInfoContainer.setVisibility(View.INVISIBLE);
168        mProgramTimeTextView.setVisibility(View.INVISIBLE);
169        mRemainingTimeView.setVisibility(View.GONE);
170    }
171}
172