Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.app.Notification; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
| 23 | import android.util.ArraySet; |
| 24 | |
| 25 | import com.android.internal.annotations.VisibleForTesting; |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 26 | import com.android.systemui.statusbar.NotificationInteractionTracker; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 27 | import com.android.systemui.statusbar.NotificationLifetimeExtender; |
| 28 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 29 | import com.android.systemui.util.time.SystemClock; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 30 | |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 31 | import javax.inject.Inject; |
| 32 | |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 33 | /** |
| 34 | * Extends the lifetime of foreground notification services such that they show for at least |
| 35 | * five seconds |
| 36 | */ |
| 37 | public 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 Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 46 | private final SystemClock mSystemClock; |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 47 | private final NotificationInteractionTracker mInteractionTracker; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 48 | |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 49 | @Inject |
| 50 | public ForegroundServiceLifetimeExtender( |
| 51 | NotificationInteractionTracker interactionTracker, |
| 52 | SystemClock systemClock) { |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 53 | mSystemClock = systemClock; |
Evan Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 54 | mInteractionTracker = interactionTracker; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 55 | } |
| 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 64 | if ((entry.getSbn().getNotification().flags |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 65 | & Notification.FLAG_FOREGROUND_SERVICE) == 0) { |
| 66 | return false; |
| 67 | } |
| 68 | |
Evan Laird | 1d7db5b | 2020-05-27 23:46:32 -0400 | [diff] [blame] | 69 | // 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 Laird | 9b2a480 | 2020-05-08 11:56:18 -0400 | [diff] [blame] | 75 | boolean hasInteracted = mInteractionTracker.hasUserInteractedWith(entry.getKey()); |
| 76 | long aliveTime = mSystemClock.uptimeMillis() - entry.getCreationTime(); |
| 77 | return aliveTime < MIN_FGS_TIME_MS && !hasInteracted; |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 78 | } |
| 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 Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 100 | mNotificationSafeToRemoveCallback.onSafeToRemove(entry.getKey()); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | }; |
Evan Laird | 7d44291 | 2019-10-02 18:33:49 -0400 | [diff] [blame] | 104 | long delayAmt = MIN_FGS_TIME_MS |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 105 | - (mSystemClock.uptimeMillis() - entry.getCreationTime()); |
Evan Laird | 7d44291 | 2019-10-02 18:33:49 -0400 | [diff] [blame] | 106 | mHandler.postDelayed(r, delayAmt); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |