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 { |
| 39 | public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value); |
| 40 | } |
| 41 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 42 | private Listener mListener; |
| 43 | private boolean mTracking; |
| 44 | |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 45 | private CompoundButton mToggle; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 46 | 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 Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 61 | 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 Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 66 | mToggle.setOnCheckedChangeListener(this); |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 67 | mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button)); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 68 | |
| 69 | mSlider = (SeekBar)findViewById(R.id.slider); |
| 70 | mSlider.setOnSeekBarChangeListener(this); |
| 71 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 72 | mLabel = (TextView)findViewById(R.id.label); |
Joe Onorato | 7536210 | 2010-12-02 16:46:12 -0800 | [diff] [blame] | 73 | mLabel.setText(a.getString(R.styleable.ToggleSlider_text)); |
| 74 | |
| 75 | a.recycle(); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public void onCheckedChanged(CompoundButton toggle, boolean checked) { |
| 79 | Drawable thumb; |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 80 | Drawable slider; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 81 | final Resources res = getContext().getResources(); |
| 82 | if (checked) { |
Daniel Sandler | e989b32 | 2011-11-09 10:34:24 -0500 | [diff] [blame] | 83 | 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 Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 87 | } else { |
Daniel Sandler | e989b32 | 2011-11-09 10:34:24 -0500 | [diff] [blame] | 88 | thumb = res.getDrawable( |
| 89 | com.android.internal.R.drawable.scrubber_control_selector_holo); |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 90 | slider = res.getDrawable( |
| 91 | com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 92 | } |
| 93 | mSlider.setThumb(thumb); |
Joe Onorato | 4b0912d | 2010-12-08 15:38:07 -0800 | [diff] [blame] | 94 | mSlider.setProgressDrawable(slider); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 95 | |
| 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 | |