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