From c835f5ab80fc2e09e798315c8844db2254dc9496 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 21:43:13 +0200 Subject: [PATCH] minor improvements --- .../libretube/fragments/PlayerFragment.kt | 3 +++ .../libretube/services/BackgroundMode.kt | 3 +++ .../sheets/VideoOptionsBottomSheet.kt | 19 +++---------------- .../libretube/util/NowPlayingNotification.kt | 1 + .../com/github/libretube/util/PlayingQueue.kt | 2 ++ 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 8d75e1ce4..daea985dd 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -687,6 +687,9 @@ class PlayerFragment : BaseFragment() { override fun onDestroy() { super.onDestroy() try { + // clear the playing queue + PlayingQueue.clear() + saveWatchPosition() nowPlayingNotification.destroy() activity?.requestedOrientation = diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 4e85a30e3..3fd4010b2 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -333,6 +333,9 @@ class BackgroundMode : Service() { * destroy the [BackgroundMode] foreground service */ override fun onDestroy() { + // clear the playing queue + PlayingQueue.clear() + // called when the user pressed stop in the notification // stop the service from being in the foreground and remove the notification if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { diff --git a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt index a9809baf2..b988c9f71 100644 --- a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -1,13 +1,9 @@ package com.github.libretube.sheets -import android.app.NotificationManager -import android.content.Context -import android.os.Build import android.os.Bundle import android.widget.Toast import com.github.libretube.R import com.github.libretube.constants.IntentData -import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.dialogs.AddToPlaylistDialog import com.github.libretube.dialogs.DownloadDialog import com.github.libretube.dialogs.ShareDialog @@ -44,18 +40,9 @@ class VideoOptionsBottomSheet( /** * Check whether the player is running by observing the notification */ - try { - val notificationManager = - context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - notificationManager.activeNotifications.forEach { - if (it.id == PLAYER_NOTIFICATION_ID) { - optionsList += context?.getString(R.string.add_to_queue)!! - } - } - } - } catch (e: Exception) { - e.printStackTrace() + if (PlayingQueue.isNotEmpty()) { + optionsList += context?.getString(R.string.play_next)!! + optionsList += context?.getString(R.string.add_to_queue)!! } setSimpleItems(optionsList) { which -> diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index b50df7208..25aec4703 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -166,6 +166,7 @@ class NowPlayingNotification( Context.NOTIFICATION_SERVICE ) as NotificationManager notificationManager.cancel(PLAYER_NOTIFICATION_ID) + player.stop() player.release() } } diff --git a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt index 2eec4e4a2..181d47588 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -51,4 +51,6 @@ object PlayingQueue { currentVideoId = videoId if (!contains(videoId)) add(videoId) } + + fun isNotEmpty() = queue.isNotEmpty() }