Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.content.Context; |
| 22 | import android.os.Bundle; |
| 23 | import android.service.notification.StatusBarNotification; |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 24 | import android.util.ArraySet; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import com.android.internal.statusbar.NotificationVisibility; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
| 29 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | 8172d3a | 2020-01-10 00:24:17 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.collection.NotifPipeline; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Ned Burns | 012048d | 2020-01-08 19:57:30 -0500 | [diff] [blame] | 32 | import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener; |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 33 | import com.android.systemui.util.time.SystemClock; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 34 | |
| 35 | import javax.inject.Inject; |
| 36 | import javax.inject.Singleton; |
| 37 | |
| 38 | /** Updates foreground service notification state in response to notification data events. */ |
| 39 | @Singleton |
| 40 | public class ForegroundServiceNotificationListener { |
| 41 | |
| 42 | private static final String TAG = "FgServiceController"; |
| 43 | private static final boolean DBG = false; |
| 44 | |
| 45 | private final Context mContext; |
| 46 | private final ForegroundServiceController mForegroundServiceController; |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 47 | private final NotificationEntryManager mEntryManager; |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 48 | |
| 49 | @Inject |
| 50 | public ForegroundServiceNotificationListener(Context context, |
| 51 | ForegroundServiceController foregroundServiceController, |
Beverly | 8c80e0c | 2019-12-03 11:25:39 -0500 | [diff] [blame] | 52 | NotificationEntryManager notificationEntryManager, |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 53 | NotifPipeline notifPipeline, |
| 54 | SystemClock systemClock) { |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 55 | mContext = context; |
| 56 | mForegroundServiceController = foregroundServiceController; |
Beverly | c29b969 | 2019-12-04 15:05:52 -0500 | [diff] [blame] | 57 | |
| 58 | // TODO: (b/145659174) remove mEntryManager when moving to NewNotifPipeline. Replaced by |
| 59 | // ForegroundCoordinator |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 60 | mEntryManager = notificationEntryManager; |
| 61 | mEntryManager.addNotificationEntryListener(new NotificationEntryListener() { |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 62 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 63 | public void onPendingEntryAdded(NotificationEntry entry) { |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 64 | addNotification(entry, entry.getImportance()); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 68 | public void onPreEntryUpdated(NotificationEntry entry) { |
| 69 | updateNotification(entry, entry.getImportance()); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onEntryRemoved( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 74 | NotificationEntry entry, |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 75 | NotificationVisibility visibility, |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 76 | boolean removedByUser, |
| 77 | int reason) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 78 | removeNotification(entry.getSbn()); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 79 | } |
| 80 | }); |
Evan Laird | ccf5c5e | 2020-02-06 14:01:28 -0500 | [diff] [blame] | 81 | mEntryManager.addNotificationLifetimeExtender( |
| 82 | new ForegroundServiceLifetimeExtender(systemClock)); |
Beverly | 8c80e0c | 2019-12-03 11:25:39 -0500 | [diff] [blame] | 83 | |
Ned Burns | 8172d3a | 2020-01-10 00:24:17 -0500 | [diff] [blame] | 84 | notifPipeline.addCollectionListener(new NotifCollectionListener() { |
Beverly | 8c80e0c | 2019-12-03 11:25:39 -0500 | [diff] [blame] | 85 | @Override |
| 86 | public void onEntryAdded(NotificationEntry entry) { |
| 87 | addNotification(entry, entry.getImportance()); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onEntryUpdated(NotificationEntry entry) { |
| 92 | updateNotification(entry, entry.getImportance()); |
| 93 | } |
| 94 | |
| 95 | @Override |
Ned Burns | 4258d70 | 2020-01-08 19:58:57 -0500 | [diff] [blame] | 96 | public void onEntryRemoved(NotificationEntry entry, int reason) { |
Beverly | 8c80e0c | 2019-12-03 11:25:39 -0500 | [diff] [blame] | 97 | removeNotification(entry.getSbn()); |
| 98 | } |
| 99 | }); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 103 | * @param entry notification that was just posted |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 104 | */ |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 105 | private void addNotification(NotificationEntry entry, int importance) { |
| 106 | updateNotification(entry, importance); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param sbn notification that was just removed |
| 111 | */ |
| 112 | private void removeNotification(StatusBarNotification sbn) { |
| 113 | mForegroundServiceController.updateUserState( |
| 114 | sbn.getUserId(), |
| 115 | new ForegroundServiceController.UserStateUpdateCallback() { |
| 116 | @Override |
| 117 | public boolean updateUserState(ForegroundServicesUserState userState) { |
| 118 | if (mForegroundServiceController.isDisclosureNotification(sbn)) { |
| 119 | // if you remove the dungeon entirely, we take that to mean there are |
| 120 | // no running services |
| 121 | userState.setRunningServices(null, 0); |
| 122 | return true; |
| 123 | } else { |
| 124 | // this is safe to call on any notification, not just |
| 125 | // FLAG_FOREGROUND_SERVICE |
| 126 | return userState.removeNotification(sbn.getPackageName(), sbn.getKey()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void userStateNotFound(int userId) { |
| 132 | if (DBG) { |
| 133 | Log.w(TAG, String.format( |
| 134 | "user %d with no known notifications got removeNotification " |
| 135 | + "for %s", |
| 136 | sbn.getUserId(), sbn)); |
| 137 | } |
| 138 | } |
| 139 | }, |
| 140 | false /* don't create */); |
| 141 | } |
| 142 | |
| 143 | /** |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 144 | * @param entry notification that was just changed in some way |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 145 | */ |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 146 | private void updateNotification(NotificationEntry entry, int newImportance) { |
| 147 | final StatusBarNotification sbn = entry.getSbn(); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 148 | mForegroundServiceController.updateUserState( |
| 149 | sbn.getUserId(), |
| 150 | userState -> { |
| 151 | if (mForegroundServiceController.isDisclosureNotification(sbn)) { |
| 152 | final Bundle extras = sbn.getNotification().extras; |
| 153 | if (extras != null) { |
| 154 | final String[] svcs = extras.getStringArray( |
| 155 | Notification.EXTRA_FOREGROUND_APPS); |
| 156 | userState.setRunningServices(svcs, sbn.getNotification().when); |
| 157 | } |
| 158 | } else { |
| 159 | userState.removeNotification(sbn.getPackageName(), sbn.getKey()); |
| 160 | if (0 != (sbn.getNotification().flags |
| 161 | & Notification.FLAG_FOREGROUND_SERVICE)) { |
| 162 | if (newImportance > NotificationManager.IMPORTANCE_MIN) { |
| 163 | userState.addImportantNotification(sbn.getPackageName(), |
| 164 | sbn.getKey()); |
| 165 | } |
| 166 | final Notification.Builder builder = |
| 167 | Notification.Builder.recoverBuilder( |
| 168 | mContext, sbn.getNotification()); |
| 169 | if (builder.usesStandardHeader()) { |
| 170 | userState.addStandardLayoutNotification( |
| 171 | sbn.getPackageName(), sbn.getKey()); |
| 172 | } |
| 173 | } |
| 174 | } |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 175 | tagForeground(entry); |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 176 | return true; |
| 177 | }, |
| 178 | true /* create if not found */); |
| 179 | } |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 180 | |
Beverly | c29b969 | 2019-12-04 15:05:52 -0500 | [diff] [blame] | 181 | // TODO: (b/145659174) remove when moving to NewNotifPipeline. Replaced by |
| 182 | // ForegroundCoordinator |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 183 | private void tagForeground(NotificationEntry entry) { |
| 184 | final StatusBarNotification sbn = entry.getSbn(); |
| 185 | ArraySet<Integer> activeOps = mForegroundServiceController.getAppOps( |
| 186 | sbn.getUserId(), |
| 187 | sbn.getPackageName()); |
| 188 | if (activeOps != null) { |
| 189 | synchronized (entry.mActiveAppOps) { |
| 190 | entry.mActiveAppOps.clear(); |
| 191 | entry.mActiveAppOps.addAll(activeOps); |
| 192 | } |
| 193 | } |
| 194 | } |
Gus Prevas | eb4e2e1 | 2018-12-28 14:57:59 -0500 | [diff] [blame] | 195 | } |