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.settings.system; 18 19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected; 20 21 import android.app.tvsettings.TvSettingsEnums; 22 import android.content.ActivityNotFoundException; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.os.Bundle; 28 import android.speech.tts.TextToSpeech; 29 import android.speech.tts.TtsEngines; 30 import android.text.TextUtils; 31 import android.util.Log; 32 import android.util.Pair; 33 34 import androidx.annotation.Keep; 35 import androidx.annotation.NonNull; 36 import androidx.preference.ListPreference; 37 import androidx.preference.Preference; 38 import androidx.preference.PreferenceScreen; 39 40 import com.android.tv.settings.R; 41 import com.android.tv.settings.SettingsPreferenceFragment; 42 43 import java.util.ArrayList; 44 import java.util.Locale; 45 46 /** 47 * The text-to-speech engine settings screen in TV Settings. 48 */ 49 @Keep 50 public class TtsEngineSettingsFragment extends SettingsPreferenceFragment implements 51 Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { 52 private static final String TAG = "TtsEngineSettings"; 53 private static final boolean DBG = false; 54 55 /** 56 * Key for the name of the TTS engine passed in to the engine 57 * settings fragment {@link TtsEngineSettingsFragment}. 58 */ 59 private static final String ARG_ENGINE_NAME = "engineName"; 60 61 /** 62 * Key for the label of the TTS engine passed in to the engine 63 * settings fragment. This is used as the title of the fragment 64 * {@link TtsEngineSettingsFragment}. 65 */ 66 private static final String ARG_ENGINE_LABEL = "engineLabel"; 67 68 /** 69 * Key for the voice data data passed in to the engine settings 70 * fragmetn {@link TtsEngineSettingsFragment}. 71 */ 72 private static final String ARG_VOICES = "voices"; 73 74 75 private static final String KEY_ENGINE_LOCALE = "tts_default_lang"; 76 private static final String KEY_ENGINE_SETTINGS = "tts_engine_settings"; 77 private static final String KEY_INSTALL_DATA = "tts_install_data"; 78 79 private static final String STATE_KEY_LOCALE_ENTRIES = "locale_entries"; 80 private static final String STATE_KEY_LOCALE_ENTRY_VALUES= "locale_entry_values"; 81 private static final String STATE_KEY_LOCALE_VALUE = "locale_value"; 82 83 private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; 84 85 private TtsEngines mEnginesHelper; 86 private ListPreference mLocalePreference; 87 private Preference mEngineSettingsPreference; 88 private Preference mInstallVoicesPreference; 89 private Intent mVoiceDataDetails; 90 91 private TextToSpeech mTts; 92 93 private int mSelectedLocaleIndex = -1; 94 95 private final TextToSpeech.OnInitListener mTtsInitListener = new TextToSpeech.OnInitListener() { 96 @Override 97 public void onInit(int status) { 98 if (status != TextToSpeech.SUCCESS) { 99 getFragmentManager().popBackStack(); 100 } else { 101 getActivity().runOnUiThread(new Runnable() { 102 @Override 103 public void run() { 104 mLocalePreference.setEnabled(true); 105 } 106 }); 107 } 108 } 109 }; 110 111 private final BroadcastReceiver mLanguagesChangedReceiver = new BroadcastReceiver() { 112 @Override 113 public void onReceive(Context context, Intent intent) { 114 // Installed or uninstalled some data packs 115 if (TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED.equals(intent.getAction())) { 116 checkTtsData(); 117 } 118 } 119 }; 120 prepareArgs(@onNull Bundle args, String engineName, String engineLabel, Intent voiceCheckData)121 public static void prepareArgs(@NonNull Bundle args, String engineName, String engineLabel, 122 Intent voiceCheckData) { 123 args.clear(); 124 125 args.putString(ARG_ENGINE_NAME, engineName); 126 args.putString(ARG_ENGINE_LABEL, engineLabel); 127 args.putParcelable(ARG_VOICES, voiceCheckData); 128 } 129 TtsEngineSettingsFragment()130 public TtsEngineSettingsFragment() {} 131 132 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)133 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 134 135 addPreferencesFromResource(R.xml.tts_engine_settings); 136 137 final PreferenceScreen screen = getPreferenceScreen(); 138 screen.setTitle(getEngineLabel()); 139 screen.setKey(getEngineName()); 140 141 mLocalePreference = (ListPreference) findPreference(KEY_ENGINE_LOCALE); 142 mLocalePreference.setOnPreferenceChangeListener(this); 143 mEngineSettingsPreference = findPreference(KEY_ENGINE_SETTINGS); 144 mEngineSettingsPreference.setOnPreferenceClickListener(this); 145 mInstallVoicesPreference = findPreference(KEY_INSTALL_DATA); 146 mInstallVoicesPreference.setOnPreferenceClickListener(this); 147 148 mEngineSettingsPreference.setTitle(getResources().getString( 149 R.string.tts_engine_settings_title, getEngineLabel())); 150 final Intent settingsIntent = mEnginesHelper.getSettingsIntent(getEngineName()); 151 mEngineSettingsPreference.setIntent(settingsIntent); 152 if (settingsIntent == null) { 153 mEngineSettingsPreference.setEnabled(false); 154 } 155 mInstallVoicesPreference.setEnabled(false); 156 157 if (savedInstanceState == null) { 158 mLocalePreference.setEnabled(false); 159 mLocalePreference.setEntries(new CharSequence[0]); 160 mLocalePreference.setEntryValues(new CharSequence[0]); 161 } else { 162 // Repopulate mLocalePreference with saved state. Will be updated later with 163 // up-to-date values when checkTtsData() calls back with results. 164 final CharSequence[] entries = 165 savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRIES); 166 final CharSequence[] entryValues = 167 savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRY_VALUES); 168 final CharSequence value = 169 savedInstanceState.getCharSequence(STATE_KEY_LOCALE_VALUE); 170 171 mLocalePreference.setEntries(entries); 172 mLocalePreference.setEntryValues(entryValues); 173 mLocalePreference.setValue(value != null ? value.toString() : null); 174 mLocalePreference.setEnabled(entries.length > 0); 175 } 176 177 } 178 179 @Override onCreate(Bundle savedInstanceState)180 public void onCreate(Bundle savedInstanceState) { 181 mEnginesHelper = new TtsEngines(getActivity()); 182 183 super.onCreate(savedInstanceState); 184 185 mVoiceDataDetails = getArguments().getParcelable(ARG_VOICES); 186 187 mTts = new TextToSpeech(getActivity().getApplicationContext(), mTtsInitListener, 188 getEngineName()); 189 190 // Check if data packs changed 191 checkTtsData(); 192 193 getActivity().registerReceiver(mLanguagesChangedReceiver, 194 new IntentFilter(TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED), 195 Context.RECEIVER_EXPORTED_UNAUDITED); 196 } 197 198 @Override onDestroy()199 public void onDestroy() { 200 getActivity().unregisterReceiver(mLanguagesChangedReceiver); 201 mTts.shutdown(); 202 super.onDestroy(); 203 } 204 205 @Override onSaveInstanceState(Bundle outState)206 public void onSaveInstanceState(Bundle outState) { 207 super.onSaveInstanceState(outState); 208 209 // Save the mLocalePreference values, so we can repopulate it with entries. 210 outState.putCharSequenceArray(STATE_KEY_LOCALE_ENTRIES, 211 mLocalePreference.getEntries()); 212 outState.putCharSequenceArray(STATE_KEY_LOCALE_ENTRY_VALUES, 213 mLocalePreference.getEntryValues()); 214 outState.putCharSequence(STATE_KEY_LOCALE_VALUE, 215 mLocalePreference.getValue()); 216 } 217 checkTtsData()218 private void checkTtsData() { 219 Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 220 intent.setPackage(getEngineName()); 221 try { 222 if (DBG) Log.d(TAG, "Updating engine: Checking voice data: " + intent.toUri(0)); 223 startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); 224 } catch (ActivityNotFoundException ex) { 225 Log.e(TAG, "Failed to check TTS data, no activity found for " + intent + ")"); 226 } 227 } 228 229 @Override onActivityResult(int requestCode, int resultCode, Intent data)230 public void onActivityResult(int requestCode, int resultCode, Intent data) { 231 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { 232 if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL) { 233 updateVoiceDetails(data); 234 } else { 235 Log.e(TAG, "CheckVoiceData activity failed"); 236 } 237 } 238 } 239 updateVoiceDetails(Intent data)240 private void updateVoiceDetails(Intent data) { 241 if (data == null){ 242 Log.e(TAG, "Engine failed voice data integrity check (null return)" + 243 mTts.getCurrentEngine()); 244 return; 245 } 246 mVoiceDataDetails = data; 247 248 if (DBG) Log.d(TAG, "Parsing voice data details, data: " + mVoiceDataDetails.toUri(0)); 249 250 final ArrayList<String> available = mVoiceDataDetails.getStringArrayListExtra( 251 TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); 252 final ArrayList<String> unavailable = mVoiceDataDetails.getStringArrayListExtra( 253 TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES); 254 255 if (unavailable != null && unavailable.size() > 0) { 256 mInstallVoicesPreference.setEnabled(true); 257 } else { 258 mInstallVoicesPreference.setEnabled(false); 259 } 260 261 if (available == null){ 262 Log.e(TAG, "TTS data check failed (available == null)."); 263 mLocalePreference.setEnabled(false); 264 } else { 265 updateDefaultLocalePref(available); 266 } 267 } 268 updateDefaultLocalePref(ArrayList<String> availableLangs)269 private void updateDefaultLocalePref(ArrayList<String> availableLangs) { 270 if (availableLangs == null || availableLangs.size() == 0) { 271 mLocalePreference.setEnabled(false); 272 return; 273 } 274 Locale currentLocale = null; 275 if (!mEnginesHelper.isLocaleSetToDefaultForEngine(getEngineName())) { 276 currentLocale = mEnginesHelper.getLocalePrefForEngine(getEngineName()); 277 } 278 279 ArrayList<Pair<String, Locale>> entryPairs = 280 new ArrayList<>(availableLangs.size()); 281 for (int i = 0; i < availableLangs.size(); i++) { 282 Locale locale = mEnginesHelper.parseLocaleString(availableLangs.get(i)); 283 if (locale != null){ 284 entryPairs.add(new Pair<>(locale.getDisplayName(), locale)); 285 } 286 } 287 288 // Sort it 289 entryPairs.sort((lhs, rhs) -> lhs.first.compareToIgnoreCase(rhs.first)); 290 291 // Get two arrays out of one of pairs 292 mSelectedLocaleIndex = 0; // Will point to the R.string.tts_lang_use_system value 293 CharSequence[] entries = new CharSequence[availableLangs.size()+1]; 294 CharSequence[] entryValues = new CharSequence[availableLangs.size()+1]; 295 296 entries[0] = getString(R.string.tts_lang_use_system); 297 entryValues[0] = ""; 298 299 int i = 1; 300 for (Pair<String, Locale> entry : entryPairs) { 301 if (entry.second.equals(currentLocale)) { 302 mSelectedLocaleIndex = i; 303 } 304 entries[i] = entry.first; 305 entryValues[i++] = entry.second.toString(); 306 } 307 308 mLocalePreference.setEntries(entries); 309 mLocalePreference.setEntryValues(entryValues); 310 mLocalePreference.setEnabled(true); 311 setLocalePreference(mSelectedLocaleIndex); 312 } 313 314 /** Set entry from entry table in mLocalePreference */ setLocalePreference(int index)315 private void setLocalePreference(int index) { 316 if (index < 0) { 317 mLocalePreference.setValue(""); 318 mLocalePreference.setSummary(R.string.tts_lang_not_selected); 319 } else { 320 mLocalePreference.setValueIndex(index); 321 mLocalePreference.setSummary(mLocalePreference.getEntries()[index]); 322 } 323 } 324 325 /** 326 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity 327 * so the required TTS files are properly installed. 328 */ installVoiceData()329 private void installVoiceData() { 330 if (TextUtils.isEmpty(getEngineName())) return; 331 Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 332 intent.setPackage(getEngineName()); 333 try { 334 startActivity(intent); 335 } catch (ActivityNotFoundException ex) { 336 Log.e(TAG, "Failed to install TTS data, no activity found for " + intent + ")"); 337 } 338 } 339 340 @Override onPreferenceClick(Preference preference)341 public boolean onPreferenceClick(Preference preference) { 342 if (preference == mInstallVoicesPreference) { 343 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_TTS_ENGINE_CONFIG_INSTALL_VOICE_DATA); 344 installVoiceData(); 345 return true; 346 } else if (preference == mEngineSettingsPreference) { 347 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_TTS_ENGINE_CONFIG_SETTINGS_GTTS_ENGINE); 348 return false; 349 } 350 351 return false; 352 } 353 354 @Override onPreferenceChange(Preference preference, Object newValue)355 public boolean onPreferenceChange(Preference preference, Object newValue) { 356 if (preference == mLocalePreference) { 357 logEntrySelected( 358 TvSettingsEnums.SYSTEM_A11Y_TTS_ENGINE_CONFIG_LANGUAGE_CHOOSE_LANGUAGE); 359 String localeString = (String) newValue; 360 updateLanguageTo((!TextUtils.isEmpty(localeString) ? 361 mEnginesHelper.parseLocaleString(localeString) : null)); 362 return true; 363 } 364 return false; 365 } 366 updateLanguageTo(Locale locale)367 private void updateLanguageTo(Locale locale) { 368 int selectedLocaleIndex = -1; 369 String localeString = (locale != null) ? locale.toString() : ""; 370 for (int i=0; i < mLocalePreference.getEntryValues().length; i++) { 371 if (localeString.equalsIgnoreCase(mLocalePreference.getEntryValues()[i].toString())) { 372 selectedLocaleIndex = i; 373 break; 374 } 375 } 376 377 if (selectedLocaleIndex == -1) { 378 Log.w(TAG, "updateLanguageTo called with unknown locale argument"); 379 return; 380 } 381 mLocalePreference.setSummary(mLocalePreference.getEntries()[selectedLocaleIndex]); 382 mSelectedLocaleIndex = selectedLocaleIndex; 383 384 mEnginesHelper.updateLocalePrefForEngine(getEngineName(), locale); 385 386 if (getEngineName().equals(mTts.getCurrentEngine())) { 387 // Null locale means "use system default" 388 mTts.setLanguage((locale != null) ? locale : Locale.getDefault()); 389 } 390 } 391 getEngineName()392 private String getEngineName() { 393 return getArguments().getString(ARG_ENGINE_NAME, ""); 394 } 395 getEngineLabel()396 private String getEngineLabel() { 397 return getArguments().getString(ARG_ENGINE_LABEL, ""); 398 } 399 400 @Override getPageId()401 protected int getPageId() { 402 return TvSettingsEnums.SYSTEM_A11Y_TTS_ENGINE_CONFIG; 403 } 404 } 405