From 6c5631d8100b24cf949df31a8f7cdaf2d64155fb Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 4 Sep 2013 13:43:53 +0100 Subject: [PATCH] VoiceSettings.java: Support reading the default_gender and espeak_variant preferences. --- .../espeak/test/VoiceSettingsTest.java | 98 +++++++++++++++++++ .../com/reecedunn/espeak/VoiceSettings.java | 50 ++++++++++ 2 files changed, 148 insertions(+) create mode 100644 android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java create mode 100644 android/src/com/reecedunn/espeak/VoiceSettings.java diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java new file mode 100644 index 00000000..65ae19e3 --- /dev/null +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2013 Reece H. Dunn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.reecedunn.espeak.test; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.test.AndroidTestCase; + +import com.reecedunn.espeak.SpeechSynthesis; +import com.reecedunn.espeak.VoiceSettings; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class VoiceSettingsTest extends AndroidTestCase +{ + // No Settings (New Install) + + public void testNoPreferences() + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs); + assertThat(settings.getVoiceVariant().toString(), is("male")); + } + + // Old Settings + + public void testDefaultGenderMale() + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE)); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs); + assertThat(settings.getVoiceVariant().toString(), is("male")); + } + + public void testDefaultGenderFemale() + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE)); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs); + assertThat(settings.getVoiceVariant().toString(), is("female")); + } + + // New Settings + + public void testEspeakVariant() + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("espeak_variant", "klatt2-old"); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs); + assertThat(settings.getVoiceVariant().toString(), is("klatt2-old")); + } + + // Mixed (Old and New) Settings + + public void testEspeakVariantWithDefaultGenderFemale() + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE)); + editor.putString("espeak_variant", "klatt4"); + editor.commit(); + + VoiceSettings settings = new VoiceSettings(prefs); + assertThat(settings.getVoiceVariant().toString(), is("klatt4")); + } +} diff --git a/android/src/com/reecedunn/espeak/VoiceSettings.java b/android/src/com/reecedunn/espeak/VoiceSettings.java new file mode 100644 index 00000000..5065caf0 --- /dev/null +++ b/android/src/com/reecedunn/espeak/VoiceSettings.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2013 Reece H. Dunn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.reecedunn.espeak; + +import android.content.SharedPreferences; + +public class VoiceSettings { + private final SharedPreferences mPreferences; + + public static final String PREF_DEFAULT_GENDER = "default_gender"; + public static final String PREF_VARIANT = "espeak_variant"; + + public VoiceSettings(SharedPreferences preferences) { + mPreferences = preferences; + } + + public VoiceVariant getVoiceVariant() { + String variant = mPreferences.getString(PREF_VARIANT, null); + if (variant == null) { + int gender = getPreferenceValue(PREF_DEFAULT_GENDER, SpeechSynthesis.GENDER_MALE); + if (gender == SpeechSynthesis.GENDER_FEMALE) { + return VoiceVariant.parseVoiceVariant(VoiceVariant.FEMALE); + } + return VoiceVariant.parseVoiceVariant(VoiceVariant.MALE); + } + return VoiceVariant.parseVoiceVariant(variant); + } + + private int getPreferenceValue(String preference, int defaultValue) { + String prefString = mPreferences.getString(preference, null); + if (prefString == null) { + return defaultValue; + } + return Integer.parseInt(prefString); + } +}