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