ChannelBannerView.java revision d8a314d5263813f1eafe22c1dffeb1fd510036a6
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.media.tv.TvContract; 22import android.net.Uri; 23import android.os.Handler; 24import android.text.TextUtils; 25import android.text.format.DateFormat; 26import android.util.AttributeSet; 27import android.util.Log; 28import android.view.View; 29import android.widget.LinearLayout; 30import android.widget.ProgressBar; 31import android.widget.TextView; 32 33import com.android.tv.R; 34import com.android.tv.data.ChannelMap; 35import com.android.tv.data.Program; 36import com.android.tv.data.StreamInfo; 37import com.android.tv.util.Utils; 38 39/** 40 * A view to render channel banner. 41 */ 42public class ChannelBannerView extends LinearLayout { 43 private TextView mClosedCaptionTextView; 44 private TextView mResolutionTextView; 45 private TextView mAspectRatioTextView; 46 private TextView mAudioChannelTextView; 47 private ProgressBar mRemainingTimeView; 48 private TextView mProgrameDescriptionTextView; 49 private TextView mChannelTextView; 50 private TextView mChannelNameTextView; 51 private TextView mProgramTextView; 52 private TextView mProgramTimeTextView; 53 private Uri mCurrentChannelUri; 54 55 private final ContentObserver mProgramUpdateObserver = new ContentObserver(new Handler()) { 56 @Override 57 public void onChange(boolean selfChange, Uri uri) { 58 updateProgramInfo(); 59 } 60 }; 61 62 public ChannelBannerView(Context context) { 63 super(context); 64 mContext = context; 65 } 66 67 public ChannelBannerView(Context context, AttributeSet attrs) { 68 super(context, attrs); 69 mContext = context; 70 } 71 72 public ChannelBannerView(Context context, AttributeSet attrs, int defStyle) { 73 super(context, attrs, defStyle); 74 mContext = context; 75 } 76 77 @Override 78 protected void onAttachedToWindow() { 79 super.onAttachedToWindow(); 80 getContext().getContentResolver().registerContentObserver(TvContract.Programs.CONTENT_URI, 81 true, mProgramUpdateObserver); 82 } 83 84 @Override 85 protected void onDetachedFromWindow() { 86 getContext().getContentResolver().unregisterContentObserver(mProgramUpdateObserver); 87 super.onDetachedFromWindow(); 88 } 89 90 @Override 91 protected void onFinishInflate() { 92 super.onFinishInflate(); 93 94 mClosedCaptionTextView = (TextView) findViewById(R.id.closed_caption); 95 mResolutionTextView = (TextView) findViewById(R.id.resolution); 96 mAspectRatioTextView = (TextView) findViewById(R.id.aspect_ratio); 97 mAudioChannelTextView = (TextView) findViewById(R.id.audio_channel); 98 mRemainingTimeView = (ProgressBar) findViewById(R.id.remaining_time); 99 mChannelTextView = (TextView) findViewById(R.id.channel_text); 100 mChannelNameTextView = (TextView) findViewById(R.id.channel_name); 101 mProgramTimeTextView = (TextView) findViewById(R.id.program_time_text); 102 mProgrameDescriptionTextView = (TextView) findViewById(R.id.program_description); 103 mProgramTextView = (TextView) findViewById(R.id.program_text); 104 } 105 106 public void updateViews(ChannelMap channelMap, StreamInfo info) { 107 if (channelMap == null || !channelMap.isLoadFinished()) { 108 return; 109 } 110 111 if (info.hasClosedCaption()) { 112 mClosedCaptionTextView.setVisibility(View.VISIBLE); 113 mClosedCaptionTextView.setText("CC"); 114 mClosedCaptionTextView.setVisibility(View.VISIBLE); 115 } else { 116 mClosedCaptionTextView.setVisibility(View.GONE); 117 } 118 if (info.getVideoDefinitionLevel() != StreamInfo.VIDEO_DEFINITION_LEVEL_UNKNOWN) { 119 mResolutionTextView.setVisibility(View.VISIBLE); 120 mResolutionTextView.setText(Utils.getVideoDefinitionLevelString( 121 info.getVideoDefinitionLevel())); 122 mResolutionTextView.setVisibility(View.VISIBLE); 123 } else { 124 mResolutionTextView.setVisibility(View.GONE); 125 } 126 // TODO: implement aspect ratio. 127 mAspectRatioTextView.setVisibility(View.GONE); 128 if (!TextUtils.isEmpty(Utils.getAudioChannelString(info.getAudioChannelCount()))) { 129 mAudioChannelTextView.setVisibility(View.VISIBLE); 130 mAudioChannelTextView.setText(Utils.getAudioChannelString(info.getAudioChannelCount())); 131 mAudioChannelTextView.setVisibility(View.VISIBLE); 132 } else { 133 mAudioChannelTextView.setVisibility(View.GONE); 134 } 135 136 String displayNumber = channelMap.getCurrentDisplayNumber(); 137 String displayName = channelMap.getCurrentDisplayName(); 138 if (displayNumber == null) { 139 displayNumber = ""; 140 } 141 if (displayName == null) { 142 displayName = ""; 143 } 144 mChannelTextView.setText(displayNumber); 145 mChannelNameTextView.setText(displayName); 146 147 mCurrentChannelUri = channelMap.getCurrentChannelUri(); 148 updateProgramInfo(); 149 } 150 151 private String getFormattedTimeString(long time) { 152 return DateFormat.format( 153 getContext().getString(R.string.channel_banner_time_format), time).toString(); 154 } 155 156 public void updateProgramInfo() { 157 if (mCurrentChannelUri == null) { 158 handleNoProgramInformation(); 159 return; 160 } 161 162 Program program = Utils.getCurrentProgram(mContext, mCurrentChannelUri); 163 if (program == null) { 164 handleNoProgramInformation(); 165 return; 166 } 167 if (!TextUtils.isEmpty(program.getTitle())) { 168 mProgramTextView.setText(program.getTitle()); 169 170 long startTime = program.getStartTimeUtcMillis(); 171 long endTime = program.getEndTimeUtcMillis(); 172 if (startTime > 0 && endTime > 0) { 173 mProgramTimeTextView.setVisibility(View.VISIBLE); 174 mRemainingTimeView.setVisibility(View.VISIBLE); 175 176 String startTimeText = getFormattedTimeString(startTime); 177 String endTimeText = getFormattedTimeString(endTime); 178 179 mProgramTimeTextView.setText(mContext.getString( 180 R.string.channel_banner_program_time_format, startTimeText, endTimeText)); 181 182 long currTime = System.currentTimeMillis(); 183 if (currTime <= startTime) { 184 mRemainingTimeView.setProgress(0); 185 } else if (currTime >= endTime) { 186 mRemainingTimeView.setProgress(100); 187 } else { 188 mRemainingTimeView.setProgress( 189 (int) (100 *(currTime - startTime) / (endTime - startTime))); 190 } 191 } else { 192 mProgramTimeTextView.setVisibility(View.GONE); 193 mRemainingTimeView.setVisibility(View.GONE); 194 } 195 } else { 196 mProgramTextView.setText(mContext.getString(R.string.channel_banner_no_title)); 197 mProgramTimeTextView.setVisibility(View.GONE); 198 mRemainingTimeView.setVisibility(View.GONE); 199 } 200 if (!TextUtils.isEmpty(program.getDescription())) { 201 mProgrameDescriptionTextView.setVisibility(View.VISIBLE); 202 mProgrameDescriptionTextView.setText(program.getDescription()); 203 } else { 204 mProgrameDescriptionTextView.setVisibility(View.GONE); 205 } 206 } 207 208 private void handleNoProgramInformation() { 209 mProgramTextView.setText(mContext.getString(R.string.channel_banner_no_title)); 210 mProgramTimeTextView.setVisibility(View.GONE); 211 mRemainingTimeView.setVisibility(View.GONE); 212 mProgrameDescriptionTextView.setVisibility(View.GONE); 213 } 214} 215