blob: 05acdd080aa53274f54a3f3bba4a974f18f04f9b [file] [log] [blame]
Evan Laird05597642019-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;
26import com.android.systemui.statusbar.NotificationLifetimeExtender;
27import com.android.systemui.statusbar.notification.collection.NotificationEntry;
28
29/**
30 * Extends the lifetime of foreground notification services such that they show for at least
31 * five seconds
32 */
33public class ForegroundServiceLifetimeExtender implements NotificationLifetimeExtender {
34
35 private static final String TAG = "FGSLifetimeExtender";
36 @VisibleForTesting
37 static final int MIN_FGS_TIME_MS = 5000;
38
39 private NotificationSafeToRemoveCallback mNotificationSafeToRemoveCallback;
40 private ArraySet<NotificationEntry> mManagedEntries = new ArraySet<>();
41 private Handler mHandler = new Handler(Looper.getMainLooper());
42
43 public ForegroundServiceLifetimeExtender() {
44 }
45
46 @Override
47 public void setCallback(@NonNull NotificationSafeToRemoveCallback callback) {
48 mNotificationSafeToRemoveCallback = callback;
49 }
50
51 @Override
52 public boolean shouldExtendLifetime(@NonNull NotificationEntry entry) {
53 if ((entry.notification.getNotification().flags
54 & Notification.FLAG_FOREGROUND_SERVICE) == 0) {
55 return false;
56 }
57
58 long currentTime = System.currentTimeMillis();
59 return currentTime - entry.notification.getPostTime() < MIN_FGS_TIME_MS;
60 }
61
62 @Override
63 public boolean shouldExtendLifetimeForPendingNotification(
64 @NonNull NotificationEntry entry) {
65 return shouldExtendLifetime(entry);
66 }
67
68 @Override
69 public void setShouldManageLifetime(
70 @NonNull NotificationEntry entry, boolean shouldManage) {
71 if (!shouldManage) {
72 mManagedEntries.remove(entry);
73 return;
74 }
75
76 mManagedEntries.add(entry);
77
78 Runnable r = () -> {
79 if (mManagedEntries.contains(entry)) {
80 mManagedEntries.remove(entry);
81 if (mNotificationSafeToRemoveCallback != null) {
82 mNotificationSafeToRemoveCallback.onSafeToRemove(entry.key);
83 }
84 }
85 };
86 long delayAmt = MIN_FGS_TIME_MS
87 - (System.currentTimeMillis() - entry.notification.getPostTime());
88 mHandler.postDelayed(r, delayAmt);
89 }
90}