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