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