blob: d1a63e2593a0e5d7e22130479e56066047e27c53 [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
19import static android.provider.Settings.Secure.TTS_USE_DEFAULTS;
20import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
Jean-Michel Trivi80368622009-06-09 19:29:27 -070021import static android.provider.Settings.Secure.TTS_DEFAULT_LANG;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070022import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY;
23import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT;
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -070024import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
Charles Chenf47cce02010-03-17 17:33:23 -070025import static android.provider.Settings.Secure.TTS_ENABLED_PLUGINS;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070026
Charles Chen0a0eb5f2010-03-16 20:09:17 -070027import android.app.AlertDialog;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070028import android.content.ContentResolver;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070029import android.content.DialogInterface;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070030import android.content.Intent;
31import android.content.pm.ActivityInfo;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070034import android.os.Bundle;
35import android.preference.ListPreference;
36import android.preference.Preference;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070037import android.preference.Preference.OnPreferenceClickListener;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070038import android.preference.PreferenceActivity;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070039import android.preference.PreferenceGroup;
40import android.preference.PreferenceScreen;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070041import android.preference.CheckBoxPreference;
42import android.provider.Settings;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070043import android.provider.Settings.SettingNotFoundException;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070044import android.speech.tts.TextToSpeech;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070045import android.util.Log;
46
Charles Chenc8298712010-02-10 13:58:23 -080047import java.util.ArrayList;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070048import java.util.List;
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -070049import java.util.Locale;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070050import java.util.StringTokenizer;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070051
Jean-Michel Trivied29a652009-06-05 18:37:29 -070052public class TextToSpeechSettings extends PreferenceActivity implements
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070053 Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
54 TextToSpeech.OnInitListener {
Jean-Michel Trivied29a652009-06-05 18:37:29 -070055
56 private static final String TAG = "TextToSpeechSettings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070057
Charles Chen0a0eb5f2010-03-16 20:09:17 -070058 private static final String SYSTEM_TTS = "com.svox.pico";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070059 private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070060 private static final String KEY_TTS_INSTALL_DATA = "tts_install_data";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070061 private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070062 private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate";
Jean-Michel Trivi80368622009-06-09 19:29:27 -070063 private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070064 private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country";
65 private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant";
Charles Chen5dbc74a2009-12-07 12:08:13 -080066 private static final String KEY_TTS_DEFAULT_SYNTH = "tts_default_synth";
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 Trivi628431d2009-07-17 16:52:54 -070078 private static final String FALLBACK_TTS_DEFAULT_SYNTH =
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -070079 TextToSpeech.Engine.DEFAULT_SYNTH;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070080
81 private Preference mPlayExample = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070082 private Preference mInstallData = null;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070083 private CheckBoxPreference mUseDefaultPref = null;
84 private ListPreference mDefaultRatePref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070085 private ListPreference mDefaultLocPref = null;
Charles Chen5dbc74a2009-12-07 12:08:13 -080086 private ListPreference mDefaultSynthPref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070087 private String mDefaultLanguage = null;
88 private String mDefaultCountry = null;
89 private String mDefaultLocVariant = null;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070090 private String mDefaultEng = "";
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070091 private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
92
93 // Array of strings used to demonstrate TTS in the different languages.
94 private String[] mDemoStrings;
95 // Index of the current string to use for the demo.
96 private int mDemoStringIndex = 0;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070097
98 private boolean mEnableDemo = false;
Charles Chenc8298712010-02-10 13:58:23 -080099 private boolean mVoicesMissing = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700100
101 private TextToSpeech mTts = null;
102
103 /**
104 * Request code (arbitrary value) for voice data check through
105 * startActivityForResult.
106 */
107 private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
Charles Chen4df6c792010-01-22 11:17:40 -0800108 private static final int GET_SAMPLE_TEXT = 1983;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700109
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700110 @Override
111 protected void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113
114 addPreferencesFromResource(R.xml.tts_settings);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700115
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700116 addEngineSpecificSettings();
117
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700118 mDemoStrings = getResources().getStringArray(R.array.tts_demo_strings);
119
Jean-Michel Trivi6dde8962009-08-11 09:06:58 -0700120 setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
121
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700122 mEnableDemo = false;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700123 initClickers();
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700124 initDefaultSettings();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700125 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700126
127
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700128 @Override
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700129 protected void onStart() {
130 super.onStart();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700131 // whenever we return to this screen, we don't know the state of the
132 // system, so we have to recheck that we can play the demo, or it must be disabled.
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700133 // TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700134 initClickers();
135 updateWidgetState();
136 checkVoiceData();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700137 }
138
139
140 @Override
141 protected void onDestroy() {
142 super.onDestroy();
143 if (mTts != null) {
144 mTts.shutdown();
145 }
146 }
147
148
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700149 private void addEngineSpecificSettings() {
150 PreferenceGroup enginesCategory = (PreferenceGroup) findPreference("tts_engines_section");
151 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
152 ResolveInfo[] enginesArray = new ResolveInfo[0];
153 PackageManager pm = getPackageManager();
154 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
155 for (int i = 0; i < enginesArray.length; i++) {
156 String prefKey = "";
157 final String pluginPackageName = enginesArray[i].activityInfo.packageName;
158 if (!enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) {
159 CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
160 prefKey = KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName;
161 chkbxPref.setKey(prefKey);
162 chkbxPref.setTitle(enginesArray[i].loadLabel(pm));
163 enginesCategory.addPreference(chkbxPref);
164 }
165 if (pluginHasSettings(pluginPackageName)) {
166 Preference pref = new Preference(this);
167 prefKey = KEY_PLUGIN_SETTINGS_PREFIX + pluginPackageName;
168 pref.setKey(prefKey);
169 pref.setTitle(enginesArray[i].loadLabel(pm));
170 CharSequence settingsLabel = getResources().getString(
171 R.string.tts_engine_name_settings, enginesArray[i].loadLabel(pm));
172 pref.setSummary(settingsLabel);
173 pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
174 public boolean onPreferenceClick(Preference preference){
175 Intent i = new Intent();
176 i.setClassName(pluginPackageName,
177 pluginPackageName + ".EngineSettings");
178 startActivity(i);
179 return true;
180 }
181 });
182 enginesCategory.addPreference(pref);
183 }
184 }
185 }
186
187 private boolean pluginHasSettings(String pluginPackageName) {
188 PackageManager pm = getPackageManager();
189 Intent i = new Intent();
190 i.setClassName(pluginPackageName, pluginPackageName + ".EngineSettings");
191 if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){
192 return true;
193 }
194 return false;
195 }
196
197
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700198 private void initClickers() {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700199 mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700200 mPlayExample.setOnPreferenceClickListener(this);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700201
202 mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
203 mInstallData.setOnPreferenceClickListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700204 }
205
206
207 private void initDefaultSettings() {
208 ContentResolver resolver = getContentResolver();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700209
210 // Find the default TTS values in the settings, initialize and store the
211 // settings if they are not found.
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700212
213 // "Use Defaults"
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700214 int useDefault = 0;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700215 mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
216 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700217 useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700218 } catch (SettingNotFoundException e) {
219 // "use default" setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700220 useDefault = TextToSpeech.Engine.USE_DEFAULTS;
221 Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700222 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700223 mUseDefaultPref.setChecked(useDefault == 1);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700224 mUseDefaultPref.setOnPreferenceChangeListener(this);
225
Charles Chen5dbc74a2009-12-07 12:08:13 -0800226 // Default synthesis engine
227 mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
228 loadEngines();
229 mDefaultSynthPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700230 String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH);
231 if (engine == null) {
232 // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech
233 engine = FALLBACK_TTS_DEFAULT_SYNTH;
234 Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine);
235 }
236 mDefaultEng = engine;
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 mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
240 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700241 mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700242 } catch (SettingNotFoundException e) {
243 // default rate setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700244 mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
245 Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700246 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700247 mDefaultRatePref.setValue(String.valueOf(mDefaultRate));
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700248 mDefaultRatePref.setOnPreferenceChangeListener(this);
249
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700250 // Default language / country / variant : these three values map to a single ListPref
251 // representing the matching Locale
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700252 mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700253 initDefaultLang();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700254 mDefaultLocPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700255 }
256
257
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700258 /**
259 * Ask the current default engine to launch the matching CHECK_TTS_DATA activity
260 * to check the required TTS files are properly installed.
261 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700262 private void checkVoiceData() {
263 PackageManager pm = getPackageManager();
264 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700265 intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700266 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
267 // query only the package that matches that of the default engine
268 for (int i = 0; i < resolveInfos.size(); i++) {
269 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
270 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
271 intent.setClassName(mDefaultEng, currentActivityInfo.name);
272 this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
273 }
274 }
275 }
276
277
278 /**
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700279 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
280 * so the required TTS files are properly installed.
281 */
282 private void installVoiceData() {
283 PackageManager pm = getPackageManager();
284 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700285 intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700286 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700287 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
288 // query only the package that matches that of the default engine
289 for (int i = 0; i < resolveInfos.size(); i++) {
290 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
291 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
292 intent.setClassName(mDefaultEng, currentActivityInfo.name);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700293 this.startActivity(intent);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700294 }
295 }
296 }
297
Charles Chen4df6c792010-01-22 11:17:40 -0800298 /**
299 * Ask the current default engine to return a string of sample text to be
300 * spoken to the user.
301 */
302 private void getSampleText() {
303 PackageManager pm = getPackageManager();
304 Intent intent = new Intent();
305 // TODO (clchen): Replace Intent string with the actual
306 // Intent defined in the list of platform Intents.
307 intent.setAction("android.speech.tts.engine.GET_SAMPLE_TEXT");
308 intent.putExtra("language", mDefaultLanguage);
309 intent.putExtra("country", mDefaultCountry);
310 intent.putExtra("variant", mDefaultLocVariant);
311 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
312 // query only the package that matches that of the default engine
313 for (int i = 0; i < resolveInfos.size(); i++) {
314 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
315 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
316 intent.setClassName(mDefaultEng, currentActivityInfo.name);
317 this.startActivityForResult(intent, GET_SAMPLE_TEXT);
318 }
319 }
320 }
321
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700322
323 /**
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700324 * Called when the TTS engine is initialized.
325 */
326 public void onInit(int status) {
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700327 if (status == TextToSpeech.SUCCESS) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700328 Log.v(TAG, "TTS engine for settings screen initialized.");
329 mEnableDemo = true;
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800330 if (mDefaultLanguage == null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800331 mDefaultLanguage = Locale.getDefault().getISO3Language();
332 }
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800333 if (mDefaultCountry == null) {
334 mDefaultCountry = Locale.getDefault().getISO3Country();
335 }
336 if (mDefaultLocVariant == null) {
337 mDefaultLocVariant = new String();
338 }
Charles Chencf3998b2010-02-11 18:13:42 -0800339 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700340 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700341 } else {
342 Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
343 mEnableDemo = false;
344 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700345 updateWidgetState();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700346 }
347
348
349 /**
350 * Called when voice data integrity check returns
351 */
352 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
353 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
Charles Chend5f013a2010-02-18 10:11:25 -0800354 if (data == null){
355 // The CHECK_TTS_DATA activity for the plugin did not run properly;
356 // disable the preview and install controls and return.
357 mEnableDemo = false;
358 mVoicesMissing = false;
359 updateWidgetState();
360 return;
361 }
Charles Chenc8298712010-02-10 13:58:23 -0800362 // TODO (clchen): Add these extras to TextToSpeech.Engine
363 ArrayList<String> available =
364 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES");
365 ArrayList<String> unavailable =
366 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES");
Charles Chend5f013a2010-02-18 10:11:25 -0800367 if ((available == null) || (unavailable == null)){
368 // The CHECK_TTS_DATA activity for the plugin did not run properly;
369 // disable the preview and install controls and return.
370 mEnableDemo = false;
371 mVoicesMissing = false;
372 updateWidgetState();
373 return;
374 }
Charles Chenc8298712010-02-10 13:58:23 -0800375 if (available.size() > 0){
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700376 if (mTts == null) {
377 mTts = new TextToSpeech(this, this);
378 }
Charles Chenc8298712010-02-10 13:58:23 -0800379 ListPreference ttsLanguagePref =
380 (ListPreference) findPreference("tts_default_lang");
381 CharSequence[] entries = new CharSequence[available.size()];
382 CharSequence[] entryValues = new CharSequence[available.size()];
383 for (int i=0; i<available.size(); i++){
384 String[] langCountryVariant = available.get(i).split("-");
385 Locale loc = null;
386 if (langCountryVariant.length == 1){
387 loc = new Locale(langCountryVariant[0]);
388 } else if (langCountryVariant.length == 2){
389 loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
390 } else if (langCountryVariant.length == 3){
391 loc = new Locale(langCountryVariant[0], langCountryVariant[1],
392 langCountryVariant[2]);
393 }
394 if (loc != null){
395 entries[i] = loc.getDisplayName();
396 entryValues[i] = available.get(i);
397 }
398 }
399 ttsLanguagePref.setEntries(entries);
400 ttsLanguagePref.setEntryValues(entryValues);
401 mEnableDemo = true;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700402 } else {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700403 mEnableDemo = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700404 }
Charles Chenc8298712010-02-10 13:58:23 -0800405
406 if (unavailable.size() > 0){
407 mVoicesMissing = true;
408 } else {
409 mVoicesMissing = false;
410 }
411
412 updateWidgetState();
Charles Chen4df6c792010-01-22 11:17:40 -0800413 } else if (requestCode == GET_SAMPLE_TEXT) {
414 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
Charles Chenec05e712010-02-18 15:06:26 -0800415 String sample = getString(R.string.tts_demo);
416 if ((data != null) && (data.getStringExtra("sampleText") != null)) {
417 sample = data.getStringExtra("sampleText");
Charles Chend5f013a2010-02-18 10:11:25 -0800418 }
Charles Chen4df6c792010-01-22 11:17:40 -0800419 if (mTts != null) {
Charles Chen4df6c792010-01-22 11:17:40 -0800420 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
421 }
422 } else {
423 // TODO: Display an error here to the user.
424 Log.e(TAG, "Did not have a sample string for the requested language");
425 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700426 }
427 }
428
429
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700430 public boolean onPreferenceChange(Preference preference, Object objValue) {
431 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
432 // "Use Defaults"
433 int value = (Boolean)objValue ? 1 : 0;
Jean-Michel Trivi80368622009-06-09 19:29:27 -0700434 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS,
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700435 value);
436 Log.i(TAG, "TTS use default settings is "+objValue.toString());
437 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
438 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700439 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700440 try {
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700441 Settings.Secure.putInt(getContentResolver(),
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700442 TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700443 if (mTts != null) {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700444 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700445 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700446 Log.i(TAG, "TTS default rate is " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700447 } catch (NumberFormatException e) {
448 Log.e(TAG, "could not persist default TTS rate setting", e);
449 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700450 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700451 // Default locale
452 ContentResolver resolver = getContentResolver();
453 parseLocaleInfo((String) objValue);
454 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
455 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
456 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
457 Log.v(TAG, "TTS default lang/country/variant set to "
458 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700459 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800460 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700461 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700462 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
463 Log.v("Settings", " selected is " + newIndex);
464 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800465 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Charles Chen5dbc74a2009-12-07 12:08:13 -0800466 mDefaultEng = objValue.toString();
467 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng);
468 if (mTts != null) {
469 mTts.setEngineByPackageName(mDefaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800470 mEnableDemo = false;
471 mVoicesMissing = false;
472 updateWidgetState();
473 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800474 }
475 Log.v("Settings", "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700476 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700477
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700478 return true;
479 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700480
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700481
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700482 /**
483 * Called when mPlayExample or mInstallData is clicked
484 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700485 public boolean onPreferenceClick(Preference preference) {
486 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800487 // Get the sample text from the TTS engine; onActivityResult will do
488 // the actual speaking
489 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700490 return true;
491 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700492 if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700493 installVoiceData();
494 // quit this activity so it needs to be restarted after installation of the voice data
495 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700496 return true;
497 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700498 return false;
499 }
500
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700501 @Override
502 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
503 if (Utils.isMonkeyRunning()) {
504 return false;
505 }
506
507 if (preference instanceof CheckBoxPreference) {
508 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
509 if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){
510 if (chkPref.isChecked()) {
511 chkPref.setChecked(false);
512 AlertDialog d = (new AlertDialog.Builder(this))
513 .setTitle(android.R.string.dialog_alert_title)
514 .setIcon(android.R.drawable.ic_dialog_alert)
515 .setMessage(getString(R.string.tts_engine_security_warning,
516 chkPref.getTitle()))
517 .setCancelable(true)
518 .setPositiveButton(android.R.string.ok,
519 new DialogInterface.OnClickListener() {
520 public void onClick(DialogInterface dialog, int which) {
521 chkPref.setChecked(true);
522 loadEngines();
523 }
524 })
525 .setNegativeButton(android.R.string.cancel,
526 new DialogInterface.OnClickListener() {
527 public void onClick(DialogInterface dialog, int which) {
528 }
529 })
530 .create();
531 d.show();
532 } else {
533 loadEngines();
534 }
535 return true;
536 }
537 }
538 return false;
539 }
540
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700541
542 private void updateWidgetState() {
543 mPlayExample.setEnabled(mEnableDemo);
544 mUseDefaultPref.setEnabled(mEnableDemo);
545 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700546 mDefaultLocPref.setEnabled(mEnableDemo);
547
Charles Chenc8298712010-02-10 13:58:23 -0800548 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700549 }
550
551
552 private void parseLocaleInfo(String locale) {
553 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
554 mDefaultLanguage = "";
555 mDefaultCountry = "";
556 mDefaultLocVariant = "";
557 if (tokenizer.hasMoreTokens()) {
558 mDefaultLanguage = tokenizer.nextToken().trim();
559 }
560 if (tokenizer.hasMoreTokens()) {
561 mDefaultCountry = tokenizer.nextToken().trim();
562 }
563 if (tokenizer.hasMoreTokens()) {
564 mDefaultLocVariant = tokenizer.nextToken().trim();
565 }
566 }
567
568
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700569 /**
570 * Initialize the default language in the UI and in the preferences.
571 * After this method has been invoked, the default language is a supported Locale.
572 */
573 private void initDefaultLang() {
574 // if there isn't already a default language preference
575 if (!hasLangPref()) {
576 // if the current Locale is supported
577 if (isCurrentLocSupported()) {
578 // then use the current Locale as the default language
579 useCurrentLocAsDefault();
580 } else {
581 // otherwise use a default supported Locale as the default language
582 useSupportedLocAsDefault();
583 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700584 }
585
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700586 // Update the language preference list with the default language and the matching
587 // demo string (at this stage there is a default language pref)
588 ContentResolver resolver = getContentResolver();
589 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
590 mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY);
591 mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700592
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700593 // update the demo string
594 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
595 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800596 if (mDemoStringIndex > -1){
597 mDefaultLocPref.setValueIndex(mDemoStringIndex);
598 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700599 }
600
601 /**
602 * (helper function for initDefaultLang() )
603 * Returns whether there is a default language in the TTS settings.
604 */
605 private boolean hasLangPref() {
606 String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG);
607 return (language != null);
608 }
609
610 /**
611 * (helper function for initDefaultLang() )
612 * Returns whether the current Locale is supported by this Settings screen
613 */
614 private boolean isCurrentLocSupported() {
615 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
616 + Locale.getDefault().getISO3Country();
617 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
618 }
619
620 /**
621 * (helper function for initDefaultLang() )
622 * Sets the default language in TTS settings to be the current Locale.
623 * This should only be used after checking that the current Locale is supported.
624 */
625 private void useCurrentLocAsDefault() {
626 Locale currentLocale = Locale.getDefault();
627 ContentResolver resolver = getContentResolver();
628 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
629 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
630 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
631 }
632
633 /**
634 * (helper function for initDefaultLang() )
635 * Sets the default language in TTS settings to be one known to be supported
636 */
637 private void useSupportedLocAsDefault() {
638 ContentResolver resolver = getContentResolver();
639 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
640 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
641 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700642 }
643
Charles Chen5dbc74a2009-12-07 12:08:13 -0800644
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700645 private void loadEngines() {
646 ListPreference enginesPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800647
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700648 // TODO (clchen): Try to see if it is possible to be more efficient here
649 // and not search for plugins again.
650 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
651 ResolveInfo[] enginesArray = new ResolveInfo[0];
652 PackageManager pm = getPackageManager();
653 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
654 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
655 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
Charles Chenf47cce02010-03-17 17:33:23 -0700656 String enabledEngines = "";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700657 for (int i = 0; i < enginesArray.length; i++) {
Charles Chenf47cce02010-03-17 17:33:23 -0700658 String pluginPackageName = enginesArray[i].activityInfo.packageName;
659 if (pluginPackageName.equals(SYSTEM_TTS)) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700660 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700661 values.add(pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700662 } else {
663 CheckBoxPreference pref = (CheckBoxPreference) findPreference(
Charles Chenf47cce02010-03-17 17:33:23 -0700664 KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700665 if ((pref != null) && pref.isChecked()){
666 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700667 values.add(pluginPackageName);
668 enabledEngines = enabledEngines + pluginPackageName + " ";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700669 }
670 }
671 }
Charles Chenf47cce02010-03-17 17:33:23 -0700672 ContentResolver resolver = getContentResolver();
673 Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800674
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700675 CharSequence entriesArray[] = new CharSequence[entries.size()];
676 CharSequence valuesArray[] = new CharSequence[values.size()];
677
678 enginesPref.setEntries(entries.toArray(entriesArray));
679 enginesPref.setEntryValues(values.toArray(valuesArray));
Charles Chen5dbc74a2009-12-07 12:08:13 -0800680 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800681
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700682}