blob: 1a63272a2260194434859421b33eafd92f18fdee [file] [log] [blame]
Jean-Michel Trivied29a652009-06-05 18:37:29 -07001/*
2 * Copyright (C) 2009 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
17package com.android.settings;
18
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070019import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070020import static android.provider.Settings.Secure.TTS_DEFAULT_LANG;
21import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -070022import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070023import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT;
Charles Chenf47cce02010-03-17 17:33:23 -070024import static android.provider.Settings.Secure.TTS_ENABLED_PLUGINS;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070025import static android.provider.Settings.Secure.TTS_USE_DEFAULTS;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070026
Charles Chen0a0eb5f2010-03-16 20:09:17 -070027import android.app.AlertDialog;
Bjorn Bringertc7762972011-03-11 16:52:51 +000028import android.content.ActivityNotFoundException;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070029import android.content.ContentResolver;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070030import android.content.Context;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070031import android.content.DialogInterface;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070032import android.content.Intent;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070033import android.content.pm.PackageManager;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070034import android.os.Bundle;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070035import android.preference.CheckBoxPreference;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070036import android.preference.ListPreference;
37import android.preference.Preference;
Narayan Kamath934d21d2011-05-05 13:41:11 +010038import android.preference.Preference.OnPreferenceClickListener;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070039import android.preference.PreferenceGroup;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070040import android.provider.Settings;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070041import android.provider.Settings.SettingNotFoundException;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070042import android.speech.tts.TextToSpeech;
Bjorn Bringertc7762972011-03-11 16:52:51 +000043import android.text.TextUtils;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070044import android.util.Log;
45
Charles Chenc8298712010-02-10 13:58:23 -080046import java.util.ArrayList;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070047import java.util.List;
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -070048import java.util.Locale;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070049import java.util.StringTokenizer;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070050
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070051public class TextToSpeechSettings extends SettingsPreferenceFragment implements
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070052 Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
53 TextToSpeech.OnInitListener {
Jean-Michel Trivied29a652009-06-05 18:37:29 -070054
Bjorn Bringertc7762972011-03-11 16:52:51 +000055 private static final boolean DBG = true;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070056 private static final String TAG = "TextToSpeechSettings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070057
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070058 private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070059 private static final String KEY_TTS_INSTALL_DATA = "tts_install_data";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070060 private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070061 private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate";
Jean-Michel Trivi80368622009-06-09 19:29:27 -070062 private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070063 private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country";
64 private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant";
Charles Chen5dbc74a2009-12-07 12:08:13 -080065 private static final String KEY_TTS_DEFAULT_SYNTH = "tts_default_synth";
Bjorn Bringertc7762972011-03-11 16:52:51 +000066 private static final String KEY_TTS_ENGINES = "tts_engines_section";
Charles Chen0a0eb5f2010-03-16 20:09:17 -070067
68 private static final String KEY_PLUGIN_ENABLED_PREFIX = "ENABLED_";
69 private static final String KEY_PLUGIN_SETTINGS_PREFIX = "SETTINGS_";
70
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070071 // TODO move default Locale values to TextToSpeech.Engine
72 private static final String DEFAULT_LANG_VAL = "eng";
73 private static final String DEFAULT_COUNTRY_VAL = "USA";
74 private static final String DEFAULT_VARIANT_VAL = "";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070075
76 private static final String LOCALE_DELIMITER = "-";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070077
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070078 private Preference mPlayExample = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070079 private Preference mInstallData = null;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070080 private CheckBoxPreference mUseDefaultPref = null;
81 private ListPreference mDefaultRatePref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070082 private ListPreference mDefaultLocPref = null;
Charles Chen5dbc74a2009-12-07 12:08:13 -080083 private ListPreference mDefaultSynthPref = null;
Bjorn Bringertc7762972011-03-11 16:52:51 +000084 private PreferenceGroup mEnginesGroup;
85
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070086 private String mDefaultLanguage = null;
87 private String mDefaultCountry = null;
88 private String mDefaultLocVariant = null;
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070089 private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
90
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070091 // Index of the current string to use for the demo.
92 private int mDemoStringIndex = 0;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070093
94 private boolean mEnableDemo = false;
Charles Chenc8298712010-02-10 13:58:23 -080095 private boolean mVoicesMissing = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070096
97 private TextToSpeech mTts = null;
Charles Chencf31e652010-04-07 14:26:31 -070098 private boolean mTtsStarted = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070099
100 /**
101 * Request code (arbitrary value) for voice data check through
102 * startActivityForResult.
103 */
104 private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
Charles Chen4df6c792010-01-22 11:17:40 -0800105 private static final int GET_SAMPLE_TEXT = 1983;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700106
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700107 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700108 public void onCreate(Bundle savedInstanceState) {
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700109 super.onCreate(savedInstanceState);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700110 addPreferencesFromResource(R.xml.tts_settings);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700111
Bjorn Bringertc7762972011-03-11 16:52:51 +0000112 getActivity().setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
Jean-Michel Trivi6dde8962009-08-11 09:06:58 -0700113
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700114 mEnableDemo = false;
Charles Chencf31e652010-04-07 14:26:31 -0700115 mTtsStarted = false;
Charles Chenbe6e8272010-03-22 16:06:05 -0700116
Charles Chen8c8185b2010-04-08 16:51:35 -0700117 Locale currentLocale = Locale.getDefault();
118 mDefaultLanguage = currentLocale.getISO3Language();
119 mDefaultCountry = currentLocale.getISO3Country();
120 mDefaultLocVariant = currentLocale.getVariant();
121
Bjorn Bringertc7762972011-03-11 16:52:51 +0000122 mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
123 mPlayExample.setOnPreferenceClickListener(this);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700124
Bjorn Bringertc7762972011-03-11 16:52:51 +0000125 mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
126 mInstallData.setOnPreferenceClickListener(this);
127
128 mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
129 mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
130 mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
131 mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
132
133 mEnginesGroup = (PreferenceGroup) findPreference(KEY_TTS_ENGINES);
134
135 mTts = new TextToSpeech(getActivity().getApplicationContext(), this);
136
137 initDefaultSettings();
138 addEngineSpecificSettings();
139 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700140
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700141 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700142 public void onStart() {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700143 super.onStart();
Charles Chencf31e652010-04-07 14:26:31 -0700144 if (mTtsStarted){
145 // whenever we return to this screen, we don't know the state of the
146 // system, so we have to recheck that we can play the demo, or it must be disabled.
147 // TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount
Charles Chencf31e652010-04-07 14:26:31 -0700148 updateWidgetState();
149 checkVoiceData();
150 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700151 }
152
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700153 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700154 public void onDestroy() {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700155 super.onDestroy();
156 if (mTts != null) {
157 mTts.shutdown();
158 }
159 }
160
Charles Chen8c8185b2010-04-08 16:51:35 -0700161 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700162 public void onPause() {
Charles Chen8c8185b2010-04-08 16:51:35 -0700163 super.onPause();
164 if ((mDefaultRatePref != null) && (mDefaultRatePref.getDialog() != null)) {
165 mDefaultRatePref.getDialog().dismiss();
166 }
167 if ((mDefaultLocPref != null) && (mDefaultLocPref.getDialog() != null)) {
168 mDefaultLocPref.getDialog().dismiss();
169 }
170 if ((mDefaultSynthPref != null) && (mDefaultSynthPref.getDialog() != null)) {
171 mDefaultSynthPref.getDialog().dismiss();
172 }
173 }
174
Bjorn Bringertc7762972011-03-11 16:52:51 +0000175 private void addEngineSpecificSettings() {
176 Context context = getActivity();
177 List<TextToSpeech.EngineInfo> engines = mTts.getEngines();
178 for (TextToSpeech.EngineInfo engine : engines) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700179 String prefKey = "";
Bjorn Bringertc7762972011-03-11 16:52:51 +0000180 final String engineName = engine.name;
181 if (!engineName.equals(TextToSpeech.Engine.DEFAULT_ENGINE)) {
182 CheckBoxPreference enablePref = new CheckBoxPreference(context);
183 prefKey = KEY_PLUGIN_ENABLED_PREFIX + engineName;
184 enablePref.setKey(prefKey);
185 enablePref.setTitle(engine.label);
Bjorn Bringertdf92f2e2011-04-19 14:37:49 +0100186 enablePref.setOnPreferenceClickListener(this);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000187 mEnginesGroup.addPreference(enablePref);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700188 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000189 if (engineHasSettings(engineName)) {
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700190 Preference pref = new Preference(context);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000191 prefKey = KEY_PLUGIN_SETTINGS_PREFIX + engineName;
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700192 pref.setKey(prefKey);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000193 pref.setTitle(engine.label);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700194 CharSequence settingsLabel = getResources().getString(
Bjorn Bringertc7762972011-03-11 16:52:51 +0000195 R.string.tts_engine_name_settings, engine.label);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700196 pref.setSummary(settingsLabel);
Narayan Kamath934d21d2011-05-05 13:41:11 +0100197 // TODO: Add a new API for this.
198 pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
199 public boolean onPreferenceClick(Preference preference) {
200 Intent i = new Intent();
201 i.setClassName(engineName,
202 engineName + ".EngineSettings");
203 startActivity(i);
204 return true;
205 }
206 });
Bjorn Bringertc7762972011-03-11 16:52:51 +0000207 mEnginesGroup.addPreference(pref);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700208 }
209 }
210 }
211
Bjorn Bringertc7762972011-03-11 16:52:51 +0000212 private boolean engineHasSettings(String enginePackageName) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700213 PackageManager pm = getPackageManager();
214 Intent i = new Intent();
Bjorn Bringertc7762972011-03-11 16:52:51 +0000215 i.setClassName(enginePackageName, enginePackageName + ".EngineSettings");
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700216 if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){
217 return true;
218 }
219 return false;
220 }
221
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700222 private void initDefaultSettings() {
223 ContentResolver resolver = getContentResolver();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700224
225 // Find the default TTS values in the settings, initialize and store the
226 // settings if they are not found.
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700227
228 // "Use Defaults"
Bjorn Bringertc7762972011-03-11 16:52:51 +0000229 int useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS,
230 TextToSpeech.Engine.USE_DEFAULTS);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700231 mUseDefaultPref.setChecked(useDefault == 1);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700232 mUseDefaultPref.setOnPreferenceChangeListener(this);
233
Charles Chen5dbc74a2009-12-07 12:08:13 -0800234 // Default synthesis engine
Charles Chen5dbc74a2009-12-07 12:08:13 -0800235 loadEngines();
236 mDefaultSynthPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700237
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700238 // Default rate
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700239 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700240 mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700241 } catch (SettingNotFoundException e) {
242 // default rate setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700243 mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
244 Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700245 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700246 mDefaultRatePref.setValue(String.valueOf(mDefaultRate));
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700247 mDefaultRatePref.setOnPreferenceChangeListener(this);
248
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700249 // Default language / country / variant : these three values map to a single ListPref
250 // representing the matching Locale
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700251 initDefaultLang();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700252 mDefaultLocPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700253 }
254
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700255 /**
256 * Ask the current default engine to launch the matching CHECK_TTS_DATA activity
257 * to check the required TTS files are properly installed.
258 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700259 private void checkVoiceData() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000260 String defaultEngine = mTts.getDefaultEngine();
261 if (TextUtils.isEmpty(defaultEngine)) return;
262 Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
263 intent.setPackage(defaultEngine);
264 try {
265 Log.v(TAG, "Checking voice data: " + intent.toUri(0));
266 startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
267 } catch (ActivityNotFoundException ex) {
268 Log.e(TAG, "Failed to check TTS data, no acitivty found for " + intent + ")");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700269 }
270 }
271
272
273 /**
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700274 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
275 * so the required TTS files are properly installed.
276 */
277 private void installVoiceData() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000278 String defaultEngine = mTts.getDefaultEngine();
279 if (TextUtils.isEmpty(defaultEngine)) return;
280 Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700281 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000282 intent.setPackage(defaultEngine);
283 try {
284 Log.v(TAG, "Installing voice data: " + intent.toUri(0));
285 startActivity(intent);
286 } catch (ActivityNotFoundException ex) {
287 Log.e(TAG, "Failed to install TTS data, no acitivty found for " + intent + ")");
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700288 }
289 }
290
Charles Chen4df6c792010-01-22 11:17:40 -0800291 /**
292 * Ask the current default engine to return a string of sample text to be
293 * spoken to the user.
294 */
295 private void getSampleText() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000296 String defaultEngine = mTts.getDefaultEngine();
297 if (TextUtils.isEmpty(defaultEngine)) return;
298 Intent intent = new Intent(TextToSpeech.Engine.ACTION_GET_SAMPLE_TEXT);
Charles Chen4df6c792010-01-22 11:17:40 -0800299 intent.putExtra("language", mDefaultLanguage);
300 intent.putExtra("country", mDefaultCountry);
301 intent.putExtra("variant", mDefaultLocVariant);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000302 intent.setPackage(defaultEngine);
303 try {
304 Log.v(TAG, "Getting sample text: " + intent.toUri(0));
305 startActivityForResult(intent, GET_SAMPLE_TEXT);
306 } catch (ActivityNotFoundException ex) {
307 Log.e(TAG, "Failed to get sample text, no acitivty found for " + intent + ")");
Charles Chen4df6c792010-01-22 11:17:40 -0800308 }
309 }
310
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700311 /**
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700312 * Called when the TTS engine is initialized.
313 */
314 public void onInit(int status) {
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700315 if (status == TextToSpeech.SUCCESS) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700316 mEnableDemo = true;
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800317 if (mDefaultLanguage == null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800318 mDefaultLanguage = Locale.getDefault().getISO3Language();
319 }
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800320 if (mDefaultCountry == null) {
321 mDefaultCountry = Locale.getDefault().getISO3Country();
322 }
323 if (mDefaultLocVariant == null) {
324 mDefaultLocVariant = new String();
325 }
Charles Chencf3998b2010-02-11 18:13:42 -0800326 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Charles Chencf31e652010-04-07 14:26:31 -0700327 updateWidgetState();
328 checkVoiceData();
329 mTtsStarted = true;
330 Log.v(TAG, "TTS engine for settings screen initialized.");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700331 } else {
332 Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
333 mEnableDemo = false;
334 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700335 updateWidgetState();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700336 }
337
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700338 /**
339 * Called when voice data integrity check returns
340 */
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700341 @Override
342 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700343 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000344 onVoiceDataIntegrityCheckDone(data);
Charles Chen4df6c792010-01-22 11:17:40 -0800345 } else if (requestCode == GET_SAMPLE_TEXT) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000346 onSampleTextReceived(resultCode, data);
347 }
348 }
349
350 private void onVoiceDataIntegrityCheckDone(Intent data) {
351 if (data == null){
352 Log.e(TAG, "TTS data check failed data = null");
353 // The CHECK_TTS_DATA activity for the plugin did not run properly;
354 // disable the preview and install controls and return.
355 mEnableDemo = false;
356 mVoicesMissing = false;
357 updateWidgetState();
358 return;
359 }
360 Log.v(TAG, "TTS data check completed, data = " + data.toUri(0));
361 ArrayList<String> available =
362 data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
363 ArrayList<String> unavailable =
364 data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES);
365 if (available == null || unavailable == null){
366 Log.e(TAG, "TTS data check failed (available == == null)");
367 // The CHECK_TTS_DATA activity for the plugin did not run properly;
368 // disable the preview and install controls and return.
369 mEnableDemo = false;
370 mVoicesMissing = false;
371 updateWidgetState();
372 return;
373 }
374 if (available.size() > 0){
375 if (mTts == null) {
376 mTts = new TextToSpeech(getActivity(), this);
Charles Chen4df6c792010-01-22 11:17:40 -0800377 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000378
379 updateDefaultLocPref(available);
380
381 mEnableDemo = true;
382 // Make sure that the default language can be used.
383 int languageResult = mTts.setLanguage(
384 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
385 if (languageResult < TextToSpeech.LANG_AVAILABLE){
386 Locale currentLocale = Locale.getDefault();
387 mDefaultLanguage = currentLocale.getISO3Language();
388 mDefaultCountry = currentLocale.getISO3Country();
389 mDefaultLocVariant = currentLocale.getVariant();
390 languageResult = mTts.setLanguage(
391 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
392 // If the default Locale isn't supported, just choose the first available
393 // language so that there is at least something.
394 if (languageResult < TextToSpeech.LANG_AVAILABLE){
395 parseLocaleInfo(mDefaultLocPref.getEntryValues()[0].toString());
396 mTts.setLanguage(
397 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
398 }
399 ContentResolver resolver = getContentResolver();
400 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
401 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
402 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
403 }
404 } else {
405 mEnableDemo = false;
406 }
407
408 if (unavailable.size() > 0){
409 mVoicesMissing = true;
410 } else {
411 mVoicesMissing = false;
412 }
413
414 updateWidgetState();
415 }
416
417 private void updateDefaultLocPref(ArrayList<String> availableLangs) {
418 CharSequence[] entries = new CharSequence[availableLangs.size()];
419 CharSequence[] entryValues = new CharSequence[availableLangs.size()];
420 int selectedLanguageIndex = -1;
421 String selectedLanguagePref = mDefaultLanguage;
422 if (mDefaultCountry.length() > 0) {
423 selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER +
424 mDefaultCountry;
425 }
426 if (mDefaultLocVariant.length() > 0) {
427 selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER +
428 mDefaultLocVariant;
429 }
430 for (int i = 0; i < availableLangs.size(); i++) {
431 String[] langCountryVariant = availableLangs.get(i).split("-");
432 Locale loc = null;
433 if (langCountryVariant.length == 1){
434 loc = new Locale(langCountryVariant[0]);
435 } else if (langCountryVariant.length == 2){
436 loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
437 } else if (langCountryVariant.length == 3){
438 loc = new Locale(langCountryVariant[0], langCountryVariant[1],
439 langCountryVariant[2]);
440 }
441 if (loc != null){
442 entries[i] = loc.getDisplayName();
443 entryValues[i] = availableLangs.get(i);
444 if (entryValues[i].equals(selectedLanguagePref)) {
445 selectedLanguageIndex = i;
446 }
447 }
448 }
449 mDefaultLocPref.setEntries(entries);
450 mDefaultLocPref.setEntryValues(entryValues);
451 if (selectedLanguageIndex > -1) {
452 mDefaultLocPref.setValueIndex(selectedLanguageIndex);
453 }
454 }
455
456 private void onSampleTextReceived(int resultCode, Intent data) {
457 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
458 String sample = getActivity().getString(R.string.tts_demo);
459 if (data != null && data.getStringExtra("sampleText") != null) {
460 sample = data.getStringExtra("sampleText");
461 }
462 Log.v(TAG, "Got sample text: " + sample);
463 if (mTts != null) {
464 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
465 }
466 } else {
467 // TODO: Display an error here to the user.
468 Log.e(TAG, "Did not have a sample string for the requested language");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700469 }
470 }
471
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700472 public boolean onPreferenceChange(Preference preference, Object objValue) {
473 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
474 // "Use Defaults"
Bjorn Bringertc7762972011-03-11 16:52:51 +0000475 int value = ((Boolean) objValue) ? 1 : 0;
476 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, value);
477 Log.i(TAG, "TTS 'use default' settings changed, now " + value);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700478 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
479 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700480 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700481 try {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000482 Settings.Secure.putInt(getContentResolver(), TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700483 if (mTts != null) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000484 mTts.setSpeechRate(mDefaultRate / 100.0f);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700485 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000486 Log.v(TAG, "TTS default rate changed, now " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700487 } catch (NumberFormatException e) {
488 Log.e(TAG, "could not persist default TTS rate setting", e);
489 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700490 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700491 // Default locale
492 ContentResolver resolver = getContentResolver();
493 parseLocaleInfo((String) objValue);
494 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
495 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
496 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
497 Log.v(TAG, "TTS default lang/country/variant set to "
498 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700499 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800500 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700501 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700502 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000503 Log.v(TAG, " selected is " + newIndex);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700504 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800505 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000506 String defaultEng = objValue.toString();
507 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, defaultEng);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800508 if (mTts != null) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000509 mTts.setEngineByPackageName(defaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800510 mEnableDemo = false;
511 mVoicesMissing = false;
512 updateWidgetState();
513 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800514 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000515 Log.v(TAG, "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700516 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700517
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700518 return true;
519 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700520
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700521
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700522 /**
523 * Called when mPlayExample or mInstallData is clicked
524 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700525 public boolean onPreferenceClick(Preference preference) {
526 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800527 // Get the sample text from the TTS engine; onActivityResult will do
528 // the actual speaking
529 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700530 return true;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000531 } else if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700532 installVoiceData();
533 // quit this activity so it needs to be restarted after installation of the voice data
534 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700535 return true;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000536 } else if (preference.getKey().startsWith(KEY_PLUGIN_ENABLED_PREFIX)) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700537 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000538 if (chkPref.isChecked()) {
539 chkPref.setChecked(false);
540 AlertDialog d = (new AlertDialog.Builder(getActivity()))
541 .setTitle(android.R.string.dialog_alert_title)
542 .setIcon(android.R.drawable.ic_dialog_alert)
543 .setMessage(
544 getActivity().getString(R.string.tts_engine_security_warning,
545 chkPref.getTitle()))
546 .setCancelable(true)
547 .setPositiveButton(android.R.string.ok,
548 new DialogInterface.OnClickListener() {
549 public void onClick(DialogInterface dialog, int which) {
550 chkPref.setChecked(true);
551 loadEngines();
552 }
553 })
554 .setNegativeButton(android.R.string.cancel,
555 new DialogInterface.OnClickListener() {
556 public void onClick(DialogInterface dialog, int which) {
557 }
558 })
559 .create();
560 d.show();
561 } else {
562 loadEngines();
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700563 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000564 return true;
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700565 }
566 return false;
567 }
568
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700569 private void updateWidgetState() {
570 mPlayExample.setEnabled(mEnableDemo);
571 mUseDefaultPref.setEnabled(mEnableDemo);
572 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700573 mDefaultLocPref.setEnabled(mEnableDemo);
574
Charles Chenc8298712010-02-10 13:58:23 -0800575 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700576 }
577
578
579 private void parseLocaleInfo(String locale) {
580 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
581 mDefaultLanguage = "";
582 mDefaultCountry = "";
583 mDefaultLocVariant = "";
584 if (tokenizer.hasMoreTokens()) {
585 mDefaultLanguage = tokenizer.nextToken().trim();
586 }
587 if (tokenizer.hasMoreTokens()) {
588 mDefaultCountry = tokenizer.nextToken().trim();
589 }
590 if (tokenizer.hasMoreTokens()) {
591 mDefaultLocVariant = tokenizer.nextToken().trim();
592 }
593 }
594
595
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700596 /**
597 * Initialize the default language in the UI and in the preferences.
598 * After this method has been invoked, the default language is a supported Locale.
599 */
600 private void initDefaultLang() {
601 // if there isn't already a default language preference
602 if (!hasLangPref()) {
603 // if the current Locale is supported
604 if (isCurrentLocSupported()) {
605 // then use the current Locale as the default language
606 useCurrentLocAsDefault();
607 } else {
608 // otherwise use a default supported Locale as the default language
609 useSupportedLocAsDefault();
610 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700611 }
612
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700613 // Update the language preference list with the default language and the matching
614 // demo string (at this stage there is a default language pref)
615 ContentResolver resolver = getContentResolver();
616 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
Charles Chen681d0b82010-04-12 12:06:30 -0700617 mDefaultCountry = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY);
618 mDefaultLocVariant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700619
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700620 // update the demo string
621 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
622 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800623 if (mDemoStringIndex > -1){
624 mDefaultLocPref.setValueIndex(mDemoStringIndex);
625 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700626 }
627
628 /**
629 * (helper function for initDefaultLang() )
630 * Returns whether there is a default language in the TTS settings.
631 */
632 private boolean hasLangPref() {
Jean-Baptiste Queru6e61b212010-04-14 10:40:48 -0700633 ContentResolver resolver = getContentResolver();
634 String language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
635 if ((language == null) || (language.length() < 1)) {
636 return false;
637 }
638 String country = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY);
639 if (country == null) {
640 return false;
641 }
642 String variant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT);
643 if (variant == null) {
644 return false;
645 }
646 return true;
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700647 }
648
649 /**
650 * (helper function for initDefaultLang() )
651 * Returns whether the current Locale is supported by this Settings screen
652 */
653 private boolean isCurrentLocSupported() {
654 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
655 + Locale.getDefault().getISO3Country();
656 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
657 }
658
659 /**
660 * (helper function for initDefaultLang() )
661 * Sets the default language in TTS settings to be the current Locale.
662 * This should only be used after checking that the current Locale is supported.
663 */
664 private void useCurrentLocAsDefault() {
665 Locale currentLocale = Locale.getDefault();
666 ContentResolver resolver = getContentResolver();
667 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
668 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
669 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
670 }
671
672 /**
673 * (helper function for initDefaultLang() )
674 * Sets the default language in TTS settings to be one known to be supported
675 */
676 private void useSupportedLocAsDefault() {
677 ContentResolver resolver = getContentResolver();
678 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
679 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
680 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700681 }
682
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700683 private void loadEngines() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000684 List<TextToSpeech.EngineInfo> engines = mTts.getEngines();
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700685 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
686 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
Bjorn Bringertc7762972011-03-11 16:52:51 +0000687 StringBuilder enabledEngines = new StringBuilder();
688
689 for (TextToSpeech.EngineInfo engine : engines) {
690 Log.v(TAG, "Engine: " + engine);
691 if (isEngineEnabled(engine.name)) {
692 entries.add(engine.label);
693 values.add(engine.name);
694 if (enabledEngines.length() > 0) enabledEngines.append(' ');
695 enabledEngines.append(engine.name);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700696 }
697 }
Charles Chenf47cce02010-03-17 17:33:23 -0700698 ContentResolver resolver = getContentResolver();
Bjorn Bringertc7762972011-03-11 16:52:51 +0000699 Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines.toString());
Charles Chen5dbc74a2009-12-07 12:08:13 -0800700
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700701 CharSequence entriesArray[] = new CharSequence[entries.size()];
702 CharSequence valuesArray[] = new CharSequence[values.size()];
703
Charles Chen8c8185b2010-04-08 16:51:35 -0700704 mDefaultSynthPref.setEntries(entries.toArray(entriesArray));
705 mDefaultSynthPref.setEntryValues(values.toArray(valuesArray));
Charles Chenbe6e8272010-03-22 16:06:05 -0700706
707 // Set the selected engine based on the saved preference
708 String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH);
Charles Chen8c8185b2010-04-08 16:51:35 -0700709 int selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(selectedEngine);
Charles Chenbe6e8272010-03-22 16:06:05 -0700710 if (selectedEngineIndex == -1){
Bjorn Bringertc7762972011-03-11 16:52:51 +0000711 selectedEngineIndex =
712 mDefaultSynthPref.findIndexOfValue(TextToSpeech.Engine.DEFAULT_ENGINE);
Charles Chenbe6e8272010-03-22 16:06:05 -0700713 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000714 if (selectedEngineIndex >= 0) {
715 mDefaultSynthPref.setValueIndex(selectedEngineIndex);
716 }
717 }
718
719 private boolean isEngineEnabled(String engineName) {
720 if (engineName.equals(TextToSpeech.Engine.DEFAULT_ENGINE)) return true;
721 String enginePref = KEY_PLUGIN_ENABLED_PREFIX + engineName;
722 return getPreferenceManager().getSharedPreferences().getBoolean(enginePref, false);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800723 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800724
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700725}