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.preference.SwitchPreference;
22import android.provider.Settings;
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 onTuningChanged(String key, String newValue) {
42        if (!StatusBarIconController.ICON_BLACKLIST.equals(key)) {
43            return;
44        }
45        mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
46        setChecked(!mBlacklist.contains(getKey()));
47    }
48
49    @Override
50    protected boolean persistBoolean(boolean value) {
51        if (!value) {
52            // If not enabled add to blacklist.
53            if (!mBlacklist.contains(getKey())) {
54                MetricsLogger.action(getContext(), MetricsLogger.TUNER_STATUS_BAR_DISABLE,
55                        getKey());
56                mBlacklist.add(getKey());
57                setList(mBlacklist);
58            }
59        } else {
60            if (mBlacklist.remove(getKey())) {
61                MetricsLogger.action(getContext(), MetricsLogger.TUNER_STATUS_BAR_ENABLE, getKey());
62                setList(mBlacklist);
63            }
64        }
65        return true;
66    }
67
68    private void setList(Set<String> blacklist) {
69        ContentResolver contentResolver = getContext().getContentResolver();
70        Settings.Secure.putStringForUser(contentResolver, StatusBarIconController.ICON_BLACKLIST,
71                TextUtils.join(",", blacklist), ActivityManager.getCurrentUser());
72    }
73}
74