blob: d584043eb7cb82b650f1fb1ccf7a804317a9019d [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.graphics.drawable.Drawable;
23import android.util.AttributeSet;
Joe Onorato1e28f412010-12-01 09:33:36 -080024import android.view.View;
25import android.widget.CompoundButton;
26import android.widget.RelativeLayout;
27import android.widget.SeekBar;
28import android.widget.TextView;
Joe Onorato1e28f412010-12-01 09:33:36 -080029
30import com.android.systemui.R;
31
Michael Wright0087a142013-02-05 16:29:39 -080032public class ToggleSlider extends RelativeLayout
Joe Onorato1e28f412010-12-01 09:33:36 -080033 implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
34 private static final String TAG = "StatusBar.ToggleSlider";
35
36 public interface Listener {
John Spurlock48f37ec2012-10-05 16:32:51 -040037 public void onInit(ToggleSlider v);
Joe Onorato1e28f412010-12-01 09:33:36 -080038 public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value);
39 }
40
Joe Onorato1e28f412010-12-01 09:33:36 -080041 private Listener mListener;
42 private boolean mTracking;
43
Joe Onorato75362102010-12-02 16:46:12 -080044 private CompoundButton mToggle;
Joe Onorato1e28f412010-12-01 09:33:36 -080045 private SeekBar mSlider;
46 private TextView mLabel;
47
48 public ToggleSlider(Context context) {
49 this(context, null);
50 }
51
52 public ToggleSlider(Context context, AttributeSet attrs) {
53 this(context, attrs, 0);
54 }
55
56 public ToggleSlider(Context context, AttributeSet attrs, int defStyle) {
57 super(context, attrs, defStyle);
58 View.inflate(context, R.layout.status_bar_toggle_slider, this);
59
Joe Onorato75362102010-12-02 16:46:12 -080060 final Resources res = context.getResources();
61 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleSlider,
62 defStyle, 0);
63
64 mToggle = (CompoundButton)findViewById(R.id.toggle);
Joe Onorato1e28f412010-12-01 09:33:36 -080065 mToggle.setOnCheckedChangeListener(this);
Joe Onorato75362102010-12-02 16:46:12 -080066 mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button));
Joe Onorato1e28f412010-12-01 09:33:36 -080067
68 mSlider = (SeekBar)findViewById(R.id.slider);
69 mSlider.setOnSeekBarChangeListener(this);
70
Joe Onorato1e28f412010-12-01 09:33:36 -080071 mLabel = (TextView)findViewById(R.id.label);
Joe Onorato75362102010-12-02 16:46:12 -080072 mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
73
74 a.recycle();
Joe Onorato1e28f412010-12-01 09:33:36 -080075 }
76
John Spurlock48f37ec2012-10-05 16:32:51 -040077 @Override
78 protected void onAttachedToWindow() {
79 super.onAttachedToWindow();
80 if (mListener != null) {
81 mListener.onInit(this);
82 }
83 }
84
Joe Onorato1e28f412010-12-01 09:33:36 -080085 public void onCheckedChanged(CompoundButton toggle, boolean checked) {
86 Drawable thumb;
Joe Onorato4b0912d2010-12-08 15:38:07 -080087 Drawable slider;
Joe Onorato1e28f412010-12-01 09:33:36 -080088 final Resources res = getContext().getResources();
89 if (checked) {
Daniel Sandlere989b322011-11-09 10:34:24 -050090 thumb = res.getDrawable(
91 com.android.internal.R.drawable.scrubber_control_disabled_holo);
92 slider = res.getDrawable(
93 R.drawable.status_bar_settings_slider_disabled);
Joe Onorato1e28f412010-12-01 09:33:36 -080094 } else {
Daniel Sandlere989b322011-11-09 10:34:24 -050095 thumb = res.getDrawable(
96 com.android.internal.R.drawable.scrubber_control_selector_holo);
Joe Onorato4b0912d2010-12-08 15:38:07 -080097 slider = res.getDrawable(
98 com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark);
Joe Onorato1e28f412010-12-01 09:33:36 -080099 }
100 mSlider.setThumb(thumb);
Joe Onorato4b0912d2010-12-08 15:38:07 -0800101 mSlider.setProgressDrawable(slider);
Joe Onorato1e28f412010-12-01 09:33:36 -0800102
103 if (mListener != null) {
104 mListener.onChanged(this, mTracking, checked, mSlider.getProgress());
105 }
106 }
107
108 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
109 if (mListener != null) {
110 mListener.onChanged(this, mTracking, mToggle.isChecked(), progress);
111 }
112 }
113
114 public void onStartTrackingTouch(SeekBar seekBar) {
115 mTracking = true;
116 if (mListener != null) {
117 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
118 }
119 mToggle.setChecked(false);
120 }
121
122 public void onStopTrackingTouch(SeekBar seekBar) {
123 mTracking = false;
124 if (mListener != null) {
125 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
126 }
127 }
128
129 public void setOnChangedListener(Listener l) {
130 mListener = l;
131 }
132
133 public void setChecked(boolean checked) {
134 mToggle.setChecked(checked);
135 }
136
137 public boolean isChecked() {
138 return mToggle.isChecked();
139 }
140
141 public void setMax(int max) {
142 mSlider.setMax(max);
143 }
144
145 public void setValue(int value) {
146 mSlider.setProgress(value);
147 }
148}
149