am 8028c1c5: Merge "SeekBarDialogPreference supports minValue and stepValue"

* commit '8028c1c523b2b7a83ca3f887818bdf9399f20f9b':
  SeekBarDialogPreference supports minValue and stepValue
main
Tadashi G. Takaoka 2013-01-21 23:42:19 -08:00 committed by Android Git Automerger
commit 0390acf1ba
2 changed files with 33 additions and 7 deletions

View File

@ -435,5 +435,7 @@
<declare-styleable name="SeekBarDialogPreference"> <declare-styleable name="SeekBarDialogPreference">
<attr name="valueFormatText" format="reference" /> <attr name="valueFormatText" format="reference" />
<attr name="maxValue" format="integer" /> <attr name="maxValue" format="integer" />
<attr name="minValue" format="integer" />
<attr name="stepValue" format="integer" />
</declare-styleable> </declare-styleable>
</resources> </resources>

View File

@ -37,6 +37,8 @@ public final class SeekBarDialogPreference extends DialogPreference
private final int mValueFormatResId; private final int mValueFormatResId;
private final int mMaxValue; private final int mMaxValue;
private final int mMinValue;
private final int mStepValue;
private TextView mValueView; private TextView mValueView;
private SeekBar mSeekBar; private SeekBar mSeekBar;
@ -49,6 +51,8 @@ public final class SeekBarDialogPreference extends DialogPreference
attrs, R.styleable.SeekBarDialogPreference, 0, 0); attrs, R.styleable.SeekBarDialogPreference, 0, 0);
mValueFormatResId = a.getResourceId(R.styleable.SeekBarDialogPreference_valueFormatText, 0); mValueFormatResId = a.getResourceId(R.styleable.SeekBarDialogPreference_valueFormatText, 0);
mMaxValue = a.getInt(R.styleable.SeekBarDialogPreference_maxValue, 0); mMaxValue = a.getInt(R.styleable.SeekBarDialogPreference_maxValue, 0);
mMinValue = a.getInt(R.styleable.SeekBarDialogPreference_minValue, 0);
mStepValue = a.getInt(R.styleable.SeekBarDialogPreference_stepValue, 0);
a.recycle(); a.recycle();
setDialogLayoutResource(R.layout.seek_bar_dialog); setDialogLayoutResource(R.layout.seek_bar_dialog);
} }
@ -70,22 +74,42 @@ public final class SeekBarDialogPreference extends DialogPreference
protected View onCreateDialogView() { protected View onCreateDialogView() {
final View view = super.onCreateDialogView(); final View view = super.onCreateDialogView();
mSeekBar = (SeekBar)view.findViewById(R.id.seek_bar_dialog_bar); mSeekBar = (SeekBar)view.findViewById(R.id.seek_bar_dialog_bar);
mSeekBar.setMax(mMaxValue); mSeekBar.setMax(mMaxValue - mMinValue);
mSeekBar.setOnSeekBarChangeListener(this); mSeekBar.setOnSeekBarChangeListener(this);
mValueView = (TextView)view.findViewById(R.id.seek_bar_dialog_value); mValueView = (TextView)view.findViewById(R.id.seek_bar_dialog_value);
return view; return view;
} }
private int getProgressFromValue(final int value) {
return value - mMinValue;
}
private int getValueFromProgress(final int progress) {
return progress + mMinValue;
}
private int clipValue(final int value) {
final int clippedValue = Math.min(mMaxValue, Math.max(mMinValue, value));
if (mStepValue <= 1) {
return clippedValue;
}
return clippedValue - (clippedValue % mStepValue);
}
private int getClippedValueFromProgress(final int progress) {
return clipValue(getValueFromProgress(progress));
}
private void setValue(final int value, final boolean fromUser) { private void setValue(final int value, final boolean fromUser) {
mValueView.setText(getValueText(value)); mValueView.setText(getValueText(value));
if (!fromUser) { if (!fromUser) {
mSeekBar.setProgress(value); mSeekBar.setProgress(getProgressFromValue(value));
} }
} }
@Override @Override
protected void onBindDialogView(final View view) { protected void onBindDialogView(final View view) {
setValue(mValueProxy.readValue(getKey()), false /* fromUser */); setValue(clipValue(mValueProxy.readValue(getKey())), false /* fromUser */);
} }
@Override @Override
@ -99,18 +123,18 @@ public final class SeekBarDialogPreference extends DialogPreference
public void onClick(final DialogInterface dialog, final int which) { public void onClick(final DialogInterface dialog, final int which) {
super.onClick(dialog, which); super.onClick(dialog, which);
if (which == DialogInterface.BUTTON_NEUTRAL) { if (which == DialogInterface.BUTTON_NEUTRAL) {
setValue(mValueProxy.readDefaultValue(getKey()), false /* fromUser */); setValue(clipValue(mValueProxy.readDefaultValue(getKey())), false /* fromUser */);
} }
if (which != DialogInterface.BUTTON_NEGATIVE) { if (which != DialogInterface.BUTTON_NEGATIVE) {
setSummary(mValueView.getText()); setSummary(mValueView.getText());
mValueProxy.writeValue(mSeekBar.getProgress(), getKey()); mValueProxy.writeValue(getClippedValueFromProgress(mSeekBar.getProgress()), getKey());
} }
} }
@Override @Override
public void onProgressChanged(final SeekBar seekBar, final int progress, public void onProgressChanged(final SeekBar seekBar, final int progress,
final boolean fromUser) { final boolean fromUser) {
setValue(progress, fromUser); setValue(getClippedValueFromProgress(progress), fromUser);
} }
@Override @Override
@ -118,6 +142,6 @@ public final class SeekBarDialogPreference extends DialogPreference
@Override @Override
public void onStopTrackingTouch(final SeekBar seekBar) { public void onStopTrackingTouch(final SeekBar seekBar) {
mValueProxy.feedbackValue(seekBar.getProgress()); mValueProxy.feedbackValue(getClippedValueFromProgress(seekBar.getProgress()));
} }
} }