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.MetricsProto.MetricsEvent;
28 import com.android.systemui.statusbar.phone.StatusBarIconController;
29 import com.android.systemui.tuner.TunerService.Tunable;
30 
31 import java.util.Set;
32 
33 public class StatusBarSwitch extends SwitchPreference implements Tunable {
34 
35     private Set<String> mBlacklist;
36 
StatusBarSwitch(Context context, AttributeSet attrs)37     public StatusBarSwitch(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
41     @Override
onAttached()42     public void onAttached() {
43         super.onAttached();
44         TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
45     }
46 
47     @Override
onDetached()48     public void onDetached() {
49         TunerService.get(getContext()).removeTunable(this);
50         super.onDetached();
51     }
52 
53     @Override
onTuningChanged(String key, String newValue)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
persistBoolean(boolean value)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 
setList(Set<String> blacklist)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