1 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * xscreensaver, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * A numeric preference as a slider, inline in the preferences list.
13 * XML options include:
15 * low, high (floats) -- smallest and largest allowed values.
16 * If low > high, the value increases as the slider's thumb moves left.
18 * lowLabel, highLabel (strings) -- labels shown at the left and right
21 * integral (boolean) -- whether to use whole numbers instead of floats;
24 package org.jwz.xscreensaver;
26 import android.content.Context;
27 import android.content.res.TypedArray;
28 import android.content.res.Resources;
29 import android.preference.Preference;
30 import android.util.AttributeSet;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.SeekBar;
34 import android.widget.TextView;
35 import android.util.Log;
37 public class SliderPreference extends Preference {
39 protected float low, high;
40 protected String low_label, high_label;
41 protected boolean integral;
42 protected float mValue;
43 protected int seekbar_ticks;
45 public SliderPreference(Context context, AttributeSet attrs) {
46 this (context, attrs, 0);
49 public SliderPreference (Context context, AttributeSet attrs, int defStyle) {
50 super (context, attrs, defStyle);
52 Resources res = context.getResources();
54 // Parse these from the "<SliderPreference>" tag
55 low = Float.parseFloat (attrs.getAttributeValue (null, "low"));
56 high = Float.parseFloat (attrs.getAttributeValue (null, "high"));
57 integral = attrs.getAttributeBooleanValue (null, "integral", false);
58 low_label = res.getString(
59 attrs.getAttributeResourceValue (null, "lowLabel", 0));
60 high_label = res.getString(
61 attrs.getAttributeResourceValue (null, "highLabel", 0));
63 seekbar_ticks = (integral
64 ? (int) Math.floor (Math.abs (high - low))
67 setWidgetLayoutResource (R.layout.slider_preference);
72 protected void onSetInitialValue (boolean restore, Object def) {
74 mValue = getPersistedFloat (low);
77 persistFloat (mValue);
79 //Log.d("xscreensaver", String.format("SLIDER INIT %s: %f",
80 // low_label, mValue));
84 protected Object onGetDefaultValue(TypedArray a, int index) {
85 return a.getFloat (index, low);
89 public float getValue() {
93 public void setValue (float value) {
96 value = Math.max (low, Math.min (high, value));
98 value = Math.max (high, Math.min (low, value));
102 value = Math.round (value);
104 if (value != mValue) {
105 //Log.d("xscreensaver", String.format("SLIDER %s: %f", low_label, value));
106 persistFloat (value);
114 protected View onCreateView (ViewGroup parent) {
115 View view = super.onCreateView(parent);
117 TextView low_view = (TextView)
118 view.findViewById (R.id.slider_preference_low);
119 low_view.setText (low_label);
121 TextView high_view = (TextView)
122 view.findViewById (R.id.slider_preference_high);
123 high_view.setText (high_label);
125 SeekBar seekbar = (SeekBar)
126 view.findViewById (R.id.slider_preference_seekbar);
127 seekbar.setMax (seekbar_ticks);
129 float ratio = (mValue - low) / (high - low);
130 int seek_value = (int) (ratio * (float) seekbar_ticks);
132 seekbar.setProgress (seek_value);
134 final SliderPreference slider = this;
136 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
139 public void onStopTrackingTouch(SeekBar seekBar) {
143 public void onStartTrackingTouch(SeekBar seekBar) {
147 public void onProgressChanged (SeekBar seekBar, int progress,
150 float ratio = (float) progress / (float) seekbar_ticks;
151 float value = low + (ratio * (high - low));
152 slider.setValue (value);
153 callChangeListener (progress);