Home | History | Annotate | Download | only in preference
      1 /*
      2  * Copyright (C) 2011 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.preference;
     18 
     19 import android.annotation.UnsupportedAppUsage;
     20 import android.content.Context;
     21 import android.content.res.TypedArray;
     22 import android.os.Parcel;
     23 import android.os.Parcelable;
     24 import android.util.AttributeSet;
     25 import android.view.KeyEvent;
     26 import android.view.View;
     27 import android.widget.SeekBar;
     28 import android.widget.SeekBar.OnSeekBarChangeListener;
     29 
     30 /**
     31  * @hide
     32  *
     33  * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
     34  *      <a href="{@docRoot}reference/androidx/preference/package-summary.html">
     35  *      Preference Library</a> for consistent behavior across all devices. For more information on
     36  *      using the AndroidX Preference Library see
     37  *      <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
     38  */
     39 @Deprecated
     40 public class SeekBarPreference extends Preference
     41         implements OnSeekBarChangeListener {
     42 
     43     private int mProgress;
     44     private int mMax;
     45     private boolean mTrackingTouch;
     46 
     47     public SeekBarPreference(
     48             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
     49         super(context, attrs, defStyleAttr, defStyleRes);
     50 
     51         TypedArray a = context.obtainStyledAttributes(
     52                 attrs, com.android.internal.R.styleable.ProgressBar, defStyleAttr, defStyleRes);
     53         setMax(a.getInt(com.android.internal.R.styleable.ProgressBar_max, mMax));
     54         a.recycle();
     55 
     56         a = context.obtainStyledAttributes(attrs,
     57                 com.android.internal.R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
     58         final int layoutResId = a.getResourceId(
     59                 com.android.internal.R.styleable.SeekBarPreference_layout,
     60                 com.android.internal.R.layout.preference_widget_seekbar);
     61         a.recycle();
     62 
     63         setLayoutResource(layoutResId);
     64     }
     65 
     66     @UnsupportedAppUsage
     67     public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     68         this(context, attrs, defStyleAttr, 0);
     69     }
     70 
     71     @UnsupportedAppUsage
     72     public SeekBarPreference(Context context, AttributeSet attrs) {
     73         this(context, attrs, com.android.internal.R.attr.seekBarPreferenceStyle);
     74     }
     75 
     76     @UnsupportedAppUsage
     77     public SeekBarPreference(Context context) {
     78         this(context, null);
     79     }
     80 
     81     @Override
     82     protected void onBindView(View view) {
     83         super.onBindView(view);
     84         SeekBar seekBar = (SeekBar) view.findViewById(
     85                 com.android.internal.R.id.seekbar);
     86         seekBar.setOnSeekBarChangeListener(this);
     87         seekBar.setMax(mMax);
     88         seekBar.setProgress(mProgress);
     89         seekBar.setEnabled(isEnabled());
     90     }
     91 
     92     @Override
     93     protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
     94         setProgress(restoreValue ? getPersistedInt(mProgress)
     95                 : (Integer) defaultValue);
     96     }
     97 
     98     @Override
     99     protected Object onGetDefaultValue(TypedArray a, int index) {
    100         return a.getInt(index, 0);
    101     }
    102 
    103     @Override
    104     public boolean onKey(View v, int keyCode, KeyEvent event) {
    105         if (event.getAction() != KeyEvent.ACTION_DOWN) {
    106             return false;
    107         }
    108 
    109         SeekBar seekBar = (SeekBar) v.findViewById(com.android.internal.R.id.seekbar);
    110         if (seekBar == null) {
    111             return false;
    112         }
    113         return seekBar.onKeyDown(keyCode, event);
    114     }
    115 
    116     public void setMax(int max) {
    117         if (max != mMax) {
    118             mMax = max;
    119             notifyChanged();
    120         }
    121     }
    122 
    123     public void setProgress(int progress) {
    124         setProgress(progress, true);
    125     }
    126 
    127     private void setProgress(int progress, boolean notifyChanged) {
    128         if (progress > mMax) {
    129             progress = mMax;
    130         }
    131         if (progress < 0) {
    132             progress = 0;
    133         }
    134         if (progress != mProgress) {
    135             mProgress = progress;
    136             persistInt(progress);
    137             if (notifyChanged) {
    138                 notifyChanged();
    139             }
    140         }
    141     }
    142 
    143     public int getProgress() {
    144         return mProgress;
    145     }
    146 
    147     /**
    148      * Persist the seekBar's progress value if callChangeListener
    149      * returns true, otherwise set the seekBar's progress to the stored value
    150      */
    151     void syncProgress(SeekBar seekBar) {
    152         int progress = seekBar.getProgress();
    153         if (progress != mProgress) {
    154             if (callChangeListener(progress)) {
    155                 setProgress(progress, false);
    156             } else {
    157                 seekBar.setProgress(mProgress);
    158             }
    159         }
    160     }
    161 
    162     @Override
    163     public void onProgressChanged(
    164             SeekBar seekBar, int progress, boolean fromUser) {
    165         if (fromUser && !mTrackingTouch) {
    166             syncProgress(seekBar);
    167         }
    168     }
    169 
    170     @Override
    171     public void onStartTrackingTouch(SeekBar seekBar) {
    172         mTrackingTouch = true;
    173     }
    174 
    175     @Override
    176     public void onStopTrackingTouch(SeekBar seekBar) {
    177         mTrackingTouch = false;
    178         if (seekBar.getProgress() != mProgress) {
    179             syncProgress(seekBar);
    180         }
    181     }
    182 
    183     @Override
    184     protected Parcelable onSaveInstanceState() {
    185         /*
    186          * Suppose a client uses this preference type without persisting. We
    187          * must save the instance state so it is able to, for example, survive
    188          * orientation changes.
    189          */
    190 
    191         final Parcelable superState = super.onSaveInstanceState();
    192         if (isPersistent()) {
    193             // No need to save instance state since it's persistent
    194             return superState;
    195         }
    196 
    197         // Save the instance state
    198         final SavedState myState = new SavedState(superState);
    199         myState.progress = mProgress;
    200         myState.max = mMax;
    201         return myState;
    202     }
    203 
    204     @Override
    205     protected void onRestoreInstanceState(Parcelable state) {
    206         if (!state.getClass().equals(SavedState.class)) {
    207             // Didn't save state for us in onSaveInstanceState
    208             super.onRestoreInstanceState(state);
    209             return;
    210         }
    211 
    212         // Restore the instance state
    213         SavedState myState = (SavedState) state;
    214         super.onRestoreInstanceState(myState.getSuperState());
    215         mProgress = myState.progress;
    216         mMax = myState.max;
    217         notifyChanged();
    218     }
    219 
    220     /**
    221      * SavedState, a subclass of {@link BaseSavedState}, will store the state
    222      * of MyPreference, a subclass of Preference.
    223      * <p>
    224      * It is important to always call through to super methods.
    225      */
    226     private static class SavedState extends BaseSavedState {
    227         int progress;
    228         int max;
    229 
    230         public SavedState(Parcel source) {
    231             super(source);
    232 
    233             // Restore the click counter
    234             progress = source.readInt();
    235             max = source.readInt();
    236         }
    237 
    238         @Override
    239         public void writeToParcel(Parcel dest, int flags) {
    240             super.writeToParcel(dest, flags);
    241 
    242             // Save the click counter
    243             dest.writeInt(progress);
    244             dest.writeInt(max);
    245         }
    246 
    247         public SavedState(Parcelable superState) {
    248             super(superState);
    249         }
    250 
    251         @SuppressWarnings("unused")
    252         public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
    253                 new Parcelable.Creator<SavedState>() {
    254             public SavedState createFromParcel(Parcel in) {
    255                 return new SavedState(in);
    256             }
    257 
    258             public SavedState[] newArray(int size) {
    259                 return new SavedState[size];
    260             }
    261         };
    262     }
    263 }
    264