1/* 2 * Copyright (C) 2013 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.settings; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.content.res.TypedArray; 22import android.graphics.drawable.Drawable; 23import android.util.AttributeSet; 24import android.view.View; 25import android.widget.CompoundButton; 26import android.widget.RelativeLayout; 27import android.widget.SeekBar; 28import android.widget.TextView; 29 30import com.android.systemui.R; 31 32public class ToggleSlider extends RelativeLayout 33 implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener { 34 private static final String TAG = "StatusBar.ToggleSlider"; 35 36 public interface Listener { 37 public void onInit(ToggleSlider v); 38 public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value); 39 } 40 41 private Listener mListener; 42 private boolean mTracking; 43 44 private CompoundButton mToggle; 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 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); 65 mToggle.setOnCheckedChangeListener(this); 66 mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button)); 67 68 mSlider = (SeekBar)findViewById(R.id.slider); 69 mSlider.setOnSeekBarChangeListener(this); 70 71 mLabel = (TextView)findViewById(R.id.label); 72 mLabel.setText(a.getString(R.styleable.ToggleSlider_text)); 73 74 a.recycle(); 75 } 76 77 @Override 78 protected void onAttachedToWindow() { 79 super.onAttachedToWindow(); 80 if (mListener != null) { 81 mListener.onInit(this); 82 } 83 } 84 85 public void onCheckedChanged(CompoundButton toggle, boolean checked) { 86 Drawable thumb; 87 Drawable slider; 88 final Resources res = getContext().getResources(); 89 if (checked) { 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); 94 } else { 95 thumb = res.getDrawable( 96 com.android.internal.R.drawable.scrubber_control_selector_holo); 97 slider = res.getDrawable( 98 com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark); 99 } 100 mSlider.setThumb(thumb); 101 mSlider.setProgressDrawable(slider); 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 150