@@ -8,66 +8,77 @@ import io.flutter.plugin.common.EventChannel.EventSink
88internal class ThreatDispatcher {
99 private val threatCache = mutableSetOf<Threat >()
1010 private val malwareCache = mutableListOf<SuspiciousAppInfo >()
11+ private var isAppInForeground = false
1112
1213 var eventSink: EventSink ? = null
1314 set(value) {
1415 field = value
1516 if (value != null ) {
16- flushThreatCache(value)
17+ isAppInForeground = true
18+ flushThreatCache()
1719 }
1820 }
1921
2022 var methodSink: MethodCallHandler .MethodSink ? = null
2123 set(value) {
2224 field = value
2325 if (value != null ) {
24- flushMalwareCache(value)
26+ isAppInForeground = true
27+ flushMalwareCache()
2528 }
2629 }
2730
31+ fun onResume () {
32+ isAppInForeground = true
33+ if (eventSink != null ) {
34+ flushThreatCache()
35+ }
36+ if (methodSink != null ) {
37+ flushMalwareCache()
38+ }
39+ }
40+
41+ fun onPause () {
42+ isAppInForeground = false
43+ }
44+
2845 fun dispatchThreat (threat : Threat ) {
29- val sink = synchronized(threatCache) {
30- val currentSink = eventSink
31- if (currentSink != null ) {
32- currentSink
33- } else {
46+ if (isAppInForeground && eventSink != null ) {
47+ eventSink?.success(threat.value)
48+ } else {
49+ synchronized(threatCache) {
3450 threatCache.add(threat)
35- null
3651 }
3752 }
38- sink?.success(threat.value)
3953 }
4054
4155 fun dispatchMalware (apps : List <SuspiciousAppInfo >) {
42- val sink = synchronized(malwareCache) {
43- val currentSink = methodSink
44- if (currentSink != null ) {
45- currentSink
46- } else {
56+ if (isAppInForeground && methodSink != null ) {
57+ methodSink?.onMalwareDetected(apps)
58+ } else {
59+ synchronized(malwareCache) {
4760 malwareCache.addAll(apps)
48- null
4961 }
5062 }
51- sink?.onMalwareDetected(apps)
5263 }
5364
54- private fun flushThreatCache (sink : EventSink ) {
65+ private fun flushThreatCache () {
5566 val threats = synchronized(threatCache) {
5667 val snapshot = threatCache.toSet()
5768 threatCache.clear()
5869 snapshot
5970 }
60- threats.forEach { sink .success(it.value) }
71+ threats.forEach { eventSink? .success(it.value) }
6172 }
6273
63- private fun flushMalwareCache (sink : MethodCallHandler . MethodSink ) {
74+ private fun flushMalwareCache () {
6475 val malware = synchronized(malwareCache) {
6576 val snapshot = malwareCache.toMutableList()
6677 malwareCache.clear()
6778 snapshot
6879 }
6980 if (malware.isNotEmpty()) {
70- sink .onMalwareDetected(malware)
81+ methodSink? .onMalwareDetected(malware)
7182 }
7283 }
7384}
0 commit comments