1 package com.android.systemui.tuner; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.provider.Settings; 6 import android.support.v14.preference.SwitchPreference; 7 import android.util.AttributeSet; 8 9 import com.android.internal.logging.MetricsLogger; 10 import com.android.systemui.R; 11 import com.android.systemui.tuner.TunerService.Tunable; 12 13 public class TunerSwitch extends SwitchPreference implements Tunable { 14 15 private final boolean mDefault; 16 private final int mAction; 17 TunerSwitch(Context context, AttributeSet attrs)18 public TunerSwitch(Context context, AttributeSet attrs) { 19 super(context, attrs); 20 21 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TunerSwitch); 22 mDefault = a.getBoolean(R.styleable.TunerSwitch_defValue, false); 23 mAction = a.getInt(R.styleable.TunerSwitch_metricsAction, -1); 24 } 25 26 @Override onAttached()27 public void onAttached() { 28 super.onAttached(); 29 TunerService.get(getContext()).addTunable(this, getKey().split(",")); 30 } 31 32 @Override onDetached()33 public void onDetached() { 34 TunerService.get(getContext()).removeTunable(this); 35 super.onDetached(); 36 } 37 38 @Override onTuningChanged(String key, String newValue)39 public void onTuningChanged(String key, String newValue) { 40 setChecked(newValue != null ? Integer.parseInt(newValue) != 0 : mDefault); 41 } 42 43 @Override onClick()44 protected void onClick() { 45 super.onClick(); 46 if (mAction != -1) { 47 MetricsLogger.action(getContext(), mAction, isChecked()); 48 } 49 } 50 51 @Override persistBoolean(boolean value)52 protected boolean persistBoolean(boolean value) { 53 for (String key : getKey().split(",")) { 54 Settings.Secure.putString(getContext().getContentResolver(), key, value ? "1" : "0"); 55 } 56 return true; 57 } 58 59 } 60