1 /*
2  * Copyright (C) 2020 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.notifcollection
18 
19 import android.app.NotificationChannel
20 import android.os.UserHandle
21 import android.service.notification.NotificationListenerService.RankingMap
22 import android.service.notification.StatusBarNotification
23 import com.android.systemui.statusbar.notification.collection.NotifCollection
24 import com.android.systemui.statusbar.notification.collection.NotificationEntry
25 import com.android.systemui.util.NamedListenerSet
26 import com.android.app.tracing.traceSection
27 
28 /**
29  * Set of classes that represent the various events that [NotifCollection] can dispatch to
30  * [NotifCollectionListener]s.
31  *
32  * These events build up in a queue and are periodically emitted in chunks by the collection.
33  */
34 
35 sealed class NotifEvent(private val traceName: String) {
dispatchTonull36     fun dispatchTo(listeners: NamedListenerSet<NotifCollectionListener>) {
37         traceSection(traceName) {
38             listeners.forEachTraced(::dispatchToListener)
39         }
40     }
41 
dispatchToListenernull42     abstract fun dispatchToListener(listener: NotifCollectionListener)
43 }
44 
45 data class BindEntryEvent(
46     val entry: NotificationEntry,
47     val sbn: StatusBarNotification
48 ) : NotifEvent("onEntryBind") {
49     override fun dispatchToListener(listener: NotifCollectionListener) {
50         listener.onEntryBind(entry, sbn)
51     }
52 }
53 
54 data class InitEntryEvent(
55     val entry: NotificationEntry
56 ) : NotifEvent("onEntryInit") {
dispatchToListenernull57     override fun dispatchToListener(listener: NotifCollectionListener) {
58         listener.onEntryInit(entry)
59     }
60 }
61 
62 data class EntryAddedEvent(
63     val entry: NotificationEntry
64 ) : NotifEvent("onEntryAdded") {
dispatchToListenernull65     override fun dispatchToListener(listener: NotifCollectionListener) {
66         listener.onEntryAdded(entry)
67     }
68 }
69 
70 data class EntryUpdatedEvent(
71     val entry: NotificationEntry,
72     val fromSystem: Boolean
73 ) : NotifEvent(if (fromSystem) "onEntryUpdated" else "onEntryUpdated fromSystem=true") {
dispatchToListenernull74     override fun dispatchToListener(listener: NotifCollectionListener) {
75         listener.onEntryUpdated(entry, fromSystem)
76     }
77 }
78 
79 data class EntryRemovedEvent(
80     val entry: NotificationEntry,
81     val reason: Int
82 ) : NotifEvent("onEntryRemoved ${cancellationReasonDebugString(reason)}") {
dispatchToListenernull83     override fun dispatchToListener(listener: NotifCollectionListener) {
84         listener.onEntryRemoved(entry, reason)
85     }
86 }
87 
88 data class CleanUpEntryEvent(
89     val entry: NotificationEntry
90 ) : NotifEvent("onEntryCleanUp") {
dispatchToListenernull91     override fun dispatchToListener(listener: NotifCollectionListener) {
92         listener.onEntryCleanUp(entry)
93     }
94 }
95 
96 data class RankingUpdatedEvent(
97     val rankingMap: RankingMap
98 ) : NotifEvent("onRankingUpdate") {
dispatchToListenernull99     override fun dispatchToListener(listener: NotifCollectionListener) {
100         listener.onRankingUpdate(rankingMap)
101     }
102 }
103 
104 class RankingAppliedEvent : NotifEvent("onRankingApplied") {
dispatchToListenernull105     override fun dispatchToListener(listener: NotifCollectionListener) {
106         listener.onRankingApplied()
107     }
108 }
109 
110 data class ChannelChangedEvent(
111     val pkgName: String,
112     val user: UserHandle,
113     val channel: NotificationChannel,
114     val modificationType: Int
115 ) : NotifEvent("onNotificationChannelModified") {
dispatchToListenernull116     override fun dispatchToListener(listener: NotifCollectionListener) {
117         listener.onNotificationChannelModified(pkgName, user, channel, modificationType)
118     }
119 }
120