From 3c5c0cfee2a5beff68d46d10aca218f4b8583516 Mon Sep 17 00:00:00 2001 From: Pedro Alvim Date: Thu, 22 Aug 2024 15:15:29 -0300 Subject: [PATCH] Fixing crash in null references --- .../flutter_overlay_window/OverlayService.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java index 265421e..a1b1bba 100644 --- a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java +++ b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/OverlayService.java @@ -55,8 +55,8 @@ public class OverlayService extends Service implements View.OnTouchListener { public static boolean isRunning = false; private WindowManager windowManager = null; private FlutterView flutterView; - private MethodChannel flutterChannel = new MethodChannel(FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG).getDartExecutor(), OverlayConstants.OVERLAY_TAG); - private BasicMessageChannel overlayMessageChannel = new BasicMessageChannel(FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG).getDartExecutor(), OverlayConstants.MESSENGER_TAG, JSONMessageCodec.INSTANCE); + private MethodChannel flutterChannel; + private BasicMessageChannel overlayMessageChannel; private int clickableFlag = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; @@ -117,6 +117,15 @@ public int onStartCommand(Intent intent, int flags, int startId) { isRunning = true; Log.d("onStartCommand", "Service started"); FlutterEngine engine = FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG); + + if(engine == null){ + return START_STICKY; + } + + if(flutterChannel == null){ + flutterChannel = new MethodChannel(engine.getDartExecutor(), OverlayConstants.OVERLAY_TAG); + } + engine.getLifecycleChannel().appIsResumed(); flutterView = new FlutterView(getApplicationContext(), new FlutterTextureView(getApplicationContext())); flutterView.attachToFlutterEngine(FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG)); @@ -139,6 +148,11 @@ public int onStartCommand(Intent intent, int flags, int startId) { resizeOverlay(width, height, enableDrag, result); } }); + + if(overlayMessageChannel == null) { + overlayMessageChannel = new BasicMessageChannel(engine.getDartExecutor(), OverlayConstants.MESSENGER_TAG, JSONMessageCodec.INSTANCE); + } + overlayMessageChannel.setMessageHandler((message, reply) -> { WindowSetup.messenger.send(message); });