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.zen;
18 
19 import android.app.NotificationManager;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.SubSettingLauncher;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 import com.android.settingslib.widget.RadioButtonPreference;
30 
31 public class ZenModeVisEffectsCustomPreferenceController
32         extends AbstractZenModePreferenceController {
33 
34     private RadioButtonPreference mPreference;
35 
36     protected static final int INTERRUPTIVE_EFFECTS =
37             NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT
38             | NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK
39             | NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS
40             | NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
41 
ZenModeVisEffectsCustomPreferenceController(Context context, Lifecycle lifecycle, String key)42     public ZenModeVisEffectsCustomPreferenceController(Context context, Lifecycle lifecycle,
43             String key) {
44         super(context, key, lifecycle);
45     }
46 
47     @Override
isAvailable()48     public boolean isAvailable() {
49         return true;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         mPreference = screen.findPreference(getPreferenceKey());
56 
57         mPreference.setExtraWidgetOnClickListener(p -> {
58             launchCustomSettings();
59         });
60 
61         mPreference.setOnClickListener(p -> {
62             launchCustomSettings();
63         });
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         super.updateState(preference);
69 
70         mPreference.setChecked(areCustomOptionsSelected());
71     }
72 
areCustomOptionsSelected()73     protected boolean areCustomOptionsSelected() {
74         boolean allEffectsSuppressed =
75                 NotificationManager.Policy.areAllVisualEffectsSuppressed(
76                         mBackend.mPolicy.suppressedVisualEffects);
77         boolean noEffectsSuppressed = mBackend.mPolicy.suppressedVisualEffects == 0;
78 
79         return !(allEffectsSuppressed || noEffectsSuppressed);
80     }
81 
select()82     protected void select() {
83         mMetricsFeatureProvider.action(mContext,
84                 SettingsEnums.ACTION_ZEN_CUSTOM, true);
85     }
86 
launchCustomSettings()87     private void launchCustomSettings() {
88         select();
89         new SubSettingLauncher(mContext)
90                 .setDestination(ZenModeBlockedEffectsSettings.class.getName())
91                 .setTitleRes(R.string.zen_mode_what_to_block_title)
92                 .setSourceMetricsCategory(SettingsEnums.SETTINGS_ZEN_NOTIFICATIONS)
93                 .launch();
94     }
95 }