blob: fefad531377fbaa97c76862e5ea49852c9ddfad2 [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
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
17package com.android.systemui.statusbar.notification.interruption;
18
19import static com.android.systemui.statusbar.StatusBarState.SHADE;
20
21import android.app.NotificationManager;
22import android.content.ContentResolver;
23import android.database.ContentObserver;
24import android.hardware.display.AmbientDisplayConfiguration;
25import android.os.Handler;
26import android.os.PowerManager;
27import android.os.RemoteException;
28import android.os.UserHandle;
29import android.provider.Settings;
30import android.service.dreams.IDreamManager;
31import android.service.notification.StatusBarNotification;
32import android.util.Log;
33
34import com.android.internal.annotations.VisibleForTesting;
35import com.android.systemui.dagger.qualifiers.Main;
36import com.android.systemui.plugins.statusbar.StatusBarStateController;
37import com.android.systemui.statusbar.StatusBarState;
38import com.android.systemui.statusbar.notification.NotificationFilter;
39import com.android.systemui.statusbar.notification.collection.NotificationEntry;
40import com.android.systemui.statusbar.policy.BatteryController;
41import com.android.systemui.statusbar.policy.HeadsUpManager;
42
43import java.util.ArrayList;
44import java.util.List;
45
46import javax.inject.Inject;
47import javax.inject.Singleton;
48
49/**
50 * Provides heads-up and pulsing state for notification entries.
51 */
52@Singleton
53public class NotificationInterruptStateProviderImpl implements NotificationInterruptStateProvider {
54 private static final String TAG = "InterruptionStateProvider";
55 private static final boolean DEBUG = true; //false;
56 private static final boolean DEBUG_HEADS_UP = true;
57 private static final boolean ENABLE_HEADS_UP = true;
58 private static final String SETTING_HEADS_UP_TICKER = "ticker_gets_heads_up";
59
60 private final List<NotificationInterruptSuppressor> mSuppressors = new ArrayList<>();
61 private final StatusBarStateController mStatusBarStateController;
62 private final NotificationFilter mNotificationFilter;
63 private final ContentResolver mContentResolver;
64 private final PowerManager mPowerManager;
65 private final IDreamManager mDreamManager;
66 private final AmbientDisplayConfiguration mAmbientDisplayConfiguration;
67 private final BatteryController mBatteryController;
68 private final ContentObserver mHeadsUpObserver;
69 private HeadsUpManager mHeadsUpManager;
70
71 @VisibleForTesting
72 protected boolean mUseHeadsUp = false;
73
74 @Inject
75 public NotificationInterruptStateProviderImpl(
76 ContentResolver contentResolver,
77 PowerManager powerManager,
78 IDreamManager dreamManager,
79 AmbientDisplayConfiguration ambientDisplayConfiguration,
80 NotificationFilter notificationFilter,
81 BatteryController batteryController,
82 StatusBarStateController statusBarStateController,
83 HeadsUpManager headsUpManager,
84 @Main Handler mainHandler) {
85 mContentResolver = contentResolver;
86 mPowerManager = powerManager;
87 mDreamManager = dreamManager;
88 mBatteryController = batteryController;
89 mAmbientDisplayConfiguration = ambientDisplayConfiguration;
90 mNotificationFilter = notificationFilter;
91 mStatusBarStateController = statusBarStateController;
92 mHeadsUpManager = headsUpManager;
93 mHeadsUpObserver = new ContentObserver(mainHandler) {
94 @Override
95 public void onChange(boolean selfChange) {
96 boolean wasUsing = mUseHeadsUp;
97 mUseHeadsUp = ENABLE_HEADS_UP
98 && Settings.Global.HEADS_UP_OFF != Settings.Global.getInt(
99 mContentResolver,
100 Settings.Global.HEADS_UP_NOTIFICATIONS_ENABLED,
101 Settings.Global.HEADS_UP_OFF);
102 Log.d(TAG, "heads up is " + (mUseHeadsUp ? "enabled" : "disabled"));
103 if (wasUsing != mUseHeadsUp) {
104 if (!mUseHeadsUp) {
105 Log.d(TAG, "dismissing any existing heads up notification on "
106 + "disable event");
107 mHeadsUpManager.releaseAllImmediately();
108 }
109 }
110 }
111 };
112
113 if (ENABLE_HEADS_UP) {
114 mContentResolver.registerContentObserver(
115 Settings.Global.getUriFor(Settings.Global.HEADS_UP_NOTIFICATIONS_ENABLED),
116 true,
117 mHeadsUpObserver);
118 mContentResolver.registerContentObserver(
119 Settings.Global.getUriFor(SETTING_HEADS_UP_TICKER), true,
120 mHeadsUpObserver);
121 }
122 mHeadsUpObserver.onChange(true); // set up
123 }
124
125 @Override
126 public void addSuppressor(NotificationInterruptSuppressor suppressor) {
127 mSuppressors.add(suppressor);
128 }
129
130 @Override
131 public boolean shouldBubbleUp(NotificationEntry entry) {
132 final StatusBarNotification sbn = entry.getSbn();
133
134 if (!canAlertCommon(entry)) {
135 return false;
136 }
137
138 if (!canAlertAwakeCommon(entry)) {
139 return false;
140 }
141
142 if (!entry.canBubble()) {
143 if (DEBUG) {
144 Log.d(TAG, "No bubble up: not allowed to bubble: " + sbn.getKey());
145 }
146 return false;
147 }
148
149 if (!entry.isBubble()) {
150 if (DEBUG) {
151 Log.d(TAG, "No bubble up: notification " + sbn.getKey()
152 + " is bubble? " + entry.isBubble());
153 }
154 return false;
155 }
156
157 if (entry.getBubbleMetadata() == null
158 || (entry.getBubbleMetadata().getShortcutId() == null
159 && entry.getBubbleMetadata().getIntent() == null)) {
160 if (DEBUG) {
161 Log.d(TAG, "No bubble up: notification: " + sbn.getKey()
162 + " doesn't have valid metadata");
163 }
164 return false;
165 }
166
167 return true;
168 }
169
170
171 @Override
172 public boolean shouldHeadsUp(NotificationEntry entry) {
173 if (mStatusBarStateController.isDozing()) {
174 return shouldHeadsUpWhenDozing(entry);
175 } else {
176 return shouldHeadsUpWhenAwake(entry);
177 }
178 }
179
180 /**
181 * When an entry was added, should we launch its fullscreen intent? Examples are Alarms or
182 * incoming calls.
183 */
184 @Override
185 public boolean shouldLaunchFullScreenIntentWhenAdded(NotificationEntry entry) {
186 return entry.getSbn().getNotification().fullScreenIntent != null
187 && (!shouldHeadsUp(entry)
188 || mStatusBarStateController.getState() == StatusBarState.KEYGUARD);
189 }
190
191 private boolean shouldHeadsUpWhenAwake(NotificationEntry entry) {
192 StatusBarNotification sbn = entry.getSbn();
193
194 if (!mUseHeadsUp) {
195 if (DEBUG_HEADS_UP) {
196 Log.d(TAG, "No heads up: no huns");
197 }
198 return false;
199 }
200
201 if (!canAlertCommon(entry)) {
202 return false;
203 }
204
205 if (!canAlertAwakeCommon(entry)) {
206 return false;
207 }
208
209 boolean inShade = mStatusBarStateController.getState() == SHADE;
210 if (entry.isBubble() && inShade) {
211 if (DEBUG_HEADS_UP) {
212 Log.d(TAG, "No heads up: in unlocked shade where notification is shown as a "
213 + "bubble: " + sbn.getKey());
214 }
215 return false;
216 }
217
218 if (entry.shouldSuppressPeek()) {
219 if (DEBUG_HEADS_UP) {
220 Log.d(TAG, "No heads up: suppressed by DND: " + sbn.getKey());
221 }
222 return false;
223 }
224
225 if (entry.getImportance() < NotificationManager.IMPORTANCE_HIGH) {
226 if (DEBUG_HEADS_UP) {
227 Log.d(TAG, "No heads up: unimportant notification: " + sbn.getKey());
228 }
229 return false;
230 }
231
232 boolean isDreaming = false;
233 try {
234 isDreaming = mDreamManager.isDreaming();
235 } catch (RemoteException e) {
236 Log.e(TAG, "Failed to query dream manager.", e);
237 }
238 boolean inUse = mPowerManager.isScreenOn() && !isDreaming;
239
240 if (!inUse) {
241 if (DEBUG_HEADS_UP) {
242 Log.d(TAG, "No heads up: not in use: " + sbn.getKey());
243 }
244 return false;
245 }
246
247 for (int i = 0; i < mSuppressors.size(); i++) {
248 if (mSuppressors.get(i).suppressAwakeHeadsUp(entry)) {
249 if (DEBUG_HEADS_UP) {
250 Log.d(TAG, "No heads up: aborted by suppressor: "
251 + mSuppressors.get(i).getName() + " sbnKey=" + sbn.getKey());
252 }
253 return false;
254 }
255 }
256 return true;
257 }
258
259 /**
260 * Whether or not the notification should "pulse" on the user's display when the phone is
261 * dozing. This displays the ambient view of the notification.
262 *
263 * @param entry the entry to check
264 * @return true if the entry should ambient pulse, false otherwise
265 */
266 private boolean shouldHeadsUpWhenDozing(NotificationEntry entry) {
267 StatusBarNotification sbn = entry.getSbn();
268
269 if (!mAmbientDisplayConfiguration.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) {
270 if (DEBUG_HEADS_UP) {
271 Log.d(TAG, "No pulsing: disabled by setting: " + sbn.getKey());
272 }
273 return false;
274 }
275
276 if (mBatteryController.isAodPowerSave()) {
277 if (DEBUG_HEADS_UP) {
278 Log.d(TAG, "No pulsing: disabled by battery saver: " + sbn.getKey());
279 }
280 return false;
281 }
282
283 if (!canAlertCommon(entry)) {
284 if (DEBUG_HEADS_UP) {
285 Log.d(TAG, "No pulsing: notification shouldn't alert: " + sbn.getKey());
286 }
287 return false;
288 }
289
290 if (entry.shouldSuppressAmbient()) {
291 if (DEBUG_HEADS_UP) {
292 Log.d(TAG, "No pulsing: ambient effect suppressed: " + sbn.getKey());
293 }
294 return false;
295 }
296
297 if (entry.getImportance() < NotificationManager.IMPORTANCE_DEFAULT) {
298 if (DEBUG_HEADS_UP) {
299 Log.d(TAG, "No pulsing: not important enough: " + sbn.getKey());
300 }
301 return false;
302 }
303 return true;
304 }
305
306 /**
307 * Common checks between regular & AOD heads up and bubbles.
308 *
309 * @param entry the entry to check
310 * @return true if these checks pass, false if the notification should not alert
311 */
312 private boolean canAlertCommon(NotificationEntry entry) {
313 StatusBarNotification sbn = entry.getSbn();
314
315 if (mNotificationFilter.shouldFilterOut(entry)) {
316 if (DEBUG || DEBUG_HEADS_UP) {
317 Log.d(TAG, "No alerting: filtered notification: " + sbn.getKey());
318 }
319 return false;
320 }
321
322 // Don't alert notifications that are suppressed due to group alert behavior
323 if (sbn.isGroup() && sbn.getNotification().suppressAlertingDueToGrouping()) {
324 if (DEBUG || DEBUG_HEADS_UP) {
325 Log.d(TAG, "No alerting: suppressed due to group alert behavior");
326 }
327 return false;
328 }
329
330 for (int i = 0; i < mSuppressors.size(); i++) {
331 if (mSuppressors.get(i).suppressInterruptions(entry)) {
332 if (DEBUG_HEADS_UP) {
333 Log.d(TAG, "No alerting: aborted by suppressor: "
334 + mSuppressors.get(i).getName() + " sbnKey=" + sbn.getKey());
335 }
336 return false;
337 }
338 }
339
340 if (entry.hasJustLaunchedFullScreenIntent()) {
341 if (DEBUG_HEADS_UP) {
342 Log.d(TAG, "No alerting: recent fullscreen: " + sbn.getKey());
343 }
344 return false;
345 }
346
347 return true;
348 }
349
350 /**
351 * Common checks between alerts that occur while the device is awake (heads up & bubbles).
352 *
353 * @param entry the entry to check
354 * @return true if these checks pass, false if the notification should not alert
355 */
356 private boolean canAlertAwakeCommon(NotificationEntry entry) {
357 StatusBarNotification sbn = entry.getSbn();
358
359 for (int i = 0; i < mSuppressors.size(); i++) {
360 if (mSuppressors.get(i).suppressAwakeInterruptions(entry)) {
361 if (DEBUG_HEADS_UP) {
362 Log.d(TAG, "No alerting: aborted by suppressor: "
363 + mSuppressors.get(i).getName() + " sbnKey=" + sbn.getKey());
364 }
365 return false;
366 }
367 }
368
369 if (isSnoozedPackage(sbn)) {
370 if (DEBUG_HEADS_UP) {
371 Log.d(TAG, "No alerting: snoozed package: " + sbn.getKey());
372 }
373 return false;
374 }
375
376 return true;
377 }
378
379 private boolean isSnoozedPackage(StatusBarNotification sbn) {
380 return mHeadsUpManager.isSnoozed(sbn.getPackageName());
381 }
382}