The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 android.preference; |
| 18 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.util.AttributeSet; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 25 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.widget.SeekBar; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 28 | import android.widget.SeekBar.OnSeekBarChangeListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @hide |
Louis Pullen-Freilich | b9596fa | 2018-11-19 17:40:56 +0000 | [diff] [blame] | 32 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | */ |
Louis Pullen-Freilich | b9596fa | 2018-11-19 17:40:56 +0000 | [diff] [blame] | 39 | @Deprecated |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 40 | public class SeekBarPreference extends Preference |
| 41 | implements OnSeekBarChangeListener { |
Jim Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 42 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 43 | private int mProgress; |
| 44 | private int mMax; |
| 45 | private boolean mTrackingTouch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 47 | public SeekBarPreference( |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 48 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 49 | super(context, attrs, defStyleAttr, defStyleRes); |
| 50 | |
Fabrice Di Meglio | 19ae4ca | 2014-06-12 20:13:24 -0700 | [diff] [blame] | 51 | TypedArray a = context.obtainStyledAttributes( |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 52 | attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes); |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 53 | setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax)); |
| 54 | a.recycle(); |
Fabrice Di Meglio | 19ae4ca | 2014-06-12 20:13:24 -0700 | [diff] [blame] | 55 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 66 | @UnsupportedAppUsage |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 67 | public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 68 | this(context, attrs, defStyleAttr, 0); |
| 69 | } |
| 70 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 71 | @UnsupportedAppUsage |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 72 | public SeekBarPreference(Context context, AttributeSet attrs) { |
Fabrice Di Meglio | 19ae4ca | 2014-06-12 20:13:24 -0700 | [diff] [blame] | 73 | this(context, attrs, com.android.internal.R.attr.seekBarPreferenceStyle); |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Mathew Inwood | f221713 | 2018-08-17 13:41:55 +0100 | [diff] [blame] | 76 | @UnsupportedAppUsage |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 77 | public SeekBarPreference(Context context) { |
| 78 | this(context, null); |
Jim Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 79 | } |
| 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | @Override |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 82 | 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 Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 91 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 92 | @Override |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 93 | protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 94 | setProgress(restoreValue ? getPersistedInt(mProgress) |
| 95 | : (Integer) defaultValue); |
| 96 | } |
| 97 | |
| 98 | @Override |
John Reck | 595d2bd | 2011-07-15 15:48:01 -0700 | [diff] [blame] | 99 | protected Object onGetDefaultValue(TypedArray a, int index) { |
| 100 | return a.getInt(index, 0); |
| 101 | } |
| 102 | |
| 103 | @Override |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 104 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Toni Barzic | e9889bf | 2015-09-29 14:50:33 -0700 | [diff] [blame] | 105 | if (event.getAction() != KeyEvent.ACTION_DOWN) { |
| 106 | return false; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 107 | } |
Toni Barzic | e9889bf | 2015-09-29 14:50:33 -0700 | [diff] [blame] | 108 | |
| 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 Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public void setMax(int max) { |
| 117 | if (max != mMax) { |
| 118 | mMax = max; |
| 119 | notifyChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 123 | 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") |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 252 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame] | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
| 263 | } |