1package com.android.systemui.tuner;
2
3import android.content.Context;
4import android.content.res.TypedArray;
5import android.provider.Settings;
6import android.support.v14.preference.SwitchPreference;
7import android.util.AttributeSet;
8
9import com.android.internal.logging.MetricsLogger;
10import com.android.systemui.R;
11import com.android.systemui.tuner.TunerService.Tunable;
12
13public class TunerSwitch extends SwitchPreference implements Tunable {
14
15    private final boolean mDefault;
16    private final int mAction;
17
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
27    public void onAttached() {
28        super.onAttached();
29        TunerService.get(getContext()).addTunable(this, getKey().split(","));
30    }
31
32    @Override
33    public void onDetached() {
34        TunerService.get(getContext()).removeTunable(this);
35        super.onDetached();
36    }
37
38    @Override
39    public void onTuningChanged(String key, String newValue) {
40        setChecked(newValue != null ? Integer.parseInt(newValue) != 0 : mDefault);
41    }
42
43    @Override
44    protected void onClick() {
45        super.onClick();
46        if (mAction != -1) {
47            MetricsLogger.action(getContext(), mAction, isChecked());
48        }
49    }
50
51    @Override
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