Configure SeekbarPreference via attr

This commit is contained in:
Adrian Ulrich 2015-10-05 10:45:15 +02:00
parent 85a8d0d4ed
commit 79048e7619
5 changed files with 96 additions and 39 deletions

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SeekBarPreference">
<attr name="sbpMaxValue" format="integer"/>
<attr name="sbpSummaryValueDivider" format="float"/>
<attr name="sbpSummaryValueAddition" format="float"/>
<attr name="sbpSummaryText" format="string"/>
<attr name="sbpSummaryFormat" format="string"/>
</declare-styleable>
</resources>

View File

@ -22,6 +22,7 @@ THE SOFTWARE.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:vanilla="http://schemas.android.com/apk/res/ch.blinkenlights.android.vanilla"
android:persistent="true">
<PreferenceScreen
android:fragment="ch.blinkenlights.android.vanilla.PreferencesActivity$EqualizerFragment"
@ -33,10 +34,12 @@ THE SOFTWARE.
android:summary="@string/replaygain_summary" />
<ch.blinkenlights.android.vanilla.SeekBarPreference
android:key="volume_during_ducking"
android:negativeButtonText="@string/restore_default"
android:dialogLayout="@layout/shake_pref"
android:title="@string/volume_during_ducking_title"
android:defaultValue="50" />
android:defaultValue="50"
vanilla:sbpSummaryText="@string/volume_during_ducking_summary"
vanilla:sbpSummaryFormat="%s %.0f%%"
vanilla:sbpMaxValue="100"/>
<CheckBoxPreference
android:key="media_button"
android:title="@string/media_button_title"

View File

@ -22,6 +22,7 @@ THE SOFTWARE.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:vanilla="http://schemas.android.com/apk/res/ch.blinkenlights.android.vanilla"
android:persistent="true">
<CheckBoxPreference
android:key="enable_track_replaygain"
@ -38,13 +39,23 @@ THE SOFTWARE.
android:negativeButtonText="@null"
android:dialogLayout="@layout/shake_pref"
android:title="@string/replaygain_bump_title"
android:defaultValue="75" />
android:defaultValue="75"
vanilla:sbpMaxValue="150"
vanilla:sbpSummaryFormat="%s%+.1fdB"
vanilla:sbpSummaryValueAddition="-75"
vanilla:sbpSummaryValueDivider="5"
/>
<ch.blinkenlights.android.vanilla.SeekBarPreference
android:key="replaygain_untagged_debump"
android:negativeButtonText="@null"
android:dialogLayout="@layout/shake_pref"
android:title="@string/replaygain_untagged_debump_title"
android:defaultValue="150" />
android:defaultValue="150"
vanilla:sbpMaxValue="150"
vanilla:sbpSummaryText="@string/replaygain_untagged_debump_summary"
vanilla:sbpSummaryFormat="%s %.1fdB"
vanilla:sbpSummaryValueAddition="-150"
vanilla:sbpSummaryValueDivider="10" />
<Preference
android:selectable="false"

View File

@ -22,6 +22,7 @@ THE SOFTWARE.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:vanilla="http://schemas.android.com/apk/res/ch.blinkenlights.android.vanilla"
android:persistent="true">
<CheckBoxPreference
android:key="enable_shake"
@ -42,5 +43,7 @@ THE SOFTWARE.
android:dialogLayout="@layout/shake_pref"
android:title="@string/shake_threshold_title"
android:defaultValue="80"
vanilla:sbpMaxValue="300"
vanilla:sbpSummaryValueDivider="10"
android:dependency="enable_shake" />
</PreferenceScreen>

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
* Copyright (C) 2015 Adrian Ulrich <adrian@blinkenlights.ch>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -38,14 +39,36 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
* The current value.
*/
private int mValue;
/**
* Our context (needed for getResources())
*/
private Context mContext;
/**
* TextView to display current threshold.
* The maximum value to use
*/
private int mMaxValue;
/**
* The initially configured value (updated on dialog close)
*/
private int mInitialValue;
/**
* The format to use for the summary
*/
private String mSummaryFormat;
/**
* The text to use in the summary
*/
private String mSummaryText;
/**
* Add given value to summary value
*/
private float mSummaryValueAddition;
/**
* Divide summary value by this value
*/
private float mSummaryValueDivider;
/**
* TextView to display current summary
*/
private TextView mValueText;
@ -53,8 +76,29 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
{
super(context, attrs);
mContext = context;
initDefaults(attrs);
}
/**
* Configures the view using the SeekBarPreference XML attributes
*
* @param attrs An AttributeSet
*/
private void initDefaults(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SeekBarPreference);
mMaxValue = a.getInteger(R.styleable.SeekBarPreference_sbpMaxValue, 0);
mSummaryValueDivider = a.getFloat(R.styleable.SeekBarPreference_sbpSummaryValueDivider, 0f);
mSummaryValueAddition = a.getFloat(R.styleable.SeekBarPreference_sbpSummaryValueAddition, 0f);
mSummaryFormat = a.getString(R.styleable.SeekBarPreference_sbpSummaryFormat);
mSummaryFormat = (mSummaryFormat == null ? "%s %.1f" : mSummaryFormat);
mSummaryText = a.getString(R.styleable.SeekBarPreference_sbpSummaryText);
mSummaryText = (mSummaryText == null ? "" : mSummaryText);
a.recycle();
}
@Override
public CharSequence getSummary()
{
@ -70,8 +114,8 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
{
mValue = restoreValue ? getPersistedInt(mValue) : (Integer)defaultValue;
}
mInitialValue = mValue = restoreValue ? getPersistedInt(mValue) : (Integer)defaultValue;
}
/**
* Create the summary for the given value.
@ -79,21 +123,15 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
* @param value The force threshold.
* @return A string representation of the threshold.
*/
private String getSummary(int value)
{
if (PrefKeys.SHAKE_THRESHOLD.equals(getKey())) {
return String.valueOf(value / 10.0f);
} else if(PrefKeys.REPLAYGAIN_BUMP.equals(getKey())) {
return String.format("%+.1fdB", 2*(value-75)/10f);
} else if(PrefKeys.REPLAYGAIN_UNTAGGED_DEBUMP.equals(getKey())) {
String summary = (String)mContext.getResources().getText(R.string.replaygain_untagged_debump_summary);
return String.format("%s %.1fdB", summary, (value-150)/10f);
} else if (PrefKeys.VOLUME_DURING_DUCKING.equals(getKey())) {
String summary = mContext.getString(R.string.volume_during_ducking_summary);
return summary + " " + value + "%";
} else {
return String.format("%d%% (%+.1fdB)", value, 20 * Math.log10(Math.pow(value / 100.0, 3)));
}
private String getSummary(int value) {
float fValue = (float)value;
if (mSummaryValueAddition != 0f)
fValue = fValue + mSummaryValueAddition;
if (mSummaryValueDivider != 0f)
fValue = fValue / mSummaryValueDivider;
return String.format(mSummaryFormat, mSummaryText, fValue);
}
@Override
@ -106,17 +144,7 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
SeekBar seekBar = (SeekBar)view.findViewById(R.id.seek_bar);
int maxValue;
String key = getKey();
if (PrefKeys.SHAKE_THRESHOLD.equals(key)) {
maxValue = 300;
} else if (PrefKeys.VOLUME_DURING_DUCKING.equals(key)) {
maxValue = 100;
} else {
maxValue = 150;
}
seekBar.setMax(maxValue);
seekBar.setMax(mMaxValue);
seekBar.setProgress(mValue);
seekBar.setOnSeekBarChangeListener(this);
@ -124,10 +152,12 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
}
@Override
protected void onDialogClosed(boolean positiveResult)
{
if(!positiveResult && PrefKeys.VOLUME_DURING_DUCKING.equals(getKey())) {
setValue(50);
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
mInitialValue = mValue;
} else {
// User aborted: Set remembered start value
setValue(mInitialValue);
}
notifyChanged();
}