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