• 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.development;
18 
19 import android.content.res.TypedArray;
20 import android.os.Bundle;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.Keep;
25 import androidx.leanback.preference.LeanbackPreferenceFragment;
26 import androidx.preference.ListPreference;
27 import androidx.preference.Preference;
28 import androidx.preference.TwoStatePreference;
29 
30 import com.android.tv.settings.R;
31 
32 @Keep
33 public class CaptionCustomFragment extends LeanbackPreferenceFragment implements
34         Preference.OnPreferenceChangeListener {
35 
36     private static final String KEY_FONT_FAMILY = "font_family";
37     private static final String KEY_TEXT_COLOR = "text_color";
38     private static final String KEY_TEXT_OPACITY = "text_opacity";
39     private static final String KEY_EDGE_TYPE = "edge_type";
40     private static final String KEY_EDGE_COLOR = "edge_color";
41     private static final String KEY_BACKGROUND_SHOW = "background_show";
42     private static final String KEY_BACKGROUND_COLOR = "background_color";
43     private static final String KEY_BACKGROUND_OPACITY = "background_opacity";
44     private static final String KEY_WINDOW_SHOW = "window_show";
45     private static final String KEY_WINDOW_COLOR = "window_color";
46     private static final String KEY_WINDOW_OPACITY = "window_opacity";
47 
48     private ListPreference mFontFamilyPref;
49     private ListPreference mTextColorPref;
50     private ListPreference mTextOpacityPref;
51     private ListPreference mEdgeTypePref;
52     private ListPreference mEdgeColorPref;
53     private TwoStatePreference mBackgroundShowPref;
54     private ListPreference mBackgroundColorPref;
55     private ListPreference mBackgroundOpacityPref;
56     private TwoStatePreference mWindowShowPref;
57     private ListPreference mWindowColorPref;
58     private ListPreference mWindowOpacityPref;
59 
60     @Override
onResume()61     public void onResume() {
62         super.onResume();
63         refresh();
64     }
65 
66     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)67     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
68         setPreferencesFromResource(R.xml.caption_custom, null);
69 
70         final TypedArray ta =
71                 getResources().obtainTypedArray(R.array.captioning_color_selector_ids);
72         final int colorLen = ta.length();
73         final String[] namedColors =
74                 getResources().getStringArray(R.array.captioning_color_selector_titles);
75         final String[] colorNames = new String[colorLen];
76         final String[] colorValues = new String[colorLen];
77         for (int i = 0; i < colorLen; i++) {
78             final int color = ta.getColor(i, 0);
79             colorValues[i] = Integer.toHexString(color & 0x00ffffff);
80             if (i < namedColors.length) {
81                 colorNames[i] = namedColors[i];
82             } else {
83                 colorNames[i] = String.format("#%06X", color & 0x00ffffff);
84             }
85         }
86         ta.recycle();
87 
88         mFontFamilyPref = (ListPreference) findPreference(KEY_FONT_FAMILY);
89         mFontFamilyPref.setOnPreferenceChangeListener(this);
90 
91         mTextColorPref = (ListPreference) findPreference(KEY_TEXT_COLOR);
92         mTextColorPref.setEntries(colorNames);
93         mTextColorPref.setEntryValues(colorValues);
94         mTextColorPref.setOnPreferenceChangeListener(this);
95 
96         mTextOpacityPref = (ListPreference) findPreference(KEY_TEXT_OPACITY);
97         mTextOpacityPref.setOnPreferenceChangeListener(this);
98         mEdgeTypePref = (ListPreference) findPreference(KEY_EDGE_TYPE);
99         mEdgeTypePref.setOnPreferenceChangeListener(this);
100 
101         mEdgeColorPref = (ListPreference) findPreference(KEY_EDGE_COLOR);
102         mEdgeColorPref.setEntries(colorNames);
103         mEdgeColorPref.setEntryValues(colorValues);
104         mEdgeColorPref.setOnPreferenceChangeListener(this);
105 
106         mBackgroundShowPref = (TwoStatePreference) findPreference(KEY_BACKGROUND_SHOW);
107 
108         mBackgroundColorPref = (ListPreference) findPreference(KEY_BACKGROUND_COLOR);
109         mBackgroundColorPref.setEntries(colorNames);
110         mBackgroundColorPref.setEntryValues(colorValues);
111         mBackgroundColorPref.setOnPreferenceChangeListener(this);
112 
113         mBackgroundOpacityPref = (ListPreference) findPreference(KEY_BACKGROUND_OPACITY);
114         mBackgroundOpacityPref.setOnPreferenceChangeListener(this);
115 
116         mWindowShowPref = (TwoStatePreference) findPreference(KEY_WINDOW_SHOW);
117 
118         mWindowColorPref = (ListPreference) findPreference(KEY_WINDOW_COLOR);
119         mWindowColorPref.setEntries(colorNames);
120         mWindowColorPref.setEntryValues(colorValues);
121         mWindowColorPref.setOnPreferenceChangeListener(this);
122 
123         mWindowOpacityPref = (ListPreference) findPreference(KEY_WINDOW_OPACITY);
124         mWindowOpacityPref.setOnPreferenceChangeListener(this);
125     }
126 
127     @Override
onPreferenceTreeClick(Preference preference)128     public boolean onPreferenceTreeClick(Preference preference) {
129         final String key = preference.getKey();
130         if (TextUtils.isEmpty(key)) {
131             return super.onPreferenceTreeClick(preference);
132         }
133         switch (key) {
134             case KEY_BACKGROUND_SHOW:
135                 setCaptionsBackgroundVisible(((TwoStatePreference) preference).isChecked());
136                 return true;
137             case KEY_WINDOW_SHOW:
138                 setCaptionsWindowVisible(((TwoStatePreference) preference).isChecked());
139                 return true;
140         }
141         return super.onPreferenceTreeClick(preference);
142     }
143 
144     @Override
onPreferenceChange(Preference preference, Object newValue)145     public boolean onPreferenceChange(Preference preference, Object newValue) {
146         final String key = preference.getKey();
147         if (TextUtils.isEmpty(key)) {
148             throw new IllegalStateException("Unknown preference change");
149         }
150         switch (key) {
151             case KEY_FONT_FAMILY:
152                 setCaptionsFontFamily((String) newValue);
153                 break;
154             case KEY_TEXT_COLOR:
155                 setCaptionsTextColor((String) newValue);
156                 break;
157             case KEY_TEXT_OPACITY:
158                 setCaptionsTextOpacity((String) newValue);
159                 break;
160             case KEY_EDGE_TYPE:
161                 setCaptionsEdgeType((String) newValue);
162                 break;
163             case KEY_EDGE_COLOR:
164                 setCaptionsEdgeColor((String) newValue);
165                 break;
166             case KEY_BACKGROUND_COLOR:
167                 setCaptionsBackgroundColor((String) newValue);
168                 break;
169             case KEY_BACKGROUND_OPACITY:
170                 setCaptionsBackgroundOpacity((String) newValue);
171                 break;
172             case KEY_WINDOW_COLOR:
173                 setCaptionsWindowColor((String) newValue);
174                 break;
175             case KEY_WINDOW_OPACITY:
176                 setCaptionsWindowOpacity((String) newValue);
177                 break;
178             default:
179                 throw new IllegalStateException("Preference change with unknown key " + key);
180         }
181         return true;
182     }
183 
refresh()184     private void refresh() {
185         mFontFamilyPref.setValue(getCaptionsFontFamily());
186         mTextColorPref.setValue(getCaptionsTextColor());
187         mTextOpacityPref.setValue(getCaptionsTextOpacity());
188         mEdgeTypePref.setValue(getCaptionsEdgeType());
189         mEdgeColorPref.setValue(getCaptionsEdgeColor());
190         mBackgroundShowPref.setChecked(isCaptionsBackgroundVisible());
191         mBackgroundColorPref.setValue(getCaptionsBackgroundColor());
192         mBackgroundOpacityPref.setValue(getCaptionsBackgroundOpacity());
193         mWindowShowPref.setChecked(isCaptionsWindowVisible());
194         mWindowColorPref.setValue(getCaptionsWindowColor());
195         mWindowOpacityPref.setValue(getCaptionsWindowOpacity());
196 
197     }
198 
getCaptionsFontFamily()199     private String getCaptionsFontFamily() {
200         final String typeface = Settings.Secure.getString(getContext().getContentResolver(),
201                 Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE);
202         return TextUtils.isEmpty(typeface) ? "default" : typeface;
203     }
204 
setCaptionsFontFamily(String fontFamily)205     private void setCaptionsFontFamily(String fontFamily) {
206         if (TextUtils.equals(fontFamily, "default")) {
207             Settings.Secure.putString(getContext().getContentResolver(),
208                     Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, null);
209         } else {
210             Settings.Secure.putString(getContext().getContentResolver(),
211                     Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, fontFamily);
212         }
213     }
214 
getCaptionsTextColor()215     private String getCaptionsTextColor() {
216         return Integer.toHexString(Settings.Secure.getInt(getContext().getContentResolver(),
217                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0) & 0x00ffffff);
218     }
219 
setCaptionsTextColor(String textColor)220     private void setCaptionsTextColor(String textColor) {
221         final int color = (int) Long.parseLong(textColor, 16) & 0x00ffffff;
222         final int alpha = Settings.Secure.getInt(getContext().getContentResolver(),
223                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0xff000000) & 0xff000000;
224         Settings.Secure.putInt(getContext().getContentResolver(),
225                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, color | alpha);
226     }
227 
getCaptionsTextOpacity()228     private String getCaptionsTextOpacity() {
229         return opacityToString(Settings.Secure.getInt(getContext().getContentResolver(),
230                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0) & 0xff000000);
231     }
232 
setCaptionsTextOpacity(String textOpacity)233     private void setCaptionsTextOpacity(String textOpacity) {
234         final int color = Settings.Secure.getInt(getContext().getContentResolver(),
235                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0) & 0x00ffffff;
236         final int alpha = (int) Long.parseLong(textOpacity, 16) & 0xff000000;
237         Settings.Secure.putInt(getContext().getContentResolver(),
238                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, color | alpha);
239     }
240 
getCaptionsEdgeType()241     private String getCaptionsEdgeType() {
242         return Integer.toString(Settings.Secure.getInt(getContext().getContentResolver(),
243                 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, 0));
244     }
245 
setCaptionsEdgeType(String edgeType)246     private void setCaptionsEdgeType(String edgeType) {
247         Settings.Secure.putInt(getContext().getContentResolver(),
248                 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, Integer.parseInt(edgeType));
249     }
250 
getCaptionsEdgeColor()251     private String getCaptionsEdgeColor() {
252         return Integer.toHexString(Settings.Secure.getInt(getContext().getContentResolver(),
253                 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, 0) & 0x00ffffff);
254     }
255 
setCaptionsEdgeColor(String edgeColor)256     private void setCaptionsEdgeColor(String edgeColor) {
257         Settings.Secure.putInt(getContext().getContentResolver(),
258                 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR,
259                 0xff000000 | (int) Long.parseLong(edgeColor, 16));
260     }
261 
isCaptionsBackgroundVisible()262     private boolean isCaptionsBackgroundVisible() {
263         return (Settings.Secure.getInt(getContext().getContentResolver(),
264                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0) & 0xff000000) != 0;
265     }
266 
setCaptionsBackgroundVisible(boolean visible)267     private void setCaptionsBackgroundVisible(boolean visible) {
268         Settings.Secure.putInt(getContext().getContentResolver(),
269                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR,
270                 visible ? 0xff000000 : 0);
271         if (!visible) {
272             mBackgroundColorPref.setValue(Integer.toHexString(0));
273             mBackgroundOpacityPref.setValue(opacityToString(0));
274         }
275     }
276 
getCaptionsBackgroundColor()277     private String getCaptionsBackgroundColor() {
278         return Integer.toHexString(Settings.Secure.getInt(getContext().getContentResolver(),
279                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0) & 0x00ffffff);
280     }
281 
setCaptionsBackgroundColor(String backgroundColor)282     private void setCaptionsBackgroundColor(String backgroundColor) {
283         final int color = (int) Long.parseLong(backgroundColor, 16) & 0x00ffffff;
284         final int alpha = Settings.Secure.getInt(getContext().getContentResolver(),
285                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0xff000000) & 0xff000000;
286         Settings.Secure.putInt(getContext().getContentResolver(),
287                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, color | alpha);
288     }
289 
getCaptionsBackgroundOpacity()290     private String getCaptionsBackgroundOpacity() {
291         return opacityToString (Settings.Secure.getInt(getContext().getContentResolver(),
292                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0) & 0xff000000);
293     }
294 
setCaptionsBackgroundOpacity(String backgroundOpacity)295     private void setCaptionsBackgroundOpacity(String backgroundOpacity) {
296         final int color = Settings.Secure.getInt(getContext().getContentResolver(),
297                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0) & 0x00ffffff;
298         final int alpha = (int) Long.parseLong(backgroundOpacity, 16) & 0xff000000;
299         Settings.Secure.putInt(getContext().getContentResolver(),
300                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, color | alpha);
301     }
302 
isCaptionsWindowVisible()303     private boolean isCaptionsWindowVisible() {
304         return (Settings.Secure.getInt(getContext().getContentResolver(),
305                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0) & 0xff000000) != 0;
306     }
307 
setCaptionsWindowVisible(boolean visible)308     private void setCaptionsWindowVisible(boolean visible) {
309         Settings.Secure.putInt(getContext().getContentResolver(),
310                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, visible ? 0xff000000 : 0);
311         if (!visible) {
312             mWindowColorPref.setValue(Integer.toHexString(0));
313             mWindowOpacityPref.setValue(opacityToString(0));
314         }
315     }
316 
getCaptionsWindowColor()317     private String getCaptionsWindowColor() {
318         return Integer.toHexString(Settings.Secure.getInt(getContext().getContentResolver(),
319                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0) & 0x00ffffff);
320     }
321 
setCaptionsWindowColor(String windowColor)322     private void setCaptionsWindowColor(String windowColor) {
323         final int color = (int) Long.parseLong(windowColor, 16) & 0x00ffffff;
324         final int alpha = Settings.Secure.getInt(getContext().getContentResolver(),
325                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0xff000000) & 0xff000000;
326         Settings.Secure.putInt(getContext().getContentResolver(),
327                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, color | alpha);
328     }
329 
getCaptionsWindowOpacity()330     private String getCaptionsWindowOpacity() {
331         return opacityToString(Settings.Secure.getInt(getContext().getContentResolver(),
332                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0) & 0xff000000);
333     }
334 
setCaptionsWindowOpacity(String windowOpacity)335     private void setCaptionsWindowOpacity(String windowOpacity) {
336         final int color = Settings.Secure.getInt(getContext().getContentResolver(),
337                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0) & 0x00ffffff;
338         final int alpha = (int) Long.parseLong(windowOpacity, 16) & 0xff000000;
339         Settings.Secure.putInt(getContext().getContentResolver(),
340                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, color | alpha);
341     }
342 
opacityToString(int opacity)343     private String opacityToString(int opacity) {
344         switch (opacity & 0xff000000) {
345             case 0x40000000:
346                 return "40FFFFFF";
347             case 0x80000000:
348                 return "80FFFFFF";
349             case 0xc0000000:
350                 return "C0FFFFFF";
351             case 0xff000000:
352             default:
353                 return "FFFFFFFF";
354         }
355     }
356 }
357