• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
26 import com.android.systemui.statusbar.NotificationInteractionTracker;
27 import com.android.systemui.statusbar.NotificationLifetimeExtender;
28 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
29 import com.android.systemui.util.time.SystemClock;
30 
31 import javax.inject.Inject;
32 
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());
46     private final SystemClock mSystemClock;
47     private final NotificationInteractionTracker mInteractionTracker;
48 
49     @Inject
ForegroundServiceLifetimeExtender( NotificationInteractionTracker interactionTracker, SystemClock systemClock)50     public ForegroundServiceLifetimeExtender(
51             NotificationInteractionTracker interactionTracker,
52             SystemClock systemClock) {
53         mSystemClock = systemClock;
54         mInteractionTracker = interactionTracker;
55     }
56 
57     @Override
setCallback(@onNull NotificationSafeToRemoveCallback callback)58     public void setCallback(@NonNull NotificationSafeToRemoveCallback callback) {
59         mNotificationSafeToRemoveCallback = callback;
60     }
61 
62     @Override
shouldExtendLifetime(@onNull NotificationEntry entry)63     public boolean shouldExtendLifetime(@NonNull NotificationEntry entry) {
64         if ((entry.getSbn().getNotification().flags
65                 & Notification.FLAG_FOREGROUND_SERVICE) == 0) {
66             return false;
67         }
68 
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 
75         boolean hasInteracted = mInteractionTracker.hasUserInteractedWith(entry.getKey());
76         long aliveTime = mSystemClock.uptimeMillis() - entry.getCreationTime();
77         return aliveTime < MIN_FGS_TIME_MS && !hasInteracted;
78     }
79 
80     @Override
shouldExtendLifetimeForPendingNotification( @onNull NotificationEntry entry)81     public boolean shouldExtendLifetimeForPendingNotification(
82             @NonNull NotificationEntry entry) {
83         return shouldExtendLifetime(entry);
84     }
85 
86     @Override
setShouldManageLifetime( @onNull NotificationEntry entry, boolean shouldManage)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) {
100                     mNotificationSafeToRemoveCallback.onSafeToRemove(entry.getKey());
101                 }
102             }
103         };
104         long delayAmt = MIN_FGS_TIME_MS
105                 - (mSystemClock.uptimeMillis() - entry.getCreationTime());
106         mHandler.postDelayed(r, delayAmt);
107     }
108 }
109 
110