OSDN Git Service

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