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 | |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.util.AttributeSet; |
Maxim Bogatov | 32e59d5 | 2015-04-30 16:57:33 -0700 | [diff] [blame] | 22 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /** |
| 26 | * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch |
| 27 | * the thumb and drag left or right to set the current progress level or use the arrow keys. |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 28 | * Placing focusable widgets to the left or right of a SeekBar is discouraged. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | * <p> |
| 30 | * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to |
| 31 | * be notified of the user's actions. |
| 32 | * |
| 33 | * @attr ref android.R.styleable#SeekBar_thumb |
| 34 | */ |
Philip Milne | ab104ba | 2013-04-19 03:53:38 +0000 | [diff] [blame] | 35 | public class SeekBar extends AbsSeekBar { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * A callback that notifies clients when the progress level has been |
| 39 | * changed. This includes changes that were initiated by the user through a |
| 40 | * touch gesture or arrow key/trackball as well as changes that were initiated |
| 41 | * programmatically. |
| 42 | */ |
| 43 | public interface OnSeekBarChangeListener { |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Notification that the progress level has changed. Clients can use the fromUser parameter |
| 47 | * to distinguish user-initiated changes from those that occurred programmatically. |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 48 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * @param seekBar The SeekBar whose progress has changed |
Keyvan Amiri | 86fb2a2 | 2016-09-29 17:53:24 -0700 | [diff] [blame] | 50 | * @param progress The current progress level. This will be in the range min..max where min |
| 51 | * and max were set by {@link ProgressBar#setMin(int)} and |
| 52 | * {@link ProgressBar#setMax(int)}, respectively. (The default values for |
| 53 | * min is 0 and max is 100.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * @param fromUser True if the progress change was initiated by the user. |
| 55 | */ |
| 56 | void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Notification that the user has started a touch gesture. Clients may want to use this |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 60 | * to disable advancing the seekbar. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * @param seekBar The SeekBar in which the touch gesture began |
| 62 | */ |
| 63 | void onStartTrackingTouch(SeekBar seekBar); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Notification that the user has finished a touch gesture. Clients may want to use this |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 67 | * to re-enable advancing the seekbar. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | * @param seekBar The SeekBar in which the touch gesture began |
| 69 | */ |
| 70 | void onStopTrackingTouch(SeekBar seekBar); |
| 71 | } |
| 72 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | private OnSeekBarChangeListener mOnSeekBarChangeListener; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [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 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | public SeekBar(Context context, AttributeSet attrs) { |
| 81 | this(context, attrs, com.android.internal.R.attr.seekBarStyle); |
| 82 | } |
| 83 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 84 | public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) { |
| 85 | this(context, attrs, defStyleAttr, 0); |
| 86 | } |
| 87 | |
| 88 | public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 89 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 93 | @UnsupportedAppUsage |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 94 | void onProgressRefresh(float scale, boolean fromUser, int progress) { |
| 95 | super.onProgressRefresh(scale, fromUser, progress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| 97 | if (mOnSeekBarChangeListener != null) { |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 98 | mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also |
| 104 | * provides notifications of when the user starts and stops a touch gesture within the SeekBar. |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 105 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * @param l The seek bar notification listener |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 107 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | * @see SeekBar.OnSeekBarChangeListener |
| 109 | */ |
| 110 | public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { |
| 111 | mOnSeekBarChangeListener = l; |
| 112 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | @Override |
| 115 | void onStartTrackingTouch() { |
Adam Powell | 1029866 | 2011-08-14 18:26:30 -0700 | [diff] [blame] | 116 | super.onStartTrackingTouch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | if (mOnSeekBarChangeListener != null) { |
| 118 | mOnSeekBarChangeListener.onStartTrackingTouch(this); |
| 119 | } |
| 120 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | @Override |
| 123 | void onStopTrackingTouch() { |
Adam Powell | 1029866 | 2011-08-14 18:26:30 -0700 | [diff] [blame] | 124 | super.onStopTrackingTouch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | if (mOnSeekBarChangeListener != null) { |
| 126 | mOnSeekBarChangeListener.onStopTrackingTouch(this); |
| 127 | } |
| 128 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 129 | |
| 130 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 131 | public CharSequence getAccessibilityClassName() { |
| 132 | return SeekBar.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 133 | } |
Maxim Bogatov | 32e59d5 | 2015-04-30 16:57:33 -0700 | [diff] [blame] | 134 | |
| 135 | /** @hide */ |
| 136 | @Override |
| 137 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 138 | super.onInitializeAccessibilityNodeInfoInternal(info); |
| 139 | |
| 140 | if (canUserSetProgress()) { |
| 141 | info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS); |
| 142 | } |
| 143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |