From aaa3e20c5aa16766fff1b3f306e785670eed52a9 Mon Sep 17 00:00:00 2001 From: evermind Date: Tue, 22 Jun 2021 17:24:14 +0200 Subject: [PATCH] service.onDestroy() should only be called from the system and not manually instead use service.stopService() which inturn calls stopSelf() and triggers hopefully onDestroy() to be called. Eventually we have to make sure that all ServiceConnections are closed to successfully stop the service now! Cleanup within stopService() and not only onDestroy() So we make sure that all listeners can react to onServiceStopped() and close their ServiceConnections. Afterwards the android framework is ready to stop the Service. --- .../java/org/schabi/newpipe/player/MainPlayer.java | 10 +++++++++- .../main/java/org/schabi/newpipe/player/Player.java | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java index 945bc9a04..7a04ec22e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java @@ -178,7 +178,10 @@ public final class MainPlayer extends Service { if (DEBUG) { Log.d(TAG, "destroy() called"); } + cleanup(); + } + private void cleanup() { if (player != null) { // Exit from fullscreen when user closes the player via notification if (player.isFullscreen()) { @@ -191,9 +194,14 @@ public final class MainPlayer extends Service { player.stopActivityBinding(); player.removePopupFromView(); player.destroy(); - } + player = null; + } + } + + public void stopService() { NotificationUtil.getInstance().cancelNotificationAndStopForeground(this); + cleanup(); stopSelf(); } diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index 0a0ad619f..146134aee 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -857,7 +857,7 @@ public final class Player implements Log.d(TAG, "onPlaybackShutdown() called"); } // destroys the service, which in turn will destroy the player - service.onDestroy(); + service.stopService(); } public void smoothStopPlayer() { @@ -1097,7 +1097,7 @@ public final class Player implements pause(); break; case ACTION_CLOSE: - service.onDestroy(); + service.stopService(); break; case ACTION_PLAY_PAUSE: playPause(); @@ -1498,7 +1498,7 @@ public final class Player implements Objects.requireNonNull(windowManager) .removeView(closeOverlayBinding.getRoot()); closeOverlayBinding = null; - service.onDestroy(); + service.stopService(); } }).start(); }