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; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 404 | } else { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 405 | mEnableDemo = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 406 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 407 | |
| 408 | if (unavailable.size() > 0){ |
| 409 | mVoicesMissing = true; |
| 410 | } else { |
| 411 | mVoicesMissing = false; |
| 412 | } |
| 413 | |
| 414 | updateWidgetState(); |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 415 | } else if (requestCode == GET_SAMPLE_TEXT) { |
| 416 | if (resultCode == TextToSpeech.LANG_AVAILABLE) { |
Charles Chen | ec05e71 | 2010-02-18 15:06:26 -0800 | [diff] [blame] | 417 | String sample = getString(R.string.tts_demo); |
| 418 | if ((data != null) && (data.getStringExtra("sampleText") != null)) { |
| 419 | sample = data.getStringExtra("sampleText"); |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 420 | } |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 421 | if (mTts != null) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 422 | mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null); |
| 423 | } |
| 424 | } else { |
| 425 | // TODO: Display an error here to the user. |
| 426 | Log.e(TAG, "Did not have a sample string for the requested language"); |
| 427 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 432 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 433 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 434 | // "Use Defaults" |
| 435 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 436 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 437 | value); |
| 438 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 439 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 440 | // Default rate |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 441 | mDefaultRate = Integer.parseInt((String) objValue); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 442 | try { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 443 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 444 | TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 445 | if (mTts != null) { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 446 | mTts.setSpeechRate((float)(mDefaultRate/100.0f)); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 447 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 448 | Log.i(TAG, "TTS default rate is " + mDefaultRate); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 449 | } catch (NumberFormatException e) { |
| 450 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 451 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 452 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 453 | // Default locale |
| 454 | ContentResolver resolver = getContentResolver(); |
| 455 | parseLocaleInfo((String) objValue); |
| 456 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 457 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 458 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 459 | Log.v(TAG, "TTS default lang/country/variant set to " |
| 460 | + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 461 | if (mTts != null) { |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 462 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 463 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 464 | int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue); |
| 465 | Log.v("Settings", " selected is " + newIndex); |
| 466 | mDemoStringIndex = newIndex > -1 ? newIndex : 0; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 467 | } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) { |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 468 | mDefaultEng = objValue.toString(); |
| 469 | Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng); |
| 470 | if (mTts != null) { |
| 471 | mTts.setEngineByPackageName(mDefaultEng); |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 472 | mEnableDemo = false; |
| 473 | mVoicesMissing = false; |
| 474 | updateWidgetState(); |
| 475 | checkVoiceData(); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 476 | } |
| 477 | Log.v("Settings", "The default synth is: " + objValue.toString()); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 478 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 479 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 480 | return true; |
| 481 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 482 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 483 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 484 | /** |
| 485 | * Called when mPlayExample or mInstallData is clicked |
| 486 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 487 | public boolean onPreferenceClick(Preference preference) { |
| 488 | if (preference == mPlayExample) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 489 | // Get the sample text from the TTS engine; onActivityResult will do |
| 490 | // the actual speaking |
| 491 | getSampleText(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 494 | if (preference == mInstallData) { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 495 | installVoiceData(); |
| 496 | // quit this activity so it needs to be restarted after installation of the voice data |
| 497 | finish(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 498 | return true; |
| 499 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 503 | @Override |
| 504 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 505 | if (Utils.isMonkeyRunning()) { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | if (preference instanceof CheckBoxPreference) { |
| 510 | final CheckBoxPreference chkPref = (CheckBoxPreference) preference; |
| 511 | if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){ |
| 512 | if (chkPref.isChecked()) { |
| 513 | chkPref.setChecked(false); |
| 514 | AlertDialog d = (new AlertDialog.Builder(this)) |
| 515 | .setTitle(android.R.string.dialog_alert_title) |
| 516 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 517 | .setMessage(getString(R.string.tts_engine_security_warning, |
| 518 | chkPref.getTitle())) |
| 519 | .setCancelable(true) |
| 520 | .setPositiveButton(android.R.string.ok, |
| 521 | new DialogInterface.OnClickListener() { |
| 522 | public void onClick(DialogInterface dialog, int which) { |
| 523 | chkPref.setChecked(true); |
| 524 | loadEngines(); |
| 525 | } |
| 526 | }) |
| 527 | .setNegativeButton(android.R.string.cancel, |
| 528 | new DialogInterface.OnClickListener() { |
| 529 | public void onClick(DialogInterface dialog, int which) { |
| 530 | } |
| 531 | }) |
| 532 | .create(); |
| 533 | d.show(); |
| 534 | } else { |
| 535 | loadEngines(); |
| 536 | } |
| 537 | return true; |
| 538 | } |
| 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 543 | |
| 544 | private void updateWidgetState() { |
| 545 | mPlayExample.setEnabled(mEnableDemo); |
| 546 | mUseDefaultPref.setEnabled(mEnableDemo); |
| 547 | mDefaultRatePref.setEnabled(mEnableDemo); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 548 | mDefaultLocPref.setEnabled(mEnableDemo); |
| 549 | |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 550 | mInstallData.setEnabled(mVoicesMissing); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | |
| 554 | private void parseLocaleInfo(String locale) { |
| 555 | StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER); |
| 556 | mDefaultLanguage = ""; |
| 557 | mDefaultCountry = ""; |
| 558 | mDefaultLocVariant = ""; |
| 559 | if (tokenizer.hasMoreTokens()) { |
| 560 | mDefaultLanguage = tokenizer.nextToken().trim(); |
| 561 | } |
| 562 | if (tokenizer.hasMoreTokens()) { |
| 563 | mDefaultCountry = tokenizer.nextToken().trim(); |
| 564 | } |
| 565 | if (tokenizer.hasMoreTokens()) { |
| 566 | mDefaultLocVariant = tokenizer.nextToken().trim(); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 571 | /** |
| 572 | * Initialize the default language in the UI and in the preferences. |
| 573 | * After this method has been invoked, the default language is a supported Locale. |
| 574 | */ |
| 575 | private void initDefaultLang() { |
| 576 | // if there isn't already a default language preference |
| 577 | if (!hasLangPref()) { |
| 578 | // if the current Locale is supported |
| 579 | if (isCurrentLocSupported()) { |
| 580 | // then use the current Locale as the default language |
| 581 | useCurrentLocAsDefault(); |
| 582 | } else { |
| 583 | // otherwise use a default supported Locale as the default language |
| 584 | useSupportedLocAsDefault(); |
| 585 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 588 | // Update the language preference list with the default language and the matching |
| 589 | // demo string (at this stage there is a default language pref) |
| 590 | ContentResolver resolver = getContentResolver(); |
| 591 | mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
| 592 | mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY); |
| 593 | mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 594 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 595 | // update the demo string |
| 596 | mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER |
| 597 | + mDefaultCountry); |
Charles Chen | 8a37e61 | 2010-02-04 15:52:30 -0800 | [diff] [blame] | 598 | if (mDemoStringIndex > -1){ |
| 599 | mDefaultLocPref.setValueIndex(mDemoStringIndex); |
| 600 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
| 604 | * (helper function for initDefaultLang() ) |
| 605 | * Returns whether there is a default language in the TTS settings. |
| 606 | */ |
| 607 | private boolean hasLangPref() { |
| 608 | String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG); |
| 609 | return (language != null); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * (helper function for initDefaultLang() ) |
| 614 | * Returns whether the current Locale is supported by this Settings screen |
| 615 | */ |
| 616 | private boolean isCurrentLocSupported() { |
| 617 | String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER |
| 618 | + Locale.getDefault().getISO3Country(); |
| 619 | return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * (helper function for initDefaultLang() ) |
| 624 | * Sets the default language in TTS settings to be the current Locale. |
| 625 | * This should only be used after checking that the current Locale is supported. |
| 626 | */ |
| 627 | private void useCurrentLocAsDefault() { |
| 628 | Locale currentLocale = Locale.getDefault(); |
| 629 | ContentResolver resolver = getContentResolver(); |
| 630 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language()); |
| 631 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country()); |
| 632 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant()); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * (helper function for initDefaultLang() ) |
| 637 | * Sets the default language in TTS settings to be one known to be supported |
| 638 | */ |
| 639 | private void useSupportedLocAsDefault() { |
| 640 | ContentResolver resolver = getContentResolver(); |
| 641 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL); |
| 642 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL); |
| 643 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 646 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 647 | private void loadEngines() { |
| 648 | ListPreference enginesPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 649 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 650 | // TODO (clchen): Try to see if it is possible to be more efficient here |
| 651 | // and not search for plugins again. |
| 652 | Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); |
| 653 | ResolveInfo[] enginesArray = new ResolveInfo[0]; |
| 654 | PackageManager pm = getPackageManager(); |
| 655 | enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray); |
| 656 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 657 | ArrayList<CharSequence> values = new ArrayList<CharSequence>(); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 658 | String enabledEngines = ""; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 659 | for (int i = 0; i < enginesArray.length; i++) { |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 660 | String pluginPackageName = enginesArray[i].activityInfo.packageName; |
| 661 | if (pluginPackageName.equals(SYSTEM_TTS)) { |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 662 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 663 | values.add(pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 664 | } else { |
| 665 | CheckBoxPreference pref = (CheckBoxPreference) findPreference( |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 666 | KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 667 | if ((pref != null) && pref.isChecked()){ |
| 668 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 669 | values.add(pluginPackageName); |
| 670 | enabledEngines = enabledEngines + pluginPackageName + " "; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 674 | ContentResolver resolver = getContentResolver(); |
| 675 | Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 676 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 677 | CharSequence entriesArray[] = new CharSequence[entries.size()]; |
| 678 | CharSequence valuesArray[] = new CharSequence[values.size()]; |
| 679 | |
| 680 | enginesPref.setEntries(entries.toArray(entriesArray)); |
| 681 | enginesPref.setEntryValues(values.toArray(valuesArray)); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame^] | 682 | |
| 683 | // Set the selected engine based on the saved preference |
| 684 | String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH); |
| 685 | int selectedEngineIndex = enginesPref.findIndexOfValue(selectedEngine); |
| 686 | if (selectedEngineIndex == -1){ |
| 687 | selectedEngineIndex = enginesPref.findIndexOfValue(SYSTEM_TTS); |
| 688 | } |
| 689 | enginesPref.setValueIndex(selectedEngineIndex); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 690 | } |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 691 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 692 | } |