blob: 5414f000797020e73396076d0f97db9cde6e5583 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
John Reck014fea22011-06-15 16:46:36 -07002 * Copyright (C) 2011 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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
17package android.preference;
18
19import android.content.Context;
John Reck014fea22011-06-15 16:46:36 -070020import android.content.res.TypedArray;
21import android.os.Parcel;
22import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.util.AttributeSet;
John Reck014fea22011-06-15 16:46:36 -070024import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.widget.SeekBar;
John Reck014fea22011-06-15 16:46:36 -070027import android.widget.SeekBar.OnSeekBarChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29/**
30 * @hide
31 */
John Reck014fea22011-06-15 16:46:36 -070032public class SeekBarPreference extends Preference
33 implements OnSeekBarChangeListener {
Jim Miller4c8aafe2011-02-03 16:17:41 -080034
John Reck014fea22011-06-15 16:46:36 -070035 private int mProgress;
36 private int mMax;
37 private boolean mTrackingTouch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
John Reck014fea22011-06-15 16:46:36 -070039 public SeekBarPreference(
Alan Viverette617feb92013-09-09 18:09:13 -070040 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
41 super(context, attrs, defStyleAttr, defStyleRes);
42
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070043 TypedArray a = context.obtainStyledAttributes(
Alan Viverette617feb92013-09-09 18:09:13 -070044 attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes);
John Reck014fea22011-06-15 16:46:36 -070045 setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax));
46 a.recycle();
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070047
48 a = context.obtainStyledAttributes(attrs,
49 com.android.internal.R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
50 final int layoutResId = a.getResourceId(
51 com.android.internal.R.styleable.SeekBarPreference_layout,
52 com.android.internal.R.layout.preference_widget_seekbar);
53 a.recycle();
54
55 setLayoutResource(layoutResId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 }
57
Alan Viverette617feb92013-09-09 18:09:13 -070058 public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
59 this(context, attrs, defStyleAttr, 0);
60 }
61
John Reck014fea22011-06-15 16:46:36 -070062 public SeekBarPreference(Context context, AttributeSet attrs) {
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070063 this(context, attrs, com.android.internal.R.attr.seekBarPreferenceStyle);
John Reck014fea22011-06-15 16:46:36 -070064 }
65
66 public SeekBarPreference(Context context) {
67 this(context, null);
Jim Miller4c8aafe2011-02-03 16:17:41 -080068 }
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 @Override
John Reck014fea22011-06-15 16:46:36 -070071 protected void onBindView(View view) {
72 super.onBindView(view);
73 SeekBar seekBar = (SeekBar) view.findViewById(
74 com.android.internal.R.id.seekbar);
75 seekBar.setOnSeekBarChangeListener(this);
76 seekBar.setMax(mMax);
77 seekBar.setProgress(mProgress);
78 seekBar.setEnabled(isEnabled());
79 }
Jim Miller4c8aafe2011-02-03 16:17:41 -080080
John Reck014fea22011-06-15 16:46:36 -070081 @Override
82 public CharSequence getSummary() {
83 return null;
84 }
85
86 @Override
87 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
88 setProgress(restoreValue ? getPersistedInt(mProgress)
89 : (Integer) defaultValue);
90 }
91
92 @Override
John Reck595d2bd2011-07-15 15:48:01 -070093 protected Object onGetDefaultValue(TypedArray a, int index) {
94 return a.getInt(index, 0);
95 }
96
97 @Override
John Reck014fea22011-06-15 16:46:36 -070098 public boolean onKey(View v, int keyCode, KeyEvent event) {
Toni Barzice9889bf2015-09-29 14:50:33 -070099 if (event.getAction() != KeyEvent.ACTION_DOWN) {
100 return false;
John Reck014fea22011-06-15 16:46:36 -0700101 }
Toni Barzice9889bf2015-09-29 14:50:33 -0700102
103 SeekBar seekBar = (SeekBar) v.findViewById(com.android.internal.R.id.seekbar);
104 if (seekBar == null) {
105 return false;
106 }
107 return seekBar.onKeyDown(keyCode, event);
John Reck014fea22011-06-15 16:46:36 -0700108 }
109
110 public void setMax(int max) {
111 if (max != mMax) {
112 mMax = max;
113 notifyChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
115 }
116
John Reck014fea22011-06-15 16:46:36 -0700117 public void setProgress(int progress) {
118 setProgress(progress, true);
119 }
120
121 private void setProgress(int progress, boolean notifyChanged) {
122 if (progress > mMax) {
123 progress = mMax;
124 }
125 if (progress < 0) {
126 progress = 0;
127 }
128 if (progress != mProgress) {
129 mProgress = progress;
130 persistInt(progress);
131 if (notifyChanged) {
132 notifyChanged();
133 }
134 }
135 }
136
137 public int getProgress() {
138 return mProgress;
139 }
140
141 /**
142 * Persist the seekBar's progress value if callChangeListener
143 * returns true, otherwise set the seekBar's progress to the stored value
144 */
145 void syncProgress(SeekBar seekBar) {
146 int progress = seekBar.getProgress();
147 if (progress != mProgress) {
148 if (callChangeListener(progress)) {
149 setProgress(progress, false);
150 } else {
151 seekBar.setProgress(mProgress);
152 }
153 }
154 }
155
156 @Override
157 public void onProgressChanged(
158 SeekBar seekBar, int progress, boolean fromUser) {
159 if (fromUser && !mTrackingTouch) {
160 syncProgress(seekBar);
161 }
162 }
163
164 @Override
165 public void onStartTrackingTouch(SeekBar seekBar) {
166 mTrackingTouch = true;
167 }
168
169 @Override
170 public void onStopTrackingTouch(SeekBar seekBar) {
171 mTrackingTouch = false;
172 if (seekBar.getProgress() != mProgress) {
173 syncProgress(seekBar);
174 }
175 }
176
177 @Override
178 protected Parcelable onSaveInstanceState() {
179 /*
180 * Suppose a client uses this preference type without persisting. We
181 * must save the instance state so it is able to, for example, survive
182 * orientation changes.
183 */
184
185 final Parcelable superState = super.onSaveInstanceState();
186 if (isPersistent()) {
187 // No need to save instance state since it's persistent
188 return superState;
189 }
190
191 // Save the instance state
192 final SavedState myState = new SavedState(superState);
193 myState.progress = mProgress;
194 myState.max = mMax;
195 return myState;
196 }
197
198 @Override
199 protected void onRestoreInstanceState(Parcelable state) {
200 if (!state.getClass().equals(SavedState.class)) {
201 // Didn't save state for us in onSaveInstanceState
202 super.onRestoreInstanceState(state);
203 return;
204 }
205
206 // Restore the instance state
207 SavedState myState = (SavedState) state;
208 super.onRestoreInstanceState(myState.getSuperState());
209 mProgress = myState.progress;
210 mMax = myState.max;
211 notifyChanged();
212 }
213
214 /**
215 * SavedState, a subclass of {@link BaseSavedState}, will store the state
216 * of MyPreference, a subclass of Preference.
217 * <p>
218 * It is important to always call through to super methods.
219 */
220 private static class SavedState extends BaseSavedState {
221 int progress;
222 int max;
223
224 public SavedState(Parcel source) {
225 super(source);
226
227 // Restore the click counter
228 progress = source.readInt();
229 max = source.readInt();
230 }
231
232 @Override
233 public void writeToParcel(Parcel dest, int flags) {
234 super.writeToParcel(dest, flags);
235
236 // Save the click counter
237 dest.writeInt(progress);
238 dest.writeInt(max);
239 }
240
241 public SavedState(Parcelable superState) {
242 super(superState);
243 }
244
245 @SuppressWarnings("unused")
246 public static final Parcelable.Creator<SavedState> CREATOR =
247 new Parcelable.Creator<SavedState>() {
248 public SavedState createFromParcel(Parcel in) {
249 return new SavedState(in);
250 }
251
252 public SavedState[] newArray(int size) {
253 return new SavedState[size];
254 }
255 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257}