Browse Source

SeekBarPreference: update the persisted value when the SeekBar changes value

The idea here is that whenever the user changes one of the parameter
values via the SeekBar control, it is reflected immediately by eSpeak.
This is similar to how e.g. the brightness preference UI works.
However, instead of updating every time the value changes while the
slider is being moved, it is updated once the touch interaction is
finished.

Also, if the user Cancels or exits the dialog with the back button,
the old setting value is restored.
master
Reece H. Dunn 12 years ago
parent
commit
dde42239f3
1 changed files with 68 additions and 9 deletions
  1. 68
    9
      android/src/com/reecedunn/espeak/SeekBarPreference.java

+ 68
- 9
android/src/com/reecedunn/espeak/SeekBarPreference.java View File

private SeekBar mSeekBar; private SeekBar mSeekBar;
private TextView mValueText; private TextView mValueText;


private int mOldProgress = 0;
private int mProgress = 0; private int mProgress = 0;
private int mDefaultValue = 0; private int mDefaultValue = 0;
private int mMin = 0; private int mMin = 0;
mProgress = progress; mProgress = progress;
String text = Integer.toString(mProgress); String text = Integer.toString(mProgress);
callChangeListener(text); 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() { public int getProgress() {
this(context, null); 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 @Override
protected View onCreateDialogView() { protected View onCreateDialogView() {
View root = super.onCreateDialogView(); View root = super.onCreateDialogView();
@Override @Override
public void onClick(View v) 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; return root;
mSeekBar.setOnSeekBarChangeListener(this); mSeekBar.setOnSeekBarChangeListener(this);
mSeekBar.setMax(mMax - mMin); mSeekBar.setMax(mMax - mMin);
mSeekBar.setProgress(mProgress - 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 @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: 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; break;
} }
super.onClick(dialog, which); 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 @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 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)); String text = String.format(getFormatter(), Integer.toString(progress + mMin));
mValueText.setText(text); mValueText.setText(text);
mSeekBar.setContentDescription(text); mSeekBar.setContentDescription(text);
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) 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);
} }
} }

Loading…
Cancel
Save