blob: c0f8cae607cad0c43bef4220c244edff4d027bc7 [file] [log] [blame]
Evan Laird3ceaa9b2019-08-05 17:11:54 -04001/*
2 * Copyright (C) 2019 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;
18
19import android.annotation.NonNull;
20import android.app.Notification;
21import android.os.Handler;
22import android.os.Looper;
23import android.util.ArraySet;
24
25import com.android.internal.annotations.VisibleForTesting;
Evan Laird9b2a4802020-05-08 11:56:18 -040026import com.android.systemui.statusbar.NotificationInteractionTracker;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040027import com.android.systemui.statusbar.NotificationLifetimeExtender;
28import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Evan Lairdccf5c5e2020-02-06 14:01:28 -050029import com.android.systemui.util.time.SystemClock;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040030
Evan Laird9b2a4802020-05-08 11:56:18 -040031import javax.inject.Inject;
32
Evan Laird3ceaa9b2019-08-05 17:11:54 -040033/**
34 * Extends the lifetime of foreground notification services such that they show for at least
35 * five seconds
36 */
37public class ForegroundServiceLifetimeExtender implements NotificationLifetimeExtender {
38
39 private static final String TAG = "FGSLifetimeExtender";
40 @VisibleForTesting
41 static final int MIN_FGS_TIME_MS = 5000;
42
43 private NotificationSafeToRemoveCallback mNotificationSafeToRemoveCallback;
44 private ArraySet<NotificationEntry> mManagedEntries = new ArraySet<>();
45 private Handler mHandler = new Handler(Looper.getMainLooper());
Evan Lairdccf5c5e2020-02-06 14:01:28 -050046 private final SystemClock mSystemClock;
Evan Laird9b2a4802020-05-08 11:56:18 -040047 private final NotificationInteractionTracker mInteractionTracker;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040048
Evan Laird9b2a4802020-05-08 11:56:18 -040049 @Inject
50 public ForegroundServiceLifetimeExtender(
51 NotificationInteractionTracker interactionTracker,
52 SystemClock systemClock) {
Evan Lairdccf5c5e2020-02-06 14:01:28 -050053 mSystemClock = systemClock;
Evan Laird9b2a4802020-05-08 11:56:18 -040054 mInteractionTracker = interactionTracker;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040055 }
56
57 @Override
58 public void setCallback(@NonNull NotificationSafeToRemoveCallback callback) {
59 mNotificationSafeToRemoveCallback = callback;
60 }
61
62 @Override
63 public boolean shouldExtendLifetime(@NonNull NotificationEntry entry) {
Ned Burns00b4b2d2019-10-17 22:09:27 -040064 if ((entry.getSbn().getNotification().flags
Evan Laird3ceaa9b2019-08-05 17:11:54 -040065 & Notification.FLAG_FOREGROUND_SERVICE) == 0) {
66 return false;
67 }
68
Evan Laird1d7db5b2020-05-27 23:46:32 -040069 // Entry has triggered a HUN or some other interruption, therefore it has been seen and the
70 // interrupter might be retaining it anyway.
71 if (entry.hasInterrupted()) {
72 return false;
73 }
74
Evan Laird9b2a4802020-05-08 11:56:18 -040075 boolean hasInteracted = mInteractionTracker.hasUserInteractedWith(entry.getKey());
76 long aliveTime = mSystemClock.uptimeMillis() - entry.getCreationTime();
77 return aliveTime < MIN_FGS_TIME_MS && !hasInteracted;
Evan Laird3ceaa9b2019-08-05 17:11:54 -040078 }
79
80 @Override
81 public boolean shouldExtendLifetimeForPendingNotification(
82 @NonNull NotificationEntry entry) {
83 return shouldExtendLifetime(entry);
84 }
85
86 @Override
87 public void setShouldManageLifetime(
88 @NonNull NotificationEntry entry, boolean shouldManage) {
89 if (!shouldManage) {
90 mManagedEntries.remove(entry);
91 return;
92 }
93
94 mManagedEntries.add(entry);
95
96 Runnable r = () -> {
97 if (mManagedEntries.contains(entry)) {
98 mManagedEntries.remove(entry);
99 if (mNotificationSafeToRemoveCallback != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400100 mNotificationSafeToRemoveCallback.onSafeToRemove(entry.getKey());
Evan Laird3ceaa9b2019-08-05 17:11:54 -0400101 }
102 }
103 };
Evan Laird7d442912019-10-02 18:33:49 -0400104 long delayAmt = MIN_FGS_TIME_MS
Evan Lairdccf5c5e2020-02-06 14:01:28 -0500105 - (mSystemClock.uptimeMillis() - entry.getCreationTime());
Evan Laird7d442912019-10-02 18:33:49 -0400106 mHandler.postDelayed(r, delayAmt);
Evan Laird3ceaa9b2019-08-05 17:11:54 -0400107 }
108}
109