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.ui.sidepanel; 18 19 import android.text.TextUtils; 20 import android.view.View; 21 import android.widget.TextView; 22 23 import com.android.tv.R; 24 import com.android.tv.data.ChannelDataManager; 25 import com.android.tv.data.ChannelDataManager.ChannelListener; 26 import com.android.tv.data.OnCurrentProgramUpdatedListener; 27 import com.android.tv.data.ProgramDataManager; 28 import com.android.tv.data.api.Channel; 29 import com.android.tv.data.api.Program; 30 31 public abstract class ChannelCheckItem extends CompoundButtonItem { 32 private final ChannelDataManager mChannelDataManager; 33 private final ProgramDataManager mProgramDataManager; 34 private Channel mChannel; 35 private TextView mProgramTitleView; 36 private TextView mChannelNumberView; 37 private final ChannelListener mChannelListener = 38 new ChannelListener() { 39 @Override 40 public void onChannelRemoved(Channel channel) {} 41 42 @Override 43 public void onChannelUpdated(Channel channel) { 44 mChannel = channel; 45 } 46 }; 47 48 private final OnCurrentProgramUpdatedListener mOnCurrentProgramUpdatedListener = 49 new OnCurrentProgramUpdatedListener() { 50 @Override 51 public void onCurrentProgramUpdated(long channelId, Program program) { 52 updateProgramTitle(program); 53 } 54 }; 55 ChannelCheckItem( Channel channel, ChannelDataManager channelDataManager, ProgramDataManager programDataManager)56 public ChannelCheckItem( 57 Channel channel, 58 ChannelDataManager channelDataManager, 59 ProgramDataManager programDataManager) { 60 super(channel.getDisplayName(), ""); 61 mChannel = channel; 62 mChannelDataManager = channelDataManager; 63 mProgramDataManager = programDataManager; 64 } 65 getChannel()66 protected Channel getChannel() { 67 return mChannel; 68 } 69 70 @Override getResourceId()71 protected int getResourceId() { 72 return R.layout.option_item_channel_check; 73 } 74 75 @Override getCompoundButtonId()76 protected int getCompoundButtonId() { 77 return R.id.check_box; 78 } 79 80 @Override getTitleViewId()81 protected int getTitleViewId() { 82 return R.id.channel_name; 83 } 84 85 @Override getDescriptionViewId()86 protected int getDescriptionViewId() { 87 return R.id.program_title; 88 } 89 90 @Override onBind(View view)91 protected void onBind(View view) { 92 super.onBind(view); 93 mChannelNumberView = (TextView) view.findViewById(R.id.channel_number); 94 mProgramTitleView = (TextView) view.findViewById(R.id.program_title); 95 mChannelDataManager.addChannelListener(mChannel.getId(), mChannelListener); 96 mProgramDataManager.addOnCurrentProgramUpdatedListener( 97 mChannel.getId(), mOnCurrentProgramUpdatedListener); 98 } 99 100 @Override onUpdate()101 protected void onUpdate() { 102 super.onUpdate(); 103 mChannelNumberView.setText(getChannel().getDisplayNumber()); 104 updateProgramTitle(mProgramDataManager.getCurrentProgram(mChannel.getId())); 105 } 106 107 @Override onUnbind()108 protected void onUnbind() { 109 mChannelDataManager.removeChannelListener(mChannel.getId(), mChannelListener); 110 mProgramDataManager.removeOnCurrentProgramUpdatedListener( 111 mChannel.getId(), mOnCurrentProgramUpdatedListener); 112 mProgramTitleView = null; 113 mChannelNumberView = null; 114 super.onUnbind(); 115 } 116 117 @Override onSelected()118 protected void onSelected() { 119 setChecked(!isChecked()); 120 } 121 updateProgramTitle(Program program)122 private void updateProgramTitle(Program program) { 123 String title = null; 124 if (program != null) { 125 title = program.getTitle(); 126 } 127 if (TextUtils.isEmpty(title)) { 128 title = mProgramTitleView.getContext().getString(R.string.no_program_information); 129 } 130 mProgramTitleView.setText(title); 131 } 132 } 133