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.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 
30 public class ZenModeVisEffectsCustomPreferenceController
31         extends AbstractZenModePreferenceController {
32 
33     private ZenCustomRadioButtonPreference mPreference;
34 
35     protected static final int INTERRUPTIVE_EFFECTS =
36             NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT
37             | NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK
38             | NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS
39             | NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
40 
ZenModeVisEffectsCustomPreferenceController(Context context, Lifecycle lifecycle, String key)41     public ZenModeVisEffectsCustomPreferenceController(Context context, Lifecycle lifecycle,
42             String key) {
43         super(context, key, lifecycle);
44     }
45 
46     @Override
isAvailable()47     public boolean isAvailable() {
48         return true;
49     }
50 
51     @Override
displayPreference(PreferenceScreen screen)52     public void displayPreference(PreferenceScreen screen) {
53         super.displayPreference(screen);
54         mPreference = screen.findPreference(getPreferenceKey());
55 
56         mPreference.setOnGearClickListener(p -> {
57             launchCustomSettings();
58 
59         });
60 
61         mPreference.setOnRadioButtonClickListener(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 }