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