MenuUpdater.java revision 65fda1eaa94968bb55d5ded10dcb0b3f37fb05f2
1/* 2 * Copyright (C) 2016 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.menu; 18 19import android.content.Context; 20import android.support.annotation.Nullable; 21 22import com.android.tv.ChannelTuner; 23import com.android.tv.TvApplication; 24import com.android.tv.common.feature.CommonFeatures; 25import com.android.tv.dvr.RecordedProgram; 26import com.android.tv.data.Channel; 27import com.android.tv.dvr.DvrDataManager; 28import com.android.tv.dvr.DvrDataManager.OnDvrScheduleLoadFinishedListener; 29import com.android.tv.dvr.DvrDataManager.OnRecordedProgramLoadFinishedListener; 30import com.android.tv.dvr.DvrDataManager.RecordedProgramListener; 31import com.android.tv.dvr.DvrDataManager.ScheduledRecordingListener; 32import com.android.tv.dvr.ScheduledRecording; 33import com.android.tv.ui.TunableTvView; 34import com.android.tv.ui.TunableTvView.OnScreenBlockingChangedListener; 35 36/** 37 * Update menu items when needed. 38 * 39 * <p>As the menu is updated when it shows up, this class handles only the dynamic updates. 40 */ 41public class MenuUpdater { 42 // Can be null for testing. 43 @Nullable 44 private final TunableTvView mTvView; 45 private final Menu mMenu; 46 @Nullable 47 private final DvrDataManager mDvrDataManager; 48 private ChannelTuner mChannelTuner; 49 50 private final OnScreenBlockingChangedListener mOnScreenBlockingChangeListener = 51 new OnScreenBlockingChangedListener() { 52 @Override 53 public void onScreenBlockingChanged(boolean blocked) { 54 mMenu.update(PlayControlsRow.ID); 55 } 56 }; 57 private final OnRecordedProgramLoadFinishedListener mRecordedProgramLoadedListener = 58 new OnRecordedProgramLoadFinishedListener() { 59 @Override 60 public void onRecordedProgramLoadFinished() { 61 mMenu.update(ChannelsRow.ID); 62 } 63 }; 64 private final RecordedProgramListener mRecordedProgramListener = 65 new RecordedProgramListener() { 66 @Override 67 public void onRecordedProgramAdded(RecordedProgram recordedProgram) { 68 mMenu.update(ChannelsRow.ID); 69 } 70 71 @Override 72 public void onRecordedProgramChanged(RecordedProgram recordedProgram) { } 73 74 @Override 75 public void onRecordedProgramRemoved(RecordedProgram recordedProgram) { 76 if (mDvrDataManager != null && mDvrDataManager.getRecordedPrograms().isEmpty() 77 && mDvrDataManager.getStartedRecordings().isEmpty() 78 && mDvrDataManager.getNonStartedScheduledRecordings().isEmpty() 79 && mDvrDataManager.getSeriesRecordings().isEmpty()) { 80 mMenu.update(ChannelsRow.ID); 81 } 82 } 83 }; 84 private final OnDvrScheduleLoadFinishedListener mDvrScheduleLoadedListener = 85 new OnDvrScheduleLoadFinishedListener() { 86 @Override 87 public void onDvrScheduleLoadFinished() { 88 mMenu.update(ChannelsRow.ID); 89 } 90 }; 91 private final ScheduledRecordingListener mScheduledRecordingListener = 92 new ScheduledRecordingListener() { 93 @Override 94 public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) { 95 mMenu.update(ChannelsRow.ID); 96 } 97 98 @Override 99 public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) { 100 if (mDvrDataManager != null && mDvrDataManager.getRecordedPrograms().isEmpty() 101 && mDvrDataManager.getStartedRecordings().isEmpty() 102 && mDvrDataManager.getNonStartedScheduledRecordings().isEmpty() 103 && mDvrDataManager.getSeriesRecordings().isEmpty()) { 104 mMenu.update(ChannelsRow.ID); 105 } 106 } 107 108 @Override 109 public void onScheduledRecordingStatusChanged(ScheduledRecording... schedules) { } 110 }; 111 112 private final ChannelTuner.Listener mChannelTunerListener = new ChannelTuner.Listener() { 113 @Override 114 public void onLoadFinished() {} 115 116 @Override 117 public void onBrowsableChannelListChanged() { 118 mMenu.update(); 119 } 120 121 @Override 122 public void onCurrentChannelUnavailable(Channel channel) {} 123 124 @Override 125 public void onChannelChanged(Channel previousChannel, Channel currentChannel) { 126 mMenu.update(ChannelsRow.ID); 127 } 128 }; 129 130 public MenuUpdater(Context context, TunableTvView tvView, Menu menu) { 131 mTvView = tvView; 132 mMenu = menu; 133 if (mTvView != null) { 134 mTvView.setOnScreenBlockedListener(mOnScreenBlockingChangeListener); 135 } 136 if (CommonFeatures.DVR.isEnabled(context)) { 137 mDvrDataManager = TvApplication.getSingletons(context).getDvrDataManager(); 138 mDvrDataManager.addDvrScheduleLoadFinishedListener(mDvrScheduleLoadedListener); 139 mDvrDataManager.addScheduledRecordingListener(mScheduledRecordingListener); 140 mDvrDataManager.addRecordedProgramLoadFinishedListener(mRecordedProgramLoadedListener); 141 mDvrDataManager.addRecordedProgramListener(mRecordedProgramListener); 142 } else { 143 mDvrDataManager = null; 144 } 145 } 146 147 /** 148 * Sets the instance of {@link ChannelTuner}. Call this method when the channel tuner is ready 149 * or not available any more. 150 */ 151 public void setChannelTuner(ChannelTuner channelTuner) { 152 if (mChannelTuner != null) { 153 mChannelTuner.removeListener(mChannelTunerListener); 154 } 155 mChannelTuner = channelTuner; 156 if (mChannelTuner != null) { 157 mChannelTuner.addListener(mChannelTunerListener); 158 } 159 mMenu.update(); 160 } 161 162 /** 163 * Called at the end of the menu's lifetime. 164 */ 165 public void release() { 166 if (mDvrDataManager != null) { 167 mDvrDataManager.removeScheduledRecordingListener(mScheduledRecordingListener); 168 mDvrDataManager.removeRecordedProgramListener(mRecordedProgramListener); 169 mDvrDataManager.removeDvrScheduleLoadFinishedListener(mDvrScheduleLoadedListener); 170 mDvrDataManager 171 .removeRecordedProgramLoadFinishedListener(mRecordedProgramLoadedListener); 172 } 173 if (mChannelTuner != null) { 174 mChannelTuner.removeListener(mChannelTunerListener); 175 } 176 if (mTvView != null) { 177 mTvView.setOnScreenBlockedListener(null); 178 } 179 } 180} 181