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 59 public static CaptionFragment newInstance() { 60 return new CaptionFragment(); 61 } 62 63 @Override 64 public void onResume() { 65 super.onResume(); 66 refresh(); 67 } 68 69 @Override 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() + 1]; 78 final String[] langLocales = new String[localeInfoList.size() + 1]; 79 langNames[0] = getString(R.string.captions_language_default); 80 langLocales[0] = ""; 81 int i = 1; 82 for (final LocalePicker.LocaleInfo info : localeInfoList) { 83 langNames[i] = info.toString(); 84 langLocales[i] = info.getLocale().toString(); 85 i++; 86 } 87 mCaptionsLanguagePref = (ListPreference) findPreference(KEY_CAPTIONS_LANGUAGE); 88 mCaptionsLanguagePref.setEntries(langNames); 89 mCaptionsLanguagePref.setEntryValues(langLocales); 90 mCaptionsLanguagePref.setOnPreferenceChangeListener(this); 91 92 mCaptionsTextSizePref = (ListPreference) findPreference(KEY_CAPTIONS_TEXT_SIZE); 93 mCaptionsTextSizePref.setOnPreferenceChangeListener(this); 94 95 mCaptionsStyleGroup = (PreferenceGroup) findPreference(KEY_CAPTIONS_STYLE_GROUP); 96 97 mCaptionsStyle0Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_0); 98 mCaptionsStyle1Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_1); 99 mCaptionsStyle2Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_2); 100 mCaptionsStyle3Pref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_3); 101 mCaptionsStyleCustomPref = (RadioPreference) findPreference(KEY_CAPTIONS_STYLE_CUSTOM); 102 } 103 104 @Override 105 public boolean onPreferenceTreeClick(Preference preference) { 106 final String key = preference.getKey(); 107 if (TextUtils.isEmpty(key)) { 108 return super.onPreferenceTreeClick(preference); 109 } 110 switch (key) { 111 case KEY_CAPTIONS_DISPLAY: 112 setCaptionsEnabled(((TwoStatePreference) preference).isChecked()); 113 return true; 114 case KEY_CAPTIONS_STYLE_0: 115 setCaptionsStyle(0); 116 mCaptionsStyle0Pref.setChecked(true); 117 mCaptionsStyle0Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 118 return true; 119 case KEY_CAPTIONS_STYLE_1: 120 setCaptionsStyle(1); 121 mCaptionsStyle1Pref.setChecked(true); 122 mCaptionsStyle1Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 123 return true; 124 case KEY_CAPTIONS_STYLE_2: 125 setCaptionsStyle(2); 126 mCaptionsStyle2Pref.setChecked(true); 127 mCaptionsStyle2Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 128 return true; 129 case KEY_CAPTIONS_STYLE_3: 130 setCaptionsStyle(3); 131 mCaptionsStyle3Pref.setChecked(true); 132 mCaptionsStyle3Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 133 return true; 134 case KEY_CAPTIONS_STYLE_CUSTOM: 135 setCaptionsStyle(-1); 136 mCaptionsStyleCustomPref.setChecked(true); 137 mCaptionsStyleCustomPref.clearOtherRadioPreferences(mCaptionsStyleGroup); 138 // Call to super to show custom configuration screen 139 return super.onPreferenceTreeClick(preference); 140 } 141 return super.onPreferenceTreeClick(preference); 142 } 143 144 @Override 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_CAPTIONS_LANGUAGE: 152 setCaptionsLocale((String) newValue); 153 break; 154 case KEY_CAPTIONS_TEXT_SIZE: 155 setCaptionsTextSize((String) newValue); 156 break; 157 default: 158 throw new IllegalStateException("Preference change with unknown key " + key); 159 } 160 return true; 161 } 162 163 private void refresh() { 164 mCaptionsDisplayPref.setChecked(getCaptionsEnabled()); 165 mCaptionsLanguagePref.setValue(getCaptionsLocale()); 166 mCaptionsTextSizePref.setValue(getCaptionsTextSize()); 167 switch (getCaptionsStyle()) { 168 default: 169 case 0: 170 mCaptionsStyle0Pref.setChecked(true); 171 mCaptionsStyle0Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 172 break; 173 case 1: 174 mCaptionsStyle1Pref.setChecked(true); 175 mCaptionsStyle1Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 176 break; 177 case 2: 178 mCaptionsStyle2Pref.setChecked(true); 179 mCaptionsStyle2Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 180 break; 181 case 3: 182 mCaptionsStyle3Pref.setChecked(true); 183 mCaptionsStyle3Pref.clearOtherRadioPreferences(mCaptionsStyleGroup); 184 break; 185 case -1: 186 mCaptionsStyleCustomPref.setChecked(true); 187 mCaptionsStyleCustomPref.clearOtherRadioPreferences(mCaptionsStyleGroup); 188 break; 189 } 190 } 191 192 private boolean getCaptionsEnabled() { 193 return Settings.Secure.getInt(getContext().getContentResolver(), 194 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, 0) != 0; 195 } 196 197 private void setCaptionsEnabled(boolean enabled) { 198 Settings.Secure.putInt(getContext().getContentResolver(), 199 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, enabled ? 1 : 0); 200 } 201 202 private int getCaptionsStyle() { 203 return Settings.Secure.getInt(getContext().getContentResolver(), 204 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0); 205 } 206 207 private void setCaptionsStyle(int style) { 208 Settings.Secure.putInt(getContext().getContentResolver(), 209 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, style); 210 LocalBroadcastManager.getInstance(getContext()) 211 .sendBroadcast(new Intent(CaptionSettingsFragment.ACTION_REFRESH_CAPTIONS_PREVIEW)); 212 } 213 214 private String getCaptionsLocale() { 215 final String captionLocale = Settings.Secure.getString(getContext().getContentResolver(), 216 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE); 217 return captionLocale == null ? "" : captionLocale; 218 } 219 220 private void setCaptionsLocale(String locale) { 221 Settings.Secure.putString(getContext().getContentResolver(), 222 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, locale); 223 } 224 225 private String getCaptionsTextSize() { 226 final float textSize = Settings.Secure.getFloat(getContext().getContentResolver(), 227 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 1); 228 229 if (0 <= textSize && textSize < .375) { 230 return "0.25"; 231 } else if (textSize < .75) { 232 return "0.5"; 233 } else if (textSize < 1.25) { 234 return "1.0"; 235 } else if (textSize < 1.75) { 236 return "1.5"; 237 } else if (textSize < 2.5) { 238 return "2.0"; 239 } 240 241 return "1.0"; 242 } 243 244 private void setCaptionsTextSize(String textSize) { 245 Settings.Secure.putFloat(getContext().getContentResolver(), 246 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, Float.parseFloat(textSize)); 247 } 248 } 249