blob: fe2ec69bcaf34d273473cfdf23bdacb0f4a6e6ac [file] [log] [blame]
Joe Onorato1e28f412010-12-01 09:33:36 -08001/*
2 * Copyright (C) 2010 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
17package com.android.systemui.statusbar.policy;
18
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
34public class ToggleSlider extends RelativeLayout
35 implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
36 private static final String TAG = "StatusBar.ToggleSlider";
37
38 public interface Listener {
39 public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value);
40 }
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
49 public ToggleSlider(Context context) {
50 this(context, null);
51 }
52
53 public ToggleSlider(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
55 }
56
57 public ToggleSlider(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 View.inflate(context, R.layout.status_bar_toggle_slider, this);
60
Joe Onorato75362102010-12-02 16:46:12 -080061 final Resources res = context.getResources();
62 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleSlider,
63 defStyle, 0);
64
65 mToggle = (CompoundButton)findViewById(R.id.toggle);
Joe Onorato1e28f412010-12-01 09:33:36 -080066 mToggle.setOnCheckedChangeListener(this);
Joe Onorato75362102010-12-02 16:46:12 -080067 mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button));
Joe Onorato1e28f412010-12-01 09:33:36 -080068
69 mSlider = (SeekBar)findViewById(R.id.slider);
70 mSlider.setOnSeekBarChangeListener(this);
71
Joe Onorato1e28f412010-12-01 09:33:36 -080072 mLabel = (TextView)findViewById(R.id.label);
Joe Onorato75362102010-12-02 16:46:12 -080073 mLabel.setText(a.getString(R.styleable.ToggleSlider_text));
74
75 a.recycle();
Joe Onorato1e28f412010-12-01 09:33:36 -080076 }
77
78 public void onCheckedChanged(CompoundButton toggle, boolean checked) {
79 Drawable thumb;
Joe Onorato4b0912d2010-12-08 15:38:07 -080080 Drawable slider;
Joe Onorato1e28f412010-12-01 09:33:36 -080081 final Resources res = getContext().getResources();
82 if (checked) {
Daniel Sandlere989b322011-11-09 10:34:24 -050083 thumb = res.getDrawable(
84 com.android.internal.R.drawable.scrubber_control_disabled_holo);
85 slider = res.getDrawable(
86 R.drawable.status_bar_settings_slider_disabled);
Joe Onorato1e28f412010-12-01 09:33:36 -080087 } else {
Daniel Sandlere989b322011-11-09 10:34:24 -050088 thumb = res.getDrawable(
89 com.android.internal.R.drawable.scrubber_control_selector_holo);
Joe Onorato4b0912d2010-12-08 15:38:07 -080090 slider = res.getDrawable(
91 com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark);
Joe Onorato1e28f412010-12-01 09:33:36 -080092 }
93 mSlider.setThumb(thumb);
Joe Onorato4b0912d2010-12-08 15:38:07 -080094 mSlider.setProgressDrawable(slider);
Joe Onorato1e28f412010-12-01 09:33:36 -080095
96 if (mListener != null) {
97 mListener.onChanged(this, mTracking, checked, mSlider.getProgress());
98 }
99 }
100
101 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
102 if (mListener != null) {
103 mListener.onChanged(this, mTracking, mToggle.isChecked(), progress);
104 }
105 }
106
107 public void onStartTrackingTouch(SeekBar seekBar) {
108 mTracking = true;
109 if (mListener != null) {
110 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
111 }
112 mToggle.setChecked(false);
113 }
114
115 public void onStopTrackingTouch(SeekBar seekBar) {
116 mTracking = false;
117 if (mListener != null) {
118 mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress());
119 }
120 }
121
122 public void setOnChangedListener(Listener l) {
123 mListener = l;
124 }
125
126 public void setChecked(boolean checked) {
127 mToggle.setChecked(checked);
128 }
129
130 public boolean isChecked() {
131 return mToggle.isChecked();
132 }
133
134 public void setMax(int max) {
135 mSlider.setMax(max);
136 }
137
138 public void setValue(int value) {
139 mSlider.setProgress(value);
140 }
141}
142