blob: f789e31faa027d0af356d88769ab3936fb044ee0 [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
Mathew Inwoodf2217132018-08-17 13:41:55 +010019import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
John Reck014fea22011-06-15 16:46:36 -070021import android.content.res.TypedArray;
22import android.os.Parcel;
23import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.AttributeSet;
John Reck014fea22011-06-15 16:46:36 -070025import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.widget.SeekBar;
John Reck014fea22011-06-15 16:46:36 -070028import android.widget.SeekBar.OnSeekBarChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30/**
31 * @hide
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000032 *
33 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
34 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
35 * Preference Library</a> for consistent behavior across all devices. For more information on
36 * using the AndroidX Preference Library see
37 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000039@Deprecated
John Reck014fea22011-06-15 16:46:36 -070040public class SeekBarPreference extends Preference
41 implements OnSeekBarChangeListener {
Jim Miller4c8aafe2011-02-03 16:17:41 -080042
John Reck014fea22011-06-15 16:46:36 -070043 private int mProgress;
44 private int mMax;
45 private boolean mTrackingTouch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
John Reck014fea22011-06-15 16:46:36 -070047 public SeekBarPreference(
Alan Viverette617feb92013-09-09 18:09:13 -070048 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49 super(context, attrs, defStyleAttr, defStyleRes);
50
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070051 TypedArray a = context.obtainStyledAttributes(
Alan Viverette617feb92013-09-09 18:09:13 -070052 attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes);
John Reck014fea22011-06-15 16:46:36 -070053 setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax));
54 a.recycle();
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070055
56 a = context.obtainStyledAttributes(attrs,
57 com.android.internal.R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
58 final int layoutResId = a.getResourceId(
59 com.android.internal.R.styleable.SeekBarPreference_layout,
60 com.android.internal.R.layout.preference_widget_seekbar);
61 a.recycle();
62
63 setLayoutResource(layoutResId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
65
Mathew Inwoodf2217132018-08-17 13:41:55 +010066 @UnsupportedAppUsage
Alan Viverette617feb92013-09-09 18:09:13 -070067 public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
68 this(context, attrs, defStyleAttr, 0);
69 }
70
Mathew Inwoodf2217132018-08-17 13:41:55 +010071 @UnsupportedAppUsage
John Reck014fea22011-06-15 16:46:36 -070072 public SeekBarPreference(Context context, AttributeSet attrs) {
Fabrice Di Meglio19ae4ca2014-06-12 20:13:24 -070073 this(context, attrs, com.android.internal.R.attr.seekBarPreferenceStyle);
John Reck014fea22011-06-15 16:46:36 -070074 }
75
Mathew Inwoodf2217132018-08-17 13:41:55 +010076 @UnsupportedAppUsage
John Reck014fea22011-06-15 16:46:36 -070077 public SeekBarPreference(Context context) {
78 this(context, null);
Jim Miller4c8aafe2011-02-03 16:17:41 -080079 }
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 @Override
John Reck014fea22011-06-15 16:46:36 -070082 protected void onBindView(View view) {
83 super.onBindView(view);
84 SeekBar seekBar = (SeekBar) view.findViewById(
85 com.android.internal.R.id.seekbar);
86 seekBar.setOnSeekBarChangeListener(this);
87 seekBar.setMax(mMax);
88 seekBar.setProgress(mProgress);
89 seekBar.setEnabled(isEnabled());
90 }
Jim Miller4c8aafe2011-02-03 16:17:41 -080091
John Reck014fea22011-06-15 16:46:36 -070092 @Override
John Reck014fea22011-06-15 16:46:36 -070093 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
94 setProgress(restoreValue ? getPersistedInt(mProgress)
95 : (Integer) defaultValue);
96 }
97
98 @Override
John Reck595d2bd2011-07-15 15:48:01 -070099 protected Object onGetDefaultValue(TypedArray a, int index) {
100 return a.getInt(index, 0);
101 }
102
103 @Override
John Reck014fea22011-06-15 16:46:36 -0700104 public boolean onKey(View v, int keyCode, KeyEvent event) {
Toni Barzice9889bf2015-09-29 14:50:33 -0700105 if (event.getAction() != KeyEvent.ACTION_DOWN) {
106 return false;
John Reck014fea22011-06-15 16:46:36 -0700107 }
Toni Barzice9889bf2015-09-29 14:50:33 -0700108
109 SeekBar seekBar = (SeekBar) v.findViewById(com.android.internal.R.id.seekbar);
110 if (seekBar == null) {
111 return false;
112 }
113 return seekBar.onKeyDown(keyCode, event);
John Reck014fea22011-06-15 16:46:36 -0700114 }
115
116 public void setMax(int max) {
117 if (max != mMax) {
118 mMax = max;
119 notifyChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
121 }
122
John Reck014fea22011-06-15 16:46:36 -0700123 public void setProgress(int progress) {
124 setProgress(progress, true);
125 }
126
127 private void setProgress(int progress, boolean notifyChanged) {
128 if (progress > mMax) {
129 progress = mMax;
130 }
131 if (progress < 0) {
132 progress = 0;
133 }
134 if (progress != mProgress) {
135 mProgress = progress;
136 persistInt(progress);
137 if (notifyChanged) {
138 notifyChanged();
139 }
140 }
141 }
142
143 public int getProgress() {
144 return mProgress;
145 }
146
147 /**
148 * Persist the seekBar's progress value if callChangeListener
149 * returns true, otherwise set the seekBar's progress to the stored value
150 */
151 void syncProgress(SeekBar seekBar) {
152 int progress = seekBar.getProgress();
153 if (progress != mProgress) {
154 if (callChangeListener(progress)) {
155 setProgress(progress, false);
156 } else {
157 seekBar.setProgress(mProgress);
158 }
159 }
160 }
161
162 @Override
163 public void onProgressChanged(
164 SeekBar seekBar, int progress, boolean fromUser) {
165 if (fromUser && !mTrackingTouch) {
166 syncProgress(seekBar);
167 }
168 }
169
170 @Override
171 public void onStartTrackingTouch(SeekBar seekBar) {
172 mTrackingTouch = true;
173 }
174
175 @Override
176 public void onStopTrackingTouch(SeekBar seekBar) {
177 mTrackingTouch = false;
178 if (seekBar.getProgress() != mProgress) {
179 syncProgress(seekBar);
180 }
181 }
182
183 @Override
184 protected Parcelable onSaveInstanceState() {
185 /*
186 * Suppose a client uses this preference type without persisting. We
187 * must save the instance state so it is able to, for example, survive
188 * orientation changes.
189 */
190
191 final Parcelable superState = super.onSaveInstanceState();
192 if (isPersistent()) {
193 // No need to save instance state since it's persistent
194 return superState;
195 }
196
197 // Save the instance state
198 final SavedState myState = new SavedState(superState);
199 myState.progress = mProgress;
200 myState.max = mMax;
201 return myState;
202 }
203
204 @Override
205 protected void onRestoreInstanceState(Parcelable state) {
206 if (!state.getClass().equals(SavedState.class)) {
207 // Didn't save state for us in onSaveInstanceState
208 super.onRestoreInstanceState(state);
209 return;
210 }
211
212 // Restore the instance state
213 SavedState myState = (SavedState) state;
214 super.onRestoreInstanceState(myState.getSuperState());
215 mProgress = myState.progress;
216 mMax = myState.max;
217 notifyChanged();
218 }
219
220 /**
221 * SavedState, a subclass of {@link BaseSavedState}, will store the state
222 * of MyPreference, a subclass of Preference.
223 * <p>
224 * It is important to always call through to super methods.
225 */
226 private static class SavedState extends BaseSavedState {
227 int progress;
228 int max;
229
230 public SavedState(Parcel source) {
231 super(source);
232
233 // Restore the click counter
234 progress = source.readInt();
235 max = source.readInt();
236 }
237
238 @Override
239 public void writeToParcel(Parcel dest, int flags) {
240 super.writeToParcel(dest, flags);
241
242 // Save the click counter
243 dest.writeInt(progress);
244 dest.writeInt(max);
245 }
246
247 public SavedState(Parcelable superState) {
248 super(superState);
249 }
250
251 @SuppressWarnings("unused")
252 public static final Parcelable.Creator<SavedState> CREATOR =
253 new Parcelable.Creator<SavedState>() {
254 public SavedState createFromParcel(Parcel in) {
255 return new SavedState(in);
256 }
257
258 public SavedState[] newArray(int size) {
259 return new SavedState[size];
260 }
261 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263}