fix: ensure correct persistent notification lifecycle (#5388)

Co-authored-by: Bnyro <bnyro@tutanota.com>
This commit is contained in:
RafaRamos 2023-12-30 15:11:30 +00:00 committed by GitHub
parent 37de6a7374
commit 65f113c445
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 18 deletions

View File

@ -189,6 +189,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private var scrubbingTimeBar = false private var scrubbingTimeBar = false
private var chaptersBottomSheet: ChaptersBottomSheet? = null private var chaptersBottomSheet: ChaptersBottomSheet? = null
// True when the video was closed through the close button on PiP mode
private var closedVideo = false
/** /**
* The orientation of the `fragment_player.xml` that's currently used * The orientation of the `fragment_player.xml` that's currently used
* This is needed in order to figure out if the current layout is the landscape one or not. * This is needed in order to figure out if the current layout is the landscape one or not.
@ -431,8 +434,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (_binding == null) return if (_binding == null) return
mainMotionLayout.progress = abs(progress) mainMotionLayout.progress = abs(progress)
binding.player.hideController() disableController()
binding.player.useController = false
commentsViewModel.setCommentSheetExpand(false) commentsViewModel.setCommentSheetExpand(false)
chaptersBottomSheet?.dismiss() chaptersBottomSheet?.dismiss()
transitionEndId = endId transitionEndId = endId
@ -446,7 +448,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
viewModel.isMiniPlayerVisible.value = false viewModel.isMiniPlayerVisible.value = false
// re-enable captions // re-enable captions
updateCurrentSubtitle(currentSubtitle) updateCurrentSubtitle(currentSubtitle)
binding.player.useController = true enableController()
commentsViewModel.setCommentSheetExpand(true) commentsViewModel.setCommentSheetExpand(true)
mainMotionLayout.progress = 0F mainMotionLayout.progress = 0F
changeOrientationMode() changeOrientationMode()
@ -454,7 +456,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
viewModel.isMiniPlayerVisible.value = true viewModel.isMiniPlayerVisible.value = true
// disable captions temporarily // disable captions temporarily
updateCurrentSubtitle(null) updateCurrentSubtitle(null)
binding.player.useController = false disableController()
commentsViewModel.setCommentSheetExpand(null) commentsViewModel.setCommentSheetExpand(null)
binding.sbSkipBtn.isGone = true binding.sbSkipBtn.isGone = true
mainMotionLayout.progress = 1F mainMotionLayout.progress = 1F
@ -466,7 +468,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.playerMotionLayout binding.playerMotionLayout
.addSwipeUpListener { .addSwipeUpListener {
if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) { if (this::streams.isInitialized && PlayerHelper.fullscreenGesturesEnabled) {
binding.player.hideController() disableController()
setFullscreen() setFullscreen()
} }
} }
@ -534,7 +536,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// hide fullscreen button if autorotation enabled // hide fullscreen button if autorotation enabled
playerBinding.fullscreen.setOnClickListener { playerBinding.fullscreen.setOnClickListener {
// hide player controller // hide player controller
binding.player.hideController() disableController()
if (viewModel.isFullscreen.value == false) { if (viewModel.isFullscreen.value == false) {
// go to fullscreen mode // go to fullscreen mode
setFullscreen() setFullscreen()
@ -774,6 +776,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (closedVideo) {
closedVideo = false
nowPlayingNotification.refreshNotification()
}
// re-enable and load video stream // re-enable and load video stream
if (this::trackSelector.isInitialized) { if (this::trackSelector.isInitialized) {
trackSelector.updateParameters { trackSelector.updateParameters {
@ -804,6 +811,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
context?.unregisterReceiver(broadcastReceiver) context?.unregisterReceiver(broadcastReceiver)
} }
_binding = null
stopVideoPlay()
}
private fun stopVideoPlay() {
try { try {
saveWatchPosition() saveWatchPosition()
@ -813,8 +826,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
_binding = null
} }
// save the watch position if video isn't finished and option enabled // save the watch position if video isn't finished and option enabled
@ -1066,8 +1077,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private fun showAutoPlayCountdown() { private fun showAutoPlayCountdown() {
if (!PlayingQueue.hasNext()) return if (!PlayingQueue.hasNext()) return
binding.player.useController = false disableController()
binding.player.hideController()
binding.autoplayCountdown.setHideSelfListener { binding.autoplayCountdown.setHideSelfListener {
// could fail if the video already got closed before // could fail if the video already got closed before
runCatching { runCatching {
@ -1525,19 +1535,21 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
super.onPictureInPictureModeChanged(isInPictureInPictureMode) super.onPictureInPictureModeChanged(isInPictureInPictureMode)
if (isInPictureInPictureMode) { if (isInPictureInPictureMode) {
// hide and disable exoPlayer controls // hide and disable exoPlayer controls
binding.player.hideController() disableController()
binding.player.useController = false
updateCurrentSubtitle(null) updateCurrentSubtitle(null)
openOrCloseFullscreenDialog(true) openOrCloseFullscreenDialog(true)
} else { } else {
enableController()
// close button got clicked in PiP mode // close button got clicked in PiP mode
// pause the video and keep the app alive // pause the video and keep the app alive
if (lifecycle.currentState == Lifecycle.State.CREATED) exoPlayer.pause() if (lifecycle.currentState == Lifecycle.State.CREATED) {
exoPlayer.pause()
// enable exoPlayer controls again nowPlayingNotification.cancelNotification()
binding.player.useController = true closedVideo = true
}
updateCurrentSubtitle(currentSubtitle) updateCurrentSubtitle(currentSubtitle)
@ -1605,6 +1617,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
private fun killPlayerFragment() { private fun killPlayerFragment() {
stopVideoPlay()
viewModel.isFullscreen.value = false viewModel.isFullscreen.value = false
viewModel.isMiniPlayerVisible.value = false viewModel.isMiniPlayerVisible.value = false
@ -1617,8 +1630,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
mainActivity.supportFragmentManager.commit { mainActivity.supportFragmentManager.commit {
remove(this@PlayerFragment) remove(this@PlayerFragment)
} }
onDestroy()
} }
/** /**
@ -1664,4 +1675,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
checkForNecessaryOrientationRestart() checkForNecessaryOrientationRestart()
} }
} }
private fun enableController() {
binding.player.useController = true
}
private fun disableController() {
binding.player.useController = false
binding.player.hideController()
}
} }

View File

@ -400,6 +400,14 @@ class NowPlayingNotification(
nManager.cancel(PLAYER_NOTIFICATION_ID) nManager.cancel(PLAYER_NOTIFICATION_ID)
} }
fun cancelNotification() {
nManager.cancel(PLAYER_NOTIFICATION_ID)
}
fun refreshNotification() {
createOrUpdateNotification()
}
companion object { companion object {
const val PLAYER_NOTIFICATION_ID = 1 const val PLAYER_NOTIFICATION_ID = 1
private const val PREV = "prev" private const val PREV = "prev"