blob: c7c361c4b4a747f24a9f1db1ed94ff05c578ac04 [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;
24import android.util.Slog;
25import android.view.View;
26import android.widget.CompoundButton;
Joe Onorato4b0912d2010-12-08 15:38:07 -080027import android.widget.ProgressBar;
Joe Onorato1e28f412010-12-01 09:33:36 -080028import android.widget.RelativeLayout;
29import android.widget.SeekBar;
30import android.widget.TextView;
Joe Onorato1e28f412010-12-01 09:33:36 -080031
32import com.android.systemui.R;
33
Michael Wright0087a142013-02-05 16:29:39 -080034public class ToggleSlider extends RelativeLayout
Joe Onorato1e28f412010-12-01 09:33:36 -080035 implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
36 private static final String TAG = "StatusBar.ToggleSlider";
37
38 public interface Listener {
John Spurlock48f37ec2012-10-05 16:32:51 -040039 public void onInit(ToggleSlider v);
Joe Onorato1e28f412010-12-01 09:33:36 -080040 public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value);
41 }
42
Joe Onorato1e28f412010-12-01 09:33:36 -080043 private Listener mListener;
44 private boolean mTracking;
45
Joe Onorato75362102010-12-02 16:46:12 -080046 private CompoundButton mToggle;
Joe Onorato1e28f412010-12-01 09:33:36 -080047 private SeekBar mSlider;
48 private TextView mLabel;
49
50 public ToggleSlider(Context context) {
51 this(context, null);
52 }
53
54 public ToggleSlider(Context context, AttributeSet attrs) {
55 this(context, attrs, 0);
56 }
57
58 public ToggleSlider(Context context, AttributeSet attrs, int defStyle) {
59 super(context, attrs, defStyle);
60 View.inflate(context, R.layout.status_bar_toggle_slider, this);
61
Joe Onorato75362102010-12-02 16:46:12 -080062 final Resources res = context.getResources();
63 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleSlider,
64 defStyle, 0);
65
66 mToggle = (CompoundButton)findViewById(R.id.toggle);
Joe Onorato1e28f412010-12-01 09:33:36 -080067 mToggle.setOnCheckedChangeListener(this);
Joe Onorato75362102010-12-02 16:46:12 -080068 mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button));
Joe Onorato1e28f412010-12-01 09:33:36 -080069
70 mSlider = (SeekBar)findViewById(R.id.slider);
71 mSlider.setOnSeekBarChangeListener(this);
72
Joe Onorato1e28f412010-12-01 09:33:36 -080073 mLabel = (TextView)findViewById(R.id.label);
Joe Onorato75362102010-12-02 16:46:12 -080074 mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
75
76 a.recycle();
Joe Onorato1e28f412010-12-01 09:33:36 -080077 }
78
John Spurlock48f37ec2012-10-05 16:32:51 -040079 @Override
80 protected void onAttachedToWindow() {
81 super.onAttachedToWindow();
82 if (mListener != null) {
83 mListener.onInit(this);
84 }
85 }
86
Joe Onorato1e28f412010-12-01 09:33:36 -080087 public void onCheckedChanged(CompoundButton toggle, boolean checked) {
88 Drawable thumb;
Joe Onorato4b0912d2010-12-08 15:38:07 -080089 Drawable slider;
Joe Onorato1e28f412010-12-01 09:33:36 -080090 final Resources res = getContext().getResources();
91 if (checked) {
Daniel Sandlere989b322011-11-09 10:34:24 -050092 thumb = res.getDrawable(
93 com.android.internal.R.drawable.scrubber_control_disabled_holo);
94 slider = res.getDrawable(
95 R.drawable.status_bar_settings_slider_disabled);
Joe Onorato1e28f412010-12-01 09:33:36 -080096 } else {
Daniel Sandlere989b322011-11-09 10:34:24 -050097 thumb = res.getDrawable(
98 com.android.internal.R.drawable.scrubber_control_selector_holo);
Joe Onorato4b0912d2010-12-08 15:38:07 -080099 slider = res.getDrawable(
100 com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark);
Joe Onorato1e28f412010-12-01 09:33:36 -0800101 }
102 mSlider.setThumb(thumb);
Joe Onorato4b0912d2010-12-08 15:38:07 -0800103 mSlider.setProgressDrawable(slider);
Joe Onorato1e28f412010-12-01 09:33:36 -0800104
105 if (mListener != null) {
106 mListener.onChanged(this, mTracking, checked, mSlider.getProgress());
107 }
108 }
109
110 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
111 if (mListener != null) {
112 mListener.onChanged(this, mTracking, mToggle.isChecked(), progress);
113 }
114 }
115
116 public void onStartTrackingTouch(SeekBar seekBar) {
117 mTracking = true;
118 if (mListener != null) {
119 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
120 }
121 mToggle.setChecked(false);
122 }
123
124 public void onStopTrackingTouch(SeekBar seekBar) {
125 mTracking = false;
126 if (mListener != null) {
127 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
128 }
129 }
130
131 public void setOnChangedListener(Listener l) {
132 mListener = l;
133 }
134
135 public void setChecked(boolean checked) {
136 mToggle.setChecked(checked);
137 }
138
139 public boolean isChecked() {
140 return mToggle.isChecked();
141 }
142
143 public void setMax(int max) {
144 mSlider.setMax(max);
145 }
146
147 public void setValue(int value) {
148 mSlider.setProgress(value);
149 }
150}
151