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