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