1 /*
2  * Copyright (C) 2018 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;
20 import android.content.Context;
21 
22 import androidx.preference.CheckBoxPreference;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settings.widget.DisabledCheckBoxPreference;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 public class ZenModeVisEffectPreferenceController
31         extends AbstractZenModePreferenceController
32         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
33 
34     protected final String mKey;
35     protected final int mEffect;
36     protected final int mMetricsCategory;
37     // if any of these effects are suppressed, this effect must be too
38     protected final int[] mParentSuppressedEffects;
39     private PreferenceScreen mScreen;
40 
ZenModeVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key, int visualEffect, int metricsCategory, int[] parentSuppressedEffects)41     public ZenModeVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key,
42             int visualEffect, int metricsCategory, int[] parentSuppressedEffects) {
43         super(context, key, lifecycle);
44         mKey = key;
45         mEffect = visualEffect;
46         mMetricsCategory = metricsCategory;
47         mParentSuppressedEffects = parentSuppressedEffects;
48     }
49 
50     @Override
getPreferenceKey()51     public String getPreferenceKey() {
52         return mKey;
53     }
54 
55     @Override
isAvailable()56     public boolean isAvailable() {
57         if (mEffect == NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS) {
58             return mContext.getResources()
59                     .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
60         }
61         return true;
62     }
63 
64     @Override
displayPreference(PreferenceScreen screen)65     public void displayPreference(PreferenceScreen screen) {
66         mScreen = screen;
67         super.displayPreference(screen);
68     }
69 
70     @Override
updateState(Preference preference)71     public void updateState(Preference preference) {
72         super.updateState(preference);
73         boolean suppressed = mBackend.isVisualEffectSuppressed(mEffect);
74         boolean parentSuppressed = false;
75         if (mParentSuppressedEffects != null) {
76             for (int parentEffect : mParentSuppressedEffects) {
77                 parentSuppressed |= mBackend.isVisualEffectSuppressed(parentEffect);
78             }
79         }
80         if (parentSuppressed) {
81             ((CheckBoxPreference) preference).setChecked(parentSuppressed);
82             onPreferenceChange(preference, parentSuppressed);
83             ((DisabledCheckBoxPreference) preference).enableCheckbox(false);
84         } else {
85             ((DisabledCheckBoxPreference) preference).enableCheckbox(true);
86             ((CheckBoxPreference) preference).setChecked(suppressed);
87         }
88     }
89 
90     @Override
onPreferenceChange(Preference preference, Object newValue)91     public boolean onPreferenceChange(Preference preference, Object newValue) {
92         final boolean suppressEffect = (Boolean) newValue;
93 
94         mMetricsFeatureProvider.action(mContext, mMetricsCategory, suppressEffect);
95         mBackend.saveVisualEffectsPolicy(mEffect, suppressEffect);
96         return true;
97     }
98 }
99