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; 18 19 import android.content.Context; 20 import android.media.tv.TvTrackInfo; 21 import android.support.annotation.IntDef; 22 import android.util.SparseArray; 23 import com.android.tv.data.DisplayMode; 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 import java.util.Locale; 27 28 /** 29 * The TvOptionsManager is responsible for keeping track of current TV options such as closed 30 * captions and display mode. Can be also used to create MenuAction items to control such options. 31 */ 32 public class TvOptionsManager { 33 @Retention(RetentionPolicy.SOURCE) 34 @IntDef({ 35 OPTION_CLOSED_CAPTIONS, 36 OPTION_DISPLAY_MODE, 37 OPTION_SYSTEMWIDE_PIP, 38 OPTION_MULTI_AUDIO, 39 OPTION_MORE_CHANNELS, 40 OPTION_DEVELOPER, 41 OPTION_SETTINGS 42 }) 43 public @interface OptionType {} 44 45 public static final int OPTION_CLOSED_CAPTIONS = 0; 46 public static final int OPTION_DISPLAY_MODE = 1; 47 public static final int OPTION_SYSTEMWIDE_PIP = 2; 48 public static final int OPTION_MULTI_AUDIO = 3; 49 public static final int OPTION_MORE_CHANNELS = 4; 50 public static final int OPTION_DEVELOPER = 5; 51 public static final int OPTION_SETTINGS = 6; 52 53 private final Context mContext; 54 private final SparseArray<OptionChangedListener> mOptionChangedListeners = new SparseArray<>(); 55 56 private String mClosedCaptionsLanguage; 57 private int mDisplayMode; 58 private String mMultiAudio; 59 TvOptionsManager(Context context)60 public TvOptionsManager(Context context) { 61 mContext = context; 62 } 63 64 /** 65 * Returns a suitable displayed string for the given option type under current settings. 66 * 67 * @param option the type of option, should be one of {@link OptionType}. 68 */ getOptionString(@ptionType int option)69 public String getOptionString(@OptionType int option) { 70 switch (option) { 71 case OPTION_CLOSED_CAPTIONS: 72 if (mClosedCaptionsLanguage == null) { 73 return mContext.getString(R.string.closed_caption_option_item_off); 74 } 75 return new Locale(mClosedCaptionsLanguage).getDisplayName(); 76 case OPTION_DISPLAY_MODE: 77 return ((MainActivity) mContext) 78 .getTvViewUiManager() 79 .isDisplayModeAvailable(mDisplayMode) 80 ? DisplayMode.getLabel(mDisplayMode, mContext) 81 : DisplayMode.getLabel(DisplayMode.MODE_NORMAL, mContext); 82 case OPTION_MULTI_AUDIO: 83 return mMultiAudio; 84 } 85 return ""; 86 } 87 88 /** Handles changing selection of closed caption. */ onClosedCaptionsChanged(TvTrackInfo track, int trackIndex)89 public void onClosedCaptionsChanged(TvTrackInfo track, int trackIndex) { 90 mClosedCaptionsLanguage = 91 (track == null) 92 ? null 93 : (track.getLanguage() != null) 94 ? track.getLanguage() 95 : mContext.getString( 96 R.string.closed_caption_unknown_language, trackIndex + 1); 97 notifyOptionChanged(OPTION_CLOSED_CAPTIONS); 98 } 99 100 /** Handles changing selection of display mode. */ onDisplayModeChanged(int displayMode)101 public void onDisplayModeChanged(int displayMode) { 102 mDisplayMode = displayMode; 103 notifyOptionChanged(OPTION_DISPLAY_MODE); 104 } 105 106 /** Handles changing selection of multi-audio. */ onMultiAudioChanged(String multiAudio)107 public void onMultiAudioChanged(String multiAudio) { 108 mMultiAudio = multiAudio; 109 notifyOptionChanged(OPTION_MULTI_AUDIO); 110 } 111 notifyOptionChanged(@ptionType int option)112 private void notifyOptionChanged(@OptionType int option) { 113 OptionChangedListener listener = mOptionChangedListeners.get(option); 114 if (listener != null) { 115 listener.onOptionChanged(option, getOptionString(option)); 116 } 117 } 118 119 /** Sets listeners to changes of the given option type. */ setOptionChangedListener(int option, OptionChangedListener listener)120 public void setOptionChangedListener(int option, OptionChangedListener listener) { 121 mOptionChangedListeners.put(option, listener); 122 } 123 124 /** An interface used to monitor option changes. */ 125 public interface OptionChangedListener { onOptionChanged(@ptionType int optionType, String newString)126 void onOptionChanged(@OptionType int optionType, String newString); 127 } 128 } 129