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 17 package com.android.systemui.settings; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.content.res.TypedArray; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.widget.CompoundButton; 26 import android.widget.CompoundButton.OnCheckedChangeListener; 27 import android.widget.RelativeLayout; 28 import android.widget.SeekBar; 29 import android.widget.SeekBar.OnSeekBarChangeListener; 30 import android.widget.TextView; 31 32 import com.android.settingslib.RestrictedLockUtils; 33 import com.android.systemui.R; 34 import com.android.systemui.statusbar.policy.BrightnessMirrorController; 35 36 public class ToggleSliderView extends RelativeLayout implements ToggleSlider { 37 private Listener mListener; 38 private boolean mTracking; 39 40 private CompoundButton mToggle; 41 private ToggleSeekBar mSlider; 42 private TextView mLabel; 43 44 private ToggleSliderView mMirror; 45 private BrightnessMirrorController mMirrorController; 46 ToggleSliderView(Context context)47 public ToggleSliderView(Context context) { 48 this(context, null); 49 } 50 ToggleSliderView(Context context, AttributeSet attrs)51 public ToggleSliderView(Context context, AttributeSet attrs) { 52 this(context, attrs, 0); 53 } 54 ToggleSliderView(Context context, AttributeSet attrs, int defStyle)55 public ToggleSliderView(Context context, AttributeSet attrs, int defStyle) { 56 super(context, attrs, defStyle); 57 58 View.inflate(context, R.layout.status_bar_toggle_slider, this); 59 60 final Resources res = context.getResources(); 61 final TypedArray a = context.obtainStyledAttributes( 62 attrs, R.styleable.ToggleSliderView, defStyle, 0); 63 64 mToggle = findViewById(R.id.toggle); 65 mToggle.setOnCheckedChangeListener(mCheckListener); 66 67 mSlider = findViewById(R.id.slider); 68 mSlider.setOnSeekBarChangeListener(mSeekListener); 69 70 mLabel = findViewById(R.id.label); 71 mLabel.setText(a.getString(R.styleable.ToggleSliderView_text)); 72 73 mSlider.setAccessibilityLabel(getContentDescription().toString()); 74 75 a.recycle(); 76 } 77 setMirror(ToggleSliderView toggleSlider)78 public void setMirror(ToggleSliderView toggleSlider) { 79 mMirror = toggleSlider; 80 if (mMirror != null) { 81 mMirror.setChecked(mToggle.isChecked()); 82 mMirror.setMax(mSlider.getMax()); 83 mMirror.setValue(mSlider.getProgress()); 84 } 85 } 86 setMirrorController(BrightnessMirrorController c)87 public void setMirrorController(BrightnessMirrorController c) { 88 mMirrorController = c; 89 } 90 91 @Override onAttachedToWindow()92 protected void onAttachedToWindow() { 93 super.onAttachedToWindow(); 94 if (mListener != null) { 95 mListener.onInit(this); 96 } 97 } 98 setEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin)99 public void setEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin) { 100 mToggle.setEnabled(admin == null); 101 mSlider.setEnabled(admin == null); 102 mSlider.setEnforcedAdmin(admin); 103 } 104 setOnChangedListener(Listener l)105 public void setOnChangedListener(Listener l) { 106 mListener = l; 107 } 108 109 @Override setChecked(boolean checked)110 public void setChecked(boolean checked) { 111 mToggle.setChecked(checked); 112 } 113 114 @Override isChecked()115 public boolean isChecked() { 116 return mToggle.isChecked(); 117 } 118 119 @Override setMax(int max)120 public void setMax(int max) { 121 mSlider.setMax(max); 122 if (mMirror != null) { 123 mMirror.setMax(max); 124 } 125 } 126 127 @Override setValue(int value)128 public void setValue(int value) { 129 mSlider.setProgress(value); 130 if (mMirror != null) { 131 mMirror.setValue(value); 132 } 133 } 134 135 @Override getValue()136 public int getValue() { 137 return mSlider.getProgress(); 138 } 139 140 @Override dispatchTouchEvent(MotionEvent ev)141 public boolean dispatchTouchEvent(MotionEvent ev) { 142 if (mMirror != null) { 143 MotionEvent copy = ev.copy(); 144 mMirror.dispatchTouchEvent(copy); 145 copy.recycle(); 146 } 147 return super.dispatchTouchEvent(ev); 148 } 149 150 private final OnCheckedChangeListener mCheckListener = new OnCheckedChangeListener() { 151 @Override 152 public void onCheckedChanged(CompoundButton toggle, boolean checked) { 153 mSlider.setEnabled(!checked); 154 155 if (mListener != null) { 156 mListener.onChanged( 157 ToggleSliderView.this, mTracking, checked, mSlider.getProgress(), false); 158 } 159 160 if (mMirror != null) { 161 mMirror.mToggle.setChecked(checked); 162 } 163 } 164 }; 165 166 private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() { 167 @Override 168 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 169 if (mListener != null) { 170 mListener.onChanged( 171 ToggleSliderView.this, mTracking, mToggle.isChecked(), progress, false); 172 } 173 } 174 175 @Override 176 public void onStartTrackingTouch(SeekBar seekBar) { 177 mTracking = true; 178 179 if (mListener != null) { 180 mListener.onChanged(ToggleSliderView.this, mTracking, mToggle.isChecked(), 181 mSlider.getProgress(), false); 182 } 183 184 mToggle.setChecked(false); 185 186 if (mMirrorController != null) { 187 mMirrorController.showMirror(); 188 mMirrorController.setLocation((View) getParent()); 189 } 190 } 191 192 @Override 193 public void onStopTrackingTouch(SeekBar seekBar) { 194 mTracking = false; 195 196 if (mListener != null) { 197 mListener.onChanged(ToggleSliderView.this, mTracking, mToggle.isChecked(), 198 mSlider.getProgress(), true); 199 } 200 201 if (mMirrorController != null) { 202 mMirrorController.hideMirror(); 203 } 204 } 205 }; 206 } 207 208