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