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