-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathThreatDispatcher.kt
More file actions
77 lines (65 loc) · 1.84 KB
/
ThreatDispatcher.kt
File metadata and controls
77 lines (65 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.freeraspreactnative.dispatchers
import com.aheaditec.talsec_security.security.api.SuspiciousAppInfo
import com.freeraspreactnative.events.ThreatEvent
import com.freeraspreactnative.interfaces.PluginThreatListener
internal object ThreatDispatcher {
private lateinit var listener: PluginThreatListener
private val threatCache = mutableSetOf<ThreatEvent>()
private val malwareCache = mutableSetOf<SuspiciousAppInfo>()
private var isAppInForeground = false
private var isListenerRegistered = false
fun registerListener(newListener: PluginThreatListener) {
listener = newListener
isListenerRegistered = true
isAppInForeground = true
flushCache()
}
fun unregisterListener() {
isListenerRegistered = false
isAppInForeground = false
}
fun onResume() {
isAppInForeground = true
if (isListenerRegistered) {
flushCache()
}
}
fun onPause() {
isAppInForeground = false
}
fun dispatchThreat(event: ThreatEvent) {
if (isAppInForeground && isListenerRegistered) {
listener.threatDetected(event)
} else {
synchronized(threatCache) {
threatCache.add(event)
}
}
}
fun dispatchMalware(apps: MutableList<SuspiciousAppInfo>) {
if (isAppInForeground && isListenerRegistered) {
listener.malwareDetected(apps)
}
else {
synchronized(malwareCache) {
malwareCache.addAll(apps)
}
}
}
private fun flushCache() {
val threats = synchronized(threatCache) {
val snapshot = threatCache.toSet()
threatCache.clear()
snapshot
}
threats.forEach { listener.threatDetected(it) }
val malware = synchronized(malwareCache) {
val snapshot = malwareCache.toMutableList()
malwareCache.clear()
snapshot
}
if (malware.isNotEmpty()) {
listener.malwareDetected(malware)
}
}
}