Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2013 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.settings;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.util.AttributeSet;
     23 import android.view.MotionEvent;
     24 import android.view.View;
     25 import android.widget.CompoundButton;
     26 import android.widget.CompoundButton.OnCheckedChangeListener;
     27 import android.widget.RelativeLayout;
     28 import android.widget.SeekBar;
     29 import android.widget.SeekBar.OnSeekBarChangeListener;
     30 import android.widget.TextView;
     31 
     32 import com.android.systemui.R;
     33 import com.android.systemui.statusbar.policy.BrightnessMirrorController;
     34 
     35 public class ToggleSlider extends RelativeLayout {
     36     public interface Listener {
     37         public void onInit(ToggleSlider v);
     38         public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value,
     39                 boolean stopTracking);
     40     }
     41 
     42     private Listener mListener;
     43     private boolean mTracking;
     44 
     45     private CompoundButton mToggle;
     46     private ToggleSeekBar mSlider;
     47     private TextView mLabel;
     48 
     49     private ToggleSlider mMirror;
     50     private BrightnessMirrorController mMirrorController;
     51 
     52     public ToggleSlider(Context context) {
     53         this(context, null);
     54     }
     55 
     56     public ToggleSlider(Context context, AttributeSet attrs) {
     57         this(context, attrs, 0);
     58     }
     59 
     60     public ToggleSlider(Context context, AttributeSet attrs, int defStyle) {
     61         super(context, attrs, defStyle);
     62 
     63         View.inflate(context, R.layout.status_bar_toggle_slider, this);
     64 
     65         final Resources res = context.getResources();
     66         final TypedArray a = context.obtainStyledAttributes(
     67                 attrs, R.styleable.ToggleSlider, defStyle, 0);
     68 
     69         mToggle = (CompoundButton) findViewById(R.id.toggle);
     70         mToggle.setOnCheckedChangeListener(mCheckListener);
     71 
     72         mSlider = (ToggleSeekBar) findViewById(R.id.slider);
     73         mSlider.setOnSeekBarChangeListener(mSeekListener);
     74 
     75         mLabel = (TextView) findViewById(R.id.label);
     76         mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
     77 
     78         mSlider.setAccessibilityLabel(getContentDescription().toString());
     79 
     80         a.recycle();
     81     }
     82 
     83     public void setMirror(ToggleSlider toggleSlider) {
     84         mMirror = toggleSlider;
     85         if (mMirror != null) {
     86             mMirror.setChecked(mToggle.isChecked());
     87             mMirror.setMax(mSlider.getMax());
     88             mMirror.setValue(mSlider.getProgress());
     89         }
     90     }
     91 
     92     public void setMirrorController(BrightnessMirrorController c) {
     93         mMirrorController = c;
     94     }
     95 
     96     @Override
     97     protected void onAttachedToWindow() {
     98         super.onAttachedToWindow();
     99         if (mListener != null) {
    100             mListener.onInit(this);
    101         }
    102     }
    103 
    104     public void setOnChangedListener(Listener l) {
    105         mListener = l;
    106     }
    107 
    108     public void setChecked(boolean checked) {
    109         mToggle.setChecked(checked);
    110     }
    111 
    112     public boolean isChecked() {
    113         return mToggle.isChecked();
    114     }
    115 
    116     public void setMax(int max) {
    117         mSlider.setMax(max);
    118         if (mMirror != null) {
    119             mMirror.setMax(max);
    120         }
    121     }
    122 
    123     public void setValue(int value) {
    124         mSlider.setProgress(value);
    125         if (mMirror != null) {
    126             mMirror.setValue(value);
    127         }
    128     }
    129 
    130     @Override
    131     public boolean dispatchTouchEvent(MotionEvent ev) {
    132         if (mMirror != null) {
    133             MotionEvent copy = ev.copy();
    134             mMirror.dispatchTouchEvent(copy);
    135             copy.recycle();
    136         }
    137         return super.dispatchTouchEvent(ev);
    138     }
    139 
    140     private final OnCheckedChangeListener mCheckListener = new OnCheckedChangeListener() {
    141         @Override
    142         public void onCheckedChanged(CompoundButton toggle, boolean checked) {
    143             mSlider.setEnabled(!checked);
    144 
    145             if (mListener != null) {
    146                 mListener.onChanged(
    147                         ToggleSlider.this, mTracking, checked, mSlider.getProgress(), false);
    148             }
    149 
    150             if (mMirror != null) {
    151                 mMirror.mToggle.setChecked(checked);
    152             }
    153         }
    154     };
    155 
    156     private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
    157         @Override
    158         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    159             if (mListener != null) {
    160                 mListener.onChanged(
    161                         ToggleSlider.this, mTracking, mToggle.isChecked(), progress, false);
    162             }
    163         }
    164 
    165         @Override
    166         public void onStartTrackingTouch(SeekBar seekBar) {
    167             mTracking = true;
    168 
    169             if (mListener != null) {
    170                 mListener.onChanged(ToggleSlider.this, mTracking, mToggle.isChecked(),
    171                         mSlider.getProgress(), false);
    172             }
    173 
    174             mToggle.setChecked(false);
    175 
    176             if (mMirrorController != null) {
    177                 mMirrorController.showMirror();
    178                 mMirrorController.setLocation((View) getParent());
    179             }
    180         }
    181 
    182         @Override
    183         public void onStopTrackingTouch(SeekBar seekBar) {
    184             mTracking = false;
    185 
    186             if (mListener != null) {
    187                 mListener.onChanged(ToggleSlider.this, mTracking, mToggle.isChecked(),
    188                         mSlider.getProgress(), true);
    189             }
    190 
    191             if (mMirrorController != null) {
    192                 mMirrorController.hideMirror();
    193             }
    194         }
    195     };
    196 }
    197 
    198