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