summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/IntervalSeekBar.java
blob: 5fbdb81d2d4811812d1cb2f626ee1981ed3fbb97 (plain)
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
package com.android.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;

/**
 * Custom SeekBar that allows setting both a minimum and maximum value.
 * This also handles floating point values (to 2 decimal places) through
 * integer conversions.
 */
public class IntervalSeekBar extends SeekBar {
    private float mMin;
    private float mMax;
    private float mDefault;
    private float mMultiplier;

    public IntervalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray seekBarType = context.obtainStyledAttributes(attrs,
                R.styleable.IntervalSeekBar, 0, 0);

        mMax = seekBarType.getFloat(R.styleable.IntervalSeekBar_max, 1.5f);
        mMin = seekBarType.getFloat(R.styleable.IntervalSeekBar_min, 0.5f);
        mDefault = seekBarType.getFloat(R.styleable.IntervalSeekBar_defaultValue, 1.0f);

        int digits = seekBarType.getInt(R.styleable.IntervalSeekBar_digits, 0);
        mMultiplier = (float) Math.pow(10, digits);

        if (mMin > mMax) {
            float temp = mMax;
            mMax = mMin;
            mMin = temp;
        }

        setMax(convertFloatToProgress(mMax));
        setProgressFloat(mDefault);

        seekBarType.recycle();
    }

    /*
     * Converts from SeekBar units (which the SeekBar uses), to scale units
     *  (which are saved).
     *  This operation is the inverse of setFontScaling.
     */
    public float getProgressFloat() {
        return (getProgress() / mMultiplier) + mMin;
    }

    /*
     * Converts from scale units (which are saved), to SeekBar units
     * (which the SeekBar uses). This also sets the SeekBar progress.
     * This operation is the inverse of getProgressFloat.
     */
    public void setProgressFloat(float progress) {
        setProgress(convertFloatToProgress(progress));
    }

    private int convertFloatToProgress(float value) {
        return Math.round((value - mMin) * mMultiplier);
    }

    public float getMinimum() {
        return mMin;
    }

    public float getMaximum() {
        return mMax;
    }

    public float getDefault() {
        return mDefault;
    }

    public void setMaximum(float max) {
        mMax = max;
        setMax(convertFloatToProgress(mMax));
    }

    public void setMinimum(float min) {
        mMin = min;
    }
}