Evan Laird | 181de62 | 2019-10-24 09:53:02 -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.statusbar.notification.collection |
| 18 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 19 | import android.app.NotificationManager.IMPORTANCE_HIGH |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 20 | import android.app.NotificationManager.IMPORTANCE_MIN |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 21 | import android.service.notification.NotificationListenerService.Ranking |
| 22 | import android.service.notification.NotificationListenerService.RankingMap |
| 23 | import android.service.notification.StatusBarNotification |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 24 | import com.android.systemui.statusbar.NotificationMediaManager |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 25 | import com.android.systemui.statusbar.notification.NotificationEntryManagerLogger |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 26 | import com.android.systemui.statusbar.notification.NotificationFilter |
| 27 | import com.android.systemui.statusbar.notification.NotificationSectionsFeatureManager |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier |
Steve Elliott | 307d385 | 2020-03-05 11:00:22 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier.Companion.TYPE_NON_PERSON |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 31 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_ALERTING |
Steve Elliott | 49671e0 | 2020-05-12 13:51:28 -0400 | [diff] [blame] | 32 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_FOREGROUND_SERVICE |
Steve Elliott | b094038 | 2020-02-20 14:24:02 -0500 | [diff] [blame] | 33 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_HEADS_UP |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_PEOPLE |
| 35 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.BUCKET_SILENT |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.notification.stack.NotificationSectionsManager.PriorityBucket |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.phone.NotificationGroupManager |
| 38 | import com.android.systemui.statusbar.policy.HeadsUpManager |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 39 | import dagger.Lazy |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 40 | import java.util.Objects |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 41 | import javax.inject.Inject |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 42 | |
| 43 | private const val TAG = "NotifRankingManager" |
| 44 | |
| 45 | /** |
| 46 | * NotificationRankingManager is responsible for holding on to the most recent [RankingMap], and |
| 47 | * updating SystemUI's set of [NotificationEntry]s with their own ranking. It also sorts and filters |
| 48 | * a set of entries (but retains none of them). We also set buckets on the entries here since |
| 49 | * bucketing is tied closely to sorting. |
| 50 | * |
| 51 | * For the curious: this class is one iteration closer to null of what used to be called |
| 52 | * NotificationData.java. |
| 53 | */ |
| 54 | open class NotificationRankingManager @Inject constructor( |
| 55 | private val mediaManagerLazy: Lazy<NotificationMediaManager>, |
| 56 | private val groupManager: NotificationGroupManager, |
| 57 | private val headsUpManager: HeadsUpManager, |
| 58 | private val notifFilter: NotificationFilter, |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 59 | private val logger: NotificationEntryManagerLogger, |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 60 | private val sectionsFeatureManager: NotificationSectionsFeatureManager, |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 61 | private val peopleNotificationIdentifier: PeopleNotificationIdentifier, |
| 62 | private val highPriorityProvider: HighPriorityProvider |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 63 | ) { |
| 64 | |
| 65 | var rankingMap: RankingMap? = null |
| 66 | protected set |
| 67 | private val mediaManager by lazy { |
| 68 | mediaManagerLazy.get() |
| 69 | } |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 70 | private val usePeopleFiltering: Boolean |
| 71 | get() = sectionsFeatureManager.isFilteringEnabled() |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 72 | private val rankingComparator: Comparator<NotificationEntry> = Comparator { a, b -> |
| 73 | val na = a.sbn |
| 74 | val nb = b.sbn |
| 75 | val aRank = a.ranking.rank |
| 76 | val bRank = b.ranking.rank |
| 77 | |
Steve Elliott | 307d385 | 2020-03-05 11:00:22 -0500 | [diff] [blame] | 78 | val aPersonType = a.getPeopleNotificationType() |
| 79 | val bPersonType = b.getPeopleNotificationType() |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 80 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 81 | val aMedia = isImportantMedia(a) |
| 82 | val bMedia = isImportantMedia(b) |
| 83 | |
| 84 | val aSystemMax = a.isSystemMax() |
| 85 | val bSystemMax = b.isSystemMax() |
| 86 | |
| 87 | val aHeadsUp = a.isRowHeadsUp |
| 88 | val bHeadsUp = b.isRowHeadsUp |
| 89 | |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 90 | val aIsHighPriority = a.isHighPriority() |
| 91 | val bIsHighPriority = b.isHighPriority() |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 92 | when { |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 93 | aHeadsUp != bHeadsUp -> if (aHeadsUp) -1 else 1 |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 94 | // Provide consistent ranking with headsUpManager |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 95 | aHeadsUp -> headsUpManager.compare(a, b) |
Julia Reynolds | 8cdfdb6 | 2020-04-24 12:03:33 -0400 | [diff] [blame] | 96 | |
| 97 | usePeopleFiltering && aPersonType != bPersonType -> |
| 98 | peopleNotificationIdentifier.compareTo(aPersonType, bPersonType) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 99 | // Upsort current media notification. |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 100 | aMedia != bMedia -> if (aMedia) -1 else 1 |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 101 | // Upsort PRIORITY_MAX system notifications |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 102 | aSystemMax != bSystemMax -> if (aSystemMax) -1 else 1 |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 103 | aIsHighPriority != bIsHighPriority -> |
| 104 | -1 * aIsHighPriority.compareTo(bIsHighPriority) |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 105 | aRank != bRank -> aRank - bRank |
| 106 | else -> nb.notification.`when`.compareTo(na.notification.`when`) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | private fun isImportantMedia(entry: NotificationEntry): Boolean { |
| 111 | val importance = entry.ranking.importance |
| 112 | return entry.key == mediaManager.mediaNotificationKey && importance > IMPORTANCE_MIN |
| 113 | } |
| 114 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 115 | fun updateRanking( |
| 116 | newRankingMap: RankingMap?, |
| 117 | entries: Collection<NotificationEntry>, |
| 118 | reason: String |
| 119 | ): List<NotificationEntry> { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 120 | // TODO: may not be ideal to guard on null here, but this code is implementing exactly what |
| 121 | // NotificationData used to do |
| 122 | if (newRankingMap != null) { |
| 123 | rankingMap = newRankingMap |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 124 | updateRankingForEntries(entries) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 125 | } |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 126 | return synchronized(this) { |
| 127 | filterAndSortLocked(entries, reason) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 128 | } |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** Uses the [rankingComparator] to sort notifications which aren't filtered */ |
| 132 | private fun filterAndSortLocked( |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 133 | entries: Collection<NotificationEntry>, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 134 | reason: String |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 135 | ): List<NotificationEntry> { |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 136 | logger.logFilterAndSort(reason) |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 137 | val filtered = entries.asSequence() |
| 138 | .filterNot(notifFilter::shouldFilterOut) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 139 | .sortedWith(rankingComparator) |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 140 | .toList() |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 141 | assignBuckets(filtered) |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 142 | return filtered |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 145 | private fun assignBuckets(entries: List<NotificationEntry>) { |
| 146 | entries.forEach { it.bucket = getBucketForEntry(it) } |
| 147 | if (!usePeopleFiltering) { |
| 148 | // If we don't have a Conversation section, just assign buckets normally based on the |
| 149 | // content. |
| 150 | return |
| 151 | } |
| 152 | // If HUNs are not continuous with the top section, break out into a new Incoming section. |
| 153 | entries.asReversed().asSequence().zipWithNext().forEach { (next, entry) -> |
| 154 | if (entry.isRowHeadsUp && entry.bucket > next.bucket) { |
| 155 | entry.bucket = BUCKET_HEADS_UP |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @PriorityBucket |
| 161 | private fun getBucketForEntry(entry: NotificationEntry): Int { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 162 | val isHeadsUp = entry.isRowHeadsUp |
| 163 | val isMedia = isImportantMedia(entry) |
| 164 | val isSystemMax = entry.isSystemMax() |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 165 | return when { |
Steve Elliott | 49671e0 | 2020-05-12 13:51:28 -0400 | [diff] [blame] | 166 | entry.sbn.notification.isForegroundService && entry.sbn.notification.isColorized -> |
| 167 | BUCKET_FOREGROUND_SERVICE |
Steve Elliott | 19b2c86 | 2020-05-05 14:15:14 -0400 | [diff] [blame] | 168 | usePeopleFiltering && entry.getPeopleNotificationType() != TYPE_NON_PERSON -> |
| 169 | BUCKET_PEOPLE |
| 170 | isHeadsUp || isMedia || isSystemMax || entry.isHighPriority() -> |
| 171 | BUCKET_ALERTING |
| 172 | else -> BUCKET_SILENT |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 176 | private fun updateRankingForEntries(entries: Iterable<NotificationEntry>) { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 177 | rankingMap?.let { rankingMap -> |
| 178 | synchronized(entries) { |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 179 | for (entry in entries) { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 180 | val newRanking = Ranking() |
| 181 | if (!rankingMap.getRanking(entry.key, newRanking)) { |
Steve Elliott | 5c42f4a | 2020-03-20 11:19:48 -0400 | [diff] [blame] | 182 | continue |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 183 | } |
| 184 | entry.ranking = newRanking |
| 185 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 186 | val newOverrideGroupKey = newRanking.overrideGroupKey |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 187 | if (!Objects.equals(entry.sbn.overrideGroupKey, newOverrideGroupKey)) { |
| 188 | val oldGroupKey = entry.sbn.groupKey |
| 189 | val oldIsGroup = entry.sbn.isGroup |
| 190 | val oldIsGroupSummary = entry.sbn.notification.isGroupSummary |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 191 | entry.sbn.overrideGroupKey = newOverrideGroupKey |
Beverly | c509412 | 2020-01-09 17:05:15 -0500 | [diff] [blame] | 192 | groupManager.onEntryUpdated(entry, oldGroupKey, oldIsGroup, |
| 193 | oldIsGroupSummary) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 194 | } |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 199 | |
Steve Elliott | 307d385 | 2020-03-05 11:00:22 -0500 | [diff] [blame] | 200 | private fun NotificationEntry.getPeopleNotificationType() = |
| 201 | peopleNotificationIdentifier.getPeopleNotificationType(sbn, ranking) |
Julia Reynolds | dc07d04 | 2020-02-18 13:30:03 -0500 | [diff] [blame] | 202 | |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 203 | private fun NotificationEntry.isHighPriority() = |
| 204 | highPriorityProvider.isHighPriority(this) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Convenience functions |
| 208 | private fun NotificationEntry.isSystemMax(): Boolean { |
| 209 | return importance >= IMPORTANCE_HIGH && sbn.isSystemNotification() |
| 210 | } |
| 211 | |
| 212 | private fun StatusBarNotification.isSystemNotification(): Boolean { |
| 213 | return "android" == packageName || "com.android.systemui" == packageName |
| 214 | } |