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.os.Build;
21 import android.os.LocaleList;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import android.support.annotation.Size;
25 import android.view.accessibility.CaptioningManager;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
30 
31 public class CaptionSettings {
32     public static final int OPTION_SYSTEM = 0;
33     public static final int OPTION_OFF = 1;
34     public static final int OPTION_ON = 2;
35 
36     private final CaptioningManager mCaptioningManager;
37     private int mOption = OPTION_SYSTEM;
38     private String mLanguage;
39     private String mTrackId;
40 
CaptionSettings(Context context)41     public CaptionSettings(Context context) {
42         mCaptioningManager =
43                 (CaptioningManager) context.getSystemService(Context.CAPTIONING_SERVICE);
44     }
45 
46     /** @deprecated use {@link #getSystemPreferenceLanguageList} instead. */
47     @Deprecated
getSystemLanguage()48     public final String getSystemLanguage() {
49         Locale l = mCaptioningManager.getLocale();
50         if (l != null) {
51             return l.getLanguage();
52         }
53         return null;
54     }
55 
56     @NonNull
57     @Size(min=1)
getSystemPreferenceLanguageList()58     public final List<String> getSystemPreferenceLanguageList() {
59         List<String> languageList = new ArrayList<>();
60         Locale l = mCaptioningManager.getLocale();
61         if (l != null) {
62             languageList.add(l.getLanguage());
63         }
64         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
65             LocaleList locales = LocaleList.getDefault();
66             for (int i = 0; i < locales.size() ; i++) {
67                 languageList.add(locales.get(i).getLanguage());
68             }
69         } else {
70             languageList.add(Locale.getDefault().getLanguage());
71         }
72         return languageList;
73     }
74 
75     /** Returns the language of closed captions based on options. */
76     @Nullable
getLanguage()77     public final String getLanguage() {
78         switch (mOption) {
79             case OPTION_SYSTEM:
80                 return getSystemPreferenceLanguageList().get(0);
81             case OPTION_OFF:
82                 return null;
83             case OPTION_ON:
84                 return mLanguage;
85         }
86         return null;
87     }
88 
isSystemSettingEnabled()89     public final boolean isSystemSettingEnabled() {
90         return mCaptioningManager.isEnabled();
91     }
92 
isEnabled()93     public final boolean isEnabled() {
94         switch (mOption) {
95             case OPTION_SYSTEM:
96                 return isSystemSettingEnabled();
97             case OPTION_OFF:
98                 return false;
99             case OPTION_ON:
100                 return true;
101         }
102         return false;
103     }
104 
getEnableOption()105     public int getEnableOption() {
106         return mOption;
107     }
108 
setEnableOption(int option)109     public void setEnableOption(int option) {
110         mOption = option;
111     }
112 
setLanguage(String language)113     public void setLanguage(String language) {
114         mLanguage = language;
115     }
116 
117     /** Returns the track ID to be used as an alternative key. */
getTrackId()118     public String getTrackId() {
119         return mTrackId;
120     }
121 
122     /** Sets the track ID to be used as an alternative key. */
setTrackId(String trackId)123     public void setTrackId(String trackId) {
124         mTrackId = trackId;
125     }
126 }
127