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 | |
| 26 | import android.content.ContentResolver; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.pm.ActivityInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
| 32 | import android.preference.ListPreference; |
| 33 | import android.preference.Preference; |
| 34 | import android.preference.PreferenceActivity; |
| 35 | import android.preference.CheckBoxPreference; |
| 36 | import android.provider.Settings; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 37 | import android.provider.Settings.SettingNotFoundException; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 38 | import android.speech.tts.TextToSpeech; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 39 | import android.util.Log; |
| 40 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 41 | import java.util.List; |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 42 | import java.util.Locale; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 43 | import java.util.StringTokenizer; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 44 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 45 | public class TextToSpeechSettings extends PreferenceActivity implements |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 46 | Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, |
| 47 | TextToSpeech.OnInitListener { |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String TAG = "TextToSpeechSettings"; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 50 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 51 | private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 52 | private static final String KEY_TTS_INSTALL_DATA = "tts_install_data"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 53 | 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] | 54 | private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate"; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 55 | private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 56 | private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country"; |
| 57 | private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant"; |
| 58 | |
| 59 | private static final String LOCALE_DELIMITER = "-"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 60 | |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame^] | 61 | private static final String FALLBACK_TTS_DEFAULT_SYNTH = |
| 62 | TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_SYNTH; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 63 | |
| 64 | private Preference mPlayExample = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 65 | private Preference mInstallData = null; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 66 | private CheckBoxPreference mUseDefaultPref = null; |
| 67 | private ListPreference mDefaultRatePref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 68 | private ListPreference mDefaultLocPref = null; |
| 69 | private String mDefaultLanguage = null; |
| 70 | private String mDefaultCountry = null; |
| 71 | private String mDefaultLocVariant = null; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 72 | private String mDefaultEng = ""; |
| 73 | |
| 74 | private boolean mEnableDemo = false; |
| 75 | |
| 76 | private TextToSpeech mTts = null; |
| 77 | |
| 78 | /** |
| 79 | * Request code (arbitrary value) for voice data check through |
| 80 | * startActivityForResult. |
| 81 | */ |
| 82 | private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Request code (arbitrary value) for voice data installation through |
| 85 | * startActivityForResult. |
| 86 | */ |
| 87 | private static final int VOICE_DATA_INSTALLATION = 1980; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 88 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 89 | @Override |
| 90 | protected void onCreate(Bundle savedInstanceState) { |
| 91 | super.onCreate(savedInstanceState); |
| 92 | |
| 93 | addPreferencesFromResource(R.xml.tts_settings); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 94 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 95 | mEnableDemo = false; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 96 | initClickers(); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 97 | initDefaultSettings(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 98 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 99 | |
| 100 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 101 | @Override |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 102 | protected void onStart() { |
| 103 | super.onStart(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 104 | // whenever we return to this screen, we don't know the state of the |
| 105 | // 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] | 106 | // 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] | 107 | initClickers(); |
| 108 | updateWidgetState(); |
| 109 | checkVoiceData(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | @Override |
| 114 | protected void onDestroy() { |
| 115 | super.onDestroy(); |
| 116 | if (mTts != null) { |
| 117 | mTts.shutdown(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 122 | private void initClickers() { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 123 | mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 124 | mPlayExample.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 125 | |
| 126 | mInstallData = findPreference(KEY_TTS_INSTALL_DATA); |
| 127 | mInstallData.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
| 131 | private void initDefaultSettings() { |
| 132 | ContentResolver resolver = getContentResolver(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 133 | int intVal = 0; |
| 134 | |
| 135 | // Find the default TTS values in the settings, initialize and store the |
| 136 | // settings if they are not found. |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 137 | |
| 138 | // "Use Defaults" |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 139 | mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
| 140 | try { |
| 141 | intVal = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS); |
| 142 | } catch (SettingNotFoundException e) { |
| 143 | // "use default" setting not found, initialize it |
| 144 | intVal = TextToSpeech.Engine.FALLBACK_TTS_USE_DEFAULTS; |
| 145 | Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, intVal); |
| 146 | } |
| 147 | mUseDefaultPref.setChecked(intVal == 1); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 148 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 149 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 150 | // Default engine |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 151 | String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH); |
| 152 | if (engine == null) { |
| 153 | // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech |
| 154 | engine = FALLBACK_TTS_DEFAULT_SYNTH; |
| 155 | Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine); |
| 156 | } |
| 157 | mDefaultEng = engine; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 158 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 159 | // Default rate |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 160 | mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
| 161 | try { |
| 162 | intVal = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE); |
| 163 | } catch (SettingNotFoundException e) { |
| 164 | // default rate setting not found, initialize it |
| 165 | intVal = TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_RATE; |
| 166 | Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, intVal); |
| 167 | } |
| 168 | mDefaultRatePref.setValue(String.valueOf(intVal)); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 169 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 170 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 171 | // Default language / country / variant : these three values map to a single ListPref |
| 172 | // representing the matching Locale |
| 173 | String language = null; |
| 174 | String country = null; |
| 175 | String variant = null; |
| 176 | mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 177 | language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 178 | if (language == null) { |
| 179 | // the default language property isn't set, use that of the current locale |
| 180 | Locale currentLocale = Locale.getDefault(); |
| 181 | language = currentLocale.getISO3Language(); |
| 182 | country = currentLocale.getISO3Country(); |
| 183 | variant = currentLocale.getVariant(); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 184 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, language); |
| 185 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, country); |
| 186 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant); |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 187 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 188 | mDefaultLanguage = language; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 189 | if (country == null) { |
| 190 | // country wasn't initialized yet because a default language was found |
| 191 | country = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 192 | if (country == null) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 193 | // default country setting not found, initialize it, as well as the variant; |
Jean-Michel Trivi | 00d4fbf | 2009-07-07 17:08:32 -0700 | [diff] [blame] | 194 | Locale currentLocale = Locale.getDefault(); |
| 195 | country = currentLocale.getISO3Country(); |
| 196 | variant = currentLocale.getVariant(); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 197 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, country); |
| 198 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 201 | mDefaultCountry = country; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 202 | if (variant == null) { |
| 203 | // variant wasn't initialized yet because a default country was found |
| 204 | variant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 205 | if (variant == null) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 206 | // default variant setting not found, initialize it |
Jean-Michel Trivi | 00d4fbf | 2009-07-07 17:08:32 -0700 | [diff] [blame] | 207 | Locale currentLocale = Locale.getDefault(); |
| 208 | variant = currentLocale.getVariant(); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 209 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, variant); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 212 | mDefaultLocVariant = variant; |
| 213 | |
| 214 | setDefaultLocalePref(language, country, variant); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 215 | mDefaultLocPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Ask the current default engine to launch the matching CHECK_TTS_DATA activity |
| 221 | * to check the required TTS files are properly installed. |
| 222 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 223 | private void checkVoiceData() { |
| 224 | PackageManager pm = getPackageManager(); |
| 225 | Intent intent = new Intent(); |
| 226 | intent.setAction("android.intent.action.CHECK_TTS_DATA"); |
| 227 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 228 | // query only the package that matches that of the default engine |
| 229 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 230 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 231 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 232 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 233 | this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 240 | * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity |
| 241 | * so the required TTS files are properly installed. |
| 242 | */ |
| 243 | private void installVoiceData() { |
| 244 | PackageManager pm = getPackageManager(); |
| 245 | Intent intent = new Intent(); |
| 246 | intent.setAction("android.intent.action.INSTALL_TTS_DATA"); |
| 247 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 248 | // query only the package that matches that of the default engine |
| 249 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 250 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 251 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 252 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 253 | this.startActivityForResult(intent, VOICE_DATA_INSTALLATION); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 260 | * Called when the TTS engine is initialized. |
| 261 | */ |
| 262 | public void onInit(int status) { |
| 263 | if (status == TextToSpeech.TTS_SUCCESS) { |
| 264 | Log.v(TAG, "TTS engine for settings screen initialized."); |
| 265 | mEnableDemo = true; |
| 266 | } else { |
| 267 | Log.v(TAG, "TTS engine for settings screen failed to initialize successfully."); |
| 268 | mEnableDemo = false; |
| 269 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 270 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | /** |
| 275 | * Called when voice data integrity check returns |
| 276 | */ |
| 277 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 278 | if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { |
| 279 | if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { |
| 280 | Log.v(TAG, "Voice data check passed"); |
| 281 | if (mTts == null) { |
| 282 | mTts = new TextToSpeech(this, this); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame^] | 283 | mTts.setLanguage(Locale.getDefault()); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 284 | } |
| 285 | } else { |
| 286 | Log.v(TAG, "Voice data check failed"); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 287 | mEnableDemo = false; |
| 288 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 294 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 295 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 296 | // "Use Defaults" |
| 297 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 298 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 299 | value); |
| 300 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 301 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 302 | // Default rate |
| 303 | int value = Integer.parseInt((String) objValue); |
| 304 | try { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 305 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 306 | TTS_DEFAULT_RATE, value); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 307 | if (mTts != null) { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 308 | mTts.setSpeechRate((float)(value/100.0f)); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 309 | } |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 310 | Log.i(TAG, "TTS default rate is "+value); |
| 311 | } catch (NumberFormatException e) { |
| 312 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 313 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 314 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 315 | // Default locale |
| 316 | ContentResolver resolver = getContentResolver(); |
| 317 | parseLocaleInfo((String) objValue); |
| 318 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 319 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 320 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 321 | Log.v(TAG, "TTS default lang/country/variant set to " |
| 322 | + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame^] | 323 | if (mTts != null) { |
| 324 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry)); |
| 325 | } |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 326 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 327 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 328 | return true; |
| 329 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 330 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 331 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Called when mPlayExample or mInstallData is clicked |
| 334 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 335 | public boolean onPreferenceClick(Preference preference) { |
| 336 | if (preference == mPlayExample) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 337 | // Play example |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 338 | if (mTts != null) { |
| 339 | mTts.speak(getResources().getString(R.string.tts_demo), |
| 340 | TextToSpeech.TTS_QUEUE_FLUSH, null); |
| 341 | } |
| 342 | return true; |
| 343 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 344 | if (preference == mInstallData) { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 345 | installVoiceData(); |
| 346 | // quit this activity so it needs to be restarted after installation of the voice data |
| 347 | finish(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 348 | return true; |
| 349 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 353 | |
| 354 | private void updateWidgetState() { |
| 355 | mPlayExample.setEnabled(mEnableDemo); |
| 356 | mUseDefaultPref.setEnabled(mEnableDemo); |
| 357 | mDefaultRatePref.setEnabled(mEnableDemo); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 358 | mDefaultLocPref.setEnabled(mEnableDemo); |
| 359 | |
| 360 | mInstallData.setEnabled(!mEnableDemo); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | private void parseLocaleInfo(String locale) { |
| 365 | StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER); |
| 366 | mDefaultLanguage = ""; |
| 367 | mDefaultCountry = ""; |
| 368 | mDefaultLocVariant = ""; |
| 369 | if (tokenizer.hasMoreTokens()) { |
| 370 | mDefaultLanguage = tokenizer.nextToken().trim(); |
| 371 | } |
| 372 | if (tokenizer.hasMoreTokens()) { |
| 373 | mDefaultCountry = tokenizer.nextToken().trim(); |
| 374 | } |
| 375 | if (tokenizer.hasMoreTokens()) { |
| 376 | mDefaultLocVariant = tokenizer.nextToken().trim(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 381 | private void setDefaultLocalePref(String language, String country, String variant) { |
| 382 | // build a string from the default lang/country/variant trio, |
| 383 | String localeString = new String(language); |
| 384 | if (country.compareTo("") != 0) { |
| 385 | localeString += LOCALE_DELIMITER + country; |
| 386 | } else { |
| 387 | localeString += LOCALE_DELIMITER + " "; |
| 388 | } |
| 389 | if (variant.compareTo("") != 0) { |
| 390 | localeString += LOCALE_DELIMITER + variant; |
| 391 | } |
| 392 | |
| 393 | if (mDefaultLocPref.findIndexOfValue(localeString) > -1) { |
| 394 | mDefaultLocPref.setValue(localeString); |
| 395 | } else { |
| 396 | mDefaultLocPref.setValueIndex(0); |
| 397 | } |
| 398 | |
| 399 | Log.v(TAG, "In initDefaultSettings: localeString=" + localeString); |
| 400 | } |
| 401 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 402 | } |