blob: d247711df41fb9ca744bcba441167dc2fbb0d86e [file] [log] [blame]
Joe Onorato1e28f412010-12-01 09:33:36 -08001/*
Michael Wright0087a142013-02-05 16:29:39 -08002 * Copyright (C) 2013 The Android Open Source Project
Joe Onorato1e28f412010-12-01 09:33:36 -08003 *
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
Michael Wright0087a142013-02-05 16:29:39 -080017package com.android.systemui.settings;
Joe Onorato1e28f412010-12-01 09:33:36 -080018
19import android.content.Context;
20import android.content.res.Resources;
Joe Onorato75362102010-12-02 16:46:12 -080021import android.content.res.TypedArray;
Joe Onorato1e28f412010-12-01 09:33:36 -080022import android.util.AttributeSet;
Adrian Roos27a2ce22015-06-02 13:44:46 -070023import android.view.MotionEvent;
Joe Onorato1e28f412010-12-01 09:33:36 -080024import android.view.View;
25import android.widget.CompoundButton;
Alan Viverettef6a67982014-04-08 15:27:24 -070026import android.widget.CompoundButton.OnCheckedChangeListener;
Joe Onorato1e28f412010-12-01 09:33:36 -080027import android.widget.RelativeLayout;
28import android.widget.SeekBar;
Alan Viverettef6a67982014-04-08 15:27:24 -070029import android.widget.SeekBar.OnSeekBarChangeListener;
Joe Onorato1e28f412010-12-01 09:33:36 -080030import android.widget.TextView;
Joe Onorato1e28f412010-12-01 09:33:36 -080031
32import com.android.systemui.R;
Adrian Roos5fd872e2014-08-12 17:28:58 +020033import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Joe Onorato1e28f412010-12-01 09:33:36 -080034
Alan Viverettef6a67982014-04-08 15:27:24 -070035public class ToggleSlider extends RelativeLayout {
Joe Onorato1e28f412010-12-01 09:33:36 -080036 public interface Listener {
John Spurlock48f37ec2012-10-05 16:32:51 -040037 public void onInit(ToggleSlider v);
Jason Monk97b87a92015-06-23 15:04:44 -040038 public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value,
39 boolean stopTracking);
Joe Onorato1e28f412010-12-01 09:33:36 -080040 }
41
Joe Onorato1e28f412010-12-01 09:33:36 -080042 private Listener mListener;
43 private boolean mTracking;
44
Joe Onorato75362102010-12-02 16:46:12 -080045 private CompoundButton mToggle;
Joe Onorato1e28f412010-12-01 09:33:36 -080046 private SeekBar mSlider;
47 private TextView mLabel;
48
Adrian Roos5fd872e2014-08-12 17:28:58 +020049 private ToggleSlider mMirror;
50 private BrightnessMirrorController mMirrorController;
51
Joe Onorato1e28f412010-12-01 09:33:36 -080052 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);
Alan Viverettef6a67982014-04-08 15:27:24 -070062
Joe Onorato1e28f412010-12-01 09:33:36 -080063 View.inflate(context, R.layout.status_bar_toggle_slider, this);
64
Joe Onorato75362102010-12-02 16:46:12 -080065 final Resources res = context.getResources();
Alan Viverettef6a67982014-04-08 15:27:24 -070066 final TypedArray a = context.obtainStyledAttributes(
67 attrs, R.styleable.ToggleSlider, defStyle, 0);
Joe Onorato75362102010-12-02 16:46:12 -080068
Alan Viverettef6a67982014-04-08 15:27:24 -070069 mToggle = (CompoundButton) findViewById(R.id.toggle);
70 mToggle.setOnCheckedChangeListener(mCheckListener);
Joe Onorato1e28f412010-12-01 09:33:36 -080071
Alan Viverettef6a67982014-04-08 15:27:24 -070072 mSlider = (SeekBar) findViewById(R.id.slider);
73 mSlider.setOnSeekBarChangeListener(mSeekListener);
Joe Onorato1e28f412010-12-01 09:33:36 -080074
Alan Viverettef6a67982014-04-08 15:27:24 -070075 mLabel = (TextView) findViewById(R.id.label);
Joe Onorato75362102010-12-02 16:46:12 -080076 mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
77
Dan Sandler187fe172015-06-11 09:56:24 -040078 setLabelFor(R.id.slider); // use our a11y text to annotate, not replace, the slider's
79
Joe Onorato75362102010-12-02 16:46:12 -080080 a.recycle();
Joe Onorato1e28f412010-12-01 09:33:36 -080081 }
82
Adrian Roos5fd872e2014-08-12 17:28:58 +020083 public void setMirror(ToggleSlider toggleSlider) {
84 mMirror = toggleSlider;
85 if (mMirror != null) {
Adrian Roos959409b2014-08-13 17:58:01 +020086 mMirror.setChecked(mToggle.isChecked());
87 mMirror.setMax(mSlider.getMax());
88 mMirror.setValue(mSlider.getProgress());
Adrian Roos5fd872e2014-08-12 17:28:58 +020089 }
90 }
91
92 public void setMirrorController(BrightnessMirrorController c) {
93 mMirrorController = c;
94 }
95
John Spurlock48f37ec2012-10-05 16:32:51 -040096 @Override
97 protected void onAttachedToWindow() {
98 super.onAttachedToWindow();
99 if (mListener != null) {
100 mListener.onInit(this);
101 }
102 }
103
Joe Onorato1e28f412010-12-01 09:33:36 -0800104 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);
Adrian Roos959409b2014-08-13 17:58:01 +0200118 if (mMirror != null) {
119 mMirror.setMax(max);
120 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800121 }
122
123 public void setValue(int value) {
124 mSlider.setProgress(value);
Adrian Roos959409b2014-08-13 17:58:01 +0200125 if (mMirror != null) {
126 mMirror.setValue(value);
127 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800128 }
Alan Viverettef6a67982014-04-08 15:27:24 -0700129
Adrian Roos27a2ce22015-06-02 13:44:46 -0700130 @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
Alan Viverettef6a67982014-04-08 15:27:24 -0700140 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(
Jason Monk97b87a92015-06-23 15:04:44 -0400147 ToggleSlider.this, mTracking, checked, mSlider.getProgress(), false);
Alan Viverettef6a67982014-04-08 15:27:24 -0700148 }
Adrian Roos5fd872e2014-08-12 17:28:58 +0200149
150 if (mMirror != null) {
151 mMirror.mToggle.setChecked(checked);
152 }
Alan Viverettef6a67982014-04-08 15:27:24 -0700153 }
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(
Jason Monk97b87a92015-06-23 15:04:44 -0400161 ToggleSlider.this, mTracking, mToggle.isChecked(), progress, false);
Alan Viverettef6a67982014-04-08 15:27:24 -0700162 }
163 }
164
165 @Override
166 public void onStartTrackingTouch(SeekBar seekBar) {
167 mTracking = true;
168
169 if (mListener != null) {
Jason Monk97b87a92015-06-23 15:04:44 -0400170 mListener.onChanged(ToggleSlider.this, mTracking, mToggle.isChecked(),
171 mSlider.getProgress(), false);
Alan Viverettef6a67982014-04-08 15:27:24 -0700172 }
173
174 mToggle.setChecked(false);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200175
Adrian Roos5fd872e2014-08-12 17:28:58 +0200176 if (mMirrorController != null) {
177 mMirrorController.showMirror();
178 mMirrorController.setLocation((View) getParent());
179 }
Alan Viverettef6a67982014-04-08 15:27:24 -0700180 }
181
182 @Override
183 public void onStopTrackingTouch(SeekBar seekBar) {
184 mTracking = false;
185
186 if (mListener != null) {
Jason Monk97b87a92015-06-23 15:04:44 -0400187 mListener.onChanged(ToggleSlider.this, mTracking, mToggle.isChecked(),
188 mSlider.getProgress(), true);
Alan Viverettef6a67982014-04-08 15:27:24 -0700189 }
Adrian Roos5fd872e2014-08-12 17:28:58 +0200190
Adrian Roos5fd872e2014-08-12 17:28:58 +0200191 if (mMirrorController != null) {
192 mMirrorController.hideMirror();
193 }
Alan Viverettef6a67982014-04-08 15:27:24 -0700194 }
195 };
Joe Onorato1e28f412010-12-01 09:33:36 -0800196}
197