Home | History | Annotate | Download | only in policy
      1 /*
      2  * Copyright (C) 2010 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 com.android.systemui.statusbar.policy;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.graphics.drawable.Drawable;
     23 import android.util.AttributeSet;
     24 import android.util.Slog;
     25 import android.view.View;
     26 import android.widget.CompoundButton;
     27 import android.widget.ProgressBar;
     28 import android.widget.RelativeLayout;
     29 import android.widget.SeekBar;
     30 import android.widget.TextView;
     31 
     32 import com.android.systemui.R;
     33 
     34 public class ToggleSlider extends RelativeLayout
     35         implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
     36     private static final String TAG = "StatusBar.ToggleSlider";
     37 
     38     public interface Listener {
     39         public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value);
     40     }
     41 
     42     private Listener mListener;
     43     private boolean mTracking;
     44 
     45     private CompoundButton mToggle;
     46     private SeekBar mSlider;
     47     private TextView mLabel;
     48 
     49     public ToggleSlider(Context context) {
     50         this(context, null);
     51     }
     52 
     53     public ToggleSlider(Context context, AttributeSet attrs) {
     54         this(context, attrs, 0);
     55     }
     56 
     57     public ToggleSlider(Context context, AttributeSet attrs, int defStyle) {
     58         super(context, attrs, defStyle);
     59         View.inflate(context, R.layout.status_bar_toggle_slider, this);
     60 
     61         final Resources res = context.getResources();
     62         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleSlider,
     63                 defStyle, 0);
     64 
     65         mToggle = (CompoundButton)findViewById(R.id.toggle);
     66         mToggle.setOnCheckedChangeListener(this);
     67         mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button));
     68 
     69         mSlider = (SeekBar)findViewById(R.id.slider);
     70         mSlider.setOnSeekBarChangeListener(this);
     71 
     72         mLabel = (TextView)findViewById(R.id.label);
     73         mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
     74 
     75         a.recycle();
     76     }
     77 
     78     public void onCheckedChanged(CompoundButton toggle, boolean checked) {
     79         Drawable thumb;
     80         Drawable slider;
     81         final Resources res = getContext().getResources();
     82         if (checked) {
     83             thumb = res.getDrawable(
     84                     com.android.internal.R.drawable.scrubber_control_disabled_holo);
     85             slider = res.getDrawable(
     86                     R.drawable.status_bar_settings_slider_disabled);
     87         } else {
     88             thumb = res.getDrawable(
     89                     com.android.internal.R.drawable.scrubber_control_selector_holo);
     90             slider = res.getDrawable(
     91                     com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark);
     92         }
     93         mSlider.setThumb(thumb);
     94         mSlider.setProgressDrawable(slider);
     95 
     96         if (mListener != null) {
     97             mListener.onChanged(this, mTracking, checked, mSlider.getProgress());
     98         }
     99     }
    100 
    101     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    102         if (mListener != null) {
    103             mListener.onChanged(this, mTracking, mToggle.isChecked(), progress);
    104         }
    105     }
    106 
    107     public void onStartTrackingTouch(SeekBar seekBar) {
    108         mTracking = true;
    109         if (mListener != null) {
    110             mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
    111         }
    112         mToggle.setChecked(false);
    113     }
    114 
    115     public void onStopTrackingTouch(SeekBar seekBar) {
    116         mTracking = false;
    117         if (mListener != null) {
    118             mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
    119         }
    120     }
    121 
    122     public void setOnChangedListener(Listener l) {
    123         mListener = l;
    124     }
    125 
    126     public void setChecked(boolean checked) {
    127         mToggle.setChecked(checked);
    128     }
    129 
    130     public boolean isChecked() {
    131         return mToggle.isChecked();
    132     }
    133 
    134     public void setMax(int max) {
    135         mSlider.setMax(max);
    136     }
    137 
    138     public void setValue(int value) {
    139         mSlider.setProgress(value);
    140     }
    141 }
    142 
    143