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