From d99386bbc20ebc68bc5c288783d0ed10d0efa9ac Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 20:07:55 +0200 Subject: [PATCH 1/7] get rid of globals --- .../main/java/com/github/libretube/Globals.kt | 12 ----------- .../libretube/fragments/PlayerFragment.kt | 15 +++++++------- .../libretube/services/BackgroundMode.kt | 6 +++--- .../libretube/services/DownloadService.kt | 10 +++++++--- .../sheets/VideoOptionsBottomSheet.kt | 4 ++-- .../github/libretube/util/AutoPlayHelper.kt | 17 ++++++++-------- .../com/github/libretube/util/PlayingQueue.kt | 20 +++++++++++++++++++ 7 files changed, 48 insertions(+), 36 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/Globals.kt create mode 100644 app/src/main/java/com/github/libretube/util/PlayingQueue.kt diff --git a/app/src/main/java/com/github/libretube/Globals.kt b/app/src/main/java/com/github/libretube/Globals.kt deleted file mode 100644 index 48fe4a64b..000000000 --- a/app/src/main/java/com/github/libretube/Globals.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.libretube - -/** - * Global variables can be stored here - */ -object Globals { - // for downloads - var IS_DOWNLOAD_RUNNING = false - - // history of played videos in the current lifecycle - val playingQueue = mutableListOf() -} 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 1c534c8c6..dc9e37fc0 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -35,7 +35,6 @@ import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import com.fasterxml.jackson.databind.ObjectMapper -import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.activities.MainActivity import com.github.libretube.adapters.ChaptersAdapter @@ -67,12 +66,14 @@ import com.github.libretube.obj.Segment import com.github.libretube.obj.Segments import com.github.libretube.obj.Streams import com.github.libretube.services.BackgroundMode +import com.github.libretube.services.DownloadService import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.ImageHelper import com.github.libretube.util.NetworkHelper import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.PlayerHelper +import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PreferenceHelper import com.google.android.exoplayer2.C import com.google.android.exoplayer2.DefaultLoadControl @@ -210,7 +211,7 @@ class PlayerFragment : BaseFragment() { context?.hideKeyboard(view) // clear the playing queue - Globals.playingQueue.clear() + PlayingQueue.clear() setUserPrefs() @@ -752,7 +753,7 @@ class PlayerFragment : BaseFragment() { } private fun playVideo() { - Globals.playingQueue += videoId!! + PlayingQueue.add(videoId!!) lifecycleScope.launchWhenCreated { streams = try { RetrofitInstance.api.getStreams(videoId!!) @@ -1023,7 +1024,7 @@ class PlayerFragment : BaseFragment() { if (response.duration > 0) { // download clicked binding.relPlayerDownload.setOnClickListener { - if (!Globals.IS_DOWNLOAD_RUNNING) { + if (!DownloadService.IS_DOWNLOAD_RUNNING) { val newFragment = DownloadDialog(videoId!!) newFragment.show(childFragmentManager, DownloadDialog::class.java.name) } else { @@ -1105,7 +1106,7 @@ class PlayerFragment : BaseFragment() { // next and previous buttons playerBinding.skipPrev.visibility = if ( - skipButtonsEnabled && Globals.playingQueue.indexOf(videoId!!) != 0 + skipButtonsEnabled && PlayingQueue.queue.indexOf(videoId!!) != 0 ) { View.VISIBLE } else { @@ -1114,8 +1115,8 @@ class PlayerFragment : BaseFragment() { playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE playerBinding.skipPrev.setOnClickListener { - val index = Globals.playingQueue.indexOf(videoId!!) - 1 - videoId = Globals.playingQueue[index] + val index = PlayingQueue.queue.indexOf(videoId!!) - 1 + videoId = PlayingQueue.queue[index] playVideo() } 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 6c75c5f21..9c16fdba4 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -11,7 +11,6 @@ import android.os.IBinder import android.os.Looper import android.widget.Toast import com.fasterxml.jackson.databind.ObjectMapper -import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.BACKGROUND_CHANNEL_ID @@ -25,6 +24,7 @@ import com.github.libretube.obj.Streams import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.PlayerHelper +import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PreferenceHelper import com.google.android.exoplayer2.C import com.google.android.exoplayer2.ExoPlayer @@ -119,7 +119,7 @@ class BackgroundMode : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { try { // clear the playing queue - Globals.playingQueue.clear() + PlayingQueue.clear() // get the intent arguments videoId = intent?.getStringExtra(IntentData.videoId)!! @@ -145,7 +145,7 @@ class BackgroundMode : Service() { seekToPosition: Long = 0 ) { // append the video to the playing queue - Globals.playingQueue += videoId + PlayingQueue.add(videoId) CoroutineScope(Dispatchers.IO).launch { try { streams = RetrofitInstance.api.getStreams(videoId) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 09c676c0b..bc52b3532 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -11,7 +11,6 @@ import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat -import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID @@ -34,7 +33,7 @@ class DownloadService : Service() { override fun onCreate() { super.onCreate() - Globals.IS_DOWNLOAD_RUNNING = true + IS_DOWNLOAD_RUNNING = true } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -175,11 +174,16 @@ class DownloadService : Service() { try { unregisterReceiver(onDownloadComplete) } catch (e: Exception) { + e.printStackTrace() } - Globals.IS_DOWNLOAD_RUNNING = false + IS_DOWNLOAD_RUNNING = false stopService(Intent(this, DownloadService::class.java)) super.onDestroy() } + + companion object { + var IS_DOWNLOAD_RUNNING = false + } } 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 0a9180ec0..68504738e 100644 --- a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -5,7 +5,6 @@ import android.content.Context import android.os.Build import android.os.Bundle import android.widget.Toast -import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.constants.PLAYER_NOTIFICATION_ID @@ -13,6 +12,7 @@ import com.github.libretube.dialogs.AddToPlaylistDialog import com.github.libretube.dialogs.DownloadDialog import com.github.libretube.dialogs.ShareDialog import com.github.libretube.util.BackgroundHelper +import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PreferenceHelper import com.github.libretube.views.BottomSheet @@ -90,7 +90,7 @@ class VideoOptionsBottomSheet( shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) } context?.getString(R.string.add_to_queue) -> { - Globals.playingQueue += videoId + PlayingQueue.add(videoId) } } } diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index 3217312e4..3a08904ae 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -1,6 +1,5 @@ package com.github.libretube.util -import com.github.libretube.Globals import com.github.libretube.api.RetrofitInstance import com.github.libretube.extensions.toID import com.github.libretube.obj.StreamItem @@ -21,9 +20,9 @@ class AutoPlayHelper( currentVideoId: String, relatedStreams: List? ): String? { - return if (Globals.playingQueue.last() != currentVideoId) { - val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) - Globals.playingQueue[currentVideoIndex + 1] + return if (PlayingQueue.queue.last() != currentVideoId) { + val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId) + PlayingQueue.queue[currentVideoIndex + 1] } else if (playlistId == null) { getNextTrendingVideoId( currentVideoId, @@ -49,8 +48,8 @@ class AutoPlayHelper( var nextStreamId: String? = null while (nextStreamId == null || ( - Globals.playingQueue.contains(nextStreamId) && - Globals.playingQueue.indexOf(videoId) > Globals.playingQueue.indexOf( + PlayingQueue.queue.contains(nextStreamId) && + PlayingQueue.queue.indexOf(videoId) > PlayingQueue.queue.indexOf( nextStreamId ) ) @@ -110,9 +109,9 @@ class AutoPlayHelper( fun getNextPlayingQueueVideoId( currentVideoId: String ): String? { - return if (Globals.playingQueue.last() != currentVideoId) { - val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId) - Globals.playingQueue[currentVideoIndex + 1] + return if (PlayingQueue.queue.last() != currentVideoId) { + val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId) + PlayingQueue.queue[currentVideoIndex + 1] } else { null } diff --git a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt new file mode 100644 index 000000000..7ce3ee241 --- /dev/null +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -0,0 +1,20 @@ +package com.github.libretube.util + +object PlayingQueue { + val queue = mutableListOf() + + fun clear() { + queue.clear() + } + + fun add(videoId: String) { + queue.add(videoId) + } + + fun playNext(currentVideoId: String, nextVideoId: String) { + queue.add( + queue.indexOf(currentVideoId), + nextVideoId + ) + } +} From 60b49f21207c86a23d65be593a2e9a79ede0fac9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 20:13:25 +0200 Subject: [PATCH 2/7] playing queue improvements --- .../github/libretube/sheets/VideoOptionsBottomSheet.kt | 3 +++ .../main/java/com/github/libretube/util/PlayingQueue.kt | 9 ++++++++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) 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 68504738e..a9809baf2 100644 --- a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -89,6 +89,9 @@ class VideoOptionsBottomSheet( // using parentFragmentManager is important here shareDialog.show(parentFragmentManager, ShareDialog::class.java.name) } + context?.getString(R.string.play_next) -> { + PlayingQueue.playNext(videoId) + } context?.getString(R.string.add_to_queue) -> { PlayingQueue.add(videoId) } 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 7ce3ee241..90cde1cdd 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -2,6 +2,7 @@ package com.github.libretube.util object PlayingQueue { val queue = mutableListOf() + val currentVideoId: String? = null fun clear() { queue.clear() @@ -11,10 +12,16 @@ object PlayingQueue { queue.add(videoId) } - fun playNext(currentVideoId: String, nextVideoId: String) { + fun playNext(nextVideoId: String) { queue.add( queue.indexOf(currentVideoId), nextVideoId ) } + + fun getNext(): String? { + val currentIndex = queue.indexOf(currentVideoId) + return if (currentIndex > queue.size) null + else queue[currentIndex + 1] + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4cfdce884..b49c26d4e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -329,6 +329,7 @@ Custom Instances Load feed in background Load the subscription feed in the background and prevent it from being auto-refreshed. + Play next Download Service From 0db7821418987aa47ba368314296e302bb18b9c9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 21:37:30 +0200 Subject: [PATCH 3/7] improve playing queue --- .../libretube/fragments/PlayerFragment.kt | 10 +++--- .../libretube/services/BackgroundMode.kt | 4 ++- .../github/libretube/util/AutoPlayHelper.kt | 30 ++-------------- .../com/github/libretube/util/PlayingQueue.kt | 35 ++++++++++++++++--- 4 files changed, 41 insertions(+), 38 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 dc9e37fc0..8d75e1ce4 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -696,6 +696,7 @@ class PlayerFragment : BaseFragment() { ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT } } catch (e: Exception) { + e.printStackTrace() } } @@ -753,7 +754,7 @@ class PlayerFragment : BaseFragment() { } private fun playVideo() { - PlayingQueue.add(videoId!!) + PlayingQueue.updateCurrent(videoId!!) lifecycleScope.launchWhenCreated { streams = try { RetrofitInstance.api.getStreams(videoId!!) @@ -876,7 +877,7 @@ class PlayerFragment : BaseFragment() { private fun playNextVideo() { if (nextStreamId == null) return // check whether there is a new video in the queue - val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId!!) + val nextQueueVideo = PlayingQueue.getNext() if (nextQueueVideo != null) nextStreamId = nextQueueVideo // by making sure that the next and the current video aren't the same saveWatchPosition() @@ -1106,7 +1107,7 @@ class PlayerFragment : BaseFragment() { // next and previous buttons playerBinding.skipPrev.visibility = if ( - skipButtonsEnabled && PlayingQueue.queue.indexOf(videoId!!) != 0 + skipButtonsEnabled && PlayingQueue.hasPrev() ) { View.VISIBLE } else { @@ -1115,8 +1116,7 @@ class PlayerFragment : BaseFragment() { playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE playerBinding.skipPrev.setOnClickListener { - val index = PlayingQueue.queue.indexOf(videoId!!) - 1 - videoId = PlayingQueue.queue[index] + videoId = PlayingQueue.getPrev() playVideo() } 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 9c16fdba4..4e85a30e3 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -162,6 +162,8 @@ class BackgroundMode : Service() { private fun playAudio( seekToPosition: Long ) { + PlayingQueue.updateCurrent(videoId) + initializePlayer() setMediaItem() @@ -247,7 +249,7 @@ class BackgroundMode : Service() { */ private fun playNextVideo() { if (nextStreamId == null || nextStreamId == videoId) return - val nextQueueVideo = autoPlayHelper.getNextPlayingQueueVideoId(videoId) + val nextQueueVideo = PlayingQueue.getNext() if (nextQueueVideo != null) nextStreamId = nextQueueVideo // play new video on background diff --git a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt index 3a08904ae..af2084374 100644 --- a/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt +++ b/app/src/main/java/com/github/libretube/util/AutoPlayHelper.kt @@ -20,12 +20,8 @@ class AutoPlayHelper( currentVideoId: String, relatedStreams: List? ): String? { - return if (PlayingQueue.queue.last() != currentVideoId) { - val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId) - PlayingQueue.queue[currentVideoIndex + 1] - } else if (playlistId == null) { + return if (playlistId == null) { getNextTrendingVideoId( - currentVideoId, relatedStreams ) } else { @@ -39,21 +35,13 @@ class AutoPlayHelper( * get the id of the next related video */ private fun getNextTrendingVideoId( - videoId: String, relatedStreams: List? ): String? { // don't play a video if it got played before already if (relatedStreams == null || relatedStreams.isEmpty()) return null var index = 0 var nextStreamId: String? = null - while (nextStreamId == null || - ( - PlayingQueue.queue.contains(nextStreamId) && - PlayingQueue.queue.indexOf(videoId) > PlayingQueue.queue.indexOf( - nextStreamId - ) - ) - ) { + while (nextStreamId == null || PlayingQueue.containsBefore(nextStreamId)) { nextStreamId = relatedStreams[index].url!!.toID() if (index + 1 < relatedStreams.size) { index += 1 @@ -102,18 +90,4 @@ class AutoPlayHelper( // return null when no nextPage is found return null } - - /** - * get the videoId of the next video in the playing queue - */ - fun getNextPlayingQueueVideoId( - currentVideoId: String - ): String? { - return if (PlayingQueue.queue.last() != currentVideoId) { - val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId) - PlayingQueue.queue[currentVideoIndex + 1] - } else { - null - } - } } 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 90cde1cdd..2eec4e4a2 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -1,8 +1,8 @@ package com.github.libretube.util object PlayingQueue { - val queue = mutableListOf() - val currentVideoId: String? = null + private val queue = mutableListOf() + private var currentVideoId: String? = null fun clear() { queue.clear() @@ -21,7 +21,34 @@ object PlayingQueue { fun getNext(): String? { val currentIndex = queue.indexOf(currentVideoId) - return if (currentIndex > queue.size) null - else queue[currentIndex + 1] + return if (currentIndex > queue.size) { + null + } else { + queue[currentIndex + 1] + } + } + + fun getPrev(): String { + return queue[ + queue.indexOf(currentVideoId) - 1 + ] + } + + fun hasPrev(): Boolean { + val currentIndex = queue.indexOf(currentVideoId) + return currentIndex >= 1 + } + + fun contains(videoId: String): Boolean { + return queue.contains(videoId) + } + + fun containsBefore(videoId: String): Boolean { + return queue.contains(videoId) && queue.indexOf(videoId) < queue.indexOf(currentVideoId) + } + + fun updateCurrent(videoId: String) { + currentVideoId = videoId + if (!contains(videoId)) add(videoId) } } From c835f5ab80fc2e09e798315c8844db2254dc9496 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 21:43:13 +0200 Subject: [PATCH 4/7] 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() } From 485cc190237f68fd852b117854dcc8fd9b7a51e2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 21:52:08 +0200 Subject: [PATCH 5/7] fix crash when videoId null --- .../main/java/com/github/libretube/db/dao/WatchPositionDao.kt | 2 +- .../java/com/github/libretube/fragments/PlayerFragment.kt | 3 ++- app/src/main/java/com/github/libretube/util/PlayingQueue.kt | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt index 09ec247ac..6855eaab4 100644 --- a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt @@ -13,7 +13,7 @@ interface WatchPositionDao { fun getAll(): List @Query("SELECT * FROM watchPosition WHERE videoId LIKE :videoId LIMIT 1") - fun findById(videoId: String): WatchPosition + fun findById(videoId: String): WatchPosition? @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(vararg watchPositions: WatchPosition) 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 daea985dd..41534f0d5 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -866,7 +866,8 @@ class PlayerFragment : BaseFragment() { var position: Long? = null Thread { try { - position = Database.watchPositionDao().findById(videoId!!).position + val watchPosition = Database.watchPositionDao().findById(videoId!!) + position = if (watchPosition != null) watchPosition.position else null // position is almost the end of the video => don't seek, start from beginning if (position!! > streams.duration!! * 1000 * 0.9) position = null } catch (e: Exception) { 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 181d47588..ecc5d6e9d 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -1,5 +1,8 @@ package com.github.libretube.util +import android.util.Log +import com.github.libretube.extensions.TAG + object PlayingQueue { private val queue = mutableListOf() private var currentVideoId: String? = null @@ -21,6 +24,7 @@ object PlayingQueue { fun getNext(): String? { val currentIndex = queue.indexOf(currentVideoId) + Log.e(TAG(), queue.toString()) return if (currentIndex > queue.size) { null } else { From 7844cc77925554132aaf3efab827fde5eacae9df Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 22:07:15 +0200 Subject: [PATCH 6/7] fix the watch progress --- .../com/github/libretube/db/DatabaseHelper.kt | 6 +++--- .../extensions/SetWatchProgressLength.kt | 2 +- .../com/github/libretube/util/PlayingQueue.kt | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index 919177c4b..e4dcb9780 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -56,9 +56,9 @@ object DatabaseHelper { fun removeWatchPosition(videoId: String) { Thread { - Database.watchPositionDao().delete( - Database.watchPositionDao().findById(videoId) - ) + Database.watchPositionDao().findById(videoId)?.let { + Database.watchPositionDao().delete(it) + } }.start() } diff --git a/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt b/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt index 0bd7d4f54..6eb979353 100644 --- a/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt +++ b/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt @@ -14,7 +14,7 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) { Thread { try { - progress = Database.watchPositionDao().findById(videoId).position + progress = Database.watchPositionDao().findById(videoId)?.position } catch (e: Exception) { progress = null } 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 ecc5d6e9d..7889d8f9f 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -1,8 +1,5 @@ package com.github.libretube.util -import android.util.Log -import com.github.libretube.extensions.TAG - object PlayingQueue { private val queue = mutableListOf() private var currentVideoId: String? = null @@ -12,20 +9,23 @@ object PlayingQueue { } fun add(videoId: String) { + if (currentVideoId == videoId) return + if (queue.contains(videoId)) queue.remove(videoId) queue.add(videoId) } - fun playNext(nextVideoId: String) { + fun playNext(videoId: String) { + if (currentVideoId == videoId) return + if (queue.contains(videoId)) queue.remove(videoId) queue.add( - queue.indexOf(currentVideoId), - nextVideoId + queue.indexOf(currentVideoId) + 1, + videoId ) } fun getNext(): String? { val currentIndex = queue.indexOf(currentVideoId) - Log.e(TAG(), queue.toString()) - return if (currentIndex > queue.size) { + return if (currentIndex >= queue.size) { null } else { queue[currentIndex + 1] @@ -40,7 +40,7 @@ object PlayingQueue { fun hasPrev(): Boolean { val currentIndex = queue.indexOf(currentVideoId) - return currentIndex >= 1 + return queue.size > currentIndex + 1 } fun contains(videoId: String): Boolean { From 658a6648ff044f54fc80cb0cce13842895b64acf Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Sep 2022 22:21:30 +0200 Subject: [PATCH 7/7] fix prev --- .../com/github/libretube/fragments/PlayerFragment.kt | 6 +++--- .../libretube/sheets/VideoOptionsBottomSheet.kt | 3 ++- .../java/com/github/libretube/util/PlayingQueue.kt | 12 +++++------- 3 files changed, 10 insertions(+), 11 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 41534f0d5..858bca57d 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -757,8 +757,9 @@ class PlayerFragment : BaseFragment() { } private fun playVideo() { - PlayingQueue.updateCurrent(videoId!!) lifecycleScope.launchWhenCreated { + PlayingQueue.updateCurrent(videoId!!) + streams = try { RetrofitInstance.api.getStreams(videoId!!) } catch (e: IOException) { @@ -866,8 +867,7 @@ class PlayerFragment : BaseFragment() { var position: Long? = null Thread { try { - val watchPosition = Database.watchPositionDao().findById(videoId!!) - position = if (watchPosition != null) watchPosition.position else null + position = Database.watchPositionDao().findById(videoId!!)?.position // position is almost the end of the video => don't seek, start from beginning if (position!! > streams.duration!! * 1000 * 0.9) position = null } catch (e: Exception) { 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 b988c9f71..ba4fa68af 100644 --- a/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/sheets/VideoOptionsBottomSheet.kt @@ -37,8 +37,9 @@ class VideoOptionsBottomSheet( ) } + /** - * Check whether the player is running by observing the notification + * Check whether the player is running and add queue options */ if (PlayingQueue.isNotEmpty()) { optionsList += context?.getString(R.string.play_next)!! 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 7889d8f9f..651ff963c 100644 --- a/app/src/main/java/com/github/libretube/util/PlayingQueue.kt +++ b/app/src/main/java/com/github/libretube/util/PlayingQueue.kt @@ -32,15 +32,13 @@ object PlayingQueue { } } - fun getPrev(): String { - return queue[ - queue.indexOf(currentVideoId) - 1 - ] + fun getPrev(): String? { + val index = queue.indexOf(currentVideoId) + return if (index > 0) queue[index - 1] else null } fun hasPrev(): Boolean { - val currentIndex = queue.indexOf(currentVideoId) - return queue.size > currentIndex + 1 + return queue.indexOf(currentVideoId) > 0 } fun contains(videoId: String): Boolean { @@ -53,7 +51,7 @@ object PlayingQueue { fun updateCurrent(videoId: String) { currentVideoId = videoId - if (!contains(videoId)) add(videoId) + queue.add(videoId) } fun isNotEmpty() = queue.isNotEmpty()