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