1 /*
2  * Copyright (C) 2017 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 
17 package com.android.settings.notification;
18 
19 import android.app.NotificationManager.Policy;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.provider.Settings;
23 import android.util.Log;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 public class ZenModeEventsPreferenceController extends AbstractZenModePreferenceController
31         implements Preference.OnPreferenceChangeListener {
32 
33     protected static final String KEY = "zen_mode_events";
34 
ZenModeEventsPreferenceController(Context context, Lifecycle lifecycle)35     public ZenModeEventsPreferenceController(Context context, Lifecycle lifecycle) {
36         super(context, KEY, lifecycle);
37     }
38 
39     @Override
getPreferenceKey()40     public String getPreferenceKey() {
41         return KEY;
42     }
43 
44     @Override
isAvailable()45     public boolean isAvailable() {
46         return true;
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         super.updateState(preference);
52 
53         SwitchPreference pref = (SwitchPreference) preference;
54         switch (getZenMode()) {
55             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
56             case Settings.Global.ZEN_MODE_ALARMS:
57                 pref.setEnabled(false);
58                 pref.setChecked(false);
59                 break;
60             default:
61                 pref.setChecked(mBackend.isPriorityCategoryEnabled(
62                         Policy.PRIORITY_CATEGORY_EVENTS));
63                 pref.setEnabled(true);
64         }
65     }
66 
67     @Override
onPreferenceChange(Preference preference, Object newValue)68     public boolean onPreferenceChange(Preference preference, Object newValue) {
69         final boolean allowEvents = (Boolean) newValue;
70         if (ZenModeSettingsBase.DEBUG) {
71             Log.d(TAG, "onPrefChange allowEvents=" + allowEvents);
72         }
73         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ZEN_ALLOW_EVENTS,
74                 allowEvents);
75         mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_EVENTS, allowEvents);
76         return true;
77     }
78 }
79