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.util;
18 
19 import android.content.Context;
20 import android.view.accessibility.CaptioningManager;
21 
22 import java.util.Locale;
23 
24 public class CaptionSettings {
25     public static final int OPTION_SYSTEM = 0;
26     public static final int OPTION_OFF = 1;
27     public static final int OPTION_ON = 2;
28 
29     private final CaptioningManager mCaptioningManager;
30     private int mOption = OPTION_SYSTEM;
31     private String mLanguage;
32     private String mTrackId;
33 
CaptionSettings(Context context)34     public CaptionSettings(Context context) {
35         mCaptioningManager = (CaptioningManager) context.getSystemService(
36                 Context.CAPTIONING_SERVICE);
37     }
38 
getSystemLanguage()39     public final String getSystemLanguage() {
40         Locale l = mCaptioningManager.getLocale();
41         if (l != null) {
42             return l.getLanguage();
43         }
44         return null;
45     }
46 
getLanguage()47     public final String getLanguage() {
48         switch (mOption) {
49             case OPTION_SYSTEM:
50                 return getSystemLanguage();
51             case OPTION_OFF:
52                 return null;
53             case OPTION_ON:
54                 return mLanguage;
55         }
56         return null;
57     }
58 
isSystemSettingEnabled()59     public final boolean isSystemSettingEnabled() {
60         return mCaptioningManager.isEnabled();
61     }
62 
isEnabled()63     public final boolean isEnabled() {
64         switch (mOption) {
65             case OPTION_SYSTEM:
66                 return isSystemSettingEnabled();
67             case OPTION_OFF:
68                 return false;
69             case OPTION_ON:
70                 return true;
71         }
72         return false;
73     }
74 
getEnableOption()75     public int getEnableOption() {
76         return mOption;
77     }
78 
setEnableOption(int option)79     public void setEnableOption(int option) {
80         mOption = option;
81     }
82 
setLanguage(String language)83     public void setLanguage(String language) {
84         mLanguage = language;
85     }
86 
87     /**
88      * Returns the track ID to be used as an alternative key.
89      */
getTrackId()90     public String getTrackId() {
91         return mTrackId;
92     }
93 
94     /**
95      * Sets the track ID to be used as an alternative key.
96      */
setTrackId(String trackId)97     public void setTrackId(String trackId) {
98         mTrackId = trackId;
99     }
100 }
101