SeekBarDialog supports neutral and dismiss listener

Change-Id: I652fd1a383ef4074c04d03a37ebc779d229474e6
main
Tadashi G. Takaoka 2013-01-10 11:55:41 +09:00
parent 95e3008d25
commit 88df3d9239
2 changed files with 47 additions and 11 deletions

View File

@ -30,6 +30,8 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
public interface Listener { public interface Listener {
public void onPositiveButtonClick(final SeekBarDialog dialog); public void onPositiveButtonClick(final SeekBarDialog dialog);
public void onNegativeButtonClick(final SeekBarDialog dialog); public void onNegativeButtonClick(final SeekBarDialog dialog);
public void onNeutralButtonClick(final SeekBarDialog dialog);
public void onDismiss(final SeekBarDialog dialog);
public void onProgressChanged(final SeekBarDialog dialog); public void onProgressChanged(final SeekBarDialog dialog);
public void onStartTrackingTouch(final SeekBarDialog dialog); public void onStartTrackingTouch(final SeekBarDialog dialog);
public void onStopTrackingTouch(final SeekBarDialog dialog); public void onStopTrackingTouch(final SeekBarDialog dialog);
@ -39,7 +41,11 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
@Override @Override
public void onPositiveButtonClick(final SeekBarDialog dialog) {} public void onPositiveButtonClick(final SeekBarDialog dialog) {}
@Override @Override
public void onNegativeButtonClick(final SeekBarDialog dialog) { dialog.dismiss(); } public void onNegativeButtonClick(final SeekBarDialog dialog) {}
@Override
public void onNeutralButtonClick(final SeekBarDialog dialog) {}
@Override
public void onDismiss(final SeekBarDialog dialog) {}
@Override @Override
public void onProgressChanged(final SeekBarDialog dialog) {} public void onProgressChanged(final SeekBarDialog dialog) {}
@Override @Override
@ -63,6 +69,9 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
dialogBuilder.setView(builder.mView); dialogBuilder.setView(builder.mView);
dialogBuilder.setPositiveButton(android.R.string.ok, this); dialogBuilder.setPositiveButton(android.R.string.ok, this);
dialogBuilder.setNegativeButton(android.R.string.cancel, this); dialogBuilder.setNegativeButton(android.R.string.cancel, this);
if (builder.mNeutralButtonTextResId != 0) {
dialogBuilder.setNeutralButton(builder.mNeutralButtonTextResId, this);
}
mDialog = dialogBuilder.create(); mDialog = dialogBuilder.create();
mListener = (builder.mListener == null) ? EMPTY_ADAPTER : builder.mListener; mListener = (builder.mListener == null) ? EMPTY_ADAPTER : builder.mListener;
mValueView = (TextView)builder.mView.findViewById(R.id.seek_bar_dialog_value); mValueView = (TextView)builder.mView.findViewById(R.id.seek_bar_dialog_value);
@ -101,15 +110,21 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
} }
@Override @Override
public void onClick(final DialogInterface dialog, int which) { public void onClick(final DialogInterface dialog, final int which) {
if (which == DialogInterface.BUTTON_POSITIVE) { switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mListener.onPositiveButtonClick(this); mListener.onPositiveButtonClick(this);
return; break;
} case DialogInterface.BUTTON_NEGATIVE:
if (which == DialogInterface.BUTTON_NEGATIVE) {
mListener.onNegativeButtonClick(this); mListener.onNegativeButtonClick(this);
break;
case DialogInterface.BUTTON_NEUTRAL:
mListener.onNeutralButtonClick(this);
break;
default:
return; return;
} }
mListener.onDismiss(this);
} }
@Override @Override
@ -135,6 +150,7 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
final AlertDialog.Builder mDialogBuilder; final AlertDialog.Builder mDialogBuilder;
final View mView; final View mView;
int mNeutralButtonTextResId;
int mMaxValue; int mMaxValue;
int mValueFormatResId; int mValueFormatResId;
int mValue; int mValue;
@ -150,8 +166,14 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
return this; return this;
} }
public Builder setNeutralButtonText(final int resId) {
mNeutralButtonTextResId = resId;
return this;
}
public Builder setMaxValue(final int max) { public Builder setMaxValue(final int max) {
mMaxValue = max; mMaxValue = max;
mValue = Math.min(mValue, max);
return this; return this;
} }
@ -161,7 +183,7 @@ public final class SeekBarDialog implements DialogInterface.OnClickListener,
} }
public Builder setValue(final int value) { public Builder setValue(final int value) {
mValue = value; mValue = Math.min(value, mMaxValue);
return this; return this;
} }

View File

@ -308,10 +308,17 @@ public final class SettingsFragment extends InputMethodSettingsFragment
final Context context = getActivity(); final Context context = getActivity();
final PreferenceScreen settingsPref = mKeypressVibrationDurationSettingsPref; final PreferenceScreen settingsPref = mKeypressVibrationDurationSettingsPref;
final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() { final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() {
private void writePreference(final SharedPreferences sp, final int value) {
sp.edit().putInt(Settings.PREF_VIBRATION_DURATION_SETTINGS, value).apply();
}
@Override @Override
public void onPositiveButtonClick(final SeekBarDialog dialog) { public void onPositiveButtonClick(final SeekBarDialog dialog) {
final int ms = dialog.getValue(); writePreference(sp, dialog.getValue());
sp.edit().putInt(Settings.PREF_VIBRATION_DURATION_SETTINGS, ms).apply(); }
@Override
public void onDismiss(final SeekBarDialog dialog) {
if (settingsPref != null) { if (settingsPref != null) {
settingsPref.setSummary(dialog.getValueText()); settingsPref.setSummary(dialog.getValueText());
} }
@ -348,10 +355,17 @@ public final class SettingsFragment extends InputMethodSettingsFragment
final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final PreferenceScreen settingsPref = mKeypressSoundVolumeSettingsPref; final PreferenceScreen settingsPref = mKeypressSoundVolumeSettingsPref;
final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() { final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() {
private void writePreference(final SharedPreferences sp, final float value) {
sp.edit().putFloat(Settings.PREF_KEYPRESS_SOUND_VOLUME, value).apply();
}
@Override @Override
public void onPositiveButtonClick(final SeekBarDialog dialog) { public void onPositiveButtonClick(final SeekBarDialog dialog) {
final float volume = dialog.getValue() / PERCENT_FLOAT; writePreference(sp, dialog.getValue() / PERCENT_FLOAT);
sp.edit().putFloat(Settings.PREF_KEYPRESS_SOUND_VOLUME, volume).apply(); }
@Override
public void onDismiss(final SeekBarDialog dialog) {
if (settingsPref != null) { if (settingsPref != null) {
settingsPref.setSummary(dialog.getValueText()); settingsPref.setSummary(dialog.getValueText());
} }