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