Rework the 'Punctuation level' (now 'Speak punctuation') preference to include a custom punctuation list.

This commit is contained in:
Reece H. Dunn
2013-09-05 14:06:57 +01:00
parent bc346db22e
commit 1de60cdc15
10 changed files with 289 additions and 27 deletions
@@ -0,0 +1,129 @@
/*
* 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.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
public class SpeakPunctuationPreference extends DialogPreference {
private RadioButton mAll;
private RadioButton mCustom;
private EditText mPunctuationCharacters;
private VoiceSettings mSettings;
public SpeakPunctuationPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.speak_punctuation_preference);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
public SpeakPunctuationPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SpeakPunctuationPreference(Context context) {
this(context, null);
}
public void setVoiceSettings(VoiceSettings settings) {
mSettings = settings;
onDataChanged(mSettings.getPunctuationLevel(), mSettings.getPunctuationCharacters());
}
private void onDataChanged(int level, String characters) {
switch (level) {
case SpeechSynthesis.PUNCT_ALL:
callChangeListener(getContext().getText(R.string.punctuation_all));
break;
case SpeechSynthesis.PUNCT_SOME:
if (characters == null || characters.isEmpty()) {
callChangeListener(getContext().getText(R.string.punctuation_none));
} else {
callChangeListener(String.format(getContext().getText(R.string.punctuation_custom_fmt).toString(), characters));
}
break;
case SpeechSynthesis.PUNCT_NONE:
callChangeListener(getContext().getText(R.string.punctuation_none));
break;
}
}
@Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
mAll = (RadioButton)root.findViewById(R.id.all);
mCustom = (RadioButton)root.findViewById(R.id.custom);
mPunctuationCharacters = (EditText)root.findViewById(R.id.punctuation_characters);
return root;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
if (mSettings.getPunctuationLevel() == SpeechSynthesis.PUNCT_ALL) {
mAll.toggle();
} else {
mCustom.toggle();
}
mPunctuationCharacters.setText(mSettings.getPunctuationCharacters());
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Editable text = mPunctuationCharacters.getText();
String characters = null;
int level;
if (text != null) {
characters = text.toString();
}
if (characters == null || characters.isEmpty()) {
level = mAll.isChecked() ? SpeechSynthesis.PUNCT_ALL : SpeechSynthesis.PUNCT_NONE;
} else {
level = mAll.isChecked() ? SpeechSynthesis.PUNCT_ALL : SpeechSynthesis.PUNCT_SOME;
}
onDataChanged(level, characters);
if (shouldCommit()) {
SharedPreferences.Editor editor = getEditor();
if (editor != null) {
editor.putString(VoiceSettings.PREF_PUNCTUATION_CHARACTERS, characters);
editor.putString(VoiceSettings.PREF_PUNCTUATION_LEVEL, Integer.toString(level));
editor.commit();
}
}
break;
}
super.onClick(dialog, which);
}
}
@@ -193,14 +193,18 @@ public class SpeechSynthesis {
}
}
public void setPunctuationCharacters(String characters) {
nativeSetPunctuationCharacters(characters);
}
/** Don't announce any punctuation characters. */
public static int PUNCT_NONE = 0;
public static final int PUNCT_NONE = 0;
/** Announce every punctuation character. */
public static int PUNCT_ALL = 1;
public static final int PUNCT_ALL = 1;
/** Announce some of the punctuation characters. */
public static int PUNCT_SOME = 2;
public static final int PUNCT_SOME = 2;
public enum UnitType {
Percentage,
@@ -345,6 +349,8 @@ public class SpeechSynthesis {
private native final int nativeGetParameter(int parameter, int current);
private native final boolean nativeSetPunctuationCharacters(String characters);
private native final boolean nativeSynthesize(String text, boolean isSsml);
private native final boolean nativeStop();
@@ -241,6 +241,7 @@ public class TtsService extends TextToSpeechService {
mEngine.PitchRange.setValue(settings.getPitchRange());
mEngine.Volume.setValue(settings.getVolume());
mEngine.Punctuation.setValue(settings.getPunctuationLevel());
mEngine.setPunctuationCharacters(settings.getPunctuationCharacters());
mEngine.synthesize(text, text.startsWith("<speak"));
}
@@ -97,6 +97,18 @@ public class TtsSettingsActivity extends PreferenceActivity {
}
}
private static Preference createSpeakPunctuationPreference(Context context, SpeechSynthesis engine, int titleRes) {
final String title = context.getString(titleRes);
final SpeakPunctuationPreference pref = new SpeakPunctuationPreference(context);
pref.setTitle(title);
pref.setDialogTitle(title);
pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
pref.setPersistent(true);
pref.setVoiceSettings(new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(context), engine));
return pref;
}
private static Preference createPreference(Context context, SpeechSynthesis.Parameter parameter, String key, int titleRes) {
final String title = context.getString(titleRes);
final int defaultValue = parameter.getDefaultValue();
@@ -165,6 +177,7 @@ public class TtsSettingsActivity extends PreferenceActivity {
SpeechSynthesis engine = new SpeechSynthesis(context, null);
group.addPreference(createSpeakPunctuationPreference(context, engine, R.string.espeak_speak_punctuation));
group.addPreference(createPreference(context, engine.Rate, "espeak_rate", R.string.setting_default_rate));
group.addPreference(createPreference(context, engine.Pitch, "espeak_pitch", R.string.setting_default_pitch));
group.addPreference(createPreference(context, engine.PitchRange, "espeak_pitch_range", R.string.espeak_pitch_range));
@@ -189,6 +202,8 @@ public class TtsSettingsActivity extends PreferenceActivity {
final SeekBarPreference seekBarPreference = (SeekBarPreference) preference;
String formatter = seekBarPreference.getFormatter();
summary = String.format(formatter, (String)newValue);
} else {
summary = (String)newValue;
}
preference.setSummary(summary);
}