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