From 22539faab54d991a614f1a205ff6565aca66ff77 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 1 Jun 2023 18:16:17 +0200 Subject: [PATCH] Fix crashes when audio and video mode running simultaneously --- .../libretube/helpers/BackgroundHelper.kt | 17 ++++++++--------- .../github/libretube/helpers/ContextHelper.kt | 15 +++++++++++++++ .../libretube/helpers/NavigationHelper.kt | 18 +++++------------- 3 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/helpers/ContextHelper.kt diff --git a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt index 9d261cabf..9db55b99e 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt @@ -31,20 +31,19 @@ object BackgroundHelper { ) { // close the previous video player if open if (!keepVideoPlayerAlive) { - (context as? MainActivity)?.supportFragmentManager?.let { fragmentManager -> - fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let { - fragmentManager.commit { remove(it) } - } + val fragmentManager = ContextHelper.unwrapActivity(context).supportFragmentManager + fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let { + fragmentManager.commit { remove(it) } } } // create an intent for the background mode service val intent = Intent(context, OnlinePlayerService::class.java) - intent.putExtra(IntentData.videoId, videoId) - intent.putExtra(IntentData.playlistId, playlistId) - intent.putExtra(IntentData.channelId, channelId) - intent.putExtra(IntentData.position, position) - intent.putExtra(IntentData.keepQueue, keepQueue) + .putExtra(IntentData.videoId, videoId) + .putExtra(IntentData.playlistId, playlistId) + .putExtra(IntentData.channelId, channelId) + .putExtra(IntentData.position, position) + .putExtra(IntentData.keepQueue, keepQueue) // start the background mode as foreground service ContextCompat.startForegroundService(context, intent) diff --git a/app/src/main/java/com/github/libretube/helpers/ContextHelper.kt b/app/src/main/java/com/github/libretube/helpers/ContextHelper.kt new file mode 100644 index 000000000..94989b136 --- /dev/null +++ b/app/src/main/java/com/github/libretube/helpers/ContextHelper.kt @@ -0,0 +1,15 @@ +package com.github.libretube.helpers + +import android.content.Context +import android.content.ContextWrapper +import com.github.libretube.ui.activities.MainActivity + +object ContextHelper { + fun unwrapActivity(context: Context): MainActivity { + var correctContext: Context? = context + while (correctContext !is MainActivity && correctContext is ContextWrapper) { + correctContext = correctContext.baseContext + } + return correctContext as MainActivity + } +} diff --git a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt index a623bf5cd..2f314dc7b 100644 --- a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt @@ -31,7 +31,7 @@ object NavigationHelper { ) { if (channelId == null) return - val activity = unwrapActivity(context) + val activity = ContextHelper.unwrapActivity(context) val bundle = bundleOf(IntentData.channelId to channelId) activity.navController.navigate(R.id.channelFragment, bundle) try { @@ -45,14 +45,6 @@ object NavigationHelper { } } - private fun unwrapActivity(context: Context): MainActivity { - var correctContext: Context? = context - while (correctContext !is MainActivity && correctContext is ContextWrapper) { - correctContext = correctContext.baseContext - } - return correctContext as MainActivity - } - /** * Navigate to the given video using the other provided parameters as well * If the audio only mode is enabled, play it in the background, else as a normal video @@ -67,9 +59,9 @@ object NavigationHelper { forceVideo: Boolean = false, ) { if (videoId == null) return + BackgroundHelper.stopBackgroundPlay(context) if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) { - BackgroundHelper.stopBackgroundPlay(context) BackgroundHelper.playOnBackground( context, videoId.toID(), @@ -92,7 +84,7 @@ object NavigationHelper { IntentData.timeStamp to timeStamp, ) - val activity = unwrapActivity(context) + val activity = ContextHelper.unwrapActivity(context) activity.supportFragmentManager.commitNow { replace(R.id.container, args = bundle) } @@ -105,7 +97,7 @@ object NavigationHelper { ) { if (playlistId == null) return - val activity = unwrapActivity(context) + val activity = ContextHelper.unwrapActivity(context) val bundle = bundleOf( IntentData.playlistId to playlistId, IntentData.playlistType to playlistType, @@ -117,7 +109,7 @@ object NavigationHelper { * Start the audio player fragment */ fun startAudioPlayer(context: Context) { - val activity = unwrapActivity(context) + val activity = ContextHelper.unwrapActivity(context) activity.supportFragmentManager.commitNow { replace(R.id.container) }