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