1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import static android.provider.Settings.Secure.TTS_USE_DEFAULTS;
import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
import static android.provider.Settings.Secure.TTS_DEFAULT_LANG;
import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY;
import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT;
import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.List;
import java.util.StringTokenizer;
public class TextToSpeechSettings extends PreferenceActivity implements
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechSettings";
private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example";
private static final String KEY_TTS_INSTALL_DATA = "tts_install_data";
private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings";
private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate";
private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang";
private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country";
private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant";
private static final String LOCALE_DELIMITER = "-";
// TODO move this to android.speech.tts.TextToSpeech.Engine
private static final String FALLBACK_TTS_DEFAULT_SYNTH = "com.svox.pico";
private Preference mPlayExample = null;
private Preference mInstallData = null;
private CheckBoxPreference mUseDefaultPref = null;
private ListPreference mDefaultRatePref = null;
private ListPreference mDefaultLocPref = null;
private String mDefaultLanguage = null;
private String mDefaultCountry = null;
private String mDefaultLocVariant = null;
private String mDefaultEng = "";
private boolean mEnableDemo = false;
private TextToSpeech mTts = null;
/**
* Request code (arbitrary value) for voice data check through
* startActivityForResult.
*/
private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
/**
* Request code (arbitrary value) for voice data installation through
* startActivityForResult.
*/
private static final int VOICE_DATA_INSTALLATION = 1980;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.tts_settings);
mEnableDemo = false;
initClickers();
initDefaultSettings();
}
@Override
protected void onStart() {
super.onStart();
// whenever we return to this screen, we don't know the state of the
// system, so we have to recheck that we can play the demo, or it must be disabled.
// TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount
initClickers();
updateWidgetState();
checkVoiceData();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTts != null) {
mTts.shutdown();
}
}
private void initClickers() {
mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE);
mPlayExample.setOnPreferenceClickListener(this);
mInstallData = findPreference(KEY_TTS_INSTALL_DATA);
mInstallData.setOnPreferenceClickListener(this);
}
private void initDefaultSettings() {
ContentResolver resolver = getContentResolver();
int intVal = 0;
// Find the default TTS values in the settings, initialize and store the
// settings if they are not found.
// "Use Defaults"
mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT);
try {
intVal = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS);
} catch (SettingNotFoundException e) {
// "use default" setting not found, initialize it
intVal = TextToSpeech.Engine.FALLBACK_TTS_USE_DEFAULTS;
Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, intVal);
}
mUseDefaultPref.setChecked(intVal == 1);
mUseDefaultPref.setOnPreferenceChangeListener(this);
// Default engine
String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH);
if (engine == null) {
// TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech
engine = FALLBACK_TTS_DEFAULT_SYNTH;
Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine);
}
mDefaultEng = engine;
// Default rate
mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE);
try {
intVal = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE);
} catch (SettingNotFoundException e) {
// default rate setting not found, initialize it
intVal = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_RATE;
Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, intVal);
}
mDefaultRatePref.setValue(String.valueOf(intVal));
mDefaultRatePref.setOnPreferenceChangeListener(this);
// Default language / country / variant : these three values map to a single ListPref
// representing the matching Locale
String language = null;
String country = null;
String variant = null;
mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG);
language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG);
if (language != null) {
mDefaultLanguage = language;
} else {
// default language setting not found, initialize it, as well as the country and variant
language = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_LANG;
country = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_COUNTRY;
variant = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_VARIANT;
Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, language);
Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, country);
Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant);
}
if (country == null) {
// country wasn't initialized yet because a default language was found
country = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY);
if (country != null) {
mDefaultCountry = country;
} else {
// default country setting not found, initialize it, as well as the variant;
country = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_COUNTRY;
variant = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_VARIANT;
Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, country);
Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant);
}
}
if (variant == null) {
// variant wasn't initialized yet because a default country was found
variant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT);
if (variant != null) {
mDefaultLocVariant = variant;
} else {
// default variant setting not found, initialize it
variant = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_VARIANT;
Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant);
}
}
// we now have the default lang/country/variant trio, build a string value from it
String localeString = new String(language);
if (country.compareTo("") != 0) {
localeString += LOCALE_DELIMITER + country;
} else {
localeString += LOCALE_DELIMITER + " ";
}
if (variant.compareTo("") != 0) {
localeString += LOCALE_DELIMITER + variant;
}
Log.v(TAG, "In initDefaultSettings: localeString=" + localeString);
// TODO handle the case where localeString isn't in the existing entries
mDefaultLocPref.setValue(localeString);
mDefaultLocPref.setOnPreferenceChangeListener(this);
}
/**
* Ask the current default engine to launch the matching CHECK_TTS_DATA activity
* to check the required TTS files are properly installed.
*/
private void checkVoiceData() {
PackageManager pm = getPackageManager();
Intent intent = new Intent();
intent.setAction("android.intent.action.CHECK_TTS_DATA");
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
// query only the package that matches that of the default engine
for (int i = 0; i < resolveInfos.size(); i++) {
ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
if (mDefaultEng.equals(currentActivityInfo.packageName)) {
intent.setClassName(mDefaultEng, currentActivityInfo.name);
this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
}
}
}
/**
* Ask the current default engine to launch the matching INSTALL_TTS_DATA activity
* so the required TTS files are properly installed.
*/
private void installVoiceData() {
PackageManager pm = getPackageManager();
Intent intent = new Intent();
intent.setAction("android.intent.action.INSTALL_TTS_DATA");
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
// query only the package that matches that of the default engine
for (int i = 0; i < resolveInfos.size(); i++) {
ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo;
if (mDefaultEng.equals(currentActivityInfo.packageName)) {
intent.setClassName(mDefaultEng, currentActivityInfo.name);
this.startActivityForResult(intent, VOICE_DATA_INSTALLATION);
}
}
}
/**
* Called when the TTS engine is initialized.
*/
public void onInit(int status) {
if (status == TextToSpeech.TTS_SUCCESS) {
Log.v(TAG, "TTS engine for settings screen initialized.");
mEnableDemo = true;
} else {
Log.v(TAG, "TTS engine for settings screen failed to initialize successfully.");
mEnableDemo = false;
}
updateWidgetState();
}
/**
* Called when voice data integrity check returns
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
Log.v(TAG, "Voice data check passed");
if (mTts == null) {
mTts = new TextToSpeech(this, this);
}
} else {
Log.v(TAG, "Voice data check failed");
mEnableDemo = false;
updateWidgetState();
}
}
}
public boolean onPreferenceChange(Preference preference, Object objValue) {
if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) {
// "Use Defaults"
int value = (Boolean)objValue ? 1 : 0;
Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS,
value);
Log.i(TAG, "TTS use default settings is "+objValue.toString());
} else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) {
// Default rate
int value = Integer.parseInt((String) objValue);
try {
Settings.Secure.putInt(getContentResolver(),
TTS_DEFAULT_RATE, value);
if (mTts != null) {
mTts.setSpeechRate(value);
}
Log.i(TAG, "TTS default rate is "+value);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist default TTS rate setting", e);
}
} else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) {
// Default locale
ContentResolver resolver = getContentResolver();
parseLocaleInfo((String) objValue);
Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
Log.v(TAG, "TTS default lang/country/variant set to "
+ mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant);
}
return true;
}
/**
* Called when mPlayExample or mInstallData is clicked
*/
public boolean onPreferenceClick(Preference preference) {
if (preference == mPlayExample) {
// Play example
if (mTts != null) {
mTts.speak(getResources().getString(R.string.tts_demo),
TextToSpeech.TTS_QUEUE_FLUSH, null);
}
return true;
}
if (preference == mInstallData) {
installVoiceData();
// quit this activity so it needs to be restarted after installation of the voice data
finish();
return true;
}
return false;
}
private void updateWidgetState() {
mPlayExample.setEnabled(mEnableDemo);
mUseDefaultPref.setEnabled(mEnableDemo);
mDefaultRatePref.setEnabled(mEnableDemo);
mDefaultLocPref.setEnabled(mEnableDemo);
mInstallData.setEnabled(!mEnableDemo);
}
private void parseLocaleInfo(String locale) {
StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER);
mDefaultLanguage = "";
mDefaultCountry = "";
mDefaultLocVariant = "";
if (tokenizer.hasMoreTokens()) {
mDefaultLanguage = tokenizer.nextToken().trim();
}
if (tokenizer.hasMoreTokens()) {
mDefaultCountry = tokenizer.nextToken().trim();
}
if (tokenizer.hasMoreTokens()) {
mDefaultLocVariant = tokenizer.nextToken().trim();
}
}
}
|