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