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; |
| 22 | |
| 23 | import android.content.ContentResolver; |
| 24 | import android.os.Bundle; |
| 25 | import android.preference.ListPreference; |
| 26 | import android.preference.Preference; |
| 27 | import android.preference.PreferenceActivity; |
| 28 | import android.preference.CheckBoxPreference; |
| 29 | import android.provider.Settings; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | public class TextToSpeechSettings extends PreferenceActivity implements |
| 33 | Preference.OnPreferenceChangeListener { |
| 34 | |
| 35 | private static final String TAG = "TextToSpeechSettings"; |
| 36 | |
| 37 | /** If there is no setting in the provider, use this. */ |
| 38 | private static final int FALLBACK_TTS_DEFAULT_RATE = 100; // 1x |
| 39 | private static final int FALLBACK_TTS_DEFAULT_PITCH = 100;// 1x |
| 40 | private static final int FALLBACK_TTS_USE_DEFAULTS = 1; |
| 41 | |
| 42 | private static final String KEY_TTS_USE_DEFAULT = |
| 43 | "toggle_use_default_tts_settings"; |
| 44 | private static final String KEY_TTS_DEFAULT_RATE = "tts_default_rate"; |
| 45 | private static final String KEY_TTS_DEFAULT_PITCH = "tts_default_pitch"; |
| 46 | |
| 47 | private CheckBoxPreference mUseDefaultPref = null; |
| 48 | private ListPreference mDefaultRatePref = null; |
| 49 | private ListPreference mDefaultPitchPref = null; |
| 50 | |
| 51 | |
| 52 | @Override |
| 53 | protected void onCreate(Bundle savedInstanceState) { |
| 54 | super.onCreate(savedInstanceState); |
| 55 | |
| 56 | addPreferencesFromResource(R.xml.tts_settings); |
| 57 | |
| 58 | initDefaultSettings(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | private void initDefaultSettings() { |
| 63 | ContentResolver resolver = getContentResolver(); |
| 64 | |
| 65 | // "Use Defaults" |
| 66 | mUseDefaultPref = |
| 67 | (CheckBoxPreference) findPreference(KEY_TTS_USE_DEFAULT); |
| 68 | mUseDefaultPref.setChecked(Settings.System.getInt(resolver, |
| 69 | TTS_USE_DEFAULTS, |
| 70 | FALLBACK_TTS_USE_DEFAULTS) == 1 ? true : false); |
| 71 | mUseDefaultPref.setOnPreferenceChangeListener(this); |
| 72 | |
| 73 | // Default rate |
| 74 | mDefaultRatePref = |
| 75 | (ListPreference) findPreference(KEY_TTS_DEFAULT_RATE); |
| 76 | mDefaultRatePref.setValue(String.valueOf(Settings.System.getInt( |
| 77 | resolver, TTS_DEFAULT_RATE, FALLBACK_TTS_DEFAULT_RATE))); |
| 78 | mDefaultRatePref.setOnPreferenceChangeListener(this); |
| 79 | |
| 80 | // Default pitch |
| 81 | mDefaultPitchPref = |
| 82 | (ListPreference) findPreference(KEY_TTS_DEFAULT_PITCH); |
| 83 | mDefaultPitchPref.setValue(String.valueOf(Settings.System.getInt( |
| 84 | resolver, TTS_DEFAULT_PITCH, FALLBACK_TTS_DEFAULT_PITCH))); |
| 85 | mDefaultPitchPref.setOnPreferenceChangeListener(this); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |
| 90 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 91 | if (KEY_TTS_USE_DEFAULT.equals(preference.getKey())) { |
| 92 | // "Use Defaults" |
| 93 | int value = (Boolean)objValue ? 1 : 0; |
| 94 | Settings.System.putInt(getContentResolver(), TTS_USE_DEFAULTS, |
| 95 | value); |
| 96 | Log.i(TAG, "TTS use default settings is "+objValue.toString()); |
| 97 | } else if (KEY_TTS_DEFAULT_RATE.equals(preference.getKey())) { |
| 98 | // Default rate |
| 99 | int value = Integer.parseInt((String) objValue); |
| 100 | try { |
| 101 | Settings.System.putInt(getContentResolver(), |
| 102 | TTS_DEFAULT_RATE, value); |
| 103 | Log.i(TAG, "TTS default rate is "+value); |
| 104 | } catch (NumberFormatException e) { |
| 105 | Log.e(TAG, "could not persist default TTS rate setting", e); |
| 106 | } |
| 107 | } else if (KEY_TTS_DEFAULT_PITCH.equals(preference.getKey())) { |
| 108 | // Default pitch |
| 109 | int value = Integer.parseInt((String) objValue); |
| 110 | try { |
| 111 | Settings.System.putInt(getContentResolver(), |
| 112 | TTS_DEFAULT_PITCH, value); |
| 113 | Log.i(TAG, "TTS default pitch is "+value); |
| 114 | } catch (NumberFormatException e) { |
| 115 | Log.e(TAG, "could not persist default TTS pitch setting", e); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | } |