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.compat.annotation.UnsupportedAppUsage; 20 import android.content.Context; 21 import android.util.AttributeSet; 22 import android.view.accessibility.AccessibilityNodeInfo; 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. 28 * Placing focusable widgets to the left or right of a SeekBar is discouraged. 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 */ 35 public class SeekBar extends AbsSeekBar { 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 { 44 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. 48 * 49 * @param seekBar The SeekBar whose progress has changed 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.) 54 * @param fromUser True if the progress change was initiated by the user. 55 */ onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)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 */ onStartTrackingTouch(SeekBar seekBar)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 */ onStopTrackingTouch(SeekBar seekBar)70 void onStopTrackingTouch(SeekBar seekBar); 71 } 72 73 @UnsupportedAppUsage 74 private OnSeekBarChangeListener mOnSeekBarChangeListener; 75 SeekBar(Context context)76 public SeekBar(Context context) { 77 this(context, null); 78 } 79 SeekBar(Context context, AttributeSet attrs)80 public SeekBar(Context context, AttributeSet attrs) { 81 this(context, attrs, com.android.internal.R.attr.seekBarStyle); 82 } 83 SeekBar(Context context, AttributeSet attrs, int defStyleAttr)84 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) { 85 this(context, attrs, defStyleAttr, 0); 86 } 87 SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)88 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 89 super(context, attrs, defStyleAttr, defStyleRes); 90 } 91 92 @Override 93 @UnsupportedAppUsage onProgressRefresh(float scale, boolean fromUser, int progress)94 void onProgressRefresh(float scale, boolean fromUser, int progress) { 95 super.onProgressRefresh(scale, fromUser, progress); 96 97 if (mOnSeekBarChangeListener != null) { 98 mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser); 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. 105 * 106 * @param l The seek bar notification listener 107 * 108 * @see SeekBar.OnSeekBarChangeListener 109 */ setOnSeekBarChangeListener(OnSeekBarChangeListener l)110 public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { 111 mOnSeekBarChangeListener = l; 112 } 113 114 @Override onStartTrackingTouch()115 void onStartTrackingTouch() { 116 super.onStartTrackingTouch(); 117 if (mOnSeekBarChangeListener != null) { 118 mOnSeekBarChangeListener.onStartTrackingTouch(this); 119 } 120 } 121 122 @Override onStopTrackingTouch()123 void onStopTrackingTouch() { 124 super.onStopTrackingTouch(); 125 if (mOnSeekBarChangeListener != null) { 126 mOnSeekBarChangeListener.onStopTrackingTouch(this); 127 } 128 } 129 130 @Override getAccessibilityClassName()131 public CharSequence getAccessibilityClassName() { 132 return SeekBar.class.getName(); 133 } 134 135 /** @hide */ 136 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)137 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 138 super.onInitializeAccessibilityNodeInfoInternal(info); 139 140 if (canUserSetProgress()) { 141 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS); 142 } 143 } 144 } 145