blob: 23959c87ced2fe43d7c2774d33d65215ca2fd5d5 [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();
Charles Chenbe6e8272010-03-22 16:06:05 -0700125
126 mTts = new TextToSpeech(this, this);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700127 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700128
129
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700130 @Override
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700131 protected void onStart() {
132 super.onStart();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700133 // whenever we return to this screen, we don't know the state of the
134 // 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 -0700135 // 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 -0700136 initClickers();
137 updateWidgetState();
138 checkVoiceData();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700139 }
140
141
142 @Override
143 protected void onDestroy() {
144 super.onDestroy();
145 if (mTts != null) {
146 mTts.shutdown();
147 }
148 }
149
150
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700151 private void addEngineSpecificSettings() {
152 PreferenceGroup enginesCategory = (PreferenceGroup) findPreference("tts_engines_section");
153 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
154 ResolveInfo[] enginesArray = new ResolveInfo[0];
155 PackageManager pm = getPackageManager();
156 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
157 for (int i = 0; i < enginesArray.length; i++) {
158 String prefKey = "";
159 final String pluginPackageName = enginesArray[i].activityInfo.packageName;
160 if (!enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) {
161 CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
162 prefKey = KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName;
163 chkbxPref.setKey(prefKey);
164 chkbxPref.setTitle(enginesArray[i].loadLabel(pm));
165 enginesCategory.addPreference(chkbxPref);
166 }
167 if (pluginHasSettings(pluginPackageName)) {
168 Preference pref = new Preference(this);
169 prefKey = KEY_PLUGIN_SETTINGS_PREFIX + pluginPackageName;
170 pref.setKey(prefKey);
171 pref.setTitle(enginesArray[i].loadLabel(pm));
172 CharSequence settingsLabel = getResources().getString(
173 R.string.tts_engine_name_settings, enginesArray[i].loadLabel(pm));
174 pref.setSummary(settingsLabel);
175 pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
176 public boolean onPreferenceClick(Preference preference){
177 Intent i = new Intent();
178 i.setClassName(pluginPackageName,
179 pluginPackageName + ".EngineSettings");
180 startActivity(i);
181 return true;
182 }
183 });
184 enginesCategory.addPreference(pref);
185 }
186 }
187 }
188
189 private boolean pluginHasSettings(String pluginPackageName) {
190 PackageManager pm = getPackageManager();
191 Intent i = new Intent();
192 i.setClassName(pluginPackageName, pluginPackageName + ".EngineSettings");
193 if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){
194 return true;
195 }
196 return false;
197 }
198
199
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700200 private void initClickers() {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700201 mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700202 mPlayExample.setOnPreferenceClickListener(this);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700203
204 mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
205 mInstallData.setOnPreferenceClickListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700206 }
207
208
209 private void initDefaultSettings() {
210 ContentResolver resolver = getContentResolver();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700211
212 // Find the default TTS values in the settings, initialize and store the
213 // settings if they are not found.
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700214
215 // "Use Defaults"
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700216 int useDefault = 0;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700217 mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
218 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700219 useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700220 } catch (SettingNotFoundException e) {
221 // "use default" setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700222 useDefault = TextToSpeech.Engine.USE_DEFAULTS;
223 Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700224 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700225 mUseDefaultPref.setChecked(useDefault == 1);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700226 mUseDefaultPref.setOnPreferenceChangeListener(this);
227
Charles Chen5dbc74a2009-12-07 12:08:13 -0800228 // Default synthesis engine
229 mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
230 loadEngines();
231 mDefaultSynthPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700232 String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH);
233 if (engine == null) {
234 // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech
235 engine = FALLBACK_TTS_DEFAULT_SYNTH;
236 Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine);
237 }
238 mDefaultEng = engine;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700239
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700240 // Default rate
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700241 mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
242 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700243 mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700244 } catch (SettingNotFoundException e) {
245 // default rate setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700246 mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
247 Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700248 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700249 mDefaultRatePref.setValue(String.valueOf(mDefaultRate));
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700250 mDefaultRatePref.setOnPreferenceChangeListener(this);
251
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700252 // Default language / country / variant : these three values map to a single ListPref
253 // representing the matching Locale
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700254 mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700255 initDefaultLang();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700256 mDefaultLocPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700257 }
258
259
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700260 /**
261 * Ask the current default engine to launch the matching CHECK_TTS_DATA activity
262 * to check the required TTS files are properly installed.
263 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700264 private void checkVoiceData() {
265 PackageManager pm = getPackageManager();
266 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700267 intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700268 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
269 // query only the package that matches that of the default engine
270 for (int i = 0; i < resolveInfos.size(); i++) {
271 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
272 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
273 intent.setClassName(mDefaultEng, currentActivityInfo.name);
274 this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
275 }
276 }
277 }
278
279
280 /**
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700281 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
282 * so the required TTS files are properly installed.
283 */
284 private void installVoiceData() {
285 PackageManager pm = getPackageManager();
286 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700287 intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700288 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700289 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
290 // query only the package that matches that of the default engine
291 for (int i = 0; i < resolveInfos.size(); i++) {
292 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
293 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
294 intent.setClassName(mDefaultEng, currentActivityInfo.name);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700295 this.startActivity(intent);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700296 }
297 }
298 }
299
Charles Chen4df6c792010-01-22 11:17:40 -0800300 /**
301 * Ask the current default engine to return a string of sample text to be
302 * spoken to the user.
303 */
304 private void getSampleText() {
305 PackageManager pm = getPackageManager();
306 Intent intent = new Intent();
307 // TODO (clchen): Replace Intent string with the actual
308 // Intent defined in the list of platform Intents.
309 intent.setAction("android.speech.tts.engine.GET_SAMPLE_TEXT");
310 intent.putExtra("language", mDefaultLanguage);
311 intent.putExtra("country", mDefaultCountry);
312 intent.putExtra("variant", mDefaultLocVariant);
313 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
314 // query only the package that matches that of the default engine
315 for (int i = 0; i < resolveInfos.size(); i++) {
316 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
317 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
318 intent.setClassName(mDefaultEng, currentActivityInfo.name);
319 this.startActivityForResult(intent, GET_SAMPLE_TEXT);
320 }
321 }
322 }
323
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700324
325 /**
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700326 * Called when the TTS engine is initialized.
327 */
328 public void onInit(int status) {
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700329 if (status == TextToSpeech.SUCCESS) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700330 Log.v(TAG, "TTS engine for settings screen initialized.");
331 mEnableDemo = true;
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800332 if (mDefaultLanguage == null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800333 mDefaultLanguage = Locale.getDefault().getISO3Language();
334 }
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800335 if (mDefaultCountry == null) {
336 mDefaultCountry = Locale.getDefault().getISO3Country();
337 }
338 if (mDefaultLocVariant == null) {
339 mDefaultLocVariant = new String();
340 }
Charles Chencf3998b2010-02-11 18:13:42 -0800341 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700342 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700343 } else {
344 Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
345 mEnableDemo = false;
346 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700347 updateWidgetState();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700348 }
349
350
351 /**
352 * Called when voice data integrity check returns
353 */
354 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
355 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
Charles Chend5f013a2010-02-18 10:11:25 -0800356 if (data == null){
357 // The CHECK_TTS_DATA activity for the plugin did not run properly;
358 // disable the preview and install controls and return.
359 mEnableDemo = false;
360 mVoicesMissing = false;
361 updateWidgetState();
362 return;
363 }
Charles Chenc8298712010-02-10 13:58:23 -0800364 // TODO (clchen): Add these extras to TextToSpeech.Engine
365 ArrayList<String> available =
366 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES");
367 ArrayList<String> unavailable =
368 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES");
Charles Chend5f013a2010-02-18 10:11:25 -0800369 if ((available == null) || (unavailable == null)){
370 // The CHECK_TTS_DATA activity for the plugin did not run properly;
371 // disable the preview and install controls and return.
372 mEnableDemo = false;
373 mVoicesMissing = false;
374 updateWidgetState();
375 return;
376 }
Charles Chenc8298712010-02-10 13:58:23 -0800377 if (available.size() > 0){
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700378 if (mTts == null) {
379 mTts = new TextToSpeech(this, this);
380 }
Charles Chenc8298712010-02-10 13:58:23 -0800381 ListPreference ttsLanguagePref =
382 (ListPreference) findPreference("tts_default_lang");
383 CharSequence[] entries = new CharSequence[available.size()];
384 CharSequence[] entryValues = new CharSequence[available.size()];
385 for (int i=0; i<available.size(); i++){
386 String[] langCountryVariant = available.get(i).split("-");
387 Locale loc = null;
388 if (langCountryVariant.length == 1){
389 loc = new Locale(langCountryVariant[0]);
390 } else if (langCountryVariant.length == 2){
391 loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
392 } else if (langCountryVariant.length == 3){
393 loc = new Locale(langCountryVariant[0], langCountryVariant[1],
394 langCountryVariant[2]);
395 }
396 if (loc != null){
397 entries[i] = loc.getDisplayName();
398 entryValues[i] = available.get(i);
399 }
400 }
401 ttsLanguagePref.setEntries(entries);
402 ttsLanguagePref.setEntryValues(entryValues);
403 mEnableDemo = true;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700404 } else {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700405 mEnableDemo = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700406 }
Charles Chenc8298712010-02-10 13:58:23 -0800407
408 if (unavailable.size() > 0){
409 mVoicesMissing = true;
410 } else {
411 mVoicesMissing = false;
412 }
413
414 updateWidgetState();
Charles Chen4df6c792010-01-22 11:17:40 -0800415 } else if (requestCode == GET_SAMPLE_TEXT) {
416 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
Charles Chenec05e712010-02-18 15:06:26 -0800417 String sample = getString(R.string.tts_demo);
418 if ((data != null) && (data.getStringExtra("sampleText") != null)) {
419 sample = data.getStringExtra("sampleText");
Charles Chend5f013a2010-02-18 10:11:25 -0800420 }
Charles Chen4df6c792010-01-22 11:17:40 -0800421 if (mTts != null) {
Charles Chen4df6c792010-01-22 11:17:40 -0800422 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
423 }
424 } else {
425 // TODO: Display an error here to the user.
426 Log.e(TAG, "Did not have a sample string for the requested language");
427 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700428 }
429 }
430
431
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700432 public boolean onPreferenceChange(Preference preference, Object objValue) {
433 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
434 // "Use Defaults"
435 int value = (Boolean)objValue ? 1 : 0;
Jean-Michel Trivi80368622009-06-09 19:29:27 -0700436 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS,
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700437 value);
438 Log.i(TAG, "TTS use default settings is "+objValue.toString());
439 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
440 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700441 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700442 try {
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700443 Settings.Secure.putInt(getContentResolver(),
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700444 TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700445 if (mTts != null) {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700446 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700447 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700448 Log.i(TAG, "TTS default rate is " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700449 } catch (NumberFormatException e) {
450 Log.e(TAG, "could not persist default TTS rate setting", e);
451 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700452 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700453 // Default locale
454 ContentResolver resolver = getContentResolver();
455 parseLocaleInfo((String) objValue);
456 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
457 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
458 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
459 Log.v(TAG, "TTS default lang/country/variant set to "
460 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700461 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800462 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700463 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700464 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
465 Log.v("Settings", " selected is " + newIndex);
466 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800467 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Charles Chen5dbc74a2009-12-07 12:08:13 -0800468 mDefaultEng = objValue.toString();
469 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng);
470 if (mTts != null) {
471 mTts.setEngineByPackageName(mDefaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800472 mEnableDemo = false;
473 mVoicesMissing = false;
474 updateWidgetState();
475 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800476 }
477 Log.v("Settings", "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700478 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700479
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700480 return true;
481 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700482
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700483
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700484 /**
485 * Called when mPlayExample or mInstallData is clicked
486 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700487 public boolean onPreferenceClick(Preference preference) {
488 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800489 // Get the sample text from the TTS engine; onActivityResult will do
490 // the actual speaking
491 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700492 return true;
493 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700494 if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700495 installVoiceData();
496 // quit this activity so it needs to be restarted after installation of the voice data
497 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700498 return true;
499 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700500 return false;
501 }
502
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700503 @Override
504 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
505 if (Utils.isMonkeyRunning()) {
506 return false;
507 }
508
509 if (preference instanceof CheckBoxPreference) {
510 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
511 if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){
512 if (chkPref.isChecked()) {
513 chkPref.setChecked(false);
514 AlertDialog d = (new AlertDialog.Builder(this))
515 .setTitle(android.R.string.dialog_alert_title)
516 .setIcon(android.R.drawable.ic_dialog_alert)
517 .setMessage(getString(R.string.tts_engine_security_warning,
518 chkPref.getTitle()))
519 .setCancelable(true)
520 .setPositiveButton(android.R.string.ok,
521 new DialogInterface.OnClickListener() {
522 public void onClick(DialogInterface dialog, int which) {
523 chkPref.setChecked(true);
524 loadEngines();
525 }
526 })
527 .setNegativeButton(android.R.string.cancel,
528 new DialogInterface.OnClickListener() {
529 public void onClick(DialogInterface dialog, int which) {
530 }
531 })
532 .create();
533 d.show();
534 } else {
535 loadEngines();
536 }
537 return true;
538 }
539 }
540 return false;
541 }
542
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700543
544 private void updateWidgetState() {
545 mPlayExample.setEnabled(mEnableDemo);
546 mUseDefaultPref.setEnabled(mEnableDemo);
547 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700548 mDefaultLocPref.setEnabled(mEnableDemo);
549
Charles Chenc8298712010-02-10 13:58:23 -0800550 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700551 }
552
553
554 private void parseLocaleInfo(String locale) {
555 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
556 mDefaultLanguage = "";
557 mDefaultCountry = "";
558 mDefaultLocVariant = "";
559 if (tokenizer.hasMoreTokens()) {
560 mDefaultLanguage = tokenizer.nextToken().trim();
561 }
562 if (tokenizer.hasMoreTokens()) {
563 mDefaultCountry = tokenizer.nextToken().trim();
564 }
565 if (tokenizer.hasMoreTokens()) {
566 mDefaultLocVariant = tokenizer.nextToken().trim();
567 }
568 }
569
570
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700571 /**
572 * Initialize the default language in the UI and in the preferences.
573 * After this method has been invoked, the default language is a supported Locale.
574 */
575 private void initDefaultLang() {
576 // if there isn't already a default language preference
577 if (!hasLangPref()) {
578 // if the current Locale is supported
579 if (isCurrentLocSupported()) {
580 // then use the current Locale as the default language
581 useCurrentLocAsDefault();
582 } else {
583 // otherwise use a default supported Locale as the default language
584 useSupportedLocAsDefault();
585 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700586 }
587
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700588 // Update the language preference list with the default language and the matching
589 // demo string (at this stage there is a default language pref)
590 ContentResolver resolver = getContentResolver();
591 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
592 mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY);
593 mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700594
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700595 // update the demo string
596 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
597 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800598 if (mDemoStringIndex > -1){
599 mDefaultLocPref.setValueIndex(mDemoStringIndex);
600 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700601 }
602
603 /**
604 * (helper function for initDefaultLang() )
605 * Returns whether there is a default language in the TTS settings.
606 */
607 private boolean hasLangPref() {
608 String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG);
609 return (language != null);
610 }
611
612 /**
613 * (helper function for initDefaultLang() )
614 * Returns whether the current Locale is supported by this Settings screen
615 */
616 private boolean isCurrentLocSupported() {
617 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
618 + Locale.getDefault().getISO3Country();
619 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
620 }
621
622 /**
623 * (helper function for initDefaultLang() )
624 * Sets the default language in TTS settings to be the current Locale.
625 * This should only be used after checking that the current Locale is supported.
626 */
627 private void useCurrentLocAsDefault() {
628 Locale currentLocale = Locale.getDefault();
629 ContentResolver resolver = getContentResolver();
630 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
631 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
632 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
633 }
634
635 /**
636 * (helper function for initDefaultLang() )
637 * Sets the default language in TTS settings to be one known to be supported
638 */
639 private void useSupportedLocAsDefault() {
640 ContentResolver resolver = getContentResolver();
641 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
642 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
643 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700644 }
645
Charles Chen5dbc74a2009-12-07 12:08:13 -0800646
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700647 private void loadEngines() {
648 ListPreference enginesPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800649
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700650 // TODO (clchen): Try to see if it is possible to be more efficient here
651 // and not search for plugins again.
652 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
653 ResolveInfo[] enginesArray = new ResolveInfo[0];
654 PackageManager pm = getPackageManager();
655 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
656 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
657 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
Charles Chenf47cce02010-03-17 17:33:23 -0700658 String enabledEngines = "";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700659 for (int i = 0; i < enginesArray.length; i++) {
Charles Chenf47cce02010-03-17 17:33:23 -0700660 String pluginPackageName = enginesArray[i].activityInfo.packageName;
661 if (pluginPackageName.equals(SYSTEM_TTS)) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700662 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700663 values.add(pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700664 } else {
665 CheckBoxPreference pref = (CheckBoxPreference) findPreference(
Charles Chenf47cce02010-03-17 17:33:23 -0700666 KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700667 if ((pref != null) && pref.isChecked()){
668 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700669 values.add(pluginPackageName);
670 enabledEngines = enabledEngines + pluginPackageName + " ";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700671 }
672 }
673 }
Charles Chenf47cce02010-03-17 17:33:23 -0700674 ContentResolver resolver = getContentResolver();
675 Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800676
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700677 CharSequence entriesArray[] = new CharSequence[entries.size()];
678 CharSequence valuesArray[] = new CharSequence[values.size()];
679
680 enginesPref.setEntries(entries.toArray(entriesArray));
681 enginesPref.setEntryValues(values.toArray(valuesArray));
Charles Chenbe6e8272010-03-22 16:06:05 -0700682
683 // Set the selected engine based on the saved preference
684 String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH);
685 int selectedEngineIndex = enginesPref.findIndexOfValue(selectedEngine);
686 if (selectedEngineIndex == -1){
687 selectedEngineIndex = enginesPref.findIndexOfValue(SYSTEM_TTS);
688 }
689 enginesPref.setValueIndex(selectedEngineIndex);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800690 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800691
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700692}