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.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     }
27 
28     @Override
onAttached()29     public void onAttached() {
30         super.onAttached();
31         Dependency.get(TunerService.class).addTunable(this, getKey().split(","));
32     }
33 
34     @Override
onDetached()35     public void onDetached() {
36         Dependency.get(TunerService.class).removeTunable(this);
37         super.onDetached();
38     }
39 
40     @Override
onTuningChanged(String key, String newValue)41     public void onTuningChanged(String key, String newValue) {
42         setChecked(TunerService.parseIntegerSwitch(newValue, mDefault));
43     }
44 
45     @Override
onClick()46     protected void onClick() {
47         super.onClick();
48         if (mAction != -1) {
49             MetricsLogger.action(getContext(), mAction, isChecked());
50         }
51     }
52 
53     @Override
persistBoolean(boolean value)54     protected boolean persistBoolean(boolean value) {
55         for (String key : getKey().split(",")) {
56             Settings.Secure.putString(getContext().getContentResolver(), key, value ? "1" : "0");
57         }
58         return true;
59     }
60 
61 }
62