Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import static android.provider.Settings.Secure.TTS_USE_DEFAULTS; |
| 20 | import static android.provider.Settings.Secure.TTS_DEFAULT_RATE; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 21 | import static android.provider.Settings.Secure.TTS_DEFAULT_LANG; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 22 | import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY; |
| 23 | import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT; |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 24 | import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH; |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 25 | import static android.provider.Settings.Secure.TTS_ENABLED_PLUGINS; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 26 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 27 | import android.app.AlertDialog; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 28 | import android.content.ContentResolver; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 29 | import android.content.DialogInterface; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.pm.ActivityInfo; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.preference.ListPreference; |
| 36 | import android.preference.Preference; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 37 | import android.preference.Preference.OnPreferenceClickListener; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 38 | import android.preference.PreferenceActivity; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 39 | import android.preference.PreferenceGroup; |
| 40 | import android.preference.PreferenceScreen; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 41 | import android.preference.CheckBoxPreference; |
| 42 | import android.provider.Settings; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 43 | import android.provider.Settings.SettingNotFoundException; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 44 | import android.speech.tts.TextToSpeech; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 45 | import android.util.Log; |
| 46 | |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 48 | import java.util.List; |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 49 | import java.util.Locale; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 50 | import java.util.StringTokenizer; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 51 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 52 | public class TextToSpeechSettings extends PreferenceActivity implements |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 53 | Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, |
| 54 | TextToSpeech.OnInitListener { |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String TAG = "TextToSpeechSettings"; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 57 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 58 | private static final String SYSTEM_TTS = "com.svox.pico"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 59 | private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 60 | private static final String KEY_TTS_INSTALL_DATA = "tts_install_data"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 61 | private static final String KEY_TTS_USE_DEFAULT = "toggle_use_default_tts_settings"; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 62 | private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate"; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 63 | private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 64 | private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country"; |
| 65 | private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant"; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 66 | private static final String KEY_TTS_DEFAULT_SYNTH = "tts_default_synth"; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 67 | |
| 68 | private static final String KEY_PLUGIN_ENABLED_PREFIX = "ENABLED_"; |
| 69 | private static final String KEY_PLUGIN_SETTINGS_PREFIX = "SETTINGS_"; |
| 70 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 71 | // 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 Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 75 | |
| 76 | private static final String LOCALE_DELIMITER = "-"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 77 | |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 78 | private static final String FALLBACK_TTS_DEFAULT_SYNTH = |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 79 | TextToSpeech.Engine.DEFAULT_SYNTH; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 80 | |
| 81 | private Preference mPlayExample = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 82 | private Preference mInstallData = null; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 83 | private CheckBoxPreference mUseDefaultPref = null; |
| 84 | private ListPreference mDefaultRatePref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 85 | private ListPreference mDefaultLocPref = null; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 86 | private ListPreference mDefaultSynthPref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 87 | private String mDefaultLanguage = null; |
| 88 | private String mDefaultCountry = null; |
| 89 | private String mDefaultLocVariant = null; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 90 | private String mDefaultEng = ""; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 91 | 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 Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 97 | |
| 98 | private boolean mEnableDemo = false; |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 99 | private boolean mVoicesMissing = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 100 | |
| 101 | private TextToSpeech mTts = null; |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 102 | private boolean mTtsStarted = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Request code (arbitrary value) for voice data check through |
| 106 | * startActivityForResult. |
| 107 | */ |
| 108 | private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 109 | private static final int GET_SAMPLE_TEXT = 1983; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 110 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 111 | @Override |
| 112 | protected void onCreate(Bundle savedInstanceState) { |
| 113 | super.onCreate(savedInstanceState); |
| 114 | |
| 115 | addPreferencesFromResource(R.xml.tts_settings); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 116 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 117 | addEngineSpecificSettings(); |
| 118 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 119 | mDemoStrings = getResources().getStringArray(R.array.tts_demo_strings); |
| 120 | |
Jean-Michel Trivi | 6dde896 | 2009-08-11 09:06:58 -0700 | [diff] [blame] | 121 | setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); |
| 122 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 123 | mEnableDemo = false; |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 124 | mTtsStarted = false; |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 125 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 126 | Locale currentLocale = Locale.getDefault(); |
| 127 | mDefaultLanguage = currentLocale.getISO3Language(); |
| 128 | mDefaultCountry = currentLocale.getISO3Country(); |
| 129 | mDefaultLocVariant = currentLocale.getVariant(); |
| 130 | |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 131 | mTts = new TextToSpeech(this, this); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 132 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 133 | |
| 134 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 135 | @Override |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 136 | protected void onStart() { |
| 137 | super.onStart(); |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 138 | if (mTtsStarted){ |
| 139 | // whenever we return to this screen, we don't know the state of the |
| 140 | // system, so we have to recheck that we can play the demo, or it must be disabled. |
| 141 | // TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount |
| 142 | initClickers(); |
| 143 | updateWidgetState(); |
| 144 | checkVoiceData(); |
| 145 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | @Override |
| 150 | protected void onDestroy() { |
| 151 | super.onDestroy(); |
| 152 | if (mTts != null) { |
| 153 | mTts.shutdown(); |
| 154 | } |
| 155 | } |
| 156 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 157 | @Override |
| 158 | protected void onPause() { |
| 159 | super.onPause(); |
| 160 | if ((mDefaultRatePref != null) && (mDefaultRatePref.getDialog() != null)) { |
| 161 | mDefaultRatePref.getDialog().dismiss(); |
| 162 | } |
| 163 | if ((mDefaultLocPref != null) && (mDefaultLocPref.getDialog() != null)) { |
| 164 | mDefaultLocPref.getDialog().dismiss(); |
| 165 | } |
| 166 | if ((mDefaultSynthPref != null) && (mDefaultSynthPref.getDialog() != null)) { |
| 167 | mDefaultSynthPref.getDialog().dismiss(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 172 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 173 | private void addEngineSpecificSettings() { |
| 174 | PreferenceGroup enginesCategory = (PreferenceGroup) findPreference("tts_engines_section"); |
| 175 | Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); |
| 176 | ResolveInfo[] enginesArray = new ResolveInfo[0]; |
| 177 | PackageManager pm = getPackageManager(); |
| 178 | enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray); |
| 179 | for (int i = 0; i < enginesArray.length; i++) { |
| 180 | String prefKey = ""; |
| 181 | final String pluginPackageName = enginesArray[i].activityInfo.packageName; |
| 182 | if (!enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) { |
| 183 | CheckBoxPreference chkbxPref = new CheckBoxPreference(this); |
| 184 | prefKey = KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName; |
| 185 | chkbxPref.setKey(prefKey); |
| 186 | chkbxPref.setTitle(enginesArray[i].loadLabel(pm)); |
| 187 | enginesCategory.addPreference(chkbxPref); |
| 188 | } |
| 189 | if (pluginHasSettings(pluginPackageName)) { |
| 190 | Preference pref = new Preference(this); |
| 191 | prefKey = KEY_PLUGIN_SETTINGS_PREFIX + pluginPackageName; |
| 192 | pref.setKey(prefKey); |
| 193 | pref.setTitle(enginesArray[i].loadLabel(pm)); |
| 194 | CharSequence settingsLabel = getResources().getString( |
| 195 | R.string.tts_engine_name_settings, enginesArray[i].loadLabel(pm)); |
| 196 | pref.setSummary(settingsLabel); |
| 197 | pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){ |
| 198 | public boolean onPreferenceClick(Preference preference){ |
| 199 | Intent i = new Intent(); |
| 200 | i.setClassName(pluginPackageName, |
| 201 | pluginPackageName + ".EngineSettings"); |
| 202 | startActivity(i); |
| 203 | return true; |
| 204 | } |
| 205 | }); |
| 206 | enginesCategory.addPreference(pref); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private boolean pluginHasSettings(String pluginPackageName) { |
| 212 | PackageManager pm = getPackageManager(); |
| 213 | Intent i = new Intent(); |
| 214 | i.setClassName(pluginPackageName, pluginPackageName + ".EngineSettings"); |
| 215 | if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){ |
| 216 | return true; |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 222 | private void initClickers() { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 223 | mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 224 | mPlayExample.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 225 | |
| 226 | mInstallData = findPreference(KEY_TTS_INSTALL_DATA); |
| 227 | mInstallData.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | private void initDefaultSettings() { |
| 232 | ContentResolver resolver = getContentResolver(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 233 | |
| 234 | // Find the default TTS values in the settings, initialize and store the |
| 235 | // settings if they are not found. |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 236 | |
| 237 | // "Use Defaults" |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 238 | int useDefault = 0; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 239 | mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
| 240 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 241 | useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 242 | } catch (SettingNotFoundException e) { |
| 243 | // "use default" setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 244 | useDefault = TextToSpeech.Engine.USE_DEFAULTS; |
| 245 | Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 246 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 247 | mUseDefaultPref.setChecked(useDefault == 1); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 248 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 249 | |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 250 | // Default synthesis engine |
| 251 | mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH); |
| 252 | loadEngines(); |
| 253 | mDefaultSynthPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 254 | String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH); |
| 255 | if (engine == null) { |
| 256 | // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech |
| 257 | engine = FALLBACK_TTS_DEFAULT_SYNTH; |
| 258 | Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine); |
| 259 | } |
| 260 | mDefaultEng = engine; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 261 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 262 | // Default rate |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 263 | mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
| 264 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 265 | mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 266 | } catch (SettingNotFoundException e) { |
| 267 | // default rate setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 268 | mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE; |
| 269 | Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 270 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 271 | mDefaultRatePref.setValue(String.valueOf(mDefaultRate)); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 272 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 273 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 274 | // Default language / country / variant : these three values map to a single ListPref |
| 275 | // representing the matching Locale |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 276 | mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG); |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 277 | initDefaultLang(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 278 | mDefaultLocPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Ask the current default engine to launch the matching CHECK_TTS_DATA activity |
| 284 | * to check the required TTS files are properly installed. |
| 285 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 286 | private void checkVoiceData() { |
| 287 | PackageManager pm = getPackageManager(); |
| 288 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 289 | intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 290 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 291 | // query only the package that matches that of the default engine |
| 292 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 293 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 294 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 295 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 296 | this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 303 | * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity |
| 304 | * so the required TTS files are properly installed. |
| 305 | */ |
| 306 | private void installVoiceData() { |
| 307 | PackageManager pm = getPackageManager(); |
| 308 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 309 | intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); |
Jean-Michel Trivi | 58ea43a | 2009-09-09 15:13:38 -0700 | [diff] [blame] | 310 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 311 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 312 | // query only the package that matches that of the default engine |
| 313 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 314 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 315 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 316 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
Jean-Michel Trivi | 58ea43a | 2009-09-09 15:13:38 -0700 | [diff] [blame] | 317 | this.startActivity(intent); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 322 | /** |
| 323 | * Ask the current default engine to return a string of sample text to be |
| 324 | * spoken to the user. |
| 325 | */ |
| 326 | private void getSampleText() { |
| 327 | PackageManager pm = getPackageManager(); |
| 328 | Intent intent = new Intent(); |
| 329 | // TODO (clchen): Replace Intent string with the actual |
| 330 | // Intent defined in the list of platform Intents. |
| 331 | intent.setAction("android.speech.tts.engine.GET_SAMPLE_TEXT"); |
| 332 | intent.putExtra("language", mDefaultLanguage); |
| 333 | intent.putExtra("country", mDefaultCountry); |
| 334 | intent.putExtra("variant", mDefaultLocVariant); |
| 335 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 336 | // query only the package that matches that of the default engine |
| 337 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 338 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 339 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 340 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 341 | this.startActivityForResult(intent, GET_SAMPLE_TEXT); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 346 | |
| 347 | /** |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 348 | * Called when the TTS engine is initialized. |
| 349 | */ |
| 350 | public void onInit(int status) { |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 351 | if (status == TextToSpeech.SUCCESS) { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 352 | mEnableDemo = true; |
Jean-Michel Trivi | 7330a88 | 2010-02-17 12:50:44 -0800 | [diff] [blame] | 353 | if (mDefaultLanguage == null) { |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 354 | mDefaultLanguage = Locale.getDefault().getISO3Language(); |
| 355 | } |
Jean-Michel Trivi | 7330a88 | 2010-02-17 12:50:44 -0800 | [diff] [blame] | 356 | if (mDefaultCountry == null) { |
| 357 | mDefaultCountry = Locale.getDefault().getISO3Country(); |
| 358 | } |
| 359 | if (mDefaultLocVariant == null) { |
| 360 | mDefaultLocVariant = new String(); |
| 361 | } |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 362 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 363 | mTts.setSpeechRate((float)(mDefaultRate/100.0f)); |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 364 | initDefaultSettings(); |
| 365 | initClickers(); |
| 366 | updateWidgetState(); |
| 367 | checkVoiceData(); |
| 368 | mTtsStarted = true; |
| 369 | Log.v(TAG, "TTS engine for settings screen initialized."); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 370 | } else { |
| 371 | Log.v(TAG, "TTS engine for settings screen failed to initialize successfully."); |
| 372 | mEnableDemo = false; |
| 373 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 374 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * Called when voice data integrity check returns |
| 380 | */ |
| 381 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 382 | if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 383 | if (data == null){ |
| 384 | // The CHECK_TTS_DATA activity for the plugin did not run properly; |
| 385 | // disable the preview and install controls and return. |
| 386 | mEnableDemo = false; |
| 387 | mVoicesMissing = false; |
| 388 | updateWidgetState(); |
| 389 | return; |
| 390 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 391 | ArrayList<String> available = |
Charles Chen | 510bc4a | 2010-04-07 17:41:07 -0700 | [diff] [blame] | 392 | data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 393 | ArrayList<String> unavailable = |
Charles Chen | 510bc4a | 2010-04-07 17:41:07 -0700 | [diff] [blame] | 394 | data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES); |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 395 | if ((available == null) || (unavailable == null)){ |
| 396 | // The CHECK_TTS_DATA activity for the plugin did not run properly; |
| 397 | // disable the preview and install controls and return. |
| 398 | mEnableDemo = false; |
| 399 | mVoicesMissing = false; |
| 400 | updateWidgetState(); |
| 401 | return; |
| 402 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 403 | if (available.size() > 0){ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 404 | if (mTts == null) { |
| 405 | mTts = new TextToSpeech(this, this); |
| 406 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 407 | ListPreference ttsLanguagePref = |
| 408 | (ListPreference) findPreference("tts_default_lang"); |
| 409 | CharSequence[] entries = new CharSequence[available.size()]; |
| 410 | CharSequence[] entryValues = new CharSequence[available.size()]; |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 411 | int selectedLanguageIndex = -1; |
| 412 | String selectedLanguagePref = mDefaultLanguage; |
| 413 | if (mDefaultCountry.length() > 0) { |
| 414 | selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + |
| 415 | mDefaultCountry; |
| 416 | } |
| 417 | if (mDefaultLocVariant.length() > 0) { |
| 418 | selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + |
| 419 | mDefaultLocVariant; |
| 420 | } |
| 421 | for (int i = 0; i < available.size(); i++) { |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 422 | String[] langCountryVariant = available.get(i).split("-"); |
| 423 | Locale loc = null; |
| 424 | if (langCountryVariant.length == 1){ |
| 425 | loc = new Locale(langCountryVariant[0]); |
| 426 | } else if (langCountryVariant.length == 2){ |
| 427 | loc = new Locale(langCountryVariant[0], langCountryVariant[1]); |
| 428 | } else if (langCountryVariant.length == 3){ |
| 429 | loc = new Locale(langCountryVariant[0], langCountryVariant[1], |
| 430 | langCountryVariant[2]); |
| 431 | } |
| 432 | if (loc != null){ |
| 433 | entries[i] = loc.getDisplayName(); |
| 434 | entryValues[i] = available.get(i); |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 435 | if (entryValues[i].equals(selectedLanguagePref)) { |
| 436 | selectedLanguageIndex = i; |
| 437 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | ttsLanguagePref.setEntries(entries); |
| 441 | ttsLanguagePref.setEntryValues(entryValues); |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 442 | if (selectedLanguageIndex > -1) { |
| 443 | ttsLanguagePref.setValueIndex(selectedLanguageIndex); |
| 444 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 445 | mEnableDemo = true; |
Charles Chen | 473111b | 2010-04-06 15:50:42 -0700 | [diff] [blame] | 446 | // Make sure that the default language can be used. |
| 447 | int languageResult = mTts.setLanguage( |
| 448 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 449 | if (languageResult < TextToSpeech.LANG_AVAILABLE){ |
| 450 | Locale currentLocale = Locale.getDefault(); |
| 451 | mDefaultLanguage = currentLocale.getISO3Language(); |
| 452 | mDefaultCountry = currentLocale.getISO3Country(); |
| 453 | mDefaultLocVariant = currentLocale.getVariant(); |
| 454 | languageResult = mTts.setLanguage( |
| 455 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 456 | // If the default Locale isn't supported, just choose the first available |
| 457 | // language so that there is at least something. |
| 458 | if (languageResult < TextToSpeech.LANG_AVAILABLE){ |
| 459 | parseLocaleInfo(ttsLanguagePref.getEntryValues()[0].toString()); |
| 460 | mTts.setLanguage( |
| 461 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 462 | } |
| 463 | ContentResolver resolver = getContentResolver(); |
| 464 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 465 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 466 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 467 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 468 | } else { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 469 | mEnableDemo = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 470 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 471 | |
| 472 | if (unavailable.size() > 0){ |
| 473 | mVoicesMissing = true; |
| 474 | } else { |
| 475 | mVoicesMissing = false; |
| 476 | } |
| 477 | |
| 478 | updateWidgetState(); |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 479 | } else if (requestCode == GET_SAMPLE_TEXT) { |
| 480 | if (resultCode == TextToSpeech.LANG_AVAILABLE) { |
Charles Chen | ec05e71 | 2010-02-18 15:06:26 -0800 | [diff] [blame] | 481 | String sample = getString(R.string.tts_demo); |
| 482 | if ((data != null) && (data.getStringExtra("sampleText") != null)) { |
| 483 | sample = data.getStringExtra("sampleText"); |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 484 | } |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 485 | if (mTts != null) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 486 | mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null); |
| 487 | } |
| 488 | } else { |
| 489 | // TODO: Display an error here to the user. |
| 490 | Log.e(TAG, "Did not have a sample string for the requested language"); |
| 491 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 496 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 497 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 498 | // "Use Defaults" |
| 499 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 500 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 501 | value); |
| 502 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 503 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 504 | // Default rate |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 505 | mDefaultRate = Integer.parseInt((String) objValue); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 506 | try { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 507 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 508 | TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 509 | if (mTts != null) { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 510 | mTts.setSpeechRate((float)(mDefaultRate/100.0f)); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 511 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 512 | Log.i(TAG, "TTS default rate is " + mDefaultRate); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 513 | } catch (NumberFormatException e) { |
| 514 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 515 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 516 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 517 | // Default locale |
| 518 | ContentResolver resolver = getContentResolver(); |
| 519 | parseLocaleInfo((String) objValue); |
| 520 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 521 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 522 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 523 | Log.v(TAG, "TTS default lang/country/variant set to " |
| 524 | + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 525 | if (mTts != null) { |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 526 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 527 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 528 | int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue); |
| 529 | Log.v("Settings", " selected is " + newIndex); |
| 530 | mDemoStringIndex = newIndex > -1 ? newIndex : 0; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 531 | } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) { |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 532 | mDefaultEng = objValue.toString(); |
| 533 | Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng); |
| 534 | if (mTts != null) { |
| 535 | mTts.setEngineByPackageName(mDefaultEng); |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 536 | mEnableDemo = false; |
| 537 | mVoicesMissing = false; |
| 538 | updateWidgetState(); |
| 539 | checkVoiceData(); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 540 | } |
| 541 | Log.v("Settings", "The default synth is: " + objValue.toString()); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 542 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 543 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 544 | return true; |
| 545 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 546 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 547 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Called when mPlayExample or mInstallData is clicked |
| 550 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 551 | public boolean onPreferenceClick(Preference preference) { |
| 552 | if (preference == mPlayExample) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 553 | // Get the sample text from the TTS engine; onActivityResult will do |
| 554 | // the actual speaking |
| 555 | getSampleText(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 556 | return true; |
| 557 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 558 | if (preference == mInstallData) { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 559 | installVoiceData(); |
| 560 | // quit this activity so it needs to be restarted after installation of the voice data |
| 561 | finish(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 562 | return true; |
| 563 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 567 | @Override |
| 568 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 569 | if (Utils.isMonkeyRunning()) { |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | if (preference instanceof CheckBoxPreference) { |
| 574 | final CheckBoxPreference chkPref = (CheckBoxPreference) preference; |
| 575 | if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){ |
| 576 | if (chkPref.isChecked()) { |
| 577 | chkPref.setChecked(false); |
| 578 | AlertDialog d = (new AlertDialog.Builder(this)) |
| 579 | .setTitle(android.R.string.dialog_alert_title) |
| 580 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 581 | .setMessage(getString(R.string.tts_engine_security_warning, |
| 582 | chkPref.getTitle())) |
| 583 | .setCancelable(true) |
| 584 | .setPositiveButton(android.R.string.ok, |
| 585 | new DialogInterface.OnClickListener() { |
| 586 | public void onClick(DialogInterface dialog, int which) { |
| 587 | chkPref.setChecked(true); |
| 588 | loadEngines(); |
| 589 | } |
| 590 | }) |
| 591 | .setNegativeButton(android.R.string.cancel, |
| 592 | new DialogInterface.OnClickListener() { |
| 593 | public void onClick(DialogInterface dialog, int which) { |
| 594 | } |
| 595 | }) |
| 596 | .create(); |
| 597 | d.show(); |
| 598 | } else { |
| 599 | loadEngines(); |
| 600 | } |
| 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | return false; |
| 605 | } |
| 606 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 607 | |
| 608 | private void updateWidgetState() { |
| 609 | mPlayExample.setEnabled(mEnableDemo); |
| 610 | mUseDefaultPref.setEnabled(mEnableDemo); |
| 611 | mDefaultRatePref.setEnabled(mEnableDemo); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 612 | mDefaultLocPref.setEnabled(mEnableDemo); |
| 613 | |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 614 | mInstallData.setEnabled(mVoicesMissing); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
| 618 | private void parseLocaleInfo(String locale) { |
| 619 | StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER); |
| 620 | mDefaultLanguage = ""; |
| 621 | mDefaultCountry = ""; |
| 622 | mDefaultLocVariant = ""; |
| 623 | if (tokenizer.hasMoreTokens()) { |
| 624 | mDefaultLanguage = tokenizer.nextToken().trim(); |
| 625 | } |
| 626 | if (tokenizer.hasMoreTokens()) { |
| 627 | mDefaultCountry = tokenizer.nextToken().trim(); |
| 628 | } |
| 629 | if (tokenizer.hasMoreTokens()) { |
| 630 | mDefaultLocVariant = tokenizer.nextToken().trim(); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Initialize the default language in the UI and in the preferences. |
| 637 | * After this method has been invoked, the default language is a supported Locale. |
| 638 | */ |
| 639 | private void initDefaultLang() { |
| 640 | // if there isn't already a default language preference |
| 641 | if (!hasLangPref()) { |
| 642 | // if the current Locale is supported |
| 643 | if (isCurrentLocSupported()) { |
| 644 | // then use the current Locale as the default language |
| 645 | useCurrentLocAsDefault(); |
| 646 | } else { |
| 647 | // otherwise use a default supported Locale as the default language |
| 648 | useSupportedLocAsDefault(); |
| 649 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 652 | // Update the language preference list with the default language and the matching |
| 653 | // demo string (at this stage there is a default language pref) |
| 654 | ContentResolver resolver = getContentResolver(); |
| 655 | mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
Charles Chen | 681d0b8 | 2010-04-12 12:06:30 -0700 | [diff] [blame] | 656 | mDefaultCountry = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY); |
| 657 | mDefaultLocVariant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 658 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 659 | // update the demo string |
| 660 | mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER |
| 661 | + mDefaultCountry); |
Charles Chen | 8a37e61 | 2010-02-04 15:52:30 -0800 | [diff] [blame] | 662 | if (mDemoStringIndex > -1){ |
| 663 | mDefaultLocPref.setValueIndex(mDemoStringIndex); |
| 664 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * (helper function for initDefaultLang() ) |
| 669 | * Returns whether there is a default language in the TTS settings. |
| 670 | */ |
| 671 | private boolean hasLangPref() { |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 672 | ContentResolver resolver = getContentResolver(); |
| 673 | String language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
| 674 | if ((language == null) || (language.length() < 1)) { |
| 675 | return false; |
| 676 | } |
| 677 | String country = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY); |
| 678 | if (country == null) { |
| 679 | return false; |
| 680 | } |
| 681 | String variant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT); |
| 682 | if (variant == null) { |
| 683 | return false; |
| 684 | } |
| 685 | return true; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /** |
| 689 | * (helper function for initDefaultLang() ) |
| 690 | * Returns whether the current Locale is supported by this Settings screen |
| 691 | */ |
| 692 | private boolean isCurrentLocSupported() { |
| 693 | String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER |
| 694 | + Locale.getDefault().getISO3Country(); |
| 695 | return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * (helper function for initDefaultLang() ) |
| 700 | * Sets the default language in TTS settings to be the current Locale. |
| 701 | * This should only be used after checking that the current Locale is supported. |
| 702 | */ |
| 703 | private void useCurrentLocAsDefault() { |
| 704 | Locale currentLocale = Locale.getDefault(); |
| 705 | ContentResolver resolver = getContentResolver(); |
| 706 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language()); |
| 707 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country()); |
| 708 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant()); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * (helper function for initDefaultLang() ) |
| 713 | * Sets the default language in TTS settings to be one known to be supported |
| 714 | */ |
| 715 | private void useSupportedLocAsDefault() { |
| 716 | ContentResolver resolver = getContentResolver(); |
| 717 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL); |
| 718 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL); |
| 719 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 722 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 723 | private void loadEngines() { |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 724 | mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 725 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 726 | // TODO (clchen): Try to see if it is possible to be more efficient here |
| 727 | // and not search for plugins again. |
| 728 | Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); |
| 729 | ResolveInfo[] enginesArray = new ResolveInfo[0]; |
| 730 | PackageManager pm = getPackageManager(); |
| 731 | enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray); |
| 732 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 733 | ArrayList<CharSequence> values = new ArrayList<CharSequence>(); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 734 | String enabledEngines = ""; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 735 | for (int i = 0; i < enginesArray.length; i++) { |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 736 | String pluginPackageName = enginesArray[i].activityInfo.packageName; |
| 737 | if (pluginPackageName.equals(SYSTEM_TTS)) { |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 738 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 739 | values.add(pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 740 | } else { |
| 741 | CheckBoxPreference pref = (CheckBoxPreference) findPreference( |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 742 | KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 743 | if ((pref != null) && pref.isChecked()){ |
| 744 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 745 | values.add(pluginPackageName); |
| 746 | enabledEngines = enabledEngines + pluginPackageName + " "; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 750 | ContentResolver resolver = getContentResolver(); |
| 751 | Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 752 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 753 | CharSequence entriesArray[] = new CharSequence[entries.size()]; |
| 754 | CharSequence valuesArray[] = new CharSequence[values.size()]; |
| 755 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 756 | mDefaultSynthPref.setEntries(entries.toArray(entriesArray)); |
| 757 | mDefaultSynthPref.setEntryValues(values.toArray(valuesArray)); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 758 | |
| 759 | // Set the selected engine based on the saved preference |
| 760 | String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH); |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 761 | int selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(selectedEngine); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 762 | if (selectedEngineIndex == -1){ |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 763 | selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(SYSTEM_TTS); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 764 | } |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 765 | mDefaultSynthPref.setValueIndex(selectedEngineIndex); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 766 | } |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 767 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 768 | } |