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.os.Build; 22 import android.util.AttributeSet; 23 import android.view.accessibility.AccessibilityNodeInfo; 24 25 26 /** 27 * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch 28 * the thumb and drag left or right to set the current progress level or use the arrow keys. 29 * Placing focusable widgets to the left or right of a SeekBar is discouraged. 30 * <p> 31 * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to 32 * be notified of the user's actions. 33 * 34 * @attr ref android.R.styleable#SeekBar_thumb 35 */ 36 public class SeekBar extends AbsSeekBar { 37 38 /** 39 * A callback that notifies clients when the progress level has been 40 * changed. This includes changes that were initiated by the user through a 41 * touch gesture or arrow key/trackball as well as changes that were initiated 42 * programmatically. 43 */ 44 public interface OnSeekBarChangeListener { 45 46 /** 47 * Notification that the progress level has changed. Clients can use the fromUser parameter 48 * to distinguish user-initiated changes from those that occurred programmatically. 49 * 50 * @param seekBar The SeekBar whose progress has changed 51 * @param progress The current progress level. This will be in the range min..max where min 52 * and max were set by {@link ProgressBar#setMin(int)} and 53 * {@link ProgressBar#setMax(int)}, respectively. (The default values for 54 * min is 0 and max is 100.) 55 * @param fromUser True if the progress change was initiated by the user. 56 */ onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)57 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser); 58 59 /** 60 * Notification that the user has started a touch gesture. Clients may want to use this 61 * to disable advancing the seekbar. 62 * @param seekBar The SeekBar in which the touch gesture began 63 */ onStartTrackingTouch(SeekBar seekBar)64 void onStartTrackingTouch(SeekBar seekBar); 65 66 /** 67 * Notification that the user has finished a touch gesture. Clients may want to use this 68 * to re-enable advancing the seekbar. 69 * @param seekBar The SeekBar in which the touch gesture began 70 */ onStopTrackingTouch(SeekBar seekBar)71 void onStopTrackingTouch(SeekBar seekBar); 72 } 73 74 @UnsupportedAppUsage 75 private OnSeekBarChangeListener mOnSeekBarChangeListener; 76 SeekBar(Context context)77 public SeekBar(Context context) { 78 this(context, null); 79 } 80 SeekBar(Context context, AttributeSet attrs)81 public SeekBar(Context context, AttributeSet attrs) { 82 this(context, attrs, com.android.internal.R.attr.seekBarStyle); 83 } 84 SeekBar(Context context, AttributeSet attrs, int defStyleAttr)85 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) { 86 this(context, attrs, defStyleAttr, 0); 87 } 88 SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)89 public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 90 super(context, attrs, defStyleAttr, defStyleRes); 91 } 92 93 @Override 94 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) onProgressRefresh(float scale, boolean fromUser, int progress)95 void onProgressRefresh(float scale, boolean fromUser, int progress) { 96 super.onProgressRefresh(scale, fromUser, progress); 97 98 if (mOnSeekBarChangeListener != null) { 99 mOnSeekBarChangeListener.onProgressChanged(this, progress, fromUser); 100 } 101 } 102 103 /** 104 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also 105 * provides notifications of when the user starts and stops a touch gesture within the SeekBar. 106 * 107 * @param l The seek bar notification listener 108 * 109 * @see SeekBar.OnSeekBarChangeListener 110 */ setOnSeekBarChangeListener(OnSeekBarChangeListener l)111 public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) { 112 mOnSeekBarChangeListener = l; 113 } 114 115 @Override onStartTrackingTouch()116 void onStartTrackingTouch() { 117 super.onStartTrackingTouch(); 118 if (mOnSeekBarChangeListener != null) { 119 mOnSeekBarChangeListener.onStartTrackingTouch(this); 120 } 121 } 122 123 @Override onStopTrackingTouch()124 void onStopTrackingTouch() { 125 super.onStopTrackingTouch(); 126 if (mOnSeekBarChangeListener != null) { 127 mOnSeekBarChangeListener.onStopTrackingTouch(this); 128 } 129 } 130 131 @Override getAccessibilityClassName()132 public CharSequence getAccessibilityClassName() { 133 return SeekBar.class.getName(); 134 } 135 136 /** @hide */ 137 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)138 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 139 super.onInitializeAccessibilityNodeInfoInternal(info); 140 141 if (canUserSetProgress()) { 142 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS); 143 } 144 } 145 } 146