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