• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.replica.replicaisland;
18 
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.widget.SeekBar;
22 import android.widget.TextView;
23 import android.widget.SeekBar.OnSeekBarChangeListener;
24 import android.content.Context;
25 import android.content.res.TypedArray;
26 import android.preference.Preference;
27 
28 public class SliderPreference extends Preference implements OnSeekBarChangeListener {
29 	private final static int MAX_SLIDER_VALUE = 100;
30 	private final static int INITIAL_VALUE = 50;
31 
32 	private int mValue = INITIAL_VALUE;
33 	private String mMinText;
34 	private String mMaxText;
35 
36 
SliderPreference(Context context)37 	public SliderPreference(Context context) {
38 		super(context);
39 
40 		setWidgetLayoutResource(R.layout.slider_preference);
41 	}
42 
SliderPreference(Context context, AttributeSet attrs)43 	public SliderPreference(Context context, AttributeSet attrs) {
44 		this(context, attrs, android.R.attr.preferenceStyle);
45 
46 		setWidgetLayoutResource(R.layout.slider_preference);
47 	}
48 
SliderPreference(Context context, AttributeSet attrs, int defStyle)49 	public SliderPreference(Context context, AttributeSet attrs, int defStyle) {
50 		super(context, attrs, defStyle);
51 
52 		TypedArray a = context.obtainStyledAttributes(attrs,
53                 R.styleable.SliderPreference, defStyle, 0);
54 		mMinText = a.getString(R.styleable.SliderPreference_minText);
55 		mMaxText = a.getString(R.styleable.SliderPreference_maxText);
56 
57         a.recycle();
58 
59         setWidgetLayoutResource(R.layout.slider_preference);
60 	}
61 
62 	@Override
onBindView(View view)63 	protected void onBindView(View view) {
64 		super.onBindView(view);
65 
66 		if (mMinText != null) {
67 			TextView minText = (TextView)view.findViewById(R.id.min);
68 			minText.setText(mMinText);
69 		}
70 
71 		if (mMaxText != null) {
72 			TextView maxText = (TextView)view.findViewById(R.id.max);
73 			maxText.setText(mMaxText);
74 		}
75 
76 		SeekBar bar = (SeekBar)view.findViewById(R.id.slider);
77 		bar.setMax(MAX_SLIDER_VALUE);
78 		bar.setProgress(mValue);
79 		bar.setOnSeekBarChangeListener(this);
80 	}
81 
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)82 	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
83 		if (fromUser) {
84 			mValue = progress;
85 			persistInt(mValue);
86 		}
87 	}
88 
onStartTrackingTouch(SeekBar seekBar)89 	public void onStartTrackingTouch(SeekBar seekBar) {
90 	}
91 
onStopTrackingTouch(SeekBar seekBar)92 	public void onStopTrackingTouch(SeekBar seekBar) {
93 	}
94 
95 
96 	@Override
onGetDefaultValue(TypedArray ta,int index)97 	protected Object onGetDefaultValue(TypedArray ta,int index) {
98 		int dValue = (int)ta.getInt(index, INITIAL_VALUE);
99 
100 		return (int)Utils.clamp(dValue, 0, MAX_SLIDER_VALUE);
101 	}
102 
103 
104 	@Override
onSetInitialValue(boolean restoreValue, Object defaultValue)105 	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
106 		mValue = defaultValue != null ? (Integer)defaultValue : INITIAL_VALUE;
107 
108 		if (!restoreValue) {
109 			persistInt(mValue);
110 		} else {
111 			mValue = getPersistedInt(mValue);
112 		}
113 	}
114 
115 
116 }