From 40d5e211cfc1c01a360265ccaa0f0458c1fc990e Mon Sep 17 00:00:00 2001 From: NBTX Date: Mon, 24 Jun 2019 00:01:54 +0100 Subject: [PATCH] Ensure all handlers are run on main thread --- .../handlers/DiscoveryRunningHandler.java | 2 +- .../handlers/ServiceDiscoveredHandler.java | 11 ++++++++++- .../handlers/ServiceLostHandler.java | 11 ++++++++++- .../handlers/ServiceResolvedHandler.java | 11 ++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/DiscoveryRunningHandler.java b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/DiscoveryRunningHandler.java index 9a40fac..67ceaf7 100644 --- a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/DiscoveryRunningHandler.java +++ b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/DiscoveryRunningHandler.java @@ -12,8 +12,8 @@ public class DiscoveryRunningHandler implements EventChannel.StreamHandler { public DiscoveryRunningHandler(){ this.handler = new Handler(Looper.getMainLooper()); - } + @Override public void onListen(Object o, EventChannel.EventSink eventSink) { sink = eventSink; diff --git a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceDiscoveredHandler.java b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceDiscoveredHandler.java index 86a9fb6..1adf39a 100644 --- a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceDiscoveredHandler.java +++ b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceDiscoveredHandler.java @@ -1,12 +1,21 @@ package eu.sndr.fluttermdnsplugin.handlers; +import android.os.Handler; +import android.os.Looper; + import java.util.Map; import io.flutter.plugin.common.EventChannel; public class ServiceDiscoveredHandler implements EventChannel.StreamHandler { + private Handler handler; EventChannel.EventSink sink; + + public ServiceDiscoveredHandler(){ + this.handler = new Handler(Looper.getMainLooper()); + } + @Override public void onListen(Object o, EventChannel.EventSink eventSink) { sink = eventSink; @@ -18,6 +27,6 @@ public void onCancel(Object o) { } public void onServiceDiscovered(Map serviceInfoMap){ - sink.success(serviceInfoMap); + handler.post(() -> sink.success(serviceInfoMap)); } } \ No newline at end of file diff --git a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceLostHandler.java b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceLostHandler.java index 3aa8141..dfcfb87 100644 --- a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceLostHandler.java +++ b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceLostHandler.java @@ -1,12 +1,21 @@ package eu.sndr.fluttermdnsplugin.handlers; +import android.os.Handler; +import android.os.Looper; + import java.util.Map; import io.flutter.plugin.common.EventChannel; public class ServiceLostHandler implements EventChannel.StreamHandler { + private Handler handler; EventChannel.EventSink sink; + + public ServiceLostHandler(){ + this.handler = new Handler(Looper.getMainLooper()); + } + @Override public void onListen(Object o, EventChannel.EventSink eventSink) { sink = eventSink; @@ -18,6 +27,6 @@ public void onCancel(Object o) { } public void onServiceLost(Map serviceInfoMap){ - sink.success(serviceInfoMap); + handler.post(() -> sink.success(serviceInfoMap)); } } \ No newline at end of file diff --git a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceResolvedHandler.java b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceResolvedHandler.java index 214b120..fe0b0f6 100644 --- a/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceResolvedHandler.java +++ b/android/src/main/java/eu/sndr/fluttermdnsplugin/handlers/ServiceResolvedHandler.java @@ -1,12 +1,21 @@ package eu.sndr.fluttermdnsplugin.handlers; +import android.os.Handler; +import android.os.Looper; + import java.util.Map; import io.flutter.plugin.common.EventChannel; public class ServiceResolvedHandler implements EventChannel.StreamHandler { + private Handler handler; EventChannel.EventSink sink; + + public ServiceResolvedHandler(){ + this.handler = new Handler(Looper.getMainLooper()); + } + @Override public void onListen(Object o, EventChannel.EventSink eventSink) { sink = eventSink; @@ -18,7 +27,7 @@ public void onCancel(Object o) { } public void onServiceResolved(Map serviceInfoMap) { - sink.success(serviceInfoMap); + handler.post(() -> sink.success(serviceInfoMap)); } }