Debian Codegraph @

android/src/com/reecedunn/espeak/TtsSettingsActivity.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 Beka Gozalishvili
3 * Copyright (C) 2013 Reece H. Dunn
4 * Copyright (C) 2011 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package com.reecedunn.espeak;
20
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.preference.ListPreference;
26 import android.preference.Preference;
27 import android.preference.Preference.OnPreferenceChangeListener;
28 import android.preference.PreferenceActivity;
29 import android.preference.PreferenceFragment;
30 import android.preference.PreferenceGroup;
31 import android.preference.PreferenceManager;
32
33 import com.reecedunn.espeak.preference.ImportVoicePreference;
34 import com.reecedunn.espeak.preference.SeekBarPreference;
35 import com.reecedunn.espeak.preference.SpeakPunctuationPreference;
36 import com.reecedunn.espeak.preference.VoiceVariantPreference;
37
38 public class TtsSettingsActivity extends PreferenceActivity {
39
40 private static Context storageContext;
41
42 @Override
43 @SuppressWarnings("deprecation")
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46
47 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
48 {
49 PreferenceManager preferenceManager = getPreferenceManager();
50 preferenceManager.setStorageDeviceProtected ();
51 }
52 // Migrate old eyes-free settings to the new settings:
53
54 storageContext = EspeakApp.getStorageContext();
55 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(storageContext);
56 final SharedPreferences.Editor editor = prefs.edit();
57
58 String pitch = prefs.getString(VoiceSettings.PREF_PITCH, null);
59 if (pitch == null) {
60 // Try the old eyes-free setting:
61 pitch = prefs.getString(VoiceSettings.PREF_DEFAULT_PITCH, "100");
62 int pitchValue = Integer.parseInt(pitch) / 2;
63 editor.putString(VoiceSettings.PREF_PITCH, Integer.toString(pitchValue));
64 }
65
66 String rate = prefs.getString(VoiceSettings.PREF_RATE, null);
67 if (rate == null) {
68 // Try the old eyes-free setting:
69 SpeechSynthesis engine = new SpeechSynthesis(storageContext, null);
70 int defaultValue = engine.Rate.getDefaultValue();
71 int maxValue = engine.Rate.getMaxValue();
72
73 rate = prefs.getString(VoiceSettings.PREF_DEFAULT_RATE, "100");
74 int rateValue = (Integer.parseInt(rate) / 100) * defaultValue;
75 if (rateValue < defaultValue) rateValue = defaultValue;
76 if (rateValue > maxValue) rateValue = maxValue;
77 editor.putString(VoiceSettings.PREF_RATE, Integer.toString(rateValue));
78 }
79
80 String variant = prefs.getString(VoiceSettings.PREF_VARIANT, null);
81 if (variant == null) {
82 String gender = prefs.getString(VoiceSettings.PREF_DEFAULT_GENDER, "0");
83 if (gender.equals("2")) {
84 editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.FEMALE);
85 } else {
86 editor.putString(VoiceSettings.PREF_VARIANT, VoiceVariant.MALE);
87 }
88 }
89
90 editor.commit();
91
92 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
93 {
94 getFragmentManager().beginTransaction().replace(
95 android.R.id.content,
96 new PrefsEspeakFragment()).commit();
97 }
98 else
99 {
100 addPreferencesFromResource(R.xml.preferences);
101 createPreferences(TtsSettingsActivity.this, getPreferenceScreen());
102 }
103 }
104
105 public static class PrefsEspeakFragment extends PreferenceFragment {
106 @Override
107 public void onCreate(Bundle savedInstanceState) {
108 super.onCreate(savedInstanceState);
109
110 addPreferencesFromResource(R.xml.preferences);
111 createPreferences(getActivity(), getPreferenceScreen());
112 }
113 }
114
115 private static Preference createImportVoicePreference(Context context) {
116 final String title = context.getString(R.string.import_voice_title);
117
118 final ImportVoicePreference pref = new ImportVoicePreference(context);
119 pref.setTitle(title);
120 pref.setDialogTitle(title);
121 pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
122 pref.setDescription(R.string.import_voice_description);
123 return pref;
124 }
125
126 private static Preference createVoiceVariantPreference(Context context, VoiceSettings settings, int titleRes) {
127 final String title = context.getString(titleRes);
128
129 final VoiceVariantPreference pref = new VoiceVariantPreference(context);
130 pref.setTitle(title);
131 pref.setDialogTitle(title);
132 pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
133 pref.setPersistent(true);
134 pref.setVoiceVariant(settings.getVoiceVariant());
135 return pref;
136 }
137
138 private static Preference createSpeakPunctuationPreference(Context context, VoiceSettings settings, int titleRes) {
139 final String title = context.getString(titleRes);
140
141 final SpeakPunctuationPreference pref = new SpeakPunctuationPreference(context);
142 pref.setTitle(title);
143 pref.setDialogTitle(title);
144 pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
145 pref.setPersistent(true);
146 pref.setVoiceSettings(settings);
147 return pref;
148 }
149
150 private static Preference createSeekBarPreference(Context context, SpeechSynthesis.Parameter parameter, String key, int titleRes) {
151 final String title = context.getString(titleRes);
152 final int defaultValue = parameter.getDefaultValue();
153
154 final SeekBarPreference pref = new SeekBarPreference(context);
155 pref.setTitle(title);
156 pref.setDialogTitle(title);
157 pref.setKey(key);
158 pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
159 pref.setPersistent(true);
160
161 switch (parameter.getUnitType())
162 {
163 case Percentage:
164 pref.setFormatter(context.getString(R.string.formatter_percentage));
165 break;
166 case WordsPerMinute:
167 pref.setFormatter(context.getString(R.string.formatter_wpm));
168 break;
169 default:
170 throw new IllegalStateException("Unsupported unit type for the parameter.");
171 }
172
173 pref.setMin(parameter.getMinValue());
174 pref.setMax(parameter.getMaxValue());
175 pref.setDefaultValue(defaultValue);
176
177 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(storageContext);
178 final String prefString = prefs.getString(key, null);
179 if (prefString == null) {
180 pref.setProgress(defaultValue);
181 } else {
182 pref.setProgress(Integer.parseInt(prefString));
183 }
184
185 return pref;
186 }
187
188 /**
189 * Since the "%s" summary is currently broken, this sets the preference
190 * change listener for all {@link ListPreference} views to fill in the
191 * summary with the current entry value.
192 */
193 private static void createPreferences(Context context, PreferenceGroup group) {
194 SpeechSynthesis engine = new SpeechSynthesis(storageContext, null);
195 VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(storageContext), engine);
196
197 group.addPreference(createImportVoicePreference(context));
198 group.addPreference(createVoiceVariantPreference(context, settings, R.string.espeak_variant));
199 group.addPreference(createSpeakPunctuationPreference(context, settings, R.string.espeak_speak_punctuation));
200 group.addPreference(createSeekBarPreference(context, engine.Rate, VoiceSettings.PREF_RATE, R.string.setting_default_rate));
201 group.addPreference(createSeekBarPreference(context, engine.Pitch, VoiceSettings.PREF_PITCH, R.string.setting_default_pitch));
202 group.addPreference(createSeekBarPreference(context, engine.PitchRange, VoiceSettings.PREF_PITCH_RANGE, R.string.espeak_pitch_range));
203 group.addPreference(createSeekBarPreference(context, engine.Volume, VoiceSettings.PREF_VOLUME, R.string.espeak_volume));
204 }
205
206 private static final OnPreferenceChangeListener mOnPreferenceChanged =
207 new OnPreferenceChangeListener() {
208 @Override
209 public boolean onPreferenceChange(Preference preference, Object newValue) {
210 if (newValue instanceof String) {
211 String summary = "";
212 if (preference instanceof ListPreference) {
213 final ListPreference listPreference = (ListPreference) preference;
214 final int index = listPreference.findIndexOfValue((String) newValue);
215 final CharSequence[] entries = listPreference.getEntries();
216
217 if (index >= 0 && index < entries.length) {
218 summary = entries[index].toString();
219 }
220 } else if (preference instanceof SeekBarPreference) {
221 final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
222 String formatter = seekBarPreference.getFormatter();
223 summary = String.format(formatter, (String)newValue);
224 } else {
225 summary = (String)newValue;
226 }
227 preference.setSummary(summary);
228 }
229 return true;
230 }
231 };
232 }