Home | History | Annotate | Download | only in menu
      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.support.annotation.Nullable;
     20 import com.android.tv.ChannelTuner;
     21 import com.android.tv.TvOptionsManager;
     22 import com.android.tv.TvOptionsManager.OptionChangedListener;
     23 import com.android.tv.TvOptionsManager.OptionType;
     24 import com.android.tv.data.api.Channel;
     25 import com.android.tv.menu.MenuRowFactory.TvOptionsRow;
     26 import com.android.tv.ui.TunableTvView;
     27 import com.android.tv.ui.TunableTvView.OnScreenBlockingChangedListener;
     28 
     29 /**
     30  * Update menu items when needed.
     31  *
     32  * <p>As the menu is updated when it shows up, this class handles only the dynamic updates.
     33  */
     34 public class MenuUpdater {
     35     private final Menu mMenu;
     36     // Can be null for testing.
     37     @Nullable private final TunableTvView mTvView;
     38     @Nullable private final TvOptionsManager mOptionsManager;
     39     private ChannelTuner mChannelTuner;
     40 
     41     private final ChannelTuner.Listener mChannelTunerListener =
     42             new ChannelTuner.Listener() {
     43                 @Override
     44                 public void onLoadFinished() {}
     45 
     46                 @Override
     47                 public void onBrowsableChannelListChanged() {
     48                     mMenu.update(ChannelsRow.ID);
     49                 }
     50 
     51                 @Override
     52                 public void onCurrentChannelUnavailable(Channel channel) {}
     53 
     54                 @Override
     55                 public void onChannelChanged(Channel previousChannel, Channel currentChannel) {
     56                     mMenu.update(ChannelsRow.ID);
     57                 }
     58             };
     59     private final OptionChangedListener mOptionChangeListener =
     60             new OptionChangedListener() {
     61                 @Override
     62                 public void onOptionChanged(@OptionType int optionType, String newString) {
     63                     mMenu.update(TvOptionsRow.ID);
     64                 }
     65             };
     66 
     67     public MenuUpdater(Menu menu, TunableTvView tvView, TvOptionsManager optionsManager) {
     68         mMenu = menu;
     69         mTvView = tvView;
     70         mOptionsManager = optionsManager;
     71         if (mTvView != null) {
     72             mTvView.setOnScreenBlockedListener(
     73                     new OnScreenBlockingChangedListener() {
     74                         @Override
     75                         public void onScreenBlockingChanged(boolean blocked) {
     76                             mMenu.update(PlayControlsRow.ID);
     77                         }
     78                     });
     79         }
     80         if (mOptionsManager != null) {
     81             mOptionsManager.setOptionChangedListener(
     82                     TvOptionsManager.OPTION_CLOSED_CAPTIONS, mOptionChangeListener);
     83             mOptionsManager.setOptionChangedListener(
     84                     TvOptionsManager.OPTION_DISPLAY_MODE, mOptionChangeListener);
     85             mOptionsManager.setOptionChangedListener(
     86                     TvOptionsManager.OPTION_MULTI_AUDIO, mOptionChangeListener);
     87         }
     88     }
     89 
     90     /**
     91      * Sets the instance of {@link ChannelTuner}. Call this method when the channel tuner is ready.
     92      */
     93     public void setChannelTuner(ChannelTuner channelTuner) {
     94         if (mChannelTuner != null) {
     95             mChannelTuner.removeListener(mChannelTunerListener);
     96         }
     97         mChannelTuner = channelTuner;
     98         if (mChannelTuner != null) {
     99             mChannelTuner.addListener(mChannelTunerListener);
    100         }
    101     }
    102 
    103     /** Called when the stream information changes. */
    104     public void onStreamInfoChanged() {
    105         mMenu.update(TvOptionsRow.ID);
    106     }
    107 
    108     /** Called at the end of the menu's lifetime. */
    109     public void release() {
    110         if (mChannelTuner != null) {
    111             mChannelTuner.removeListener(mChannelTunerListener);
    112         }
    113         if (mTvView != null) {
    114             mTvView.setOnScreenBlockedListener(null);
    115         }
    116         if (mOptionsManager != null) {
    117             mOptionsManager.setOptionChangedListener(TvOptionsManager.OPTION_CLOSED_CAPTIONS, null);
    118             mOptionsManager.setOptionChangedListener(TvOptionsManager.OPTION_DISPLAY_MODE, null);
    119             mOptionsManager.setOptionChangedListener(TvOptionsManager.OPTION_MULTI_AUDIO, null);
    120         }
    121     }
    122 }
    123