• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.support.v7.preference.Preference;
23 
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settingslib.RestrictedSwitchPreference;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnResume;
29 
30 public class DndPreferenceController extends NotificationPreferenceController
31         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
32 
33     private static final String KEY_BYPASS_DND = "bypass_dnd";
34 
DndPreferenceController(Context context, NotificationBackend backend)35     public DndPreferenceController(Context context, NotificationBackend backend) {
36         super(context, backend);
37     }
38 
39     @Override
getPreferenceKey()40     public String getPreferenceKey() {
41         return KEY_BYPASS_DND;
42     }
43 
44     @Override
isAvailable()45     public boolean isAvailable() {
46         if (!super.isAvailable() || mChannel == null) {
47             return false;
48         }
49         return true;
50     }
51 
updateState(Preference preference)52     public void updateState(Preference preference) {
53         if (mChannel != null) {
54             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
55             pref.setDisabledByAdmin(mAdmin);
56             pref.setEnabled(isChannelConfigurable() && !pref.isDisabledByAdmin());
57             pref.setChecked(mChannel.canBypassDnd());
58         }
59     }
60 
61     @Override
onPreferenceChange(Preference preference, Object newValue)62     public boolean onPreferenceChange(Preference preference, Object newValue) {
63         if (mChannel != null) {
64             final boolean bypassZenMode = (Boolean) newValue;
65             mChannel.setBypassDnd(bypassZenMode);
66             mChannel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
67             saveChannel();
68         }
69         return true;
70     }
71 }
72