From 7fff7728aae97de06994f863892571dff9301322 Mon Sep 17 00:00:00 2001 From: Serhii Stepanov Date: Mon, 5 Feb 2024 19:28:09 +0100 Subject: [PATCH] fix multiple unresponsive and unclosable overlays can be opened --- .../FlutterOverlayWindowPlugin.java | 2 +- .../OverlayService.java | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java index 6c5ce2cb..453e0a20 100644 --- a/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java +++ b/android/src/main/java/flutter/overlay/window/flutter_overlay_window/FlutterOverlayWindowPlugin.java @@ -6,6 +6,7 @@ import android.net.Uri; import android.os.Build; import android.provider.Settings; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -110,7 +111,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { } else { result.notImplemented(); } - } @Override 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 f8aad1a2..36b0025b 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 @@ -76,6 +76,14 @@ public IBinder onBind(Intent intent) { public void onDestroy() { Log.d("OverLay", "Destroying the overlay window service"); isRunning = false; + if(windowManager != null && flutterView != null) { + windowManager.removeView(flutterView); + windowManager = null; + } + if(flutterView != null) { + flutterView.detachFromFlutterEngine(); + flutterView = null; + } NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(OverlayConstants.NOTIFICATION_ID); if(CachedMessageChannels.overlayMessageChannel != null){ @@ -90,20 +98,16 @@ public int onStartCommand(Intent intent, int flags, int startId) { mResources = getApplicationContext().getResources(); boolean isCloseWindow = intent.getBooleanExtra(INTENT_EXTRA_IS_CLOSE_WINDOW, false); if (isCloseWindow) { - if (windowManager != null) { - windowManager.removeView(flutterView); - windowManager = null; - flutterView.detachFromFlutterEngine(); - stopSelf(); - } - isRunning = false; + stopSelf(); return START_STICKY; } - if (windowManager != null) { + if(windowManager != null && flutterView != null) { windowManager.removeView(flutterView); windowManager = null; + } + if(flutterView != null) { flutterView.detachFromFlutterEngine(); - stopSelf(); + flutterView = null; } isRunning = true; Log.d("onStartCommand", "Service started"); @@ -128,7 +132,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { BasicMessageChannel overlayMessageChannel = new BasicMessageChannel<>(FlutterEngineCache.getInstance().get(OverlayConstants.CACHED_TAG).getDartExecutor().getBinaryMessenger(), OverlayConstants.MESSENGER_TAG, JSONMessageCodec.INSTANCE); overlayMessageChannel.setMessageHandler((message, reply) -> { - if(CachedMessageChannels.mainAppMessageChannel == null) { + if (CachedMessageChannels.mainAppMessageChannel == null) { reply.reply(false); return; }