Move the Preference implementations to com.reecedunn.espeak.preference.

This commit is contained in:
Reece H. Dunn
2013-09-16 15:40:05 +01:00
parent 9e99b70965
commit ed305acc29
4 changed files with 18 additions and 3 deletions
@@ -0,0 +1,221 @@
/*
* 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.preference;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import com.reecedunn.espeak.R;
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
private SeekBar mSeekBar;
private TextView mValueText;
private int mOldProgress = 0;
private int mProgress = 0;
private int mDefaultValue = 0;
private int mMin = 0;
private int mMax = 100;
private String mFormatter = "%s";
public void setProgress(int progress) {
mProgress = progress;
String text = Integer.toString(mProgress);
callChangeListener(text);
// Update the last saved value to the so it can be restored later if
// the user cancels the dialog. This needs to be done here as well
// as the onProgressChanged handler as the SeekBar will not be
// initialized at this point.
mOldProgress = mProgress;
}
public int getProgress() {
return mProgress;
}
public void setDefaultValue(int defaultValue) {
mDefaultValue = defaultValue;
}
public int getDefaultValue() {
return mDefaultValue;
}
public void setMin(int min) {
mMin = min;
}
public int getMin() {
return mMin;
}
public void setMax(int max) {
mMax = max;
}
public int getMax() {
return mMax;
}
public void setFormatter(String formatter) {
mFormatter = formatter;
}
public String getFormatter() {
return mFormatter;
}
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.seekbar_preference);
setLayoutResource(R.layout.information_view);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
public SeekBarPreference(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public SeekBarPreference(Context context)
{
this(context, null);
}
private void persistSettings(int progress) {
mProgress = progress;
String text = Integer.toString(mProgress);
callChangeListener(text);
if (shouldCommit()) {
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey(), text);
editor.commit();
}
}
@Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
mSeekBar = (SeekBar)root.findViewById(R.id.seekBar);
mValueText = (TextView)root.findViewById(R.id.valueText);
Button reset = (Button)root.findViewById(R.id.resetToDefault);
reset.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
int defaultValue = getDefaultValue();
mSeekBar.setProgress(defaultValue - mMin);
// Persist the value here to ensure that eSpeak is using the
// new value the next time e.g. TalkBack reads part of the UI.
persistSettings(defaultValue);
}
});
return root;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mSeekBar.setOnSeekBarChangeListener(this);
mSeekBar.setMax(mMax - mMin);
mSeekBar.setProgress(mProgress - mMin);
// Update the last saved value to the so it can be restored later if
// the user cancels the dialog.
mOldProgress = mProgress;
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Update the last saved value so this will be persisted when
// the dialog is dismissed.
mOldProgress = mSeekBar.getProgress() + mMin;
break;
}
super.onClick(dialog, which);
}
@Override
public void onDismiss(DialogInterface dialog) {
// There are 3 ways to dismiss a dialog:
// 1. Pressing the OK (positive) button.
// 2. Pressing the Cancel (negative) button.
// 3. Pressing the Back button.
//
// For [1], the new value needs to be persisted. For [2] and [3], the
// old value needs to be persisted (so the last saved value is
// restored). As there is no easy way to override the Dialog's back
// button pressed handler, the following approach is used:
//
// 1. If the user presses the OK button, the last saved value is
// updated to be the new value (see the onClick handler).
//
// 2. In all cases, the last saved value is persisted when the dialog
// is closed (in this onDismiss handler).
persistSettings(mOldProgress);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// This callback gets called frequently when the user is moving the
// slider, so constantly persisting the seeker value will be annoying.
//
// If the value is being set programatically, persisting the seeker
// value here will cause the speech rate to be set to 80 WPM (via the
// onBindDialogView handler).
String text = String.format(getFormatter(), Integer.toString(progress + mMin));
mValueText.setText(text);
mSeekBar.setContentDescription(text);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// After the user has let go of the slider, the new value is
// persisted to ensure that eSpeak is using the new value the
// next time e.g. TalkBack reads part of the UI.
persistSettings(mSeekBar.getProgress() + mMin);
}
}
@@ -0,0 +1,145 @@
/*
* 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.preference;
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;
import com.reecedunn.espeak.R;
import com.reecedunn.espeak.SpeechSynthesis;
import com.reecedunn.espeak.VoiceSettings;
public class SpeakPunctuationPreference extends DialogPreference {
private RadioButton mAll;
private RadioButton mCustom;
private RadioButton mNone;
private EditText mPunctuationCharacters;
private VoiceSettings mSettings;
public SpeakPunctuationPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.speak_punctuation_preference);
setLayoutResource(R.layout.information_view);
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);
mNone = (RadioButton)root.findViewById(R.id.none);
mPunctuationCharacters = (EditText)root.findViewById(R.id.punctuation_characters);
return root;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
switch (mSettings.getPunctuationLevel()) {
case SpeechSynthesis.PUNCT_ALL:
mAll.toggle();
break;
case SpeechSynthesis.PUNCT_SOME:
mCustom.toggle();
break;
case SpeechSynthesis.PUNCT_NONE:
mNone.toggle();
break;
}
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 (mNone.isChecked()) {
level = SpeechSynthesis.PUNCT_NONE;
} else 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);
}
}
@@ -0,0 +1,301 @@
/*
* 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.preference;
import android.app.Activity;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import com.reecedunn.espeak.R;
import com.reecedunn.espeak.ResourceIdListAdapter;
import com.reecedunn.espeak.VoiceSettings;
import com.reecedunn.espeak.VoiceVariant;
import java.util.List;
public class VoiceVariantPreference extends DialogPreference {
private Spinner mCategory;
private Spinner mVariant;
private int mCategoryIndex = 0;
private int mVariantIndex = 0;
static class ViewHolder
{
public TextView text;
}
private class VariantData {
private final int name;
private final Object arg;
private final VoiceVariant variant;
protected VariantData(int name, String variant) {
this(name, null, variant);
}
protected VariantData(int name, Object arg, String variant) {
this.name = name;
this.arg = arg;
this.variant = VoiceVariant.parseVoiceVariant(variant);
}
public String getDisplayName(Context context) {
String text = context.getText(name).toString();
if (arg == null) {
return text;
}
return String.format(text, arg);
}
public VoiceVariant getVariant() {
return variant;
}
}
public class VariantDataListAdapter extends ArrayAdapter<VariantData>
{
private final LayoutInflater mInflater;
public VariantDataListAdapter(Activity context, VariantData[] resources)
{
super(context, android.R.layout.simple_list_item_1, resources);
mInflater = context.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
holder = new ViewHolder();
holder.text = (TextView)convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText(getItem(position).getDisplayName(getContext()));
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getView(position, convertView, parent);
}
}
private Integer[] categories = {
R.string.variant_male,
R.string.variant_female,
R.string.variant_klatt,
R.string.variant_nvda,
R.string.variant_young,
R.string.variant_old,
R.string.variant_croak,
R.string.variant_whisper,
};
private VariantData[][] variants = {
{ // Male
new VariantData(R.string.variant_default, "male"),
new VariantData(R.string.variant_n, 1, "m1"),
new VariantData(R.string.variant_n, 2, "m2"),
new VariantData(R.string.variant_n, 3, "m3"),
new VariantData(R.string.variant_n, 4, "m4"),
new VariantData(R.string.variant_n, 5, "m5"),
new VariantData(R.string.variant_n, 6, "m6"),
new VariantData(R.string.variant_n, 7, "m7"),
},{ // Female
new VariantData(R.string.variant_default, "female"),
new VariantData(R.string.variant_n, 1, "f1"),
new VariantData(R.string.variant_n, 2, "f2"),
new VariantData(R.string.variant_n, 3, "f3"),
new VariantData(R.string.variant_n, 4, "f4"),
new VariantData(R.string.variant_n, 5, "f5"),
},{ // Klatt
new VariantData(R.string.variant_n, 1, "klatt"),
new VariantData(R.string.variant_n, 2, "klatt2"),
new VariantData(R.string.variant_n, 3, "klatt3"),
new VariantData(R.string.variant_n, 4, "klatt4"),
},{ // NVDA
new VariantData(R.string.variant_andy, "Andy"),
new VariantData(R.string.variant_annie, "Annie"),
new VariantData(R.string.variant_anxious_andy, "AnxiousAndy"),
new VariantData(R.string.variant_aunty, "aunty"),
new VariantData(R.string.variant_boris, "boris"),
new VariantData(R.string.variant_denis, "Denis"),
new VariantData(R.string.variant_gene, "Gene"),
new VariantData(R.string.variant_gene2, "Gene2"),
new VariantData(R.string.variant_iven, "iven"),
new VariantData(R.string.variant_iven2, "iven2"),
new VariantData(R.string.variant_iven3, "iven3"),
new VariantData(R.string.variant_jacky, "Jacky"),
new VariantData(R.string.variant_john, "john"),
new VariantData(R.string.variant_kaukovalta, "kaukovalta"),
new VariantData(R.string.variant_lee, "Lee"),
new VariantData(R.string.variant_linda, "linda"),
new VariantData(R.string.variant_mario, "Mario"),
new VariantData(R.string.variant_max, "max"),
new VariantData(R.string.variant_michael, "Michael"),
new VariantData(R.string.variant_michel, "michel"),
new VariantData(R.string.variant_mr_serious, "Mr serious"),
new VariantData(R.string.variant_norbert, "norbert"),
new VariantData(R.string.variant_quincy, "quincy"),
new VariantData(R.string.variant_rob, "rob"),
new VariantData(R.string.variant_robert, "robert"),
new VariantData(R.string.variant_storm, "Storm"),
new VariantData(R.string.variant_travis, "travis"),
new VariantData(R.string.variant_tweaky, "Tweaky"),
new VariantData(R.string.variant_zac, "zac"),
},{ // Young
new VariantData(R.string.variant_male, "male-young"),
new VariantData(R.string.variant_female, "female-young"),
},{ // Old
new VariantData(R.string.variant_male, "male-old"),
new VariantData(R.string.variant_female, "female-old"),
},{ // Croak
new VariantData(R.string.variant_male, "croak"),
},{ // Whisper
new VariantData(R.string.variant_male, "whisper"),
new VariantData(R.string.variant_female, "whisperf"),
},
};
public VoiceVariantPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.voice_variant_preference);
setLayoutResource(R.layout.information_view);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
public VoiceVariantPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VoiceVariantPreference(Context context) {
this(context, null);
}
public void setVoiceVariant(VoiceVariant variant) {
for (int i = 0; i < variants.length; ++i) {
VariantData[] items = variants[i];
for (int j = 0; j < items.length; ++j) {
if (items[j].getVariant().equals(variant)) {
mCategoryIndex = i;
mVariantIndex = j;
onDataChanged();
return;
}
}
}
onDataChanged();
}
@Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
mCategory = (Spinner)root.findViewById(R.id.category);
mVariant = (Spinner)root.findViewById(R.id.variant);
return root;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
// Cache the indices so they don't get overwritten by the OnItemSelectedListener handlers.
final int category = mCategoryIndex;
final int variant = mVariantIndex;
mCategory.setAdapter(new ResourceIdListAdapter((Activity)getContext(), categories));
mCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
private boolean mInitializing = true;
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
mVariant.setAdapter(new VariantDataListAdapter((Activity) getContext(), variants[position]));
if (mInitializing) {
mVariant.setSelection(variant);
mInitializing = false;
}
mCategoryIndex = position;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
mVariant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
mVariantIndex = position;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
mCategory.setSelection(category);
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
onDataChanged();
if (shouldCommit()) {
SharedPreferences.Editor editor = getEditor();
if (editor != null) {
VoiceVariant variant = variants[mCategoryIndex][mVariantIndex].getVariant();
editor.putString(VoiceSettings.PREF_VARIANT, variant.toString());
editor.commit();
}
}
break;
}
super.onClick(dialog, which);
}
private void onDataChanged() {
Context context = getContext();
CharSequence category = context.getText(categories[mCategoryIndex]);
CharSequence variant = variants[mCategoryIndex][mVariantIndex].getDisplayName(context);
callChangeListener(String.format("%s (%s)", category, variant));
}
}