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