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.ZenDeviceEffects;
21 
22 import androidx.annotation.NonNull;
23 import androidx.preference.Preference;
24 import androidx.preference.TwoStatePreference;
25 
26 public class ZenModeDisplayEffectPreferenceController extends AbstractZenModePreferenceController
27         implements Preference.OnPreferenceChangeListener {
28 
ZenModeDisplayEffectPreferenceController(Context context, String key, ZenModesBackend backend)29     public ZenModeDisplayEffectPreferenceController(Context context, String key,
30             ZenModesBackend backend) {
31         super(context, key, backend);
32     }
33 
34     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)35     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
36         TwoStatePreference pref = (TwoStatePreference) preference;
37         ZenDeviceEffects effects =  zenMode.getRule().getDeviceEffects();
38         if (effects == null) {
39             pref.setChecked(false);
40         } else {
41             switch (getPreferenceKey()) {
42                 case "effect_greyscale":
43                     pref.setChecked(effects.shouldDisplayGrayscale());
44                     break;
45                 case "effect_aod":
46                     pref.setChecked(effects.shouldSuppressAmbientDisplay());
47                     break;
48                 case "effect_wallpaper":
49                     pref.setChecked(effects.shouldDimWallpaper());
50                     break;
51                 case "effect_dark_theme":
52                     pref.setChecked(effects.shouldUseNightMode());
53                     break;
54             }
55         }
56     }
57 
58     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)59     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
60         final boolean allow = (Boolean) newValue;
61         return saveMode(zenMode -> {
62             ZenDeviceEffects.Builder updatedEffects = new ZenDeviceEffects.Builder(
63                     zenMode.getDeviceEffects());
64             switch (getPreferenceKey()) {
65                 case "effect_greyscale":
66                     updatedEffects.setShouldDisplayGrayscale(allow);
67                     break;
68                 case "effect_aod":
69                     updatedEffects.setShouldSuppressAmbientDisplay(allow);
70                     break;
71                 case "effect_wallpaper":
72                     updatedEffects.setShouldDimWallpaper(allow);
73                     break;
74                 case "effect_dark_theme":
75                     updatedEffects.setShouldUseNightMode(allow);
76                     break;
77             }
78             zenMode.getRule().setDeviceEffects(updatedEffects.build());
79             return zenMode;
80         });
81     }
82 }
83