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.app;
18 
19 import android.app.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.provider.Settings;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settings.notification.NotificationBackend;
28 import com.android.settingslib.RestrictedSwitchPreference;
29 
30 public class LightsPreferenceController extends NotificationPreferenceController
31         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
32 
33     private static final String KEY_LIGHTS = "lights";
34 
LightsPreferenceController(Context context, NotificationBackend backend)35     public LightsPreferenceController(Context context, NotificationBackend backend) {
36         super(context, backend);
37     }
38 
39     @Override
getPreferenceKey()40     public String getPreferenceKey() {
41         return KEY_LIGHTS;
42     }
43 
44     @Override
isAvailable()45     public boolean isAvailable() {
46         if (!super.isAvailable()) {
47             return false;
48         }
49         if (mChannel == null) {
50             return false;
51         }
52         return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT)
53                 && canPulseLight()
54                 && !isDefaultChannel();
55     }
56 
57     @Override
isIncludedInFilter()58     boolean isIncludedInFilter() {
59         return mPreferenceFilter.contains(NotificationChannel.EDIT_LOCKED_DEVICE);
60     }
61 
updateState(Preference preference)62     public void updateState(Preference preference) {
63         if (mChannel != null) {
64             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
65             pref.setDisabledByAdmin(mAdmin);
66             pref.setEnabled(!pref.isDisabledByAdmin());
67             pref.setChecked(mChannel.shouldShowLights());
68         }
69     }
70 
71     @Override
onPreferenceChange(Preference preference, Object newValue)72     public boolean onPreferenceChange(Preference preference, Object newValue) {
73         if (mChannel != null) {
74             final boolean lights = (Boolean) newValue;
75             mChannel.enableLights(lights);
76             saveChannel();
77         }
78         return true;
79     }
80 
canPulseLight()81     boolean canPulseLight() {
82         if (!mContext.getResources()
83                 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) {
84             return false;
85         }
86         return Settings.System.getInt(mContext.getContentResolver(),
87                 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) == 1;
88     }
89 
90 }
91