abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.common/IntSlideBarPreference.java
blob: 0bdf2c72d6cfac2d74b6b4c136ee1c3f5ce08926 (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
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
package juloo.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.SeekBar;

/*
 ** IntSlideBarPreference
 ** -
 ** Open a dialog showing a seekbar
 ** -
 ** xml attrs:
 **   android:defaultValue  Default value (int)
 **   min                   min value (int)
 **   max                   max value (int)
 ** -
 ** Summary field allow to show the current value using %s flag
 */
public class IntSlideBarPreference extends DialogPreference
  implements SeekBar.OnSeekBarChangeListener
{
  private LinearLayout _layout;
  private TextView _textView;
  private SeekBar _seekBar;

  private int _min;

  private String _initialSummary;

  public IntSlideBarPreference(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    _initialSummary = getSummary().toString();
    _textView = new TextView(context);
    _textView.setPadding(48, 40, 48, 40);
    _seekBar = new SeekBar(context);
    _seekBar.setOnSeekBarChangeListener(this);
    _min = attrs.getAttributeIntValue(null, "min", 0);
    int max = attrs.getAttributeIntValue(null, "max", 0);
    _seekBar.setMax(max - _min);
    _layout = new LinearLayout(getContext());
    _layout.setOrientation(LinearLayout.VERTICAL);
    _layout.addView(_textView);
    _layout.addView(_seekBar);
  }

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
  {
    updateText();
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar)
  {
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar)
  {
  }

  @Override
  protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue)
  {
    int value;

    if (restorePersistedValue)
    {
      value = getPersistedInt(_min);
    }
    else
    {
      value = (Integer)defaultValue;
      persistInt(value);
    }
    _seekBar.setProgress(value - _min);
    updateText();
  }

  @Override
  protected Object onGetDefaultValue(TypedArray a, int index)
  {
    return (a.getInt(index, _min));
  }

  @Override
  protected void onDialogClosed(boolean positiveResult)
  {
    if (positiveResult)
      persistInt(_seekBar.getProgress() + _min);
    else
      _seekBar.setProgress(getPersistedInt(_min) - _min);

    updateText();
  }

  protected View onCreateDialogView()
  {
    ViewGroup parent = (ViewGroup)_layout.getParent();

    if (parent != null)
      parent.removeView(_layout);
    return (_layout);
  }

  private void updateText()
  {
    String f = String.format(_initialSummary, _seekBar.getProgress() + _min);

    _textView.setText(f);
    setSummary(f);
  }
}