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