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 
17 package com.android.tv.menu;
18 
19 import android.content.Context;
20 import android.support.annotation.Nullable;
21 
22 import com.android.tv.ChannelTuner;
23 import com.android.tv.data.Channel;
24 import com.android.tv.ui.TunableTvView;
25 import com.android.tv.ui.TunableTvView.OnScreenBlockingChangedListener;
26 
27 /**
28  * Update menu items when needed.
29  *
30  * <p>As the menu is updated when it shows up, this class handles only the dynamic updates.
31  */
32 public class MenuUpdater {
33     // Can be null for testing.
34     @Nullable
35     private final TunableTvView mTvView;
36     private final Menu mMenu;
37     private ChannelTuner mChannelTuner;
38 
39     private final ChannelTuner.Listener mChannelTunerListener = new ChannelTuner.Listener() {
40         @Override
41         public void onLoadFinished() {}
42 
43         @Override
44         public void onBrowsableChannelListChanged() {
45             mMenu.update();
46         }
47 
48         @Override
49         public void onCurrentChannelUnavailable(Channel channel) {}
50 
51         @Override
52         public void onChannelChanged(Channel previousChannel, Channel currentChannel) {
53             mMenu.update(ChannelsRow.ID);
54         }
55     };
56 
MenuUpdater(Context context, TunableTvView tvView, Menu menu)57     public MenuUpdater(Context context, TunableTvView tvView, Menu menu) {
58         mTvView = tvView;
59         mMenu = menu;
60         if (mTvView != null) {
61             mTvView.setOnScreenBlockedListener(new OnScreenBlockingChangedListener() {
62                     @Override
63                     public void onScreenBlockingChanged(boolean blocked) {
64                         mMenu.update(PlayControlsRow.ID);
65                     }
66             });
67         }
68     }
69 
70     /**
71      * Sets the instance of {@link ChannelTuner}. Call this method when the channel tuner is ready
72      * or not available any more.
73      */
setChannelTuner(ChannelTuner channelTuner)74     public void setChannelTuner(ChannelTuner channelTuner) {
75         if (mChannelTuner != null) {
76             mChannelTuner.removeListener(mChannelTunerListener);
77         }
78         mChannelTuner = channelTuner;
79         if (mChannelTuner != null) {
80             mChannelTuner.addListener(mChannelTunerListener);
81         }
82         mMenu.update();
83     }
84 
85     /**
86      * Called at the end of the menu's lifetime.
87      */
release()88     public void release() {
89         if (mChannelTuner != null) {
90             mChannelTuner.removeListener(mChannelTunerListener);
91         }
92         if (mTvView != null) {
93             mTvView.setOnScreenBlockedListener(null);
94         }
95     }
96 }
97