Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.util.Slog; |
| 25 | import android.view.View; |
| 26 | import android.widget.CompoundButton; |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 27 | import android.widget.ProgressBar; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 28 | import android.widget.RelativeLayout; |
| 29 | import android.widget.SeekBar; |
| 30 | import android.widget.TextView; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 31 | |
| 32 | import com.android.systemui.R; |
| 33 | |
| 34 | public class ToggleSlider extends RelativeLayout |
| 35 | implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener { |
| 36 | private static final String TAG = "StatusBar.ToggleSlider"; |
| 37 | |
| 38 | public interface Listener { |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 39 | public void onInit(ToggleSlider v); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 40 | public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value); |
| 41 | } |
| 42 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 43 | private Listener mListener; |
| 44 | private boolean mTracking; |
| 45 | |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 46 | private CompoundButton mToggle; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 47 | 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 Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 62 | 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 Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 67 | mToggle.setOnCheckedChangeListener(this); |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 68 | mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button)); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 69 | |
| 70 | mSlider = (SeekBar)findViewById(R.id.slider); |
| 71 | mSlider.setOnSeekBarChangeListener(this); |
| 72 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 73 | mLabel = (TextView)findViewById(R.id.label); |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 74 | mLabel.setText(a.getString(R.styleable.ToggleSlider_text)); |
| 75 | |
| 76 | a.recycle(); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 77 | } |
| 78 | |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 79 | @Override |
| 80 | protected void onAttachedToWindow() { |
| 81 | super.onAttachedToWindow(); |
| 82 | if (mListener != null) { |
| 83 | mListener.onInit(this); |
| 84 | } |
| 85 | } |
| 86 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 87 | public void onCheckedChanged(CompoundButton toggle, boolean checked) { |
| 88 | Drawable thumb; |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 89 | Drawable slider; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 90 | final Resources res = getContext().getResources(); |
| 91 | if (checked) { |
Daniel Sandler | e989b32 | 2011-11-09 10:34:24 -0500 | [diff] [blame] | 92 | 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 Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 96 | } else { |
Daniel Sandler | e989b32 | 2011-11-09 10:34:24 -0500 | [diff] [blame] | 97 | thumb = res.getDrawable( |
| 98 | com.android.internal.R.drawable.scrubber_control_selector_holo); |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 99 | slider = res.getDrawable( |
| 100 | com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 101 | } |
| 102 | mSlider.setThumb(thumb); |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 103 | mSlider.setProgressDrawable(slider); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 104 | |
| 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 | |