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