Browse Source

Hook up the volume parameter to a slider preference dialog.

master
Reece H. Dunn 12 years ago
parent
commit
ac6099ebf1

+ 14
- 0
android/res/layout/seekbar_preference.xml View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="20dp" />

</LinearLayout>

+ 1
- 0
android/res/values/strings.xml View File

@@ -132,5 +132,6 @@
<string name="speak">Speak</string>
<string name="ssml">Load SSML Template</string>
<string name="synthesis_demo">Enter text to speak:</string>
<string name="espeak_volume">Volume</string>

</resources>

+ 110
- 0
android/src/com/reecedunn/espeak/SeekBarPreference.java View File

@@ -0,0 +1,110 @@
/*
* 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.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;

public class SeekBarPreference extends DialogPreference
{
private SeekBar mSeekBar;

private int mProgress = 0;
private int mMin = 0;
private int mMax = 100;

public void setProgress(int progress) {
mProgress = progress + mMin;
String text = Integer.toString(mProgress);
callChangeListener(text);
}

public int getProgress() {
return mProgress;
}

public void setMin(int min) {
mProgress -= mMin;
mMin = min;
mProgress += mMin;
String text = Integer.toString(mProgress);
callChangeListener(text);
}

public int getMin() {
return mMin;
}

public void setMax(int max) {
mMax = max;
}

public int getMax() {
return mMax;
}

public SeekBarPreference(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setDialogLayoutResource(R.layout.seekbar_preference);
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);
}

@Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
mSeekBar = (SeekBar)root.findViewById(R.id.seekBar);
return root;
}

@Override
protected void onBindDialogView(View view) {
mSeekBar.setMax(mMax - mMin);
mSeekBar.setProgress(mProgress + mMin);
}

@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mProgress = mSeekBar.getProgress() + mMin;
String text = Integer.toString(mProgress);
callChangeListener(text);
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putString(getKey(), text);
editor.commit();
break;
}
super.onClick(dialog, which);
}
}

+ 1
- 0
android/src/com/reecedunn/espeak/TtsService.java View File

@@ -224,6 +224,7 @@ public class TtsService extends TextToSpeechService {
mEngine.setVoice(mMatchingVoice, null, gender, SpeechSynthesis.AGE_ANY);
mEngine.Rate.setValue(rate, request.getSpeechRate());
mEngine.Pitch.setValue(pitch, request.getPitch());
mEngine.Volume.setValue(getPreferenceValue("espeak_volume", mEngine.Volume.getDefaultValue()));
mEngine.synthesize(text, text.startsWith("<speak"));
}


+ 54
- 21
android/src/com/reecedunn/espeak/TtsSettingsActivity.java View File

@@ -17,6 +17,8 @@

package com.reecedunn.espeak;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
@@ -25,6 +27,7 @@ import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;

public class TtsSettingsActivity extends PreferenceActivity {
@Override
@@ -41,7 +44,7 @@ public class TtsSettingsActivity extends PreferenceActivity {
else
{
addPreferencesFromResource(R.xml.preferences);
fixListSummaries(getPreferenceScreen());
createPreferences(TtsSettingsActivity.this, getPreferenceScreen());
}
}

@@ -51,16 +54,40 @@ public class TtsSettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);
fixListSummaries(getPreferenceScreen());
createPreferences(getActivity(), getPreferenceScreen());
}
}

private static Preference createPreference(Context context, SpeechSynthesis.Parameter parameter, String key, int titleRes) {
final String title = context.getString(titleRes);

final SeekBarPreference pref = new SeekBarPreference(context);
pref.setTitle(title);
pref.setDialogTitle(title);
pref.setKey(key);
pref.setOnPreferenceChangeListener(mOnPreferenceChanged);
pref.setPersistent(true);

pref.setMin(parameter.getMinValue());
pref.setMax(parameter.getMaxValue());

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String prefString = prefs.getString(key, null);
if (prefString == null) {
pref.setProgress(parameter.getDefaultValue());
} else {
pref.setProgress(Integer.parseInt(prefString));
}

return pref;
}

/**
* Since the "%s" summary is currently broken, this sets the preference
* change listener for all {@link ListPreference} views to fill in the
* summary with the current entry value.
*/
private static void fixListSummaries(PreferenceGroup group) {
private static void createPreferences(Context context, PreferenceGroup group) {
if (group == null) {
return;
}
@@ -71,34 +98,40 @@ public class TtsSettingsActivity extends PreferenceActivity {
final Preference preference = group.getPreference(i);

if (preference instanceof PreferenceGroup) {
fixListSummaries((PreferenceGroup) preference);
createPreferences(null, (PreferenceGroup) preference);
} else if (preference instanceof ListPreference) {
preference.setOnPreferenceChangeListener(mPreferenceChangeListener);
preference.setOnPreferenceChangeListener(mOnPreferenceChanged);
}
}

if (context == null) {
return;
}

SpeechSynthesis engine = new SpeechSynthesis(context, null);

group.addPreference(createPreference(context, engine.Volume, "espeak_volume", R.string.espeak_volume));
}

/**
* Listens for preference changes and updates the summary to reflect the
* current setting. This shouldn't be necessary, since preferences are
* supposed to automatically do this when the summary is set to "%s".
*/
private static final OnPreferenceChangeListener mPreferenceChangeListener =
private static final OnPreferenceChangeListener mOnPreferenceChanged =
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof ListPreference && newValue instanceof String) {
final ListPreference listPreference = (ListPreference) preference;
final int index = listPreference.findIndexOfValue((String) newValue);
final CharSequence[] entries = listPreference.getEntries();

if (index >= 0 && index < entries.length) {
preference.setSummary(entries[index].toString().replaceAll("%", "%%"));
} else {
preference.setSummary("");
if (newValue instanceof String) {
String summary = "";
if (preference instanceof ListPreference) {
final ListPreference listPreference = (ListPreference) preference;
final int index = listPreference.findIndexOfValue((String) newValue);
final CharSequence[] entries = listPreference.getEntries();

if (index >= 0 && index < entries.length) {
summary = entries[index].toString();
}
} else if (preference instanceof SeekBarPreference) {
summary = (String)newValue;
}
preference.setSummary(summary.replaceAll("%", "%%"));
}

return true;
}
};

Loading…
Cancel
Save