blob: fa9ea583fa465d46c3bfa605d0cb965d8ea2c12c [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;
Charles Chen473111b2010-04-06 15:50:42 -0700404 // Make sure that the default language can be used.
405 int languageResult = mTts.setLanguage(
406 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
407 if (languageResult < TextToSpeech.LANG_AVAILABLE){
408 Locale currentLocale = Locale.getDefault();
409 mDefaultLanguage = currentLocale.getISO3Language();
410 mDefaultCountry = currentLocale.getISO3Country();
411 mDefaultLocVariant = currentLocale.getVariant();
412 languageResult = mTts.setLanguage(
413 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
414 // If the default Locale isn't supported, just choose the first available
415 // language so that there is at least something.
416 if (languageResult < TextToSpeech.LANG_AVAILABLE){
417 parseLocaleInfo(ttsLanguagePref.getEntryValues()[0].toString());
418 mTts.setLanguage(
419 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
420 }
421 ContentResolver resolver = getContentResolver();
422 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
423 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
424 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
425 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700426 } else {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700427 mEnableDemo = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700428 }
Charles Chenc8298712010-02-10 13:58:23 -0800429
430 if (unavailable.size() > 0){
431 mVoicesMissing = true;
432 } else {
433 mVoicesMissing = false;
434 }
435
436 updateWidgetState();
Charles Chen4df6c792010-01-22 11:17:40 -0800437 } else if (requestCode == GET_SAMPLE_TEXT) {
438 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
Charles Chenec05e712010-02-18 15:06:26 -0800439 String sample = getString(R.string.tts_demo);
440 if ((data != null) && (data.getStringExtra("sampleText") != null)) {
441 sample = data.getStringExtra("sampleText");
Charles Chend5f013a2010-02-18 10:11:25 -0800442 }
Charles Chen4df6c792010-01-22 11:17:40 -0800443 if (mTts != null) {
Charles Chen4df6c792010-01-22 11:17:40 -0800444 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
445 }
446 } else {
447 // TODO: Display an error here to the user.
448 Log.e(TAG, "Did not have a sample string for the requested language");
449 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700450 }
451 }
452
453
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700454 public boolean onPreferenceChange(Preference preference, Object objValue) {
455 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
456 // "Use Defaults"
457 int value = (Boolean)objValue ? 1 : 0;
Jean-Michel Trivi80368622009-06-09 19:29:27 -0700458 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS,
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700459 value);
460 Log.i(TAG, "TTS use default settings is "+objValue.toString());
461 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
462 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700463 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700464 try {
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700465 Settings.Secure.putInt(getContentResolver(),
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700466 TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700467 if (mTts != null) {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700468 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700469 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700470 Log.i(TAG, "TTS default rate is " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700471 } catch (NumberFormatException e) {
472 Log.e(TAG, "could not persist default TTS rate setting", e);
473 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700474 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700475 // Default locale
476 ContentResolver resolver = getContentResolver();
477 parseLocaleInfo((String) objValue);
478 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
479 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
480 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
481 Log.v(TAG, "TTS default lang/country/variant set to "
482 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700483 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800484 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700485 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700486 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
487 Log.v("Settings", " selected is " + newIndex);
488 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800489 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Charles Chen5dbc74a2009-12-07 12:08:13 -0800490 mDefaultEng = objValue.toString();
491 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng);
492 if (mTts != null) {
493 mTts.setEngineByPackageName(mDefaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800494 mEnableDemo = false;
495 mVoicesMissing = false;
496 updateWidgetState();
497 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800498 }
499 Log.v("Settings", "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700500 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700501
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700502 return true;
503 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700504
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700505
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700506 /**
507 * Called when mPlayExample or mInstallData is clicked
508 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700509 public boolean onPreferenceClick(Preference preference) {
510 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800511 // Get the sample text from the TTS engine; onActivityResult will do
512 // the actual speaking
513 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700514 return true;
515 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700516 if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700517 installVoiceData();
518 // quit this activity so it needs to be restarted after installation of the voice data
519 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700520 return true;
521 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700522 return false;
523 }
524
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700525 @Override
526 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
527 if (Utils.isMonkeyRunning()) {
528 return false;
529 }
530
531 if (preference instanceof CheckBoxPreference) {
532 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
533 if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){
534 if (chkPref.isChecked()) {
535 chkPref.setChecked(false);
536 AlertDialog d = (new AlertDialog.Builder(this))
537 .setTitle(android.R.string.dialog_alert_title)
538 .setIcon(android.R.drawable.ic_dialog_alert)
539 .setMessage(getString(R.string.tts_engine_security_warning,
540 chkPref.getTitle()))
541 .setCancelable(true)
542 .setPositiveButton(android.R.string.ok,
543 new DialogInterface.OnClickListener() {
544 public void onClick(DialogInterface dialog, int which) {
545 chkPref.setChecked(true);
546 loadEngines();
547 }
548 })
549 .setNegativeButton(android.R.string.cancel,
550 new DialogInterface.OnClickListener() {
551 public void onClick(DialogInterface dialog, int which) {
552 }
553 })
554 .create();
555 d.show();
556 } else {
557 loadEngines();
558 }
559 return true;
560 }
561 }
562 return false;
563 }
564
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700565
566 private void updateWidgetState() {
567 mPlayExample.setEnabled(mEnableDemo);
568 mUseDefaultPref.setEnabled(mEnableDemo);
569 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700570 mDefaultLocPref.setEnabled(mEnableDemo);
571
Charles Chenc8298712010-02-10 13:58:23 -0800572 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700573 }
574
575
576 private void parseLocaleInfo(String locale) {
577 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
578 mDefaultLanguage = "";
579 mDefaultCountry = "";
580 mDefaultLocVariant = "";
581 if (tokenizer.hasMoreTokens()) {
582 mDefaultLanguage = tokenizer.nextToken().trim();
583 }
584 if (tokenizer.hasMoreTokens()) {
585 mDefaultCountry = tokenizer.nextToken().trim();
586 }
587 if (tokenizer.hasMoreTokens()) {
588 mDefaultLocVariant = tokenizer.nextToken().trim();
589 }
590 }
591
592
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700593 /**
594 * Initialize the default language in the UI and in the preferences.
595 * After this method has been invoked, the default language is a supported Locale.
596 */
597 private void initDefaultLang() {
598 // if there isn't already a default language preference
599 if (!hasLangPref()) {
600 // if the current Locale is supported
601 if (isCurrentLocSupported()) {
602 // then use the current Locale as the default language
603 useCurrentLocAsDefault();
604 } else {
605 // otherwise use a default supported Locale as the default language
606 useSupportedLocAsDefault();
607 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700608 }
609
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700610 // Update the language preference list with the default language and the matching
611 // demo string (at this stage there is a default language pref)
612 ContentResolver resolver = getContentResolver();
613 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
614 mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY);
615 mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700616
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700617 // update the demo string
618 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
619 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800620 if (mDemoStringIndex > -1){
621 mDefaultLocPref.setValueIndex(mDemoStringIndex);
622 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700623 }
624
625 /**
626 * (helper function for initDefaultLang() )
627 * Returns whether there is a default language in the TTS settings.
628 */
629 private boolean hasLangPref() {
630 String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG);
631 return (language != null);
632 }
633
634 /**
635 * (helper function for initDefaultLang() )
636 * Returns whether the current Locale is supported by this Settings screen
637 */
638 private boolean isCurrentLocSupported() {
639 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
640 + Locale.getDefault().getISO3Country();
641 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
642 }
643
644 /**
645 * (helper function for initDefaultLang() )
646 * Sets the default language in TTS settings to be the current Locale.
647 * This should only be used after checking that the current Locale is supported.
648 */
649 private void useCurrentLocAsDefault() {
650 Locale currentLocale = Locale.getDefault();
651 ContentResolver resolver = getContentResolver();
652 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
653 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
654 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
655 }
656
657 /**
658 * (helper function for initDefaultLang() )
659 * Sets the default language in TTS settings to be one known to be supported
660 */
661 private void useSupportedLocAsDefault() {
662 ContentResolver resolver = getContentResolver();
663 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
664 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
665 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700666 }
667
Charles Chen5dbc74a2009-12-07 12:08:13 -0800668
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700669 private void loadEngines() {
670 ListPreference enginesPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800671
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700672 // TODO (clchen): Try to see if it is possible to be more efficient here
673 // and not search for plugins again.
674 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
675 ResolveInfo[] enginesArray = new ResolveInfo[0];
676 PackageManager pm = getPackageManager();
677 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
678 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
679 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
Charles Chenf47cce02010-03-17 17:33:23 -0700680 String enabledEngines = "";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700681 for (int i = 0; i < enginesArray.length; i++) {
Charles Chenf47cce02010-03-17 17:33:23 -0700682 String pluginPackageName = enginesArray[i].activityInfo.packageName;
683 if (pluginPackageName.equals(SYSTEM_TTS)) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700684 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700685 values.add(pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700686 } else {
687 CheckBoxPreference pref = (CheckBoxPreference) findPreference(
Charles Chenf47cce02010-03-17 17:33:23 -0700688 KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700689 if ((pref != null) && pref.isChecked()){
690 entries.add(enginesArray[i].loadLabel(pm));
Charles Chenf47cce02010-03-17 17:33:23 -0700691 values.add(pluginPackageName);
692 enabledEngines = enabledEngines + pluginPackageName + " ";
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700693 }
694 }
695 }
Charles Chenf47cce02010-03-17 17:33:23 -0700696 ContentResolver resolver = getContentResolver();
697 Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800698
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700699 CharSequence entriesArray[] = new CharSequence[entries.size()];
700 CharSequence valuesArray[] = new CharSequence[values.size()];
701
702 enginesPref.setEntries(entries.toArray(entriesArray));
703 enginesPref.setEntryValues(values.toArray(valuesArray));
Charles Chenbe6e8272010-03-22 16:06:05 -0700704
705 // Set the selected engine based on the saved preference
706 String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH);
707 int selectedEngineIndex = enginesPref.findIndexOfValue(selectedEngine);
708 if (selectedEngineIndex == -1){
709 selectedEngineIndex = enginesPref.findIndexOfValue(SYSTEM_TTS);
710 }
711 enginesPref.setValueIndex(selectedEngineIndex);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800712 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800713
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700714}