diff --git a/android/src/com/reecedunn/espeak/SeekBarPreference.java b/android/src/com/reecedunn/espeak/SeekBarPreference.java index a0fc3391..8a0d6b9b 100644 --- a/android/src/com/reecedunn/espeak/SeekBarPreference.java +++ b/android/src/com/reecedunn/espeak/SeekBarPreference.java @@ -31,6 +31,7 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee private SeekBar mSeekBar; private TextView mValueText; + private int mOldProgress = 0; private int mProgress = 0; private int mDefaultValue = 0; private int mMin = 0; @@ -41,6 +42,13 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee 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() { @@ -97,6 +105,17 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee 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(); @@ -108,7 +127,13 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee @Override public void onClick(View v) { - mSeekBar.setProgress(getDefaultValue() - mMin); + 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; @@ -120,28 +145,57 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee 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: - mProgress = mSeekBar.getProgress() + mMin; - String text = Integer.toString(mProgress); - callChangeListener(text); - if (shouldCommit()) { - SharedPreferences.Editor editor = getEditor(); - editor.putString(getKey(), text); - editor.commit(); - } + // 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); @@ -155,5 +209,10 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee @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); } }