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 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 19 | import static android.provider.Settings.Secure.TTS_DEFAULT_COUNTRY; |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 20 | import static android.provider.Settings.Secure.TTS_DEFAULT_LANG; |
| 21 | import static android.provider.Settings.Secure.TTS_DEFAULT_RATE; |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 22 | import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH; |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 23 | import static android.provider.Settings.Secure.TTS_DEFAULT_VARIANT; |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 24 | import static android.provider.Settings.Secure.TTS_ENABLED_PLUGINS; |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 25 | import static android.provider.Settings.Secure.TTS_USE_DEFAULTS; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 26 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 27 | import android.app.Activity; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 28 | import android.app.AlertDialog; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 31 | import android.content.DialogInterface; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
| 33 | import android.content.pm.ActivityInfo; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.ResolveInfo; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 37 | import android.preference.CheckBoxPreference; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 38 | import android.preference.ListPreference; |
| 39 | import android.preference.Preference; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 40 | import android.preference.PreferenceGroup; |
| 41 | import android.preference.PreferenceScreen; |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame^] | 42 | import android.preference.Preference.OnPreferenceClickListener; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 43 | import android.provider.Settings; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 44 | import android.provider.Settings.SettingNotFoundException; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 45 | import android.speech.tts.TextToSpeech; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 46 | import android.util.Log; |
| 47 | |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 49 | import java.util.List; |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 50 | import java.util.Locale; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 51 | import java.util.StringTokenizer; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 52 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 53 | public class TextToSpeechSettings extends SettingsPreferenceFragment implements |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 54 | Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, |
| 55 | TextToSpeech.OnInitListener { |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 56 | |
| 57 | private static final String TAG = "TextToSpeechSettings"; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 58 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 59 | private static final String SYSTEM_TTS = "com.svox.pico"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 60 | private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 61 | private static final String KEY_TTS_INSTALL_DATA = "tts_install_data"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 62 | 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] | 63 | private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate"; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 64 | private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang"; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 65 | private static final String KEY_TTS_DEFAULT_COUNTRY = "tts_default_country"; |
| 66 | private static final String KEY_TTS_DEFAULT_VARIANT = "tts_default_variant"; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 67 | private static final String KEY_TTS_DEFAULT_SYNTH = "tts_default_synth"; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 68 | |
| 69 | private static final String KEY_PLUGIN_ENABLED_PREFIX = "ENABLED_"; |
| 70 | private static final String KEY_PLUGIN_SETTINGS_PREFIX = "SETTINGS_"; |
| 71 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 72 | // TODO move default Locale values to TextToSpeech.Engine |
| 73 | private static final String DEFAULT_LANG_VAL = "eng"; |
| 74 | private static final String DEFAULT_COUNTRY_VAL = "USA"; |
| 75 | private static final String DEFAULT_VARIANT_VAL = ""; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 76 | |
| 77 | private static final String LOCALE_DELIMITER = "-"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 78 | |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 79 | private static final String FALLBACK_TTS_DEFAULT_SYNTH = |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 80 | TextToSpeech.Engine.DEFAULT_SYNTH; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 81 | |
| 82 | private Preference mPlayExample = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 83 | private Preference mInstallData = null; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 84 | private CheckBoxPreference mUseDefaultPref = null; |
| 85 | private ListPreference mDefaultRatePref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 86 | private ListPreference mDefaultLocPref = null; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 87 | private ListPreference mDefaultSynthPref = null; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 88 | private String mDefaultLanguage = null; |
| 89 | private String mDefaultCountry = null; |
| 90 | private String mDefaultLocVariant = null; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 91 | private String mDefaultEng = ""; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 92 | private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE; |
| 93 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 94 | // Index of the current string to use for the demo. |
| 95 | private int mDemoStringIndex = 0; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 96 | |
| 97 | private boolean mEnableDemo = false; |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 98 | private boolean mVoicesMissing = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 99 | |
| 100 | private TextToSpeech mTts = null; |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 101 | private boolean mTtsStarted = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Request code (arbitrary value) for voice data check through |
| 105 | * startActivityForResult. |
| 106 | */ |
| 107 | private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 108 | private static final int GET_SAMPLE_TEXT = 1983; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 109 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 110 | @Override |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 111 | public void onCreate(Bundle savedInstanceState) { |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 112 | super.onCreate(savedInstanceState); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 113 | addPreferencesFromResource(R.xml.tts_settings); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 114 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 115 | final Activity activity = getActivity(); |
| 116 | addEngineSpecificSettings(activity); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 117 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 118 | activity.setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); |
Jean-Michel Trivi | 6dde896 | 2009-08-11 09:06:58 -0700 | [diff] [blame] | 119 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 120 | mEnableDemo = false; |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 121 | mTtsStarted = false; |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 122 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 123 | Locale currentLocale = Locale.getDefault(); |
| 124 | mDefaultLanguage = currentLocale.getISO3Language(); |
| 125 | mDefaultCountry = currentLocale.getISO3Country(); |
| 126 | mDefaultLocVariant = currentLocale.getVariant(); |
| 127 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 128 | mTts = new TextToSpeech(activity, this); |
Amith Yamasani | 02cf71a | 2010-09-21 15:48:52 -0700 | [diff] [blame^] | 129 | initClickers(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 130 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 131 | |
| 132 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 133 | @Override |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 134 | public void onStart() { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 135 | super.onStart(); |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 136 | if (mTtsStarted){ |
| 137 | // whenever we return to this screen, we don't know the state of the |
| 138 | // system, so we have to recheck that we can play the demo, or it must be disabled. |
| 139 | // TODO make the TTS service listen to "changes in the system", i.e. sd card un/mount |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 140 | updateWidgetState(); |
| 141 | checkVoiceData(); |
| 142 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | @Override |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 147 | public void onDestroy() { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 148 | super.onDestroy(); |
| 149 | if (mTts != null) { |
| 150 | mTts.shutdown(); |
| 151 | } |
| 152 | } |
| 153 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 154 | @Override |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 155 | public void onPause() { |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 156 | super.onPause(); |
| 157 | if ((mDefaultRatePref != null) && (mDefaultRatePref.getDialog() != null)) { |
| 158 | mDefaultRatePref.getDialog().dismiss(); |
| 159 | } |
| 160 | if ((mDefaultLocPref != null) && (mDefaultLocPref.getDialog() != null)) { |
| 161 | mDefaultLocPref.getDialog().dismiss(); |
| 162 | } |
| 163 | if ((mDefaultSynthPref != null) && (mDefaultSynthPref.getDialog() != null)) { |
| 164 | mDefaultSynthPref.getDialog().dismiss(); |
| 165 | } |
| 166 | } |
| 167 | |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 168 | private void addEngineSpecificSettings(Context context) { |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 169 | PreferenceGroup enginesCategory = (PreferenceGroup) findPreference("tts_engines_section"); |
| 170 | Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); |
| 171 | ResolveInfo[] enginesArray = new ResolveInfo[0]; |
| 172 | PackageManager pm = getPackageManager(); |
| 173 | enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray); |
| 174 | for (int i = 0; i < enginesArray.length; i++) { |
| 175 | String prefKey = ""; |
| 176 | final String pluginPackageName = enginesArray[i].activityInfo.packageName; |
| 177 | if (!enginesArray[i].activityInfo.packageName.equals(SYSTEM_TTS)) { |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 178 | CheckBoxPreference chkbxPref = new CheckBoxPreference(context); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 179 | prefKey = KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName; |
| 180 | chkbxPref.setKey(prefKey); |
| 181 | chkbxPref.setTitle(enginesArray[i].loadLabel(pm)); |
| 182 | enginesCategory.addPreference(chkbxPref); |
| 183 | } |
| 184 | if (pluginHasSettings(pluginPackageName)) { |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 185 | Preference pref = new Preference(context); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 186 | prefKey = KEY_PLUGIN_SETTINGS_PREFIX + pluginPackageName; |
| 187 | pref.setKey(prefKey); |
| 188 | pref.setTitle(enginesArray[i].loadLabel(pm)); |
| 189 | CharSequence settingsLabel = getResources().getString( |
| 190 | R.string.tts_engine_name_settings, enginesArray[i].loadLabel(pm)); |
| 191 | pref.setSummary(settingsLabel); |
| 192 | pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){ |
| 193 | public boolean onPreferenceClick(Preference preference){ |
| 194 | Intent i = new Intent(); |
| 195 | i.setClassName(pluginPackageName, |
| 196 | pluginPackageName + ".EngineSettings"); |
| 197 | startActivity(i); |
| 198 | return true; |
| 199 | } |
| 200 | }); |
| 201 | enginesCategory.addPreference(pref); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private boolean pluginHasSettings(String pluginPackageName) { |
| 207 | PackageManager pm = getPackageManager(); |
| 208 | Intent i = new Intent(); |
| 209 | i.setClassName(pluginPackageName, pluginPackageName + ".EngineSettings"); |
| 210 | if (pm.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY) != null){ |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 217 | private void initClickers() { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 218 | mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 219 | mPlayExample.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 220 | |
| 221 | mInstallData = findPreference(KEY_TTS_INSTALL_DATA); |
| 222 | mInstallData.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | private void initDefaultSettings() { |
| 227 | ContentResolver resolver = getContentResolver(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 228 | |
| 229 | // Find the default TTS values in the settings, initialize and store the |
| 230 | // settings if they are not found. |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 231 | |
| 232 | // "Use Defaults" |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 233 | int useDefault = 0; |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 234 | mUseDefaultPref = (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
| 235 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 236 | useDefault = Settings.Secure.getInt(resolver, TTS_USE_DEFAULTS); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 237 | } catch (SettingNotFoundException e) { |
| 238 | // "use default" setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 239 | useDefault = TextToSpeech.Engine.USE_DEFAULTS; |
| 240 | Settings.Secure.putInt(resolver, TTS_USE_DEFAULTS, useDefault); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 241 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 242 | mUseDefaultPref.setChecked(useDefault == 1); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 243 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 244 | |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 245 | // Default synthesis engine |
| 246 | mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH); |
| 247 | loadEngines(); |
| 248 | mDefaultSynthPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 249 | String engine = Settings.Secure.getString(resolver, TTS_DEFAULT_SYNTH); |
| 250 | if (engine == null) { |
| 251 | // TODO move FALLBACK_TTS_DEFAULT_SYNTH to TextToSpeech |
| 252 | engine = FALLBACK_TTS_DEFAULT_SYNTH; |
| 253 | Settings.Secure.putString(resolver, TTS_DEFAULT_SYNTH, engine); |
| 254 | } |
| 255 | mDefaultEng = engine; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 256 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 257 | // Default rate |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 258 | mDefaultRatePref = (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
| 259 | try { |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 260 | mDefaultRate = Settings.Secure.getInt(resolver, TTS_DEFAULT_RATE); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 261 | } catch (SettingNotFoundException e) { |
| 262 | // default rate setting not found, initialize it |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 263 | mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE; |
| 264 | Settings.Secure.putInt(resolver, TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 265 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 266 | mDefaultRatePref.setValue(String.valueOf(mDefaultRate)); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 267 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 268 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 269 | // Default language / country / variant : these three values map to a single ListPref |
| 270 | // representing the matching Locale |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 271 | mDefaultLocPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG); |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 272 | initDefaultLang(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 273 | mDefaultLocPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Ask the current default engine to launch the matching CHECK_TTS_DATA activity |
| 279 | * to check the required TTS files are properly installed. |
| 280 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 281 | private void checkVoiceData() { |
| 282 | PackageManager pm = getPackageManager(); |
| 283 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 284 | intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 285 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 286 | // query only the package that matches that of the default engine |
| 287 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 288 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 289 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 290 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 291 | this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 298 | * Ask the current default engine to launch the matching INSTALL_TTS_DATA activity |
| 299 | * so the required TTS files are properly installed. |
| 300 | */ |
| 301 | private void installVoiceData() { |
| 302 | PackageManager pm = getPackageManager(); |
| 303 | Intent intent = new Intent(); |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 304 | intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); |
Jean-Michel Trivi | 58ea43a | 2009-09-09 15:13:38 -0700 | [diff] [blame] | 305 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 306 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 307 | // query only the package that matches that of the default engine |
| 308 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 309 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 310 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 311 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
Jean-Michel Trivi | 58ea43a | 2009-09-09 15:13:38 -0700 | [diff] [blame] | 312 | this.startActivity(intent); |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Ask the current default engine to return a string of sample text to be |
| 319 | * spoken to the user. |
| 320 | */ |
| 321 | private void getSampleText() { |
| 322 | PackageManager pm = getPackageManager(); |
| 323 | Intent intent = new Intent(); |
| 324 | // TODO (clchen): Replace Intent string with the actual |
| 325 | // Intent defined in the list of platform Intents. |
| 326 | intent.setAction("android.speech.tts.engine.GET_SAMPLE_TEXT"); |
| 327 | intent.putExtra("language", mDefaultLanguage); |
| 328 | intent.putExtra("country", mDefaultCountry); |
| 329 | intent.putExtra("variant", mDefaultLocVariant); |
| 330 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 331 | // query only the package that matches that of the default engine |
| 332 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 333 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 334 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 335 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 336 | this.startActivityForResult(intent, GET_SAMPLE_TEXT); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 341 | |
| 342 | /** |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 343 | * Called when the TTS engine is initialized. |
| 344 | */ |
| 345 | public void onInit(int status) { |
Jean-Michel Trivi | 387dc0c | 2009-07-28 15:13:35 -0700 | [diff] [blame] | 346 | if (status == TextToSpeech.SUCCESS) { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 347 | mEnableDemo = true; |
Jean-Michel Trivi | 7330a88 | 2010-02-17 12:50:44 -0800 | [diff] [blame] | 348 | if (mDefaultLanguage == null) { |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 349 | mDefaultLanguage = Locale.getDefault().getISO3Language(); |
| 350 | } |
Jean-Michel Trivi | 7330a88 | 2010-02-17 12:50:44 -0800 | [diff] [blame] | 351 | if (mDefaultCountry == null) { |
| 352 | mDefaultCountry = Locale.getDefault().getISO3Country(); |
| 353 | } |
| 354 | if (mDefaultLocVariant == null) { |
| 355 | mDefaultLocVariant = new String(); |
| 356 | } |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 357 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 358 | mTts.setSpeechRate(mDefaultRate/100.0f); |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 359 | initDefaultSettings(); |
Charles Chen | cf31e65 | 2010-04-07 14:26:31 -0700 | [diff] [blame] | 360 | updateWidgetState(); |
| 361 | checkVoiceData(); |
| 362 | mTtsStarted = true; |
| 363 | Log.v(TAG, "TTS engine for settings screen initialized."); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 364 | } else { |
| 365 | Log.v(TAG, "TTS engine for settings screen failed to initialize successfully."); |
| 366 | mEnableDemo = false; |
| 367 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 368 | updateWidgetState(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | /** |
| 373 | * Called when voice data integrity check returns |
| 374 | */ |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 375 | @Override |
| 376 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 377 | if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 378 | if (data == null){ |
| 379 | // The CHECK_TTS_DATA activity for the plugin did not run properly; |
| 380 | // disable the preview and install controls and return. |
| 381 | mEnableDemo = false; |
| 382 | mVoicesMissing = false; |
| 383 | updateWidgetState(); |
| 384 | return; |
| 385 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 386 | ArrayList<String> available = |
Charles Chen | 510bc4a | 2010-04-07 17:41:07 -0700 | [diff] [blame] | 387 | data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 388 | ArrayList<String> unavailable = |
Charles Chen | 510bc4a | 2010-04-07 17:41:07 -0700 | [diff] [blame] | 389 | data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES); |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 390 | if ((available == null) || (unavailable == null)){ |
| 391 | // The CHECK_TTS_DATA activity for the plugin did not run properly; |
| 392 | // disable the preview and install controls and return. |
| 393 | mEnableDemo = false; |
| 394 | mVoicesMissing = false; |
| 395 | updateWidgetState(); |
| 396 | return; |
| 397 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 398 | if (available.size() > 0){ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 399 | if (mTts == null) { |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 400 | mTts = new TextToSpeech(getActivity(), this); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 401 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 402 | ListPreference ttsLanguagePref = |
| 403 | (ListPreference) findPreference("tts_default_lang"); |
| 404 | CharSequence[] entries = new CharSequence[available.size()]; |
| 405 | CharSequence[] entryValues = new CharSequence[available.size()]; |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 406 | int selectedLanguageIndex = -1; |
| 407 | String selectedLanguagePref = mDefaultLanguage; |
| 408 | if (mDefaultCountry.length() > 0) { |
| 409 | selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + |
| 410 | mDefaultCountry; |
| 411 | } |
| 412 | if (mDefaultLocVariant.length() > 0) { |
| 413 | selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + |
| 414 | mDefaultLocVariant; |
| 415 | } |
| 416 | for (int i = 0; i < available.size(); i++) { |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 417 | String[] langCountryVariant = available.get(i).split("-"); |
| 418 | Locale loc = null; |
| 419 | if (langCountryVariant.length == 1){ |
| 420 | loc = new Locale(langCountryVariant[0]); |
| 421 | } else if (langCountryVariant.length == 2){ |
| 422 | loc = new Locale(langCountryVariant[0], langCountryVariant[1]); |
| 423 | } else if (langCountryVariant.length == 3){ |
| 424 | loc = new Locale(langCountryVariant[0], langCountryVariant[1], |
| 425 | langCountryVariant[2]); |
| 426 | } |
| 427 | if (loc != null){ |
| 428 | entries[i] = loc.getDisplayName(); |
| 429 | entryValues[i] = available.get(i); |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 430 | if (entryValues[i].equals(selectedLanguagePref)) { |
| 431 | selectedLanguageIndex = i; |
| 432 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | ttsLanguagePref.setEntries(entries); |
| 436 | ttsLanguagePref.setEntryValues(entryValues); |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 437 | if (selectedLanguageIndex > -1) { |
| 438 | ttsLanguagePref.setValueIndex(selectedLanguageIndex); |
| 439 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 440 | mEnableDemo = true; |
Charles Chen | 473111b | 2010-04-06 15:50:42 -0700 | [diff] [blame] | 441 | // Make sure that the default language can be used. |
| 442 | int languageResult = mTts.setLanguage( |
| 443 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 444 | if (languageResult < TextToSpeech.LANG_AVAILABLE){ |
| 445 | Locale currentLocale = Locale.getDefault(); |
| 446 | mDefaultLanguage = currentLocale.getISO3Language(); |
| 447 | mDefaultCountry = currentLocale.getISO3Country(); |
| 448 | mDefaultLocVariant = currentLocale.getVariant(); |
| 449 | languageResult = mTts.setLanguage( |
| 450 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 451 | // If the default Locale isn't supported, just choose the first available |
| 452 | // language so that there is at least something. |
| 453 | if (languageResult < TextToSpeech.LANG_AVAILABLE){ |
| 454 | parseLocaleInfo(ttsLanguagePref.getEntryValues()[0].toString()); |
| 455 | mTts.setLanguage( |
| 456 | new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
| 457 | } |
| 458 | ContentResolver resolver = getContentResolver(); |
| 459 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 460 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 461 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 462 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 463 | } else { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 464 | mEnableDemo = false; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 465 | } |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 466 | |
| 467 | if (unavailable.size() > 0){ |
| 468 | mVoicesMissing = true; |
| 469 | } else { |
| 470 | mVoicesMissing = false; |
| 471 | } |
| 472 | |
| 473 | updateWidgetState(); |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 474 | } else if (requestCode == GET_SAMPLE_TEXT) { |
| 475 | if (resultCode == TextToSpeech.LANG_AVAILABLE) { |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 476 | String sample = getActivity().getString(R.string.tts_demo); |
Charles Chen | ec05e71 | 2010-02-18 15:06:26 -0800 | [diff] [blame] | 477 | if ((data != null) && (data.getStringExtra("sampleText") != null)) { |
| 478 | sample = data.getStringExtra("sampleText"); |
Charles Chen | d5f013a | 2010-02-18 10:11:25 -0800 | [diff] [blame] | 479 | } |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 480 | if (mTts != null) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 481 | mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null); |
| 482 | } |
| 483 | } else { |
| 484 | // TODO: Display an error here to the user. |
| 485 | Log.e(TAG, "Did not have a sample string for the requested language"); |
| 486 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 490 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 491 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 492 | // "Use Defaults" |
| 493 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 494 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 495 | value); |
| 496 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 497 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 498 | // Default rate |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 499 | mDefaultRate = Integer.parseInt((String) objValue); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 500 | try { |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 501 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 502 | TTS_DEFAULT_RATE, mDefaultRate); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 503 | if (mTts != null) { |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 504 | mTts.setSpeechRate(mDefaultRate/100.0f); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 505 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 506 | Log.i(TAG, "TTS default rate is " + mDefaultRate); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 507 | } catch (NumberFormatException e) { |
| 508 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 509 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 510 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 511 | // Default locale |
| 512 | ContentResolver resolver = getContentResolver(); |
| 513 | parseLocaleInfo((String) objValue); |
| 514 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage); |
| 515 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry); |
| 516 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant); |
| 517 | Log.v(TAG, "TTS default lang/country/variant set to " |
| 518 | + mDefaultLanguage + "/" + mDefaultCountry + "/" + mDefaultLocVariant); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 519 | if (mTts != null) { |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 520 | mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant)); |
Jean-Michel Trivi | 628431d | 2009-07-17 16:52:54 -0700 | [diff] [blame] | 521 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 522 | int newIndex = mDefaultLocPref.findIndexOfValue((String)objValue); |
| 523 | Log.v("Settings", " selected is " + newIndex); |
| 524 | mDemoStringIndex = newIndex > -1 ? newIndex : 0; |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 525 | } else if (KEY_TTS_DEFAULT_SYNTH.equals(preference.getKey())) { |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 526 | mDefaultEng = objValue.toString(); |
| 527 | Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mDefaultEng); |
| 528 | if (mTts != null) { |
| 529 | mTts.setEngineByPackageName(mDefaultEng); |
Charles Chen | cf3998b | 2010-02-11 18:13:42 -0800 | [diff] [blame] | 530 | mEnableDemo = false; |
| 531 | mVoicesMissing = false; |
| 532 | updateWidgetState(); |
| 533 | checkVoiceData(); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 534 | } |
| 535 | Log.v("Settings", "The default synth is: " + objValue.toString()); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 536 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 537 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 540 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 541 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 542 | /** |
| 543 | * Called when mPlayExample or mInstallData is clicked |
| 544 | */ |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 545 | public boolean onPreferenceClick(Preference preference) { |
| 546 | if (preference == mPlayExample) { |
Charles Chen | 4df6c79 | 2010-01-22 11:17:40 -0800 | [diff] [blame] | 547 | // Get the sample text from the TTS engine; onActivityResult will do |
| 548 | // the actual speaking |
| 549 | getSampleText(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 550 | return true; |
| 551 | } |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 552 | if (preference == mInstallData) { |
Jean-Michel Trivi | 2acc02e | 2009-06-25 10:03:43 -0700 | [diff] [blame] | 553 | installVoiceData(); |
| 554 | // quit this activity so it needs to be restarted after installation of the voice data |
| 555 | finish(); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 556 | return true; |
| 557 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 561 | @Override |
| 562 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 563 | if (Utils.isMonkeyRunning()) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | if (preference instanceof CheckBoxPreference) { |
| 568 | final CheckBoxPreference chkPref = (CheckBoxPreference) preference; |
| 569 | if (!chkPref.getKey().equals(KEY_TTS_USE_DEFAULT)){ |
| 570 | if (chkPref.isChecked()) { |
| 571 | chkPref.setChecked(false); |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 572 | AlertDialog d = (new AlertDialog.Builder(getActivity())) |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 573 | .setTitle(android.R.string.dialog_alert_title) |
| 574 | .setIcon(android.R.drawable.ic_dialog_alert) |
Daisuke Miyakawa | 49a305e | 2010-09-13 17:52:13 -0700 | [diff] [blame] | 575 | .setMessage( |
| 576 | getActivity().getString(R.string.tts_engine_security_warning, |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 577 | chkPref.getTitle())) |
| 578 | .setCancelable(true) |
| 579 | .setPositiveButton(android.R.string.ok, |
| 580 | new DialogInterface.OnClickListener() { |
| 581 | public void onClick(DialogInterface dialog, int which) { |
| 582 | chkPref.setChecked(true); |
| 583 | loadEngines(); |
| 584 | } |
| 585 | }) |
| 586 | .setNegativeButton(android.R.string.cancel, |
| 587 | new DialogInterface.OnClickListener() { |
| 588 | public void onClick(DialogInterface dialog, int which) { |
| 589 | } |
| 590 | }) |
| 591 | .create(); |
| 592 | d.show(); |
| 593 | } else { |
| 594 | loadEngines(); |
| 595 | } |
| 596 | return true; |
| 597 | } |
| 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 602 | |
| 603 | private void updateWidgetState() { |
| 604 | mPlayExample.setEnabled(mEnableDemo); |
| 605 | mUseDefaultPref.setEnabled(mEnableDemo); |
| 606 | mDefaultRatePref.setEnabled(mEnableDemo); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 607 | mDefaultLocPref.setEnabled(mEnableDemo); |
| 608 | |
Charles Chen | c829871 | 2010-02-10 13:58:23 -0800 | [diff] [blame] | 609 | mInstallData.setEnabled(mVoicesMissing); |
Jean-Michel Trivi | 1e6a45a | 2009-06-22 16:03:40 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | private void parseLocaleInfo(String locale) { |
| 614 | StringTokenizer tokenizer = new StringTokenizer(locale, LOCALE_DELIMITER); |
| 615 | mDefaultLanguage = ""; |
| 616 | mDefaultCountry = ""; |
| 617 | mDefaultLocVariant = ""; |
| 618 | if (tokenizer.hasMoreTokens()) { |
| 619 | mDefaultLanguage = tokenizer.nextToken().trim(); |
| 620 | } |
| 621 | if (tokenizer.hasMoreTokens()) { |
| 622 | mDefaultCountry = tokenizer.nextToken().trim(); |
| 623 | } |
| 624 | if (tokenizer.hasMoreTokens()) { |
| 625 | mDefaultLocVariant = tokenizer.nextToken().trim(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 630 | /** |
| 631 | * Initialize the default language in the UI and in the preferences. |
| 632 | * After this method has been invoked, the default language is a supported Locale. |
| 633 | */ |
| 634 | private void initDefaultLang() { |
| 635 | // if there isn't already a default language preference |
| 636 | if (!hasLangPref()) { |
| 637 | // if the current Locale is supported |
| 638 | if (isCurrentLocSupported()) { |
| 639 | // then use the current Locale as the default language |
| 640 | useCurrentLocAsDefault(); |
| 641 | } else { |
| 642 | // otherwise use a default supported Locale as the default language |
| 643 | useSupportedLocAsDefault(); |
| 644 | } |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 647 | // Update the language preference list with the default language and the matching |
| 648 | // demo string (at this stage there is a default language pref) |
| 649 | ContentResolver resolver = getContentResolver(); |
| 650 | mDefaultLanguage = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
Charles Chen | 681d0b8 | 2010-04-12 12:06:30 -0700 | [diff] [blame] | 651 | mDefaultCountry = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY); |
| 652 | mDefaultLocVariant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 653 | |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 654 | // update the demo string |
| 655 | mDemoStringIndex = mDefaultLocPref.findIndexOfValue(mDefaultLanguage + LOCALE_DELIMITER |
| 656 | + mDefaultCountry); |
Charles Chen | 8a37e61 | 2010-02-04 15:52:30 -0800 | [diff] [blame] | 657 | if (mDemoStringIndex > -1){ |
| 658 | mDefaultLocPref.setValueIndex(mDemoStringIndex); |
| 659 | } |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** |
| 663 | * (helper function for initDefaultLang() ) |
| 664 | * Returns whether there is a default language in the TTS settings. |
| 665 | */ |
| 666 | private boolean hasLangPref() { |
Jean-Baptiste Queru | 6e61b21 | 2010-04-14 10:40:48 -0700 | [diff] [blame] | 667 | ContentResolver resolver = getContentResolver(); |
| 668 | String language = Settings.Secure.getString(resolver, TTS_DEFAULT_LANG); |
| 669 | if ((language == null) || (language.length() < 1)) { |
| 670 | return false; |
| 671 | } |
| 672 | String country = Settings.Secure.getString(resolver, TTS_DEFAULT_COUNTRY); |
| 673 | if (country == null) { |
| 674 | return false; |
| 675 | } |
| 676 | String variant = Settings.Secure.getString(resolver, TTS_DEFAULT_VARIANT); |
| 677 | if (variant == null) { |
| 678 | return false; |
| 679 | } |
| 680 | return true; |
Jean-Michel Trivi | e8e23db | 2009-09-02 15:15:33 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * (helper function for initDefaultLang() ) |
| 685 | * Returns whether the current Locale is supported by this Settings screen |
| 686 | */ |
| 687 | private boolean isCurrentLocSupported() { |
| 688 | String currentLocID = Locale.getDefault().getISO3Language() + LOCALE_DELIMITER |
| 689 | + Locale.getDefault().getISO3Country(); |
| 690 | return (mDefaultLocPref.findIndexOfValue(currentLocID) > -1); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * (helper function for initDefaultLang() ) |
| 695 | * Sets the default language in TTS settings to be the current Locale. |
| 696 | * This should only be used after checking that the current Locale is supported. |
| 697 | */ |
| 698 | private void useCurrentLocAsDefault() { |
| 699 | Locale currentLocale = Locale.getDefault(); |
| 700 | ContentResolver resolver = getContentResolver(); |
| 701 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, currentLocale.getISO3Language()); |
| 702 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, currentLocale.getISO3Country()); |
| 703 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, currentLocale.getVariant()); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * (helper function for initDefaultLang() ) |
| 708 | * Sets the default language in TTS settings to be one known to be supported |
| 709 | */ |
| 710 | private void useSupportedLocAsDefault() { |
| 711 | ContentResolver resolver = getContentResolver(); |
| 712 | Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, DEFAULT_LANG_VAL); |
| 713 | Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, DEFAULT_COUNTRY_VAL); |
| 714 | Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, DEFAULT_VARIANT_VAL); |
Jean-Michel Trivi | 44fbbea | 2009-07-06 14:04:54 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 717 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 718 | private void loadEngines() { |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 719 | mDefaultSynthPref = (ListPreference) findPreference(KEY_TTS_DEFAULT_SYNTH); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 720 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 721 | // TODO (clchen): Try to see if it is possible to be more efficient here |
| 722 | // and not search for plugins again. |
| 723 | Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); |
| 724 | ResolveInfo[] enginesArray = new ResolveInfo[0]; |
| 725 | PackageManager pm = getPackageManager(); |
| 726 | enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray); |
| 727 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 728 | ArrayList<CharSequence> values = new ArrayList<CharSequence>(); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 729 | String enabledEngines = ""; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 730 | for (int i = 0; i < enginesArray.length; i++) { |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 731 | String pluginPackageName = enginesArray[i].activityInfo.packageName; |
| 732 | if (pluginPackageName.equals(SYSTEM_TTS)) { |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 733 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 734 | values.add(pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 735 | } else { |
| 736 | CheckBoxPreference pref = (CheckBoxPreference) findPreference( |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 737 | KEY_PLUGIN_ENABLED_PREFIX + pluginPackageName); |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 738 | if ((pref != null) && pref.isChecked()){ |
| 739 | entries.add(enginesArray[i].loadLabel(pm)); |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 740 | values.add(pluginPackageName); |
| 741 | enabledEngines = enabledEngines + pluginPackageName + " "; |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
Charles Chen | f47cce0 | 2010-03-17 17:33:23 -0700 | [diff] [blame] | 745 | ContentResolver resolver = getContentResolver(); |
| 746 | Settings.Secure.putString(resolver, TTS_ENABLED_PLUGINS, enabledEngines); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 747 | |
Charles Chen | 0a0eb5f | 2010-03-16 20:09:17 -0700 | [diff] [blame] | 748 | CharSequence entriesArray[] = new CharSequence[entries.size()]; |
| 749 | CharSequence valuesArray[] = new CharSequence[values.size()]; |
| 750 | |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 751 | mDefaultSynthPref.setEntries(entries.toArray(entriesArray)); |
| 752 | mDefaultSynthPref.setEntryValues(values.toArray(valuesArray)); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 753 | |
| 754 | // Set the selected engine based on the saved preference |
| 755 | String selectedEngine = Settings.Secure.getString(getContentResolver(), TTS_DEFAULT_SYNTH); |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 756 | int selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(selectedEngine); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 757 | if (selectedEngineIndex == -1){ |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 758 | selectedEngineIndex = mDefaultSynthPref.findIndexOfValue(SYSTEM_TTS); |
Charles Chen | be6e827 | 2010-03-22 16:06:05 -0700 | [diff] [blame] | 759 | } |
Charles Chen | 8c8185b | 2010-04-08 16:51:35 -0700 | [diff] [blame] | 760 | mDefaultSynthPref.setValueIndex(selectedEngineIndex); |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 761 | } |
Charles Chen | 5dbc74a | 2009-12-07 12:08:13 -0800 | [diff] [blame] | 762 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 763 | } |