The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 21 | import android.util.ValueModel; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 22 | import android.view.accessibility.AccessibilityEvent; |
| 23 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch |
| 29 | * the thumb and drag left or right to set the current progress level or use the arrow keys. |
| 30 | * Placing focusable widgets to the left or right of a SeekBar is discouraged. |
| 31 | * <p> |
| 32 | * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to |
| 33 | * be notified of the user's actions. |
| 34 | * |
| 35 | * @attr ref android.R.styleable#SeekBar_thumb |
| 36 | */ |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 37 | public class SeekBar extends AbsSeekBar implements ValueEditor<Integer> { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * A callback that notifies clients when the progress level has been |
| 41 | * changed. This includes changes that were initiated by the user through a |
| 42 | * touch gesture or arrow key/trackball as well as changes that were initiated |
| 43 | * programmatically. |
| 44 | */ |
| 45 | public interface OnSeekBarChangeListener { |
| 46 | |
| 47 | /** |
| 48 | * Notification that the progress level has changed. Clients can use the fromUser parameter |
| 49 | * to distinguish user-initiated changes from those that occurred programmatically. |
| 50 | * |
| 51 | * @param seekBar The SeekBar whose progress has changed |
| 52 | * @param progress The current progress level. This will be in the range 0..max where max |
| 53 | * was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.) |
| 54 | * @param fromUser True if the progress change was initiated by the user. |
| 55 | */ |
| 56 | void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser); |
| 57 | |
| 58 | /** |
| 59 | * Notification that the user has started a touch gesture. Clients may want to use this |
| 60 | * to disable advancing the seekbar. |
| 61 | * @param seekBar The SeekBar in which the touch gesture began |
| 62 | */ |
| 63 | void onStartTrackingTouch(SeekBar seekBar); |
| 64 | |
| 65 | /** |
| 66 | * Notification that the user has finished a touch gesture. Clients may want to use this |
| 67 | * to re-enable advancing the seekbar. |
| 68 | * @param seekBar The SeekBar in which the touch gesture began |
| 69 | */ |
| 70 | void onStopTrackingTouch(SeekBar seekBar); |
| 71 | } |
| 72 | |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 73 | private ValueModel<Integer> mValueModel = ValueModel.EMPTY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | private OnSeekBarChangeListener mOnSeekBarChangeListener; |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | public SeekBar(Context context) { |
| 77 | this(context, null); |
| 78 | } |
| 79 | |
| 80 | public SeekBar(Context context, AttributeSet attrs) { |
| 81 | this(context, attrs, com.android.internal.R.attr.seekBarStyle); |
| 82 | } |
| 83 | |
| 84 | public SeekBar(Context context, AttributeSet attrs, int defStyle) { |
| 85 | super(context, attrs, defStyle); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | void onProgressRefresh(float scale, boolean fromUser) { |
| 90 | super.onProgressRefresh(scale, fromUser); |
| 91 | |
| 92 | if (mOnSeekBarChangeListener != null) { |
| 93 | mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser); |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 94 | if (fromUser) { |
| 95 | mValueModel.set(getProgress()); |
| 96 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Philip Milne | 989709a | 2012-06-22 16:09:04 -0700 | [diff] [blame^] | 100 | @Override |
| 101 | public ValueModel<Integer> getValueModel() { |
| 102 | return mValueModel; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setValueModel(ValueModel<Integer> valueModel) { |
| 107 | mValueModel = valueModel; |
| 108 | setProgress(mValueModel.get()); |
| 109 | } |
| 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also |
| 113 | * provides notifications of when the user starts and stops a touch gesture within the SeekBar. |
| 114 | * |
| 115 | * @param l The seek bar notification listener |
| 116 | * |
| 117 | * @see SeekBar.OnSeekBarChangeListener |
| 118 | */ |
| 119 | public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { |
| 120 | mOnSeekBarChangeListener = l; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | void onStartTrackingTouch() { |
Adam Powell | 1029866 | 2011-08-14 18:26:30 -0700 | [diff] [blame] | 125 | super.onStartTrackingTouch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | if (mOnSeekBarChangeListener != null) { |
| 127 | mOnSeekBarChangeListener.onStartTrackingTouch(this); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | void onStopTrackingTouch() { |
Adam Powell | 1029866 | 2011-08-14 18:26:30 -0700 | [diff] [blame] | 133 | super.onStopTrackingTouch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | if (mOnSeekBarChangeListener != null) { |
| 135 | mOnSeekBarChangeListener.onStopTrackingTouch(this); |
| 136 | } |
| 137 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 141 | super.onInitializeAccessibilityEvent(event); |
| 142 | event.setClassName(SeekBar.class.getName()); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 147 | super.onInitializeAccessibilityNodeInfo(info); |
| 148 | info.setClassName(SeekBar.class.getName()); |
| 149 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |