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