1 /**
2  * Copyright (C) 2015 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.NotificationManager.Policy;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.provider.SearchIndexableResource;
24 import android.support.v14.preference.SwitchPreference;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 import android.util.Log;
28 
29 import com.android.internal.logging.MetricsLogger;
30 import com.android.internal.logging.MetricsProto.MetricsEvent;
31 import com.android.settings.search.BaseSearchIndexProvider;
32 import com.android.settings.search.Indexable;
33 import com.android.settings.R;
34 
35 import java.util.Arrays;
36 import java.util.List;
37 
38 public class ZenModeVisualInterruptionSettings extends ZenModeSettingsBase {
39 
40     private static final String KEY_SCREEN_OFF = "screenOff";
41     private static final String KEY_SCREEN_ON = "screenOn";
42 
43     private SwitchPreference mScreenOff;
44     private SwitchPreference mScreenOn;
45 
46     private boolean mDisableListeners;
47     private NotificationManager.Policy mPolicy;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         addPreferencesFromResource(R.xml.zen_mode_visual_interruptions_settings);
53         final PreferenceScreen root = getPreferenceScreen();
54 
55         mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
56 
57         mScreenOff = (SwitchPreference) root.findPreference(KEY_SCREEN_OFF);
58         if (!getResources()
59                 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) {
60             mScreenOff.setSummary(R.string.zen_mode_screen_off_summary_no_led);
61         }
62         mScreenOff.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
63             @Override
64             public boolean onPreferenceChange(Preference preference, Object newValue) {
65                 if (mDisableListeners) return true;
66                 final boolean val = (Boolean) newValue;
67                 MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_WHEN_SCREEN_OFF, val);
68                 if (DEBUG) Log.d(TAG, "onPrefChange suppressWhenScreenOff=" + val);
69                 savePolicy(getNewSuppressedEffects(val, Policy.SUPPRESSED_EFFECT_SCREEN_OFF));
70                 return true;
71             }
72         });
73 
74         mScreenOn = (SwitchPreference) root.findPreference(KEY_SCREEN_ON);
75         mScreenOn.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
76             @Override
77             public boolean onPreferenceChange(Preference preference, Object newValue) {
78                 if (mDisableListeners) return true;
79                 final boolean val = (Boolean) newValue;
80                 MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_WHEN_SCREEN_ON, val);
81                 if (DEBUG) Log.d(TAG, "onPrefChange suppressWhenScreenOn=" + val);
82                 savePolicy(getNewSuppressedEffects(val, Policy.SUPPRESSED_EFFECT_SCREEN_ON));
83                 return true;
84             }
85         });
86     }
87 
88     @Override
getMetricsCategory()89     protected int getMetricsCategory() {
90         return MetricsEvent.NOTIFICATION_ZEN_MODE_VISUAL_INTERRUPTIONS;
91     }
92 
93     @Override
onZenModeChanged()94     protected void onZenModeChanged() {
95         // Don't care
96     }
97 
98     @Override
onZenModeConfigChanged()99     protected void onZenModeConfigChanged() {
100         mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
101         updateControls();
102     }
103 
updateControls()104     private void updateControls() {
105         mDisableListeners = true;
106         mScreenOff.setChecked(isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF));
107         mScreenOn.setChecked(isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_ON));
108         mDisableListeners = false;
109     }
110 
isEffectSuppressed(int effect)111     private boolean isEffectSuppressed(int effect) {
112         return (mPolicy.suppressedVisualEffects & effect) != 0;
113     }
114 
getNewSuppressedEffects(boolean suppress, int effectType)115     private int getNewSuppressedEffects(boolean suppress, int effectType) {
116         int effects = mPolicy.suppressedVisualEffects;
117         if (suppress) {
118             effects |= effectType;
119         } else {
120             effects &= ~effectType;
121         }
122         return effects;
123     }
124 
savePolicy(int suppressedVisualEffects)125     private void savePolicy(int suppressedVisualEffects) {
126         mPolicy = new Policy(mPolicy.priorityCategories,
127                 mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders,
128                 suppressedVisualEffects);
129         NotificationManager.from(mContext).setNotificationPolicy(mPolicy);
130     }
131 }
132