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 | |
| 19 | import android.content.Context; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 20 | import android.content.res.TypedArray; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 24 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.widget.SeekBar; |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 27 | import android.widget.SeekBar.OnSeekBarChangeListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @hide |
| 31 | */ |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 32 | public class SeekBarPreference extends Preference |
| 33 | implements OnSeekBarChangeListener { |
Jim Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 34 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 35 | private int mProgress; |
| 36 | private int mMax; |
| 37 | private boolean mTrackingTouch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 39 | public SeekBarPreference( |
| 40 | Context context, AttributeSet attrs, int defStyle) { |
| 41 | super(context, attrs, defStyle); |
| 42 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 43 | com.android.internal.R.styleable.ProgressBar, defStyle, 0); |
| 44 | setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax)); |
| 45 | a.recycle(); |
| 46 | setLayoutResource(com.android.internal.R.layout.preference_widget_seekbar); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 49 | public SeekBarPreference(Context context, AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public SeekBarPreference(Context context) { |
| 54 | this(context, null); |
Jim Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 55 | } |
| 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | @Override |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 58 | protected void onBindView(View view) { |
| 59 | super.onBindView(view); |
| 60 | SeekBar seekBar = (SeekBar) view.findViewById( |
| 61 | com.android.internal.R.id.seekbar); |
| 62 | seekBar.setOnSeekBarChangeListener(this); |
| 63 | seekBar.setMax(mMax); |
| 64 | seekBar.setProgress(mProgress); |
| 65 | seekBar.setEnabled(isEnabled()); |
| 66 | } |
Jim Miller | 4c8aafe | 2011-02-03 16:17:41 -0800 | [diff] [blame] | 67 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 68 | @Override |
| 69 | public CharSequence getSummary() { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 75 | setProgress(restoreValue ? getPersistedInt(mProgress) |
| 76 | : (Integer) defaultValue); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 81 | if (event.getAction() != KeyEvent.ACTION_UP) { |
| 82 | if (keyCode == KeyEvent.KEYCODE_PLUS |
| 83 | || keyCode == KeyEvent.KEYCODE_EQUALS) { |
| 84 | setProgress(getProgress() + 1); |
| 85 | return true; |
| 86 | } |
| 87 | if (keyCode == KeyEvent.KEYCODE_MINUS) { |
| 88 | setProgress(getProgress() - 1); |
| 89 | return true; |
| 90 | } |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | public void setMax(int max) { |
| 96 | if (max != mMax) { |
| 97 | mMax = max; |
| 98 | notifyChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
John Reck | 014fea2 | 2011-06-15 16:46:36 -0700 | [diff] [blame^] | 102 | public void setProgress(int progress) { |
| 103 | setProgress(progress, true); |
| 104 | } |
| 105 | |
| 106 | private void setProgress(int progress, boolean notifyChanged) { |
| 107 | if (progress > mMax) { |
| 108 | progress = mMax; |
| 109 | } |
| 110 | if (progress < 0) { |
| 111 | progress = 0; |
| 112 | } |
| 113 | if (progress != mProgress) { |
| 114 | mProgress = progress; |
| 115 | persistInt(progress); |
| 116 | if (notifyChanged) { |
| 117 | notifyChanged(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public int getProgress() { |
| 123 | return mProgress; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Persist the seekBar's progress value if callChangeListener |
| 128 | * returns true, otherwise set the seekBar's progress to the stored value |
| 129 | */ |
| 130 | void syncProgress(SeekBar seekBar) { |
| 131 | int progress = seekBar.getProgress(); |
| 132 | if (progress != mProgress) { |
| 133 | if (callChangeListener(progress)) { |
| 134 | setProgress(progress, false); |
| 135 | } else { |
| 136 | seekBar.setProgress(mProgress); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onProgressChanged( |
| 143 | SeekBar seekBar, int progress, boolean fromUser) { |
| 144 | if (fromUser && !mTrackingTouch) { |
| 145 | syncProgress(seekBar); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 151 | mTrackingTouch = true; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 156 | mTrackingTouch = false; |
| 157 | if (seekBar.getProgress() != mProgress) { |
| 158 | syncProgress(seekBar); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected Parcelable onSaveInstanceState() { |
| 164 | /* |
| 165 | * Suppose a client uses this preference type without persisting. We |
| 166 | * must save the instance state so it is able to, for example, survive |
| 167 | * orientation changes. |
| 168 | */ |
| 169 | |
| 170 | final Parcelable superState = super.onSaveInstanceState(); |
| 171 | if (isPersistent()) { |
| 172 | // No need to save instance state since it's persistent |
| 173 | return superState; |
| 174 | } |
| 175 | |
| 176 | // Save the instance state |
| 177 | final SavedState myState = new SavedState(superState); |
| 178 | myState.progress = mProgress; |
| 179 | myState.max = mMax; |
| 180 | return myState; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | protected void onRestoreInstanceState(Parcelable state) { |
| 185 | if (!state.getClass().equals(SavedState.class)) { |
| 186 | // Didn't save state for us in onSaveInstanceState |
| 187 | super.onRestoreInstanceState(state); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Restore the instance state |
| 192 | SavedState myState = (SavedState) state; |
| 193 | super.onRestoreInstanceState(myState.getSuperState()); |
| 194 | mProgress = myState.progress; |
| 195 | mMax = myState.max; |
| 196 | notifyChanged(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * SavedState, a subclass of {@link BaseSavedState}, will store the state |
| 201 | * of MyPreference, a subclass of Preference. |
| 202 | * <p> |
| 203 | * It is important to always call through to super methods. |
| 204 | */ |
| 205 | private static class SavedState extends BaseSavedState { |
| 206 | int progress; |
| 207 | int max; |
| 208 | |
| 209 | public SavedState(Parcel source) { |
| 210 | super(source); |
| 211 | |
| 212 | // Restore the click counter |
| 213 | progress = source.readInt(); |
| 214 | max = source.readInt(); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void writeToParcel(Parcel dest, int flags) { |
| 219 | super.writeToParcel(dest, flags); |
| 220 | |
| 221 | // Save the click counter |
| 222 | dest.writeInt(progress); |
| 223 | dest.writeInt(max); |
| 224 | } |
| 225 | |
| 226 | public SavedState(Parcelable superState) { |
| 227 | super(superState); |
| 228 | } |
| 229 | |
| 230 | @SuppressWarnings("unused") |
| 231 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 232 | new Parcelable.Creator<SavedState>() { |
| 233 | public SavedState createFromParcel(Parcel in) { |
| 234 | return new SavedState(in); |
| 235 | } |
| 236 | |
| 237 | public SavedState[] newArray(int size) { |
| 238 | return new SavedState[size]; |
| 239 | } |
| 240 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
| 242 | } |