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.internal.logging.MetricsProto.MetricsEvent;
28import com.android.systemui.statusbar.phone.StatusBarIconController;
29import com.android.systemui.tuner.TunerService.Tunable;
30
31import java.util.Set;
32
33public class StatusBarSwitch extends SwitchPreference implements Tunable {
34
35    private Set<String> mBlacklist;
36
37    public StatusBarSwitch(Context context, AttributeSet attrs) {
38        super(context, attrs);
39    }
40
41    @Override
42    public void onAttached() {
43        super.onAttached();
44        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
45    }
46
47    @Override
48    public void onDetached() {
49        TunerService.get(getContext()).removeTunable(this);
50        super.onDetached();
51    }
52
53    @Override
54    public void onTuningChanged(String key, String newValue) {
55        if (!StatusBarIconController.ICON_BLACKLIST.equals(key)) {
56            return;
57        }
58        mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
59        setChecked(!mBlacklist.contains(getKey()));
60    }
61
62    @Override
63    protected boolean persistBoolean(boolean value) {
64        if (!value) {
65            // If not enabled add to blacklist.
66            if (!mBlacklist.contains(getKey())) {
67                MetricsLogger.action(getContext(), MetricsEvent.TUNER_STATUS_BAR_DISABLE,
68                        getKey());
69                mBlacklist.add(getKey());
70                setList(mBlacklist);
71            }
72        } else {
73            if (mBlacklist.remove(getKey())) {
74                MetricsLogger.action(getContext(), MetricsEvent.TUNER_STATUS_BAR_ENABLE, getKey());
75                setList(mBlacklist);
76            }
77        }
78        return true;
79    }
80
81    private void setList(Set<String> blacklist) {
82        ContentResolver contentResolver = getContext().getContentResolver();
83        Settings.Secure.putStringForUser(contentResolver, StatusBarIconController.ICON_BLACKLIST,
84                TextUtils.join(",", blacklist), ActivityManager.getCurrentUser());
85    }
86}
87