StatusBarSwitch.java revision 87ccd55e8a90ff5d1c30f852941d523a83ab735a
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16package com.android.systemui.tuner;
17
18import android.app.ActivityManager;
19import android.content.ContentResolver;
20import android.content.Context;
21import android.provider.Settings;
22import android.support.v14.preference.SwitchPreference;
23import android.text.TextUtils;
24import android.util.AttributeSet;
25
26import com.android.internal.logging.MetricsLogger;
27import com.android.systemui.statusbar.phone.StatusBarIconController;
28import com.android.systemui.tuner.TunerService.Tunable;
29
30import java.util.Set;
31
32public class StatusBarSwitch extends SwitchPreference implements Tunable {
33
34    private Set<String> mBlacklist;
35
36    public StatusBarSwitch(Context context, AttributeSet attrs) {
37        super(context, attrs);
38    }
39
40    @Override
41    public void onAttached() {
42        super.onAttached();
43        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
44    }
45
46    @Override
47    public void onDetached() {
48        TunerService.get(getContext()).removeTunable(this);
49        super.onDetached();
50    }
51
52    @Override
53    public void onTuningChanged(String key, String newValue) {
54        if (!StatusBarIconController.ICON_BLACKLIST.equals(key)) {
55            return;
56        }
57        mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
58        setChecked(!mBlacklist.contains(getKey()));
59    }
60
61    @Override
62    protected boolean persistBoolean(boolean value) {
63        if (!value) {
64            // If not enabled add to blacklist.
65            if (!mBlacklist.contains(getKey())) {
66                MetricsLogger.action(getContext(), MetricsLogger.TUNER_STATUS_BAR_DISABLE,
67                        getKey());
68                mBlacklist.add(getKey());
69                setList(mBlacklist);
70            }
71        } else {
72            if (mBlacklist.remove(getKey())) {
73                MetricsLogger.action(getContext(), MetricsLogger.TUNER_STATUS_BAR_ENABLE, getKey());
74                setList(mBlacklist);
75            }
76        }
77        return true;
78    }
79
80    private void setList(Set<String> blacklist) {
81        ContentResolver contentResolver = getContext().getContentResolver();
82        Settings.Secure.putStringForUser(contentResolver, StatusBarIconController.ICON_BLACKLIST,
83                TextUtils.join(",", blacklist), ActivityManager.getCurrentUser());
84    }
85}
86