• 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.NotificationManager;
20 import android.content.Context;
21 import android.support.v7.preference.CheckBoxPreference;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 import com.android.settings.widget.DisabledCheckBoxPreference;
28 
29 public class ZenModeVisEffectPreferenceController
30         extends AbstractZenModePreferenceController
31         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
32 
33     protected final String mKey;
34     protected final int mEffect;
35     protected final int mMetricsCategory;
36     // if any of these effects are suppressed, this effect must be too
37     protected final int[] mParentSuppressedEffects;
38     private PreferenceScreen mScreen;
39 
ZenModeVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key, int visualEffect, int metricsCategory, int[] parentSuppressedEffects)40     public ZenModeVisEffectPreferenceController(Context context, Lifecycle lifecycle, String key,
41             int visualEffect, int metricsCategory, int[] parentSuppressedEffects) {
42         super(context, key, lifecycle);
43         mKey = key;
44         mEffect = visualEffect;
45         mMetricsCategory = metricsCategory;
46         mParentSuppressedEffects = parentSuppressedEffects;
47     }
48 
49     @Override
getPreferenceKey()50     public String getPreferenceKey() {
51         return mKey;
52     }
53 
54     @Override
isAvailable()55     public boolean isAvailable() {
56         if (mEffect == NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS) {
57             return mContext.getResources()
58                     .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
59         }
60         return true;
61     }
62 
63     @Override
displayPreference(PreferenceScreen screen)64     public void displayPreference(PreferenceScreen screen) {
65         mScreen = screen;
66         super.displayPreference(screen);
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         super.updateState(preference);
72         boolean suppressed = mBackend.isVisualEffectSuppressed(mEffect);
73         boolean parentSuppressed = false;
74         if (mParentSuppressedEffects != null) {
75             for (int parentEffect : mParentSuppressedEffects) {
76                 parentSuppressed |= mBackend.isVisualEffectSuppressed(parentEffect);
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 
93         mMetricsFeatureProvider.action(mContext, mMetricsCategory, suppressEffect);
94         mBackend.saveVisualEffectsPolicy(mEffect, suppressEffect);
95         return true;
96     }
97 }
98