-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathSeekBarPreference.java
More file actions
179 lines (153 loc) · 5.51 KB
/
Copy pathSeekBarPreference.java
File metadata and controls
179 lines (153 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package org.pocketworkstation.pckeyboard;
import java.util.Locale;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
/**
* SeekBarPreference provides a dialog for editing float-valued preferences with a slider.
*/
public class SeekBarPreference extends DialogPreference {
private TextView mMinText;
private TextView mMaxText;
private TextView mValText;
private SeekBar mSeek;
private float mMin;
private float mMax;
private float mVal;
private float mPrevVal;
private float mStep;
private boolean mAsPercent;
private boolean mLogScale;
private String mDisplayFormat;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
protected void init(Context context, AttributeSet attrs) {
setDialogLayoutResource(R.layout.seek_bar_dialog);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference);
mMin = a.getFloat(R.styleable.SeekBarPreference_minValue, 0.0f);
mMax = a.getFloat(R.styleable.SeekBarPreference_maxValue, 100.0f);
mStep = a.getFloat(R.styleable.SeekBarPreference_step, 0.0f);
mAsPercent = a.getBoolean(R.styleable.SeekBarPreference_asPercent, false);
mLogScale = a.getBoolean(R.styleable.SeekBarPreference_logScale, false);
mDisplayFormat = a.getString(R.styleable.SeekBarPreference_displayFormat);
if (a != null) {
a.recycle();
}
}
@Override
protected Float onGetDefaultValue(TypedArray a, int index) {
return a.getFloat(index, 0.0f);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
if (restorePersistedValue) {
setVal(getPersistedFloat(0.0f));
} else {
setVal((Float) defaultValue);
}
savePrevVal();
}
private String formatFloatDisplay(Float val) {
// Use current locale for format, this is for display only.
if (mAsPercent) {
return String.format("%d%%", (int) (val * 100));
}
if (mDisplayFormat != null) {
return String.format(mDisplayFormat, val);
} else {
return Float.toString(val);
}
}
private void showVal() {
mValText.setText(formatFloatDisplay(mVal));
}
protected void setVal(Float val) {
mVal = val;
}
protected void savePrevVal() {
mPrevVal = mVal;
}
protected void restoreVal() {
mVal = mPrevVal;
}
protected String getValString() {
return Float.toString(mVal);
}
private float percentToSteppedVal(int percent, float min, float max, float step, boolean logScale) {
float val;
if (logScale) {
val = (float) Math.exp(percentToSteppedVal(percent, (float) Math.log(min), (float) Math.log(max), step, false));
} else {
float delta = percent * (max - min) / 100;
if (step != 0.0f) {
delta = Math.round(delta / step) * step;
}
val = min + delta;
}
// Hack: Round number to 2 significant digits so that it looks nicer.
val = Float.valueOf(String.format(Locale.US, "%.2g", val));
return val;
}
private int getPercent(float val, float min, float max) {
return (int) (100 * (val - min) / (max - min));
}
private int getProgressVal() {
if (mLogScale) {
return getPercent((float) Math.log(mVal), (float) Math.log(mMin), (float) Math.log(mMax));
} else {
return getPercent(mVal, mMin, mMax);
}
}
@Override
protected void onBindDialogView(View view) {
mSeek = (SeekBar) view.findViewById(R.id.seekBarPref);
mMinText = (TextView) view.findViewById(R.id.seekMin);
mMaxText = (TextView) view.findViewById(R.id.seekMax);
mValText = (TextView) view.findViewById(R.id.seekVal);
showVal();
mMinText.setText(formatFloatDisplay(mMin));
mMaxText.setText(formatFloatDisplay(mMax));
mSeek.setProgress(getProgressVal());
mSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
float newVal = percentToSteppedVal(progress, mMin, mMax, mStep, mLogScale);
if (newVal != mVal) {
onChange(newVal);
}
setVal(newVal);
mSeek.setProgress(getProgressVal());
}
showVal();
}
});
super.onBindDialogView(view);
}
public void onChange(float val) {
// override in subclasses
}
@Override
public CharSequence getSummary() {
return formatFloatDisplay(mVal);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (!positiveResult) {
restoreVal();
return;
}
if (shouldPersist()) {
persistFloat(mVal);
savePrevVal();
}
notifyChanged();
}
}