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"; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 58 | // TODO move default Locale values to TextToSpeech.Engine |
| 59 | private static final String DEFAULT_LANG_VAL = "eng"; |
| 60 | private static final String DEFAULT_COUNTRY_VAL = "USA"; |
| 61 | private static final String DEFAULT_VARIANT_VAL = ""; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 62 | |
| 63 | private static final String LOCALE_DELIMITER = "-"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 64 | |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 65 | private static final String FALLBACK_TTS_DEFAULT_SYNTH = |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 66 | TextToSpeech.Engine.DEFAULT_SYNTH; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 67 | |
| 68 | private Preference mPlayExample = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 69 | private Preference mInstallData = null; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 70 | private CheckBoxPreference mUseDefaultPref = null; |
| 71 | private ListPreference mDefaultRatePref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 72 | private ListPreference mDefaultLocPref = null; |
| 73 | private String mDefaultLanguage = null; |
| 74 | private String mDefaultCountry = null; |
| 75 | private String mDefaultLocVariant = null; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 76 | private String mDefaultEng = ""; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 77 | private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE; |
| 78 | |
| 79 | // Array of strings used to demonstrate TTS in the different languages. |
| 80 | private String[] mDemoStrings; |
| 81 | // Index of the current string to use for the demo. |
| 82 | private int mDemoStringIndex = 0; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 83 | |
| 84 | private boolean mEnableDemo = false; |
| 85 | |
| 86 | private TextToSpeech mTts = null; |
| 87 | |
| 88 | /** |
| 89 | * Request code (arbitrary value) for voice data check through |
| 90 | * startActivityForResult. |
| 91 | */ |
| 92 | private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Request code (arbitrary value) for voice data installation through |
| 95 | * startActivityForResult. |
| 96 | */ |
| 97 | private static final int VOICE_DATA_INSTALLATION = 1980; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 98 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 99 | @Override |
| 100 | protected void onCreate(Bundle savedInstanceState) { |
| 101 | super.onCreate(savedInstanceState); |
| 102 | |
| 103 | addPreferencesFromResource(R.xml.tts_settings); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 104 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 105 | mDemoStrings = getResources().getStringArray(R.array.tts_demo_strings); |
| 106 | |
Jean-Michel Trivi | 6dde896 | 2009-08-11 09:06:58 -0700 | [diff] [blame] | 107 | setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); |
| 108 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 109 | mEnableDemo = false; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 110 | initClickers(); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 111 | initDefaultSettings(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 112 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 113 | |
| 114 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 115 | @Override |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 116 | protected void onStart() { |
| 117 | super.onStart(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 118 | // whenever we return to this screen, we don't know the state of the |
| 119 | // 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] | 120 | // 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] | 121 | initClickers(); |
| 122 | updateWidgetState(); |
| 123 | checkVoiceData(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | @Override |
| 128 | protected void onDestroy() { |
| 129 | super.onDestroy(); |
| 130 | if (mTts != null) { |
| 131 | mTts.shutdown(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 136 | private void initClickers() { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 137 | mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 138 | mPlayExample.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 139 | |
| 140 | mInstallData = findPreference(KEY_TTS_INSTALL_DATA); |
| 141 | mInstallData.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | private void initDefaultSettings() { |
| 146 | ContentResolver resolver = getContentResolver(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 147 | |
| 148 | // Find the default TTS values in the settings, initialize and store the |
| 149 | // settings if they are not found. |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 150 | |
| 151 | // "Use Defaults" |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 152 | int useDefault = 0; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 153 | mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
| 154 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 155 | useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 156 | } catch (SettingNotFoundException e) { |
| 157 | // "use default" setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 158 | useDefault = TextToSpeech.Engine.USE_DEFAULTS; |
| 159 | Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 160 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 161 | mUseDefaultPref.setChecked(useDefault == 1); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 162 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 163 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 164 | // Default engine |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 165 | String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH); |
| 166 | if (engine == null) { |
| 167 | // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech |
| 168 | engine = FALLBACK_TTS_DEFAULT_SYNTH; |
| 169 | Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine); |
| 170 | } |
| 171 | mDefaultEng = engine; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 172 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 173 | // Default rate |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 174 | mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
| 175 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 176 | mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 177 | } catch (SettingNotFoundException e) { |
| 178 | // default rate setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 179 | mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE; |
| 180 | Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 181 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 182 | mDefaultRatePref.setValue(String.valueOf(mDefaultRate)); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 183 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 184 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 185 | // Default language / country / variant : these three values map to a single ListPref |
| 186 | // representing the matching Locale |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 187 | mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG); |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 188 | initDefaultLang(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 189 | mDefaultLocPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 193 | /** |
| 194 | * Ask the current default engine to launch the matching CHECK_TTS_DATA activity |
| 195 | * to check the required TTS files are properly installed. |
| 196 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 197 | private void checkVoiceData() { |
| 198 | PackageManager pm = getPackageManager(); |
| 199 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 200 | intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 201 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 202 | // query only the package that matches that of the default engine |
| 203 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 204 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 205 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 206 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 207 | this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 214 | * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity |
| 215 | * so the required TTS files are properly installed. |
| 216 | */ |
| 217 | private void installVoiceData() { |
| 218 | PackageManager pm = getPackageManager(); |
| 219 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 220 | intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 221 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 222 | // query only the package that matches that of the default engine |
| 223 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 224 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 225 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 226 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 227 | this.startActivityForResult(intent, VOICE_DATA_INSTALLATION); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 234 | * Called when the TTS engine is initialized. |
| 235 | */ |
| 236 | public void onInit(int status) { |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 237 | if (status == TextToSpeech.SUCCESS) { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 238 | Log.v(TAG, "TTS engine for settings screen initialized."); |
| 239 | mEnableDemo = true; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 240 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry)); |
| 241 | mTts.setSpeechRate((float)(mDefaultRate/100.0f)); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 242 | } else { |
| 243 | Log.v(TAG, "TTS engine for settings screen failed to initialize successfully."); |
| 244 | mEnableDemo = false; |
| 245 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 246 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * Called when voice data integrity check returns |
| 252 | */ |
| 253 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 254 | if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { |
| 255 | if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { |
| 256 | Log.v(TAG, "Voice data check passed"); |
| 257 | if (mTts == null) { |
| 258 | mTts = new TextToSpeech(this, this); |
| 259 | } |
| 260 | } else { |
| 261 | Log.v(TAG, "Voice data check failed"); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 262 | mEnableDemo = false; |
| 263 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 269 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 270 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 271 | // "Use Defaults" |
| 272 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 273 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 274 | value); |
| 275 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 276 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 277 | // Default rate |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 278 | mDefaultRate = Integer.parseInt((String) objValue); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 279 | try { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 280 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 281 | TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 282 | if (mTts != null) { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 283 | mTts.setSpeechRate((float)(mDefaultRate/100.0f)); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 284 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 285 | Log.i(TAG, "TTS default rate is " + mDefaultRate); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 286 | } catch (NumberFormatException e) { |
| 287 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 288 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 289 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 290 | // Default locale |
| 291 | ContentResolver resolver = getContentResolver(); |
| 292 | parseLocaleInfo((String) objValue); |
| 293 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 294 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 295 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 296 | Log.v(TAG, "TTS default lang/country/variant set to " |
| 297 | + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 298 | if (mTts != null) { |
| 299 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry)); |
| 300 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 301 | int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue); |
| 302 | Log.v("Settings", " selected is " + newIndex); |
| 303 | mDemoStringIndex = newIndex > -1 ? newIndex : 0; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 304 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 305 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 306 | return true; |
| 307 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 308 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 309 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Called when mPlayExample or mInstallData is clicked |
| 312 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 313 | public boolean onPreferenceClick(Preference preference) { |
| 314 | if (preference == mPlayExample) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 315 | // Play example |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 316 | if (mTts != null) { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 317 | mTts.speak(mDemoStrings[mDemoStringIndex], TextToSpeech.QUEUE_FLUSH, null); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 318 | } |
| 319 | return true; |
| 320 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 321 | if (preference == mInstallData) { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 322 | installVoiceData(); |
| 323 | // quit this activity so it needs to be restarted after installation of the voice data |
| 324 | finish(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 325 | return true; |
| 326 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 330 | |
| 331 | private void updateWidgetState() { |
| 332 | mPlayExample.setEnabled(mEnableDemo); |
| 333 | mUseDefaultPref.setEnabled(mEnableDemo); |
| 334 | mDefaultRatePref.setEnabled(mEnableDemo); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 335 | mDefaultLocPref.setEnabled(mEnableDemo); |
| 336 | |
| 337 | mInstallData.setEnabled(!mEnableDemo); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | private void parseLocaleInfo(String locale) { |
| 342 | StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER); |
| 343 | mDefaultLanguage = ""; |
| 344 | mDefaultCountry = ""; |
| 345 | mDefaultLocVariant = ""; |
| 346 | if (tokenizer.hasMoreTokens()) { |
| 347 | mDefaultLanguage = tokenizer.nextToken().trim(); |
| 348 | } |
| 349 | if (tokenizer.hasMoreTokens()) { |
| 350 | mDefaultCountry = tokenizer.nextToken().trim(); |
| 351 | } |
| 352 | if (tokenizer.hasMoreTokens()) { |
| 353 | mDefaultLocVariant = tokenizer.nextToken().trim(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Initialize the default language in the UI and in the preferences. |
| 360 | * After this method has been invoked, the default language is a supported Locale. |
| 361 | */ |
| 362 | private void initDefaultLang() { |
| 363 | // if there isn't already a default language preference |
| 364 | if (!hasLangPref()) { |
| 365 | // if the current Locale is supported |
| 366 | if (isCurrentLocSupported()) { |
| 367 | // then use the current Locale as the default language |
| 368 | useCurrentLocAsDefault(); |
| 369 | } else { |
| 370 | // otherwise use a default supported Locale as the default language |
| 371 | useSupportedLocAsDefault(); |
| 372 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 375 | // Update the language preference list with the default language and the matching |
| 376 | // demo string (at this stage there is a default language pref) |
| 377 | ContentResolver resolver = getContentResolver(); |
| 378 | mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
| 379 | mDefaultCountry = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_COUNTRY); |
| 380 | mDefaultLocVariant = Settings.Secure.getString(resolver, KEY_TTS_DEFAULT_VARIANT); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 381 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 382 | // update the demo string |
| 383 | mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER |
| 384 | + mDefaultCountry); |
| 385 | mDefaultLocPref.setValueIndex(mDemoStringIndex); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * (helper function for initDefaultLang() ) |
| 390 | * Returns whether there is a default language in the TTS settings. |
| 391 | */ |
| 392 | private boolean hasLangPref() { |
| 393 | String language = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_LANG); |
| 394 | return (language != null); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * (helper function for initDefaultLang() ) |
| 399 | * Returns whether the current Locale is supported by this Settings screen |
| 400 | */ |
| 401 | private boolean isCurrentLocSupported() { |
| 402 | String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER |
| 403 | + Locale.getDefault().getISO3Country(); |
| 404 | return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * (helper function for initDefaultLang() ) |
| 409 | * Sets the default language in TTS settings to be the current Locale. |
| 410 | * This should only be used after checking that the current Locale is supported. |
| 411 | */ |
| 412 | private void useCurrentLocAsDefault() { |
| 413 | Locale currentLocale = Locale.getDefault(); |
| 414 | ContentResolver resolver = getContentResolver(); |
| 415 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language()); |
| 416 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country()); |
| 417 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant()); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * (helper function for initDefaultLang() ) |
| 422 | * Sets the default language in TTS settings to be one known to be supported |
| 423 | */ |
| 424 | private void useSupportedLocAsDefault() { |
| 425 | ContentResolver resolver = getContentResolver(); |
| 426 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL); |
| 427 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL); |
| 428 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 431 | } |