1 /*
2  * Copyright (C) 2024 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.modes;
18 
19 import android.content.Context;
20 import android.service.notification.ZenPolicy;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.CheckBoxPreference;
25 import androidx.preference.Preference;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.settings.widget.DisabledCheckBoxPreference;
29 
30 public class ZenModeNotifVisPreferenceController extends AbstractZenModePreferenceController
31         implements Preference.OnPreferenceChangeListener {
32 
33     @VisibleForTesting protected @ZenPolicy.VisualEffect int mEffect;
34 
35     // if any of these effects are suppressed, this effect must be too
36     @VisibleForTesting protected @ZenPolicy.VisualEffect int[] mParentSuppressedEffects;
37 
ZenModeNotifVisPreferenceController(Context context, String key, @ZenPolicy.VisualEffect int visualEffect, @ZenPolicy.VisualEffect int[] parentSuppressedEffects, ZenModesBackend backend)38     public ZenModeNotifVisPreferenceController(Context context, String key,
39             @ZenPolicy.VisualEffect int visualEffect,
40             @ZenPolicy.VisualEffect int[] parentSuppressedEffects, ZenModesBackend backend) {
41         super(context, key, backend);
42         mEffect = visualEffect;
43         mParentSuppressedEffects = parentSuppressedEffects;
44     }
45 
46     @Override
isAvailable()47     public boolean isAvailable() {
48         if (!super.isAvailable()) {
49             return false;
50         }
51 
52         if (mEffect == ZenPolicy.VISUAL_EFFECT_LIGHTS) {
53             return mContext.getResources()
54                     .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
55         }
56         return true;
57     }
58 
59     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)60     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
61         boolean suppressed = !zenMode.getPolicy().isVisualEffectAllowed(mEffect, false);
62         boolean parentSuppressed = false;
63         if (mParentSuppressedEffects != null) {
64             for (@ZenPolicy.VisualEffect int parentEffect : mParentSuppressedEffects) {
65                 if (!zenMode.getPolicy().isVisualEffectAllowed(parentEffect, true)) {
66                     parentSuppressed = true;
67                 }
68             }
69         }
70         if (parentSuppressed) {
71             ((TwoStatePreference) preference).setChecked(true);
72             onPreferenceChange(preference, true);
73             preference.setEnabled(false);
74         } else {
75             preference.setEnabled(true);
76             ((TwoStatePreference) preference).setChecked(suppressed);
77         }
78     }
79 
80     @Override
onPreferenceChange(Preference preference, Object newValue)81     public boolean onPreferenceChange(Preference preference, Object newValue) {
82         final boolean allowEffect = !((Boolean) newValue);
83         return savePolicy(policy -> policy.showVisualEffect(mEffect, allowEffect));
84     }
85 }
86