Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import static android.provider.Settings.Secure.TTS_USE_DEFAULTS; |
| 20 | import static android.provider.Settings.Secure.TTS_DEFAULT_RATE; |
| 21 | import static android.provider.Settings.Secure.TTS_DEFAULT_PITCH; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 22 | import static android.provider.Settings.Secure.TTS_DEFAULT_LANG; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 23 | |
| 24 | import android.content.ContentResolver; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 25 | import android.content.Intent; |
| 26 | import android.content.pm.ActivityInfo; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
| 30 | import android.preference.ListPreference; |
| 31 | import android.preference.Preference; |
| 32 | import android.preference.PreferenceActivity; |
| 33 | import android.preference.CheckBoxPreference; |
| 34 | import android.provider.Settings; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 35 | import android.speech.tts.TextToSpeech; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 38 | import java.util.List; |
| 39 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 40 | public class TextToSpeechSettings extends PreferenceActivity implements |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 41 | Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, |
| 42 | TextToSpeech.OnInitListener { |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = "TextToSpeechSettings"; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 45 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 46 | private static final String KEY_TTS_PLAY_EXAMPLE = "tts_play_example"; |
| 47 | 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] | 48 | private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate"; |
| 49 | private static final String KEY_TTS_DEFAULT_PITCH = "tts_default_pitch"; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 50 | private static final String KEY_TTS_DEFAULT_LANG = "tts_default_lang"; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 51 | |
| 52 | // TODO move this to android.speech.tts.TextToSpeech.Engine |
| 53 | private static final String FALLBACK_TTS_DEFAULT_SYNTH = "com.svox.pico"; |
| 54 | |
| 55 | private Preference mPlayExample = null; |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 56 | private CheckBoxPreference mUseDefaultPref = null; |
| 57 | private ListPreference mDefaultRatePref = null; |
| 58 | private ListPreference mDefaultPitchPref = null; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 59 | private ListPreference mDefaultLangPref = null; |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 60 | private String mDefaultEng = ""; |
| 61 | |
| 62 | private boolean mEnableDemo = false; |
| 63 | |
| 64 | private TextToSpeech mTts = null; |
| 65 | |
| 66 | /** |
| 67 | * Request code (arbitrary value) for voice data check through |
| 68 | * startActivityForResult. |
| 69 | */ |
| 70 | private static final int VOICE_DATA_INTEGRITY_CHECK = 1977; |
| 71 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 72 | @Override |
| 73 | protected void onCreate(Bundle savedInstanceState) { |
| 74 | super.onCreate(savedInstanceState); |
| 75 | |
| 76 | addPreferencesFromResource(R.xml.tts_settings); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 77 | |
| 78 | initDemo(); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 79 | initDefaultSettings(); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 80 | |
| 81 | checkVoiceData(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | @Override |
| 86 | protected void onResume() { |
| 87 | super.onResume(); |
| 88 | // whenever we return to this screen, we don't know the state of the |
| 89 | // system, so we have to recheck that we can play the demo, or it must be disabled. |
| 90 | mEnableDemo = false; |
| 91 | initDemo(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | @Override |
| 96 | protected void onDestroy() { |
| 97 | super.onDestroy(); |
| 98 | if (mTts != null) { |
| 99 | mTts.shutdown(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | private void initDemo() { |
| 105 | mPlayExample = findPreference(KEY_TTS_PLAY_EXAMPLE); |
| 106 | mPlayExample.setEnabled(mEnableDemo); |
| 107 | mPlayExample.setOnPreferenceClickListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | private void initDefaultSettings() { |
| 112 | ContentResolver resolver = getContentResolver(); |
| 113 | |
| 114 | // "Use Defaults" |
| 115 | mUseDefaultPref = |
| 116 | (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 117 | mUseDefaultPref.setChecked(Settings.Secure.getInt(resolver, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 118 | TTS_USE_DEFAULTS, |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 119 | TextToSpeech.Engine.FALLBACK_TTS_USE_DEFAULTS) == 1 ? true : false); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 120 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 121 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 122 | // Default engine |
| 123 | mDefaultEng = FALLBACK_TTS_DEFAULT_SYNTH; |
| 124 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 125 | // Default rate |
| 126 | mDefaultRatePref = |
| 127 | (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 128 | mDefaultRatePref.setValue(String.valueOf(Settings.Secure.getInt( |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 129 | resolver, TTS_DEFAULT_RATE, TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_RATE))); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 130 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 131 | |
| 132 | // Default pitch |
| 133 | mDefaultPitchPref = |
| 134 | (ListPreference) findPreference(KEY_TTS_DEFAULT_PITCH); |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 135 | mDefaultPitchPref.setValue(String.valueOf(Settings.Secure.getInt( |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 136 | resolver, TTS_DEFAULT_PITCH, TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_PITCH))); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 137 | mDefaultPitchPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 138 | |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 139 | // Default language |
| 140 | mDefaultLangPref = |
| 141 | (ListPreference) findPreference(KEY_TTS_DEFAULT_LANG); |
| 142 | String defaultLang = String.valueOf(Settings.Secure.getString(resolver, |
| 143 | TTS_DEFAULT_LANG)); |
| 144 | if (defaultLang.compareTo("null") == 0) { |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 145 | mDefaultLangPref.setValue(TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_LANG); |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 146 | Log.i(TAG, "TTS initDefaultSettings() default lang null "); |
| 147 | } else { |
| 148 | mDefaultLangPref.setValue(defaultLang); |
| 149 | Log.i(TAG, "TTS initDefaultSettings() default lang is "+defaultLang); |
| 150 | } |
| 151 | mDefaultLangPref.setOnPreferenceChangeListener(this); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 155 | private void checkVoiceData() { |
| 156 | PackageManager pm = getPackageManager(); |
| 157 | Intent intent = new Intent(); |
| 158 | intent.setAction("android.intent.action.CHECK_TTS_DATA"); |
| 159 | List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); |
| 160 | // query only the package that matches that of the default engine |
| 161 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 162 | ActivityInfo currentActivityInfo = resolveInfos.get(i).activityInfo; |
| 163 | if (mDefaultEng.equals(currentActivityInfo.packageName)) { |
| 164 | intent.setClassName(mDefaultEng, currentActivityInfo.name); |
| 165 | this.startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Called when the TTS engine is initialized. |
| 173 | */ |
| 174 | public void onInit(int status) { |
| 175 | if (status == TextToSpeech.TTS_SUCCESS) { |
| 176 | Log.v(TAG, "TTS engine for settings screen initialized."); |
| 177 | mEnableDemo = true; |
| 178 | } else { |
| 179 | Log.v(TAG, "TTS engine for settings screen failed to initialize successfully."); |
| 180 | mEnableDemo = false; |
| 181 | } |
| 182 | mPlayExample.setEnabled(mEnableDemo); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Called when voice data integrity check returns |
| 188 | */ |
| 189 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 190 | if (requestCode == VOICE_DATA_INTEGRITY_CHECK) { |
| 191 | if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { |
| 192 | Log.v(TAG, "Voice data check passed"); |
| 193 | if (mTts == null) { |
| 194 | mTts = new TextToSpeech(this, this); |
| 195 | } |
| 196 | } else { |
| 197 | Log.v(TAG, "Voice data check failed"); |
| 198 | |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 204 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 205 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 206 | // "Use Defaults" |
| 207 | int value = (Boolean)objValue ? 1 : 0; |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 208 | Settings.Secure.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 209 | value); |
| 210 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 211 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 212 | // Default rate |
| 213 | int value = Integer.parseInt((String) objValue); |
| 214 | try { |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 215 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 216 | TTS_DEFAULT_RATE, value); |
| 217 | Log.i(TAG, "TTS default rate is "+value); |
| 218 | } catch (NumberFormatException e) { |
| 219 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 220 | } |
| 221 | } else if (KEY_TTS_DEFAULT_PITCH.equals(preference.getKey())) { |
| 222 | // Default pitch |
| 223 | int value = Integer.parseInt((String) objValue); |
| 224 | try { |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 225 | Settings.Secure.putInt(getContentResolver(), |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 226 | TTS_DEFAULT_PITCH, value); |
| 227 | Log.i(TAG, "TTS default pitch is "+value); |
| 228 | } catch (NumberFormatException e) { |
| 229 | Log.e(TAG, "could not persist default TTS pitch setting", e); |
| 230 | } |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 231 | } else if (KEY_TTS_DEFAULT_LANG.equals(preference.getKey())) { |
Jean-Michel Trivi | 8036862 | 2009-06-09 19:29:27 -0700 | [diff] [blame] | 232 | // Default language |
| 233 | String value = (String) objValue; |
| 234 | Settings.Secure.putString(getContentResolver(), |
| 235 | TTS_DEFAULT_LANG, value); |
| 236 | Log.i(TAG, "TTS default lang is "+value); |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
Jean-Michel Trivi | 74e565d | 2009-06-18 18:44:52 -0700 | [diff] [blame^] | 242 | |
| 243 | public boolean onPreferenceClick(Preference preference) { |
| 244 | if (preference == mPlayExample) { |
| 245 | if (mTts != null) { |
| 246 | mTts.speak(getResources().getString(R.string.tts_demo), |
| 247 | TextToSpeech.TTS_QUEUE_FLUSH, null); |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
Jean-Michel Trivi | ed29a65 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 254 | } |