• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.SettingsEnums;
20 import android.content.Context;
21 import android.service.notification.ZenPolicy;
22 import android.util.Pair;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.CheckBoxPreference;
26 import androidx.preference.Preference;
27 
28 import com.android.internal.logging.nano.MetricsProto;
29 import com.android.settings.widget.DisabledCheckBoxPreference;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 
32 public class ZenRuleVisEffectPreferenceController extends AbstractZenCustomRulePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
35     private final int mMetricsCategory;
36 
37     @VisibleForTesting protected @ZenPolicy.VisualEffect int mEffect;
38 
39     // if any of these effects are suppressed, this effect must be too
40     @VisibleForTesting protected @ZenPolicy.VisualEffect int[] mParentSuppressedEffects;
41 
ZenRuleVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key, @ZenPolicy.VisualEffect int visualEffect, int metricsCategory, @ZenPolicy.VisualEffect int[] parentSuppressedEffects)42     public ZenRuleVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key,
43             @ZenPolicy.VisualEffect int visualEffect, int metricsCategory,
44             @ZenPolicy.VisualEffect int[] parentSuppressedEffects) {
45         super(context, key, lifecycle);
46         mEffect = visualEffect;
47         mMetricsCategory = metricsCategory;
48         mParentSuppressedEffects = parentSuppressedEffects;
49     }
50 
51     @Override
isAvailable()52     public boolean isAvailable() {
53         if (!super.isAvailable()) {
54             return false;
55         }
56 
57         if (mEffect == ZenPolicy.VISUAL_EFFECT_LIGHTS) {
58             return mContext.getResources()
59                     .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
60         }
61         return true;
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         super.updateState(preference);
67         if (mRule == null || mRule.getZenPolicy() == null) {
68             return;
69         }
70 
71         boolean suppressed = !mRule.getZenPolicy().isVisualEffectAllowed(mEffect, false);
72         boolean parentSuppressed = false;
73         if (mParentSuppressedEffects != null) {
74             for (@ZenPolicy.VisualEffect int parentEffect : mParentSuppressedEffects) {
75                 if (!mRule.getZenPolicy().isVisualEffectAllowed(parentEffect, true)) {
76                     parentSuppressed = true;
77                 }
78             }
79         }
80         if (parentSuppressed) {
81             ((CheckBoxPreference) preference).setChecked(parentSuppressed);
82             onPreferenceChange(preference, parentSuppressed);
83             ((DisabledCheckBoxPreference) preference).enableCheckbox(false);
84         } else {
85             ((DisabledCheckBoxPreference) preference).enableCheckbox(true);
86             ((CheckBoxPreference) preference).setChecked(suppressed);
87         }
88     }
89 
90     @Override
onPreferenceChange(Preference preference, Object newValue)91     public boolean onPreferenceChange(Preference preference, Object newValue) {
92         final boolean suppressEffect = (Boolean) newValue;
93         mMetricsFeatureProvider.action(mContext, mMetricsCategory,
94                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_TOGGLE_EXCEPTION,
95                         suppressEffect ? 1 : 0),
96                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_RULE_ID, mId));
97 
98         mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
99                 .showVisualEffect(mEffect, !suppressEffect)
100                 .build());
101         mBackend.updateZenRule(mId, mRule);
102         return true;
103     }
104 }
105