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