blob: 6b97ef2ae2fffa48ef401018ef249c18b776a555 [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;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070025
Charles Chen0a0eb5f2010-03-16 20:09:17 -070026import android.app.AlertDialog;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070027import android.content.ContentResolver;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070028import android.content.DialogInterface;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070029import android.content.Intent;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070033import android.os.Bundle;
34import android.preference.ListPreference;
35import android.preference.Preference;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070036import android.preference.Preference.OnPreferenceClickListener;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070037import android.preference.PreferenceActivity;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070038import android.preference.PreferenceGroup;
39import android.preference.PreferenceScreen;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070040import android.preference.CheckBoxPreference;
41import android.provider.Settings;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070042import android.provider.Settings.SettingNotFoundException;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070043import android.speech.tts.TextToSpeech;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070044import android.util.Log;
45
Charles Chenc8298712010-02-10 13:58:23 -080046import java.util.ArrayList;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070047import java.util.List;
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -070048import java.util.Locale;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070049import java.util.StringTokenizer;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070050
Jean-Michel Trivied29a652009-06-05 18:37:29 -070051public class TextToSpeechSettings extends PreferenceActivity implements
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070052 Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
53 TextToSpeech.OnInitListener {
Jean-Michel Trivied29a652009-06-05 18:37:29 -070054
55 private static final String TAG = "TextToSpeechSettings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070056
Charles Chen0a0eb5f2010-03-16 20:09:17 -070057 private static final String SYSTEM_TTS = "com.svox.pico";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070058 private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070059 private static final String KEY_TTS_INSTALL_DATA = "tts_install_data";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070060 private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070061 private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate";
Jean-Michel Trivi80368622009-06-09 19:29:27 -070062 private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070063 private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country";
64 private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant";
Charles Chen5dbc74a2009-12-07 12:08:13 -080065 private static final String KEY_TTS_DEFAULT_SYNTH = "tts_default_synth";
Charles Chen0a0eb5f2010-03-16 20:09:17 -070066
67 private static final String KEY_PLUGIN_ENABLED_PREFIX = "ENABLED_";
68 private static final String KEY_PLUGIN_SETTINGS_PREFIX = "SETTINGS_";
69
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070070 // TODO move default Locale values to TextToSpeech.Engine
71 private static final String DEFAULT_LANG_VAL = "eng";
72 private static final String DEFAULT_COUNTRY_VAL = "USA";
73 private static final String DEFAULT_VARIANT_VAL = "";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070074
75 private static final String LOCALE_DELIMITER = "-";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070076
Jean-Michel Trivi628431d2009-07-17 16:52:54 -070077 private static final String FALLBACK_TTS_DEFAULT_SYNTH =
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -070078 TextToSpeech.Engine.DEFAULT_SYNTH;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070079
80 private Preference mPlayExample = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070081 private Preference mInstallData = null;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070082 private CheckBoxPreference mUseDefaultPref = null;
83 private ListPreference mDefaultRatePref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070084 private ListPreference mDefaultLocPref = null;
Charles Chen5dbc74a2009-12-07 12:08:13 -080085 private ListPreference mDefaultSynthPref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070086 private String mDefaultLanguage = null;
87 private String mDefaultCountry = null;
88 private String mDefaultLocVariant = null;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070089 private String mDefaultEng = "";
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070090 private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
91
92 // Array of strings used to demonstrate TTS in the different languages.
93 private String[] mDemoStrings;
94 // Index of the current string to use for the demo.
95 private int mDemoStringIndex = 0;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070096
97 private boolean mEnableDemo = false;
Charles Chenc8298712010-02-10 13:58:23 -080098 private boolean mVoicesMissing = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070099
100 private TextToSpeech mTts = null;
101
102 /**
103 * Request code (arbitrary value) for voice data check through
104 * startActivityForResult.
105 */
106 private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
Charles Chen4df6c792010-01-22 11:17:40 -0800107 private static final int GET_SAMPLE_TEXT = 1983;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700108
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700109 @Override
110 protected void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
112
113 addPreferencesFromResource(R.xml.tts_settings);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700114
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700115 addEngineSpecificSettings();
116
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700117 mDemoStrings = getResources().getStringArray(R.array.tts_demo_strings);
118
Jean-Michel Trivi6dde8962009-08-11 09:06:58 -0700119 setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
120
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700121 mEnableDemo = false;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700122 initClickers();
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700123 initDefaultSettings();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700124 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700125
126
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700127 @Override
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700128 protected void onStart() {
129 super.onStart();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700130 // whenever we return to this screen, we don't know the state of the
131 // 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 -0700132 // 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 -0700133 initClickers();
134 updateWidgetState();
135 checkVoiceData();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700136 }
137
138
139 @Override
140 protected void onDestroy() {
141 super.onDestroy();
142 if (mTts != null) {
143 mTts.shutdown();
144 }
145 }
146
147
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700148 private void addEngineSpecificSettings() {
149 PreferenceGroup enginesCategory = (PreferenceGroup) findPreference("tts_engines_section");
150 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
151 ResolveInfo[] enginesArray = new ResolveInfo[0];
152 PackageManager pm = getPackageManager();
153 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
154 for (int i = 0; i < enginesArray.length; i++) {
155 String prefKey = "";
156 final String pluginPackageName = enginesArray[i].activityInfo.packageName;
157 if (!enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) {
158 CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
159 prefKey = KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName;
160 chkbxPref.setKey(prefKey);
161 chkbxPref.setTitle(enginesArray[i].loadLabel(pm));
162 enginesCategory.addPreference(chkbxPref);
163 }
164 if (pluginHasSettings(pluginPackageName)) {
165 Preference pref = new Preference(this);
166 prefKey = KEY_PLUGIN_SETTINGS_PREFIX + pluginPackageName;
167 pref.setKey(prefKey);
168 pref.setTitle(enginesArray[i].loadLabel(pm));
169 CharSequence settingsLabel = getResources().getString(
170 R.string.tts_engine_name_settings, enginesArray[i].loadLabel(pm));
171 pref.setSummary(settingsLabel);
172 pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
173 public boolean onPreferenceClick(Preference preference){
174 Intent i = new Intent();
175 i.setClassName(pluginPackageName,
176 pluginPackageName + ".EngineSettings");
177 startActivity(i);
178 return true;
179 }
180 });
181 enginesCategory.addPreference(pref);
182 }
183 }
184 }
185
186 private boolean pluginHasSettings(String pluginPackageName) {
187 PackageManager pm = getPackageManager();
188 Intent i = new Intent();
189 i.setClassName(pluginPackageName, pluginPackageName + ".EngineSettings");
190 if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){
191 return true;
192 }
193 return false;
194 }
195
196
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700197 private void initClickers() {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700198 mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700199 mPlayExample.setOnPreferenceClickListener(this);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700200
201 mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
202 mInstallData.setOnPreferenceClickListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700203 }
204
205
206 private void initDefaultSettings() {
207 ContentResolver resolver = getContentResolver();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700208
209 // Find the default TTS values in the settings, initialize and store the
210 // settings if they are not found.
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700211
212 // "Use Defaults"
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700213 int useDefault = 0;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700214 mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
215 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700216 useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700217 } catch (SettingNotFoundException e) {
218 // "use default" setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700219 useDefault = TextToSpeech.Engine.USE_DEFAULTS;
220 Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700221 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700222 mUseDefaultPref.setChecked(useDefault == 1);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700223 mUseDefaultPref.setOnPreferenceChangeListener(this);
224
Charles Chen5dbc74a2009-12-07 12:08:13 -0800225 // Default synthesis engine
226 mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
227 loadEngines();
228 mDefaultSynthPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700229 String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH);
230 if (engine == null) {
231 // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech
232 engine = FALLBACK_TTS_DEFAULT_SYNTH;
233 Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine);
234 }
235 mDefaultEng = engine;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700236
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700237 // Default rate
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700238 mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
239 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700240 mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700241 } catch (SettingNotFoundException e) {
242 // default rate setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700243 mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
244 Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700245 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700246 mDefaultRatePref.setValue(String.valueOf(mDefaultRate));
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700247 mDefaultRatePref.setOnPreferenceChangeListener(this);
248
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700249 // Default language / country / variant : these three values map to a single ListPref
250 // representing the matching Locale
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700251 mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700252 initDefaultLang();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700253 mDefaultLocPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700254 }
255
256
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700257 /**
258 * Ask the current default engine to launch the matching CHECK_TTS_DATA activity
259 * to check the required TTS files are properly installed.
260 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700261 private void checkVoiceData() {
262 PackageManager pm = getPackageManager();
263 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700264 intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700265 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
266 // query only the package that matches that of the default engine
267 for (int i = 0; i < resolveInfos.size(); i++) {
268 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
269 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
270 intent.setClassName(mDefaultEng, currentActivityInfo.name);
271 this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
272 }
273 }
274 }
275
276
277 /**
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700278 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
279 * so the required TTS files are properly installed.
280 */
281 private void installVoiceData() {
282 PackageManager pm = getPackageManager();
283 Intent intent = new Intent();
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700284 intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700285 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700286 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
287 // query only the package that matches that of the default engine
288 for (int i = 0; i < resolveInfos.size(); i++) {
289 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
290 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
291 intent.setClassName(mDefaultEng, currentActivityInfo.name);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700292 this.startActivity(intent);
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700293 }
294 }
295 }
296
Charles Chen4df6c792010-01-22 11:17:40 -0800297 /**
298 * Ask the current default engine to return a string of sample text to be
299 * spoken to the user.
300 */
301 private void getSampleText() {
302 PackageManager pm = getPackageManager();
303 Intent intent = new Intent();
304 // TODO (clchen): Replace Intent string with the actual
305 // Intent defined in the list of platform Intents.
306 intent.setAction("android.speech.tts.engine.GET_SAMPLE_TEXT");
307 intent.putExtra("language", mDefaultLanguage);
308 intent.putExtra("country", mDefaultCountry);
309 intent.putExtra("variant", mDefaultLocVariant);
310 List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
311 // query only the package that matches that of the default engine
312 for (int i = 0; i < resolveInfos.size(); i++) {
313 ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
314 if (mDefaultEng.equals(currentActivityInfo.packageName)) {
315 intent.setClassName(mDefaultEng, currentActivityInfo.name);
316 this.startActivityForResult(intent, GET_SAMPLE_TEXT);
317 }
318 }
319 }
320
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700321
322 /**
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700323 * Called when the TTS engine is initialized.
324 */
325 public void onInit(int status) {
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700326 if (status == TextToSpeech.SUCCESS) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700327 Log.v(TAG, "TTS engine for settings screen initialized.");
328 mEnableDemo = true;
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800329 if (mDefaultLanguage == null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800330 mDefaultLanguage = Locale.getDefault().getISO3Language();
331 }
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800332 if (mDefaultCountry == null) {
333 mDefaultCountry = Locale.getDefault().getISO3Country();
334 }
335 if (mDefaultLocVariant == null) {
336 mDefaultLocVariant = new String();
337 }
Charles Chencf3998b2010-02-11 18:13:42 -0800338 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700339 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700340 } else {
341 Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
342 mEnableDemo = false;
343 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700344 updateWidgetState();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700345 }
346
347
348 /**
349 * Called when voice data integrity check returns
350 */
351 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
352 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
Charles Chend5f013a2010-02-18 10:11:25 -0800353 if (data == null){
354 // The CHECK_TTS_DATA activity for the plugin did not run properly;
355 // disable the preview and install controls and return.
356 mEnableDemo = false;
357 mVoicesMissing = false;
358 updateWidgetState();
359 return;
360 }
Charles Chenc8298712010-02-10 13:58:23 -0800361 // TODO (clchen): Add these extras to TextToSpeech.Engine
362 ArrayList<String> available =
363 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES");
364 ArrayList<String> unavailable =
365 data.getStringArrayListExtra("TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES");
Charles Chend5f013a2010-02-18 10:11:25 -0800366 if ((available == null) || (unavailable == null)){
367 // The CHECK_TTS_DATA activity for the plugin did not run properly;
368 // disable the preview and install controls and return.
369 mEnableDemo = false;
370 mVoicesMissing = false;
371 updateWidgetState();
372 return;
373 }
Charles Chenc8298712010-02-10 13:58:23 -0800374 if (available.size() > 0){
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700375 if (mTts == null) {
376 mTts = new TextToSpeech(this, this);
377 }
Charles Chenc8298712010-02-10 13:58:23 -0800378 ListPreference ttsLanguagePref =
379 (ListPreference) findPreference("tts_default_lang");
380 CharSequence[] entries = new CharSequence[available.size()];
381 CharSequence[] entryValues = new CharSequence[available.size()];
382 for (int i=0; i<available.size(); i++){
383 String[] langCountryVariant = available.get(i).split("-");
384 Locale loc = null;
385 if (langCountryVariant.length == 1){
386 loc = new Locale(langCountryVariant[0]);
387 } else if (langCountryVariant.length == 2){
388 loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
389 } else if (langCountryVariant.length == 3){
390 loc = new Locale(langCountryVariant[0], langCountryVariant[1],
391 langCountryVariant[2]);
392 }
393 if (loc != null){
394 entries[i] = loc.getDisplayName();
395 entryValues[i] = available.get(i);
396 }
397 }
398 ttsLanguagePref.setEntries(entries);
399 ttsLanguagePref.setEntryValues(entryValues);
400 mEnableDemo = true;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700401 } else {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700402 mEnableDemo = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700403 }
Charles Chenc8298712010-02-10 13:58:23 -0800404
405 if (unavailable.size() > 0){
406 mVoicesMissing = true;
407 } else {
408 mVoicesMissing = false;
409 }
410
411 updateWidgetState();
Charles Chen4df6c792010-01-22 11:17:40 -0800412 } else if (requestCode == GET_SAMPLE_TEXT) {
413 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
Charles Chenec05e712010-02-18 15:06:26 -0800414 String sample = getString(R.string.tts_demo);
415 if ((data != null) && (data.getStringExtra("sampleText") != null)) {
416 sample = data.getStringExtra("sampleText");
Charles Chend5f013a2010-02-18 10:11:25 -0800417 }
Charles Chen4df6c792010-01-22 11:17:40 -0800418 if (mTts != null) {
Charles Chen4df6c792010-01-22 11:17:40 -0800419 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
420 }
421 } else {
422 // TODO: Display an error here to the user.
423 Log.e(TAG, "Did not have a sample string for the requested language");
424 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700425 }
426 }
427
428
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700429 public boolean onPreferenceChange(Preference preference, Object objValue) {
430 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
431 // "Use Defaults"
432 int value = (Boolean)objValue ? 1 : 0;
Jean-Michel Trivi80368622009-06-09 19:29:27 -0700433 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS,
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700434 value);
435 Log.i(TAG, "TTS use default settings is "+objValue.toString());
436 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
437 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700438 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700439 try {
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700440 Settings.Secure.putInt(getContentResolver(),
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700441 TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700442 if (mTts != null) {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700443 mTts.setSpeechRate((float)(mDefaultRate/100.0f));
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700444 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700445 Log.i(TAG, "TTS default rate is " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700446 } catch (NumberFormatException e) {
447 Log.e(TAG, "could not persist default TTS rate setting", e);
448 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700449 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700450 // Default locale
451 ContentResolver resolver = getContentResolver();
452 parseLocaleInfo((String) objValue);
453 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
454 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
455 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
456 Log.v(TAG, "TTS default lang/country/variant set to "
457 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700458 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800459 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700460 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700461 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
462 Log.v("Settings", " selected is " + newIndex);
463 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800464 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Charles Chen5dbc74a2009-12-07 12:08:13 -0800465 mDefaultEng = objValue.toString();
466 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng);
467 if (mTts != null) {
468 mTts.setEngineByPackageName(mDefaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800469 mEnableDemo = false;
470 mVoicesMissing = false;
471 updateWidgetState();
472 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800473 }
474 Log.v("Settings", "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700475 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700476
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700477 return true;
478 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700479
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700480
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700481 /**
482 * Called when mPlayExample or mInstallData is clicked
483 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700484 public boolean onPreferenceClick(Preference preference) {
485 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800486 // Get the sample text from the TTS engine; onActivityResult will do
487 // the actual speaking
488 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700489 return true;
490 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700491 if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700492 installVoiceData();
493 // quit this activity so it needs to be restarted after installation of the voice data
494 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700495 return true;
496 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700497 return false;
498 }
499
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700500 @Override
501 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
502 if (Utils.isMonkeyRunning()) {
503 return false;
504 }
505
506 if (preference instanceof CheckBoxPreference) {
507 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
508 if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){
509 if (chkPref.isChecked()) {
510 chkPref.setChecked(false);
511 AlertDialog d = (new AlertDialog.Builder(this))
512 .setTitle(android.R.string.dialog_alert_title)
513 .setIcon(android.R.drawable.ic_dialog_alert)
514 .setMessage(getString(R.string.tts_engine_security_warning,
515 chkPref.getTitle()))
516 .setCancelable(true)
517 .setPositiveButton(android.R.string.ok,
518 new DialogInterface.OnClickListener() {
519 public void onClick(DialogInterface dialog, int which) {
520 chkPref.setChecked(true);
521 loadEngines();
522 }
523 })
524 .setNegativeButton(android.R.string.cancel,
525 new DialogInterface.OnClickListener() {
526 public void onClick(DialogInterface dialog, int which) {
527 }
528 })
529 .create();
530 d.show();
531 } else {
532 loadEngines();
533 }
534 return true;
535 }
536 }
537 return false;
538 }
539
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700540
541 private void updateWidgetState() {
542 mPlayExample.setEnabled(mEnableDemo);
543 mUseDefaultPref.setEnabled(mEnableDemo);
544 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700545 mDefaultLocPref.setEnabled(mEnableDemo);
546
Charles Chenc8298712010-02-10 13:58:23 -0800547 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700548 }
549
550
551 private void parseLocaleInfo(String locale) {
552 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
553 mDefaultLanguage = "";
554 mDefaultCountry = "";
555 mDefaultLocVariant = "";
556 if (tokenizer.hasMoreTokens()) {
557 mDefaultLanguage = tokenizer.nextToken().trim();
558 }
559 if (tokenizer.hasMoreTokens()) {
560 mDefaultCountry = tokenizer.nextToken().trim();
561 }
562 if (tokenizer.hasMoreTokens()) {
563 mDefaultLocVariant = tokenizer.nextToken().trim();
564 }
565 }
566
567
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700568 /**
569 * Initialize the default language in the UI and in the preferences.
570 * After this method has been invoked, the default language is a supported Locale.
571 */
572 private void initDefaultLang() {
573 // if there isn't already a default language preference
574 if (!hasLangPref()) {
575 // if the current Locale is supported
576 if (isCurrentLocSupported()) {
577 // then use the current Locale as the default language
578 useCurrentLocAsDefault();
579 } else {
580 // otherwise use a default supported Locale as the default language
581 useSupportedLocAsDefault();
582 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700583 }
584
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700585 // Update the language preference list with the default language and the matching
586 // demo string (at this stage there is a default language pref)
587 ContentResolver resolver = getContentResolver();
588 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
589 mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY);
590 mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700591
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700592 // update the demo string
593 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
594 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800595 if (mDemoStringIndex > -1){
596 mDefaultLocPref.setValueIndex(mDemoStringIndex);
597 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700598 }
599
600 /**
601 * (helper function for initDefaultLang() )
602 * Returns whether there is a default language in the TTS settings.
603 */
604 private boolean hasLangPref() {
605 String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG);
606 return (language != null);
607 }
608
609 /**
610 * (helper function for initDefaultLang() )
611 * Returns whether the current Locale is supported by this Settings screen
612 */
613 private boolean isCurrentLocSupported() {
614 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
615 + Locale.getDefault().getISO3Country();
616 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
617 }
618
619 /**
620 * (helper function for initDefaultLang() )
621 * Sets the default language in TTS settings to be the current Locale.
622 * This should only be used after checking that the current Locale is supported.
623 */
624 private void useCurrentLocAsDefault() {
625 Locale currentLocale = Locale.getDefault();
626 ContentResolver resolver = getContentResolver();
627 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
628 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
629 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
630 }
631
632 /**
633 * (helper function for initDefaultLang() )
634 * Sets the default language in TTS settings to be one known to be supported
635 */
636 private void useSupportedLocAsDefault() {
637 ContentResolver resolver = getContentResolver();
638 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
639 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
640 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700641 }
642
Charles Chen5dbc74a2009-12-07 12:08:13 -0800643
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700644 private void loadEngines() {
645 ListPreference enginesPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800646
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700647 // TODO (clchen): Try to see if it is possible to be more efficient here
648 // and not search for plugins again.
649 Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
650 ResolveInfo[] enginesArray = new ResolveInfo[0];
651 PackageManager pm = getPackageManager();
652 enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
653 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
654 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
655 for (int i = 0; i < enginesArray.length; i++) {
656 if (enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) {
657 entries.add(enginesArray[i].loadLabel(pm));
658 values.add(enginesArray[i].activityInfo.packageName);
659 } else {
660 CheckBoxPreference pref = (CheckBoxPreference) findPreference(
661 KEY_PLUGIN_ENABLED_PREFIX + enginesArray[i].activityInfo.packageName);
662 if ((pref != null) && pref.isChecked()){
663 entries.add(enginesArray[i].loadLabel(pm));
664 values.add(enginesArray[i].activityInfo.packageName);
665 }
666 }
667 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800668
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700669 CharSequence entriesArray[] = new CharSequence[entries.size()];
670 CharSequence valuesArray[] = new CharSequence[values.size()];
671
672 enginesPref.setEntries(entries.toArray(entriesArray));
673 enginesPref.setEntryValues(values.toArray(valuesArray));
Charles Chen5dbc74a2009-12-07 12:08:13 -0800674 }
Charles Chen5dbc74a2009-12-07 12:08:13 -0800675
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700676}