• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.system;
18 
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.provider.Settings;
22 import android.support.v4.content.LocalBroadcastManager;
23 import android.support.v7.preference.ListPreference;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceGroup;
26 import android.support.v7.preference.TwoStatePreference;
27 import android.text.TextUtils;
28 
29 import com.android.internal.app.LocalePicker;
30 import com.android.internal.logging.nano.MetricsProto;
31 import com.android.tv.settings.R;
32 import com.android.tv.settings.RadioPreference;
33 import com.android.tv.settings.SettingsPreferenceFragment;
34 
35 import java.util.List;
36 
37 /**
38  * The "Captions" screen in TV settings.
39  */
40 public class CaptionFragment extends SettingsPreferenceFragment implements
41         Preference.OnPreferenceChangeListener {
42 
43     private static final String KEY_CAPTIONS_DISPLAY = "captions_display";
44     private static final String KEY_CAPTIONS_LANGUAGE = "captions_language";
45     private static final String KEY_CAPTIONS_TEXT_SIZE = "captions_text_size";
46     private static final String KEY_CAPTIONS_STYLE_GROUP = "captions_style";
47     private static final String KEY_CAPTIONS_STYLE_0 = "captions_style_0";
48     private static final String KEY_CAPTIONS_STYLE_1 = "captions_style_1";
49     private static final String KEY_CAPTIONS_STYLE_2 = "captions_style_2";
50     private static final String KEY_CAPTIONS_STYLE_3 = "captions_style_3";
51     private static final String KEY_CAPTIONS_STYLE_CUSTOM = "captions_style_custom";
52 
53     private TwoStatePreference mCaptionsDisplayPref;
54     private ListPreference mCaptionsLanguagePref;
55     private ListPreference mCaptionsTextSizePref;
56     private PreferenceGroup mCaptionsStyleGroup;
57     private RadioPreference mCaptionsStyle0Pref;
58     private RadioPreference mCaptionsStyle1Pref;
59     private RadioPreference mCaptionsStyle2Pref;
60     private RadioPreference mCaptionsStyle3Pref;
61     private RadioPreference mCaptionsStyleCustomPref;
62 
newInstance()63     public static CaptionFragment newInstance() {
64         return new CaptionFragment();
65     }
66 
67     @Override
onResume()68     public void onResume() {
69         super.onResume();
70         refresh();
71     }
72 
73     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)74     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
75         setPreferencesFromResource(R.xml.caption, null);
76 
77         mCaptionsDisplayPref = (TwoStatePreference) findPreference(KEY_CAPTIONS_DISPLAY);
78 
79         final List<LocalePicker.LocaleInfo> localeInfoList =
80                 LocalePicker.getAllAssetLocales(getContext(), false);
81         final String[] langNames = new String[localeInfoList.size() + 1];
82         final String[] langLocales = new String[localeInfoList.size() + 1];
83         langNames[0] = getString(R.string.captions_language_default);
84         langLocales[0] = "";
85         int i = 1;
86         for (final LocalePicker.LocaleInfo info : localeInfoList) {
87             langNames[i] = info.toString();
88             langLocales[i] = info.getLocale().toString();
89             i++;
90         }
91         mCaptionsLanguagePref = (ListPreference) findPreference(KEY_CAPTIONS_LANGUAGE);
92         mCaptionsLanguagePref.setEntries(langNames);
93         mCaptionsLanguagePref.setEntryValues(langLocales);
94         mCaptionsLanguagePref.setOnPreferenceChangeListener(this);
95 
96         mCaptionsTextSizePref = (ListPreference) findPreference(KEY_CAPTIONS_TEXT_SIZE);
97         mCaptionsTextSizePref.setOnPreferenceChangeListener(this);
98 
99         mCaptionsStyleGroup = (PreferenceGroup) findPreference(KEY_CAPTIONS_STYLE_GROUP);
100 
101         mCaptionsStyle0Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_0);
102         mCaptionsStyle1Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_1);
103         mCaptionsStyle2Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_2);
104         mCaptionsStyle3Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_3);
105         mCaptionsStyleCustomPref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_CUSTOM);
106     }
107 
108     @Override
onPreferenceTreeClick(Preference preference)109     public boolean onPreferenceTreeClick(Preference preference) {
110         final String key = preference.getKey();
111         if (TextUtils.isEmpty(key)) {
112             return super.onPreferenceTreeClick(preference);
113         }
114         switch (key) {
115             case KEY_CAPTIONS_DISPLAY:
116                 setCaptionsEnabled(((TwoStatePreference) preference).isChecked());
117                 return true;
118             case KEY_CAPTIONS_STYLE_0:
119                 setCaptionsStyle(0);
120                 mCaptionsStyle0Pref.setChecked(true);
121                 mCaptionsStyle0Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
122                 return true;
123             case KEY_CAPTIONS_STYLE_1:
124                 setCaptionsStyle(1);
125                 mCaptionsStyle1Pref.setChecked(true);
126                 mCaptionsStyle1Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
127                 return true;
128             case KEY_CAPTIONS_STYLE_2:
129                 setCaptionsStyle(2);
130                 mCaptionsStyle2Pref.setChecked(true);
131                 mCaptionsStyle2Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
132                 return true;
133             case KEY_CAPTIONS_STYLE_3:
134                 setCaptionsStyle(3);
135                 mCaptionsStyle3Pref.setChecked(true);
136                 mCaptionsStyle3Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
137                 return true;
138             case KEY_CAPTIONS_STYLE_CUSTOM:
139                 setCaptionsStyle(-1);
140                 mCaptionsStyleCustomPref.setChecked(true);
141                 mCaptionsStyleCustomPref.clearOtherRadioPreferences(mCaptionsStyleGroup);
142                 // Call to super to show custom configuration screen
143                 return super.onPreferenceTreeClick(preference);
144         }
145         return super.onPreferenceTreeClick(preference);
146     }
147 
148     @Override
onPreferenceChange(Preference preference, Object newValue)149     public boolean onPreferenceChange(Preference preference, Object newValue) {
150         final String key = preference.getKey();
151         if (TextUtils.isEmpty(key)) {
152             throw new IllegalStateException("Unknown preference change");
153         }
154         switch (key) {
155             case KEY_CAPTIONS_LANGUAGE:
156                 setCaptionsLocale((String) newValue);
157                 break;
158             case KEY_CAPTIONS_TEXT_SIZE:
159                 setCaptionsTextSize((String) newValue);
160                 break;
161             default:
162                 throw new IllegalStateException("Preference change with unknown key " + key);
163         }
164         return true;
165     }
166 
refresh()167     private void refresh() {
168         mCaptionsDisplayPref.setChecked(getCaptionsEnabled());
169         mCaptionsLanguagePref.setValue(getCaptionsLocale());
170         mCaptionsTextSizePref.setValue(getCaptionsTextSize());
171         switch (getCaptionsStyle()) {
172             default:
173             case 0:
174                 mCaptionsStyle0Pref.setChecked(true);
175                 mCaptionsStyle0Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
176                 break;
177             case 1:
178                 mCaptionsStyle1Pref.setChecked(true);
179                 mCaptionsStyle1Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
180                 break;
181             case 2:
182                 mCaptionsStyle2Pref.setChecked(true);
183                 mCaptionsStyle2Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
184                 break;
185             case 3:
186                 mCaptionsStyle3Pref.setChecked(true);
187                 mCaptionsStyle3Pref.clearOtherRadioPreferences(mCaptionsStyleGroup);
188                 break;
189             case -1:
190                 mCaptionsStyleCustomPref.setChecked(true);
191                 mCaptionsStyleCustomPref.clearOtherRadioPreferences(mCaptionsStyleGroup);
192                 break;
193         }
194     }
195 
getCaptionsEnabled()196     private boolean getCaptionsEnabled() {
197         return Settings.Secure.getInt(getContext().getContentResolver(),
198             Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, 0) != 0;
199     }
200 
setCaptionsEnabled(boolean enabled)201     private void setCaptionsEnabled(boolean enabled) {
202         Settings.Secure.putInt(getContext().getContentResolver(),
203                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, enabled ? 1 : 0);
204     }
205 
getCaptionsStyle()206     private int getCaptionsStyle() {
207         return Settings.Secure.getInt(getContext().getContentResolver(),
208                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0);
209     }
210 
setCaptionsStyle(int style)211     private void setCaptionsStyle(int style) {
212         Settings.Secure.putInt(getContext().getContentResolver(),
213                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, style);
214         LocalBroadcastManager.getInstance(getContext())
215                 .sendBroadcast(new Intent(CaptionSettingsFragment.ACTION_REFRESH_CAPTIONS_PREVIEW));
216     }
217 
getCaptionsLocale()218     private String getCaptionsLocale() {
219         final String captionLocale = Settings.Secure.getString(getContext().getContentResolver(),
220                 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE);
221         return captionLocale == null ? "" : captionLocale;
222     }
223 
setCaptionsLocale(String locale)224     private void setCaptionsLocale(String locale) {
225         Settings.Secure.putString(getContext().getContentResolver(),
226                 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, locale);
227     }
228 
getCaptionsTextSize()229     private String getCaptionsTextSize() {
230         final float textSize = Settings.Secure.getFloat(getContext().getContentResolver(),
231                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 1);
232 
233         if (0 <= textSize && textSize < .375) {
234             return "0.25";
235         } else if (textSize < .75) {
236             return "0.5";
237         } else if (textSize < 1.25) {
238             return "1.0";
239         } else if (textSize < 1.75) {
240             return "1.5";
241         } else if (textSize < 2.5) {
242             return "2.0";
243         }
244 
245         return "1.0";
246     }
247 
setCaptionsTextSize(String textSize)248     private void setCaptionsTextSize(String textSize) {
249         Settings.Secure.putFloat(getContext().getContentResolver(),
250                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, Float.parseFloat(textSize));
251     }
252 
253     @Override
getMetricsCategory()254     public int getMetricsCategory() {
255         return MetricsProto.MetricsEvent.ACCESSIBILITY_CAPTION_PROPERTIES;
256     }
257 }
258