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