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