From 333e6b152f0d284ea72d1e8d51fb9033a492a474 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Sep 2022 16:59:51 +0200 Subject: [PATCH] save the watch position when pausing --- .../com/github/libretube/services/BackgroundMode.kt | 11 +++++++++++ .../github/libretube/ui/fragments/PlayerFragment.kt | 11 +++++++++++ 2 files changed, 22 insertions(+) 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 b3a4f0ae1..2c715e3a9 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -5,6 +5,7 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.app.Service import android.content.Intent +import android.media.session.PlaybackState import android.os.Build import android.os.Handler import android.os.IBinder @@ -20,6 +21,8 @@ import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.IntentData import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.db.DatabaseHelper +import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.util.AutoPlayHelper import com.github.libretube.util.NowPlayingNotification @@ -221,6 +224,14 @@ class BackgroundMode : Service() { Player.STATE_IDLE -> { onDestroy() } + PlaybackState.STATE_PAUSED -> { + query { + DatabaseHelper.saveWatchPosition( + videoId, + player?.currentPosition ?: 0L + ) + } + } Player.STATE_BUFFERING -> {} Player.STATE_READY -> {} } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 7e60fe5bf..493c8c505 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -51,6 +51,7 @@ import com.github.libretube.extensions.TAG import com.github.libretube.extensions.await import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.hideKeyboard +import com.github.libretube.extensions.query import com.github.libretube.extensions.toID import com.github.libretube.interfaces.PlayerOptionsInterface import com.github.libretube.models.PlayerViewModel @@ -1012,6 +1013,16 @@ class PlayerFragment : BaseFragment() { binding.playImageView.setImageResource(R.drawable.ic_play) } + // save the watch position when paused + if (playbackState == PlaybackState.STATE_PAUSED) { + query { + DatabaseHelper.saveWatchPosition( + videoId!!, + exoPlayer.currentPosition + ) + } + } + // listen for the stop button in the notification if (playbackState == PlaybackState.STATE_STOPPED) { // finish PiP by finishing the activity