blob: 34ce2515a6649e89c626c90a787bfe3360b453e7 [file] [log] [blame]
Jean-Michel Trivied29a652009-06-05 18:37:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070019import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070020import static android.provider.Settings.Secure.TTS_DEFAULT_LANG;
21import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -070022import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070023import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT;
Charles Chenf47cce02010-03-17 17:33:23 -070024import static android.provider.Settings.Secure.TTS_ENABLED_PLUGINS;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070025import static android.provider.Settings.Secure.TTS_USE_DEFAULTS;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070026
Charles Chen0a0eb5f2010-03-16 20:09:17 -070027import android.app.AlertDialog;
Bjorn Bringertc7762972011-03-11 16:52:51 +000028import android.content.ActivityNotFoundException;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070029import android.content.ContentResolver;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070030import android.content.Context;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070031import android.content.DialogInterface;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070032import android.content.Intent;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070033import android.content.pm.PackageManager;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070034import android.os.Bundle;
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070035import android.preference.CheckBoxPreference;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070036import android.preference.ListPreference;
37import android.preference.Preference;
Narayan Kamath934d21d2011-05-05 13:41:11 +010038import android.preference.Preference.OnPreferenceClickListener;
Charles Chen0a0eb5f2010-03-16 20:09:17 -070039import android.preference.PreferenceGroup;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070040import android.provider.Settings;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070041import android.provider.Settings.SettingNotFoundException;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070042import android.speech.tts.TextToSpeech;
Narayan Kamath3dbba082011-06-10 16:50:48 +010043import android.speech.tts.TextToSpeech.EngineInfo;
44import android.speech.tts.TtsEngines;
Bjorn Bringertc7762972011-03-11 16:52:51 +000045import android.text.TextUtils;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070046import android.util.Log;
47
Charles Chenc8298712010-02-10 13:58:23 -080048import java.util.ArrayList;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070049import java.util.List;
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -070050import java.util.Locale;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070051import java.util.StringTokenizer;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070052
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -070053public class TextToSpeechSettings extends SettingsPreferenceFragment implements
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070054 Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
55 TextToSpeech.OnInitListener {
Jean-Michel Trivied29a652009-06-05 18:37:29 -070056
57 private static final String TAG = "TextToSpeechSettings";
Jean-Michel Trivied29a652009-06-05 18:37:29 -070058
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";
Bjorn Bringertc7762972011-03-11 16:52:51 +000067 private static final String KEY_TTS_ENGINES = "tts_engines_section";
Charles Chen0a0eb5f2010-03-16 20:09:17 -070068
69 private static final String KEY_PLUGIN_ENABLED_PREFIX = "ENABLED_";
70 private static final String KEY_PLUGIN_SETTINGS_PREFIX = "SETTINGS_";
71
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070072 // TODO move default Locale values to TextToSpeech.Engine
73 private static final String DEFAULT_LANG_VAL = "eng";
74 private static final String DEFAULT_COUNTRY_VAL = "USA";
75 private static final String DEFAULT_VARIANT_VAL = "";
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070076
77 private static final String LOCALE_DELIMITER = "-";
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070078
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070079 private Preference mPlayExample = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070080 private Preference mInstallData = null;
Jean-Michel Trivied29a652009-06-05 18:37:29 -070081 private CheckBoxPreference mUseDefaultPref = null;
82 private ListPreference mDefaultRatePref = null;
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -070083 private ListPreference mDefaultLocPref = null;
Charles Chen5dbc74a2009-12-07 12:08:13 -080084 private ListPreference mDefaultSynthPref = null;
Bjorn Bringertc7762972011-03-11 16:52:51 +000085 private PreferenceGroup mEnginesGroup;
86
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 Trivie8e23db2009-09-02 15:15:33 -070090 private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
91
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -070092 // Index of the current string to use for the demo.
93 private int mDemoStringIndex = 0;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070094
95 private boolean mEnableDemo = false;
Charles Chenc8298712010-02-10 13:58:23 -080096 private boolean mVoicesMissing = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -070097
98 private TextToSpeech mTts = null;
Narayan Kamath3dbba082011-06-10 16:50:48 +010099 private TtsEngines mEnginesHelper = null;
Charles Chencf31e652010-04-07 14:26:31 -0700100 private boolean mTtsStarted = false;
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700101
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
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700110 public void onCreate(Bundle savedInstanceState) {
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700111 super.onCreate(savedInstanceState);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700112 addPreferencesFromResource(R.xml.tts_settings);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700113
Bjorn Bringertc7762972011-03-11 16:52:51 +0000114 getActivity().setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
Jean-Michel Trivi6dde8962009-08-11 09:06:58 -0700115
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700116 mEnableDemo = false;
Charles Chencf31e652010-04-07 14:26:31 -0700117 mTtsStarted = false;
Charles Chenbe6e8272010-03-22 16:06:05 -0700118
Charles Chen8c8185b2010-04-08 16:51:35 -0700119 Locale currentLocale = Locale.getDefault();
120 mDefaultLanguage = currentLocale.getISO3Language();
121 mDefaultCountry = currentLocale.getISO3Country();
122 mDefaultLocVariant = currentLocale.getVariant();
123
Bjorn Bringertc7762972011-03-11 16:52:51 +0000124 mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
125 mPlayExample.setOnPreferenceClickListener(this);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700126
Bjorn Bringertc7762972011-03-11 16:52:51 +0000127 mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
128 mInstallData.setOnPreferenceClickListener(this);
129
130 mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
131 mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH);
132 mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
133 mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
134
135 mEnginesGroup = (PreferenceGroup) findPreference(KEY_TTS_ENGINES);
136
137 mTts = new TextToSpeech(getActivity().getApplicationContext(), this);
Narayan Kamath3dbba082011-06-10 16:50:48 +0100138 mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
Bjorn Bringertc7762972011-03-11 16:52:51 +0000139
140 initDefaultSettings();
141 addEngineSpecificSettings();
142 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700143
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700144 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700145 public void onStart() {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700146 super.onStart();
Charles Chencf31e652010-04-07 14:26:31 -0700147 if (mTtsStarted){
148 // whenever we return to this screen, we don't know the state of the
149 // system, so we have to recheck that we can play the demo, or it must be disabled.
150 // TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount
Charles Chencf31e652010-04-07 14:26:31 -0700151 updateWidgetState();
152 checkVoiceData();
153 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700154 }
155
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700156 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700157 public void onDestroy() {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700158 super.onDestroy();
159 if (mTts != null) {
160 mTts.shutdown();
161 }
162 }
163
Charles Chen8c8185b2010-04-08 16:51:35 -0700164 @Override
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700165 public void onPause() {
Charles Chen8c8185b2010-04-08 16:51:35 -0700166 super.onPause();
167 if ((mDefaultRatePref != null) && (mDefaultRatePref.getDialog() != null)) {
168 mDefaultRatePref.getDialog().dismiss();
169 }
170 if ((mDefaultLocPref != null) && (mDefaultLocPref.getDialog() != null)) {
171 mDefaultLocPref.getDialog().dismiss();
172 }
173 if ((mDefaultSynthPref != null) && (mDefaultSynthPref.getDialog() != null)) {
174 mDefaultSynthPref.getDialog().dismiss();
175 }
176 }
177
Bjorn Bringertc7762972011-03-11 16:52:51 +0000178 private void addEngineSpecificSettings() {
179 Context context = getActivity();
Narayan Kamath3dbba082011-06-10 16:50:48 +0100180 List<EngineInfo> engines = mTts.getEngines();
181 for (EngineInfo engine : engines) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000182 final String engineName = engine.name;
Narayan Kamath3dbba082011-06-10 16:50:48 +0100183 if (!engine.system) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000184 CheckBoxPreference enablePref = new CheckBoxPreference(context);
Narayan Kamath3dbba082011-06-10 16:50:48 +0100185 enablePref.setKey(KEY_PLUGIN_ENABLED_PREFIX + engineName);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000186 enablePref.setTitle(engine.label);
Bjorn Bringertdf92f2e2011-04-19 14:37:49 +0100187 enablePref.setOnPreferenceClickListener(this);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000188 mEnginesGroup.addPreference(enablePref);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700189 }
Narayan Kamath3dbba082011-06-10 16:50:48 +0100190
Bjorn Bringertc7762972011-03-11 16:52:51 +0000191 if (engineHasSettings(engineName)) {
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700192 Preference pref = new Preference(context);
Narayan Kamath3dbba082011-06-10 16:50:48 +0100193 pref.setKey(KEY_PLUGIN_SETTINGS_PREFIX + engineName);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000194 pref.setTitle(engine.label);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700195 CharSequence settingsLabel = getResources().getString(
Bjorn Bringertc7762972011-03-11 16:52:51 +0000196 R.string.tts_engine_name_settings, engine.label);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700197 pref.setSummary(settingsLabel);
Narayan Kamath934d21d2011-05-05 13:41:11 +0100198 // TODO: Add a new API for this.
199 pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
200 public boolean onPreferenceClick(Preference preference) {
201 Intent i = new Intent();
202 i.setClassName(engineName,
203 engineName + ".EngineSettings");
204 startActivity(i);
205 return true;
206 }
207 });
Bjorn Bringertc7762972011-03-11 16:52:51 +0000208 mEnginesGroup.addPreference(pref);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700209 }
210 }
211 }
212
Bjorn Bringertc7762972011-03-11 16:52:51 +0000213 private boolean engineHasSettings(String enginePackageName) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700214 PackageManager pm = getPackageManager();
215 Intent i = new Intent();
Bjorn Bringertc7762972011-03-11 16:52:51 +0000216 i.setClassName(enginePackageName, enginePackageName + ".EngineSettings");
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700217 if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){
218 return true;
219 }
220 return false;
221 }
222
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700223 private void initDefaultSettings() {
224 ContentResolver resolver = getContentResolver();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700225
226 // Find the default TTS values in the settings, initialize and store the
227 // settings if they are not found.
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700228
229 // "Use Defaults"
Bjorn Bringertc7762972011-03-11 16:52:51 +0000230 int useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS,
231 TextToSpeech.Engine.USE_DEFAULTS);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700232 mUseDefaultPref.setChecked(useDefault == 1);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700233 mUseDefaultPref.setOnPreferenceChangeListener(this);
234
Charles Chen5dbc74a2009-12-07 12:08:13 -0800235 // Default synthesis engine
Charles Chen5dbc74a2009-12-07 12:08:13 -0800236 loadEngines();
237 mDefaultSynthPref.setOnPreferenceChangeListener(this);
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700238
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700239 // Default rate
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700240 try {
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700241 mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700242 } catch (SettingNotFoundException e) {
243 // default rate setting not found, initialize it
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700244 mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
245 Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700246 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700247 mDefaultRatePref.setValue(String.valueOf(mDefaultRate));
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700248 mDefaultRatePref.setOnPreferenceChangeListener(this);
249
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700250 // Default language / country / variant : these three values map to a single ListPref
251 // representing the matching Locale
Jean-Michel 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
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700256 /**
257 * Ask the current default engine to launch the matching CHECK_TTS_DATA activity
258 * to check the required TTS files are properly installed.
259 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700260 private void checkVoiceData() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000261 String defaultEngine = mTts.getDefaultEngine();
262 if (TextUtils.isEmpty(defaultEngine)) return;
263 Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
264 intent.setPackage(defaultEngine);
265 try {
266 Log.v(TAG, "Checking voice data: " + intent.toUri(0));
267 startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
268 } catch (ActivityNotFoundException ex) {
269 Log.e(TAG, "Failed to check TTS data, no acitivty found for " + intent + ")");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700270 }
271 }
272
273
274 /**
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700275 * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
276 * so the required TTS files are properly installed.
277 */
278 private void installVoiceData() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000279 String defaultEngine = mTts.getDefaultEngine();
280 if (TextUtils.isEmpty(defaultEngine)) return;
281 Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
Jean-Michel Trivi58ea43a2009-09-09 15:13:38 -0700282 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000283 intent.setPackage(defaultEngine);
284 try {
285 Log.v(TAG, "Installing voice data: " + intent.toUri(0));
286 startActivity(intent);
287 } catch (ActivityNotFoundException ex) {
288 Log.e(TAG, "Failed to install TTS data, no acitivty found for " + intent + ")");
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700289 }
290 }
291
Charles Chen4df6c792010-01-22 11:17:40 -0800292 /**
293 * Ask the current default engine to return a string of sample text to be
294 * spoken to the user.
295 */
296 private void getSampleText() {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000297 String defaultEngine = mTts.getDefaultEngine();
298 if (TextUtils.isEmpty(defaultEngine)) return;
299 Intent intent = new Intent(TextToSpeech.Engine.ACTION_GET_SAMPLE_TEXT);
Charles Chen4df6c792010-01-22 11:17:40 -0800300 intent.putExtra("language", mDefaultLanguage);
301 intent.putExtra("country", mDefaultCountry);
302 intent.putExtra("variant", mDefaultLocVariant);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000303 intent.setPackage(defaultEngine);
304 try {
305 Log.v(TAG, "Getting sample text: " + intent.toUri(0));
306 startActivityForResult(intent, GET_SAMPLE_TEXT);
307 } catch (ActivityNotFoundException ex) {
308 Log.e(TAG, "Failed to get sample text, no acitivty found for " + intent + ")");
Charles Chen4df6c792010-01-22 11:17:40 -0800309 }
310 }
311
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700312 /**
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700313 * Called when the TTS engine is initialized.
314 */
315 public void onInit(int status) {
Jean-Michel Trivi387dc0c2009-07-28 15:13:35 -0700316 if (status == TextToSpeech.SUCCESS) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700317 mEnableDemo = true;
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800318 if (mDefaultLanguage == null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800319 mDefaultLanguage = Locale.getDefault().getISO3Language();
320 }
Jean-Michel Trivi7330a882010-02-17 12:50:44 -0800321 if (mDefaultCountry == null) {
322 mDefaultCountry = Locale.getDefault().getISO3Country();
323 }
324 if (mDefaultLocVariant == null) {
325 mDefaultLocVariant = new String();
326 }
Charles Chencf3998b2010-02-11 18:13:42 -0800327 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Charles Chencf31e652010-04-07 14:26:31 -0700328 updateWidgetState();
329 checkVoiceData();
330 mTtsStarted = true;
331 Log.v(TAG, "TTS engine for settings screen initialized.");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700332 } else {
333 Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
334 mEnableDemo = false;
335 }
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700336 updateWidgetState();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700337 }
338
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700339 /**
340 * Called when voice data integrity check returns
341 */
Daisuke Miyakawa49a305e2010-09-13 17:52:13 -0700342 @Override
343 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700344 if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000345 onVoiceDataIntegrityCheckDone(data);
Charles Chen4df6c792010-01-22 11:17:40 -0800346 } else if (requestCode == GET_SAMPLE_TEXT) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000347 onSampleTextReceived(resultCode, data);
348 }
349 }
350
351 private void onVoiceDataIntegrityCheckDone(Intent data) {
352 if (data == null){
353 Log.e(TAG, "TTS data check failed 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 }
361 Log.v(TAG, "TTS data check completed, data = " + data.toUri(0));
362 ArrayList<String> available =
363 data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
364 ArrayList<String> unavailable =
365 data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES);
366 if (available == null || unavailable == null){
367 Log.e(TAG, "TTS data check failed (available == == null)");
368 // The CHECK_TTS_DATA activity for the plugin did not run properly;
369 // disable the preview and install controls and return.
370 mEnableDemo = false;
371 mVoicesMissing = false;
372 updateWidgetState();
373 return;
374 }
375 if (available.size() > 0){
376 if (mTts == null) {
377 mTts = new TextToSpeech(getActivity(), this);
Charles Chen4df6c792010-01-22 11:17:40 -0800378 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000379
380 updateDefaultLocPref(available);
381
382 mEnableDemo = true;
383 // Make sure that the default language can be used.
384 int languageResult = mTts.setLanguage(
385 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
386 if (languageResult < TextToSpeech.LANG_AVAILABLE){
387 Locale currentLocale = Locale.getDefault();
388 mDefaultLanguage = currentLocale.getISO3Language();
389 mDefaultCountry = currentLocale.getISO3Country();
390 mDefaultLocVariant = currentLocale.getVariant();
391 languageResult = mTts.setLanguage(
392 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
393 // If the default Locale isn't supported, just choose the first available
394 // language so that there is at least something.
395 if (languageResult < TextToSpeech.LANG_AVAILABLE){
396 parseLocaleInfo(mDefaultLocPref.getEntryValues()[0].toString());
397 mTts.setLanguage(
398 new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
399 }
400 ContentResolver resolver = getContentResolver();
401 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
402 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
403 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
404 }
405 } else {
406 mEnableDemo = false;
407 }
408
409 if (unavailable.size() > 0){
410 mVoicesMissing = true;
411 } else {
412 mVoicesMissing = false;
413 }
414
415 updateWidgetState();
416 }
417
418 private void updateDefaultLocPref(ArrayList<String> availableLangs) {
419 CharSequence[] entries = new CharSequence[availableLangs.size()];
420 CharSequence[] entryValues = new CharSequence[availableLangs.size()];
421 int selectedLanguageIndex = -1;
422 String selectedLanguagePref = mDefaultLanguage;
423 if (mDefaultCountry.length() > 0) {
424 selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER +
425 mDefaultCountry;
426 }
427 if (mDefaultLocVariant.length() > 0) {
428 selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER +
429 mDefaultLocVariant;
430 }
431 for (int i = 0; i < availableLangs.size(); i++) {
432 String[] langCountryVariant = availableLangs.get(i).split("-");
433 Locale loc = null;
434 if (langCountryVariant.length == 1){
435 loc = new Locale(langCountryVariant[0]);
436 } else if (langCountryVariant.length == 2){
437 loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
438 } else if (langCountryVariant.length == 3){
439 loc = new Locale(langCountryVariant[0], langCountryVariant[1],
440 langCountryVariant[2]);
441 }
442 if (loc != null){
443 entries[i] = loc.getDisplayName();
444 entryValues[i] = availableLangs.get(i);
445 if (entryValues[i].equals(selectedLanguagePref)) {
446 selectedLanguageIndex = i;
447 }
448 }
449 }
450 mDefaultLocPref.setEntries(entries);
451 mDefaultLocPref.setEntryValues(entryValues);
452 if (selectedLanguageIndex > -1) {
453 mDefaultLocPref.setValueIndex(selectedLanguageIndex);
454 }
455 }
456
457 private void onSampleTextReceived(int resultCode, Intent data) {
458 if (resultCode == TextToSpeech.LANG_AVAILABLE) {
459 String sample = getActivity().getString(R.string.tts_demo);
460 if (data != null && data.getStringExtra("sampleText") != null) {
461 sample = data.getStringExtra("sampleText");
462 }
463 Log.v(TAG, "Got sample text: " + sample);
464 if (mTts != null) {
465 mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
466 }
467 } else {
468 // TODO: Display an error here to the user.
469 Log.e(TAG, "Did not have a sample string for the requested language");
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700470 }
471 }
472
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700473 public boolean onPreferenceChange(Preference preference, Object objValue) {
474 if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
475 // "Use Defaults"
Bjorn Bringertc7762972011-03-11 16:52:51 +0000476 int value = ((Boolean) objValue) ? 1 : 0;
477 Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, value);
478 Log.i(TAG, "TTS 'use default' settings changed, now " + value);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700479 } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
480 // Default rate
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700481 mDefaultRate = Integer.parseInt((String) objValue);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700482 try {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000483 Settings.Secure.putInt(getContentResolver(), TTS_DEFAULT_RATE, mDefaultRate);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700484 if (mTts != null) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000485 mTts.setSpeechRate(mDefaultRate / 100.0f);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700486 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000487 Log.v(TAG, "TTS default rate changed, now " + mDefaultRate);
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700488 } catch (NumberFormatException e) {
489 Log.e(TAG, "could not persist default TTS rate setting", e);
490 }
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700491 } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700492 // Default locale
493 ContentResolver resolver = getContentResolver();
494 parseLocaleInfo((String) objValue);
495 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
496 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
497 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
498 Log.v(TAG, "TTS default lang/country/variant set to "
499 + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700500 if (mTts != null) {
Charles Chencf3998b2010-02-11 18:13:42 -0800501 mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
Jean-Michel Trivi628431d2009-07-17 16:52:54 -0700502 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700503 int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue);
Bjorn Bringertc7762972011-03-11 16:52:51 +0000504 Log.v(TAG, " selected is " + newIndex);
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700505 mDemoStringIndex = newIndex > -1 ? newIndex : 0;
Charles Chen5dbc74a2009-12-07 12:08:13 -0800506 } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000507 String defaultEng = objValue.toString();
508 Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, defaultEng);
Charles Chen5dbc74a2009-12-07 12:08:13 -0800509 if (mTts != null) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000510 mTts.setEngineByPackageName(defaultEng);
Charles Chencf3998b2010-02-11 18:13:42 -0800511 mEnableDemo = false;
512 mVoicesMissing = false;
513 updateWidgetState();
514 checkVoiceData();
Charles Chen5dbc74a2009-12-07 12:08:13 -0800515 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000516 Log.v(TAG, "The default synth is: " + objValue.toString());
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700517 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700518
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700519 return true;
520 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700521
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700522
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700523 /**
524 * Called when mPlayExample or mInstallData is clicked
525 */
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700526 public boolean onPreferenceClick(Preference preference) {
527 if (preference == mPlayExample) {
Charles Chen4df6c792010-01-22 11:17:40 -0800528 // Get the sample text from the TTS engine; onActivityResult will do
529 // the actual speaking
530 getSampleText();
Jean-Michel Trivi74e565d2009-06-18 18:44:52 -0700531 return true;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000532 } else if (preference == mInstallData) {
Jean-Michel Trivi2acc02e2009-06-25 10:03:43 -0700533 installVoiceData();
534 // quit this activity so it needs to be restarted after installation of the voice data
535 finish();
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700536 return true;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000537 } else if (preference.getKey().startsWith(KEY_PLUGIN_ENABLED_PREFIX)) {
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700538 final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
Bjorn Bringertc7762972011-03-11 16:52:51 +0000539 if (chkPref.isChecked()) {
540 chkPref.setChecked(false);
541 AlertDialog d = (new AlertDialog.Builder(getActivity()))
542 .setTitle(android.R.string.dialog_alert_title)
543 .setIcon(android.R.drawable.ic_dialog_alert)
544 .setMessage(
545 getActivity().getString(R.string.tts_engine_security_warning,
546 chkPref.getTitle()))
547 .setCancelable(true)
548 .setPositiveButton(android.R.string.ok,
549 new DialogInterface.OnClickListener() {
550 public void onClick(DialogInterface dialog, int which) {
551 chkPref.setChecked(true);
552 loadEngines();
553 }
554 })
555 .setNegativeButton(android.R.string.cancel,
556 new DialogInterface.OnClickListener() {
557 public void onClick(DialogInterface dialog, int which) {
558 }
559 })
560 .create();
561 d.show();
562 } else {
563 loadEngines();
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700564 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000565 return true;
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700566 }
567 return false;
568 }
569
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700570 private void updateWidgetState() {
571 mPlayExample.setEnabled(mEnableDemo);
572 mUseDefaultPref.setEnabled(mEnableDemo);
573 mDefaultRatePref.setEnabled(mEnableDemo);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700574 mDefaultLocPref.setEnabled(mEnableDemo);
575
Charles Chenc8298712010-02-10 13:58:23 -0800576 mInstallData.setEnabled(mVoicesMissing);
Jean-Michel Trivi1e6a45a2009-06-22 16:03:40 -0700577 }
578
579
580 private void parseLocaleInfo(String locale) {
581 StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
582 mDefaultLanguage = "";
583 mDefaultCountry = "";
584 mDefaultLocVariant = "";
585 if (tokenizer.hasMoreTokens()) {
586 mDefaultLanguage = tokenizer.nextToken().trim();
587 }
588 if (tokenizer.hasMoreTokens()) {
589 mDefaultCountry = tokenizer.nextToken().trim();
590 }
591 if (tokenizer.hasMoreTokens()) {
592 mDefaultLocVariant = tokenizer.nextToken().trim();
593 }
594 }
595
596
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700597 /**
598 * Initialize the default language in the UI and in the preferences.
599 * After this method has been invoked, the default language is a supported Locale.
600 */
601 private void initDefaultLang() {
602 // if there isn't already a default language preference
603 if (!hasLangPref()) {
604 // if the current Locale is supported
605 if (isCurrentLocSupported()) {
606 // then use the current Locale as the default language
607 useCurrentLocAsDefault();
608 } else {
609 // otherwise use a default supported Locale as the default language
610 useSupportedLocAsDefault();
611 }
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700612 }
613
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700614 // Update the language preference list with the default language and the matching
615 // demo string (at this stage there is a default language pref)
616 ContentResolver resolver = getContentResolver();
617 mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
Charles Chen681d0b82010-04-12 12:06:30 -0700618 mDefaultCountry = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY);
619 mDefaultLocVariant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700620
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700621 // update the demo string
622 mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER
623 + mDefaultCountry);
Charles Chen8a37e612010-02-04 15:52:30 -0800624 if (mDemoStringIndex > -1){
625 mDefaultLocPref.setValueIndex(mDemoStringIndex);
626 }
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700627 }
628
629 /**
630 * (helper function for initDefaultLang() )
631 * Returns whether there is a default language in the TTS settings.
632 */
633 private boolean hasLangPref() {
Jean-Baptiste Queru6e61b212010-04-14 10:40:48 -0700634 ContentResolver resolver = getContentResolver();
635 String language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
636 if ((language == null) || (language.length() < 1)) {
637 return false;
638 }
639 String country = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY);
640 if (country == null) {
641 return false;
642 }
643 String variant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT);
644 if (variant == null) {
645 return false;
646 }
647 return true;
Jean-Michel Trivie8e23db2009-09-02 15:15:33 -0700648 }
649
650 /**
651 * (helper function for initDefaultLang() )
652 * Returns whether the current Locale is supported by this Settings screen
653 */
654 private boolean isCurrentLocSupported() {
655 String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER
656 + Locale.getDefault().getISO3Country();
657 return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1);
658 }
659
660 /**
661 * (helper function for initDefaultLang() )
662 * Sets the default language in TTS settings to be the current Locale.
663 * This should only be used after checking that the current Locale is supported.
664 */
665 private void useCurrentLocAsDefault() {
666 Locale currentLocale = Locale.getDefault();
667 ContentResolver resolver = getContentResolver();
668 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language());
669 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country());
670 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant());
671 }
672
673 /**
674 * (helper function for initDefaultLang() )
675 * Sets the default language in TTS settings to be one known to be supported
676 */
677 private void useSupportedLocAsDefault() {
678 ContentResolver resolver = getContentResolver();
679 Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL);
680 Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL);
681 Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL);
Jean-Michel Trivi44fbbea2009-07-06 14:04:54 -0700682 }
683
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700684 private void loadEngines() {
Narayan Kamath3dbba082011-06-10 16:50:48 +0100685 List<EngineInfo> engines = mEnginesHelper.getEngines();
Narayan Kamath62f153d2011-06-14 16:37:11 +0100686 updateUserEnabledEngines(engines);
687
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700688 ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
689 ArrayList<CharSequence> values = new ArrayList<CharSequence>();
Bjorn Bringertc7762972011-03-11 16:52:51 +0000690 StringBuilder enabledEngines = new StringBuilder();
691
Narayan Kamath3dbba082011-06-10 16:50:48 +0100692 for (EngineInfo engine : engines) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000693 Log.v(TAG, "Engine: " + engine);
Narayan Kamath3dbba082011-06-10 16:50:48 +0100694 if (mEnginesHelper.isEngineEnabled(engine.name)) {
Bjorn Bringertc7762972011-03-11 16:52:51 +0000695 entries.add(engine.label);
696 values.add(engine.name);
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700697 }
698 }
Narayan Kamath62f153d2011-06-14 16:37:11 +0100699
Charles Chen5dbc74a2009-12-07 12:08:13 -0800700
Charles Chen0a0eb5f2010-03-16 20:09:17 -0700701 CharSequence entriesArray[] = new CharSequence[entries.size()];
702 CharSequence valuesArray[] = new CharSequence[values.size()];
703
Charles Chen8c8185b2010-04-08 16:51:35 -0700704 mDefaultSynthPref.setEntries(entries.toArray(entriesArray));
705 mDefaultSynthPref.setEntryValues(values.toArray(valuesArray));
Charles Chenbe6e8272010-03-22 16:06:05 -0700706
707 // Set the selected engine based on the saved preference
708 String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH);
Charles Chen8c8185b2010-04-08 16:51:35 -0700709 int selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(selectedEngine);
Charles Chenbe6e8272010-03-22 16:06:05 -0700710 if (selectedEngineIndex == -1){
Narayan Kamath3dbba082011-06-10 16:50:48 +0100711 selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(
712 mEnginesHelper.getHighestRankedEngineName());
Charles Chenbe6e8272010-03-22 16:06:05 -0700713 }
Bjorn Bringertc7762972011-03-11 16:52:51 +0000714 if (selectedEngineIndex >= 0) {
715 mDefaultSynthPref.setValueIndex(selectedEngineIndex);
716 }
717 }
718
Narayan Kamath62f153d2011-06-14 16:37:11 +0100719 /*
720 * Write out the list of engines enabled by the user to a
721 * shared preference.
722 */
723 private void updateUserEnabledEngines(List<EngineInfo> engines) {
724 StringBuilder enginesList = new StringBuilder();
725 for (EngineInfo engine : engines) {
726 if (isEngineUserEnabled(engine.name)) {
727 if (enginesList.length() > 0) enginesList.append(' ');
728 enginesList.append(engine.name);
729 }
730 }
731
732 ContentResolver resolver = getContentResolver();
733 Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enginesList.toString());
734 }
735
736 private boolean isEngineUserEnabled(String engineName) {
737 String enginePref = KEY_PLUGIN_ENABLED_PREFIX + engineName;
738 return getPreferenceManager().getSharedPreferences().getBoolean(enginePref, false);
739 }
740
Jean-Michel Trivied29a652009-06-05 18:37:29 -0700741}