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.service.notification.NotificationListenerService.RankingMap
20 import android.service.notification.StatusBarNotification
21 import com.android.systemui.statusbar.notification.collection.NotifCollection
22 import com.android.systemui.statusbar.notification.collection.NotificationEntry
23 
24 /**
25  * Set of classes that represent the various events that [NotifCollection] can dispatch to
26  * [NotifCollectionListener]s.
27  *
28  * These events build up in a queue and are periodically emitted in chunks by the collection.
29  */
30 
31 sealed class NotifEvent {
32     fun dispatchTo(listeners: List<NotifCollectionListener>) {
33         for (i in listeners.indices) {
34             dispatchToListener(listeners[i])
35         }
36     }
37 
38     abstract fun dispatchToListener(listener: NotifCollectionListener)
39 }
40 
41 data class BindEntryEvent(
42     val entry: NotificationEntry,
43     val sbn: StatusBarNotification
44 ) : NotifEvent() {
45     override fun dispatchToListener(listener: NotifCollectionListener) {
46         listener.onEntryBind(entry, sbn)
47     }
48 }
49 
50 data class InitEntryEvent(
51     val entry: NotificationEntry
52 ) : NotifEvent() {
53     override fun dispatchToListener(listener: NotifCollectionListener) {
54         listener.onEntryInit(entry)
55     }
56 }
57 
58 data class EntryAddedEvent(
59     val entry: NotificationEntry
60 ) : NotifEvent() {
61     override fun dispatchToListener(listener: NotifCollectionListener) {
62         listener.onEntryAdded(entry)
63     }
64 }
65 
66 data class EntryUpdatedEvent(
67     val entry: NotificationEntry,
68     val fromSystem: Boolean
69 ) : NotifEvent() {
70     override fun dispatchToListener(listener: NotifCollectionListener) {
71         listener.onEntryUpdated(entry, fromSystem)
72     }
73 }
74 
75 data class EntryRemovedEvent(
76     val entry: NotificationEntry,
77     val reason: Int
78 ) : NotifEvent() {
79     override fun dispatchToListener(listener: NotifCollectionListener) {
80         listener.onEntryRemoved(entry, reason)
81     }
82 }
83 
84 data class CleanUpEntryEvent(
85     val entry: NotificationEntry
86 ) : NotifEvent() {
87     override fun dispatchToListener(listener: NotifCollectionListener) {
88         listener.onEntryCleanUp(entry)
89     }
90 }
91 
92 data class RankingUpdatedEvent(
93     val rankingMap: RankingMap
94 ) : NotifEvent() {
95     override fun dispatchToListener(listener: NotifCollectionListener) {
96         listener.onRankingUpdate(rankingMap)
97     }
98 }
99 
100 class RankingAppliedEvent() : NotifEvent() {
101     override fun dispatchToListener(listener: NotifCollectionListener) {
102         listener.onRankingApplied()
103     }
104 }
105