fix: orientation change issues in portrait mode

This commit is contained in:
Bnyro 2023-10-23 17:53:17 +02:00
parent 54dfb2d8e9
commit ae40d08dec

View File

@ -20,6 +20,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams import android.view.ViewGroup.LayoutParams
import android.widget.Toast import android.widget.Toast
import android.window.OnBackInvokedDispatcher
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.TransitionAdapter import androidx.constraintlayout.motion.widget.TransitionAdapter
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
@ -192,6 +193,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private var scrubbingTimeBar = false private var scrubbingTimeBar = false
private var chaptersBottomSheet: ChaptersBottomSheet? = null private var chaptersBottomSheet: ChaptersBottomSheet? = null
/**
* 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.
*/
private var playerLayoutOrientation = Int.MIN_VALUE
private val fullscreenDialog by lazy { private val fullscreenDialog by lazy {
object: Dialog(requireContext(), android.R.style.Theme_Black_NoTitleBar_Fullscreen) { object: Dialog(requireContext(), android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
override fun onBackPressed() { override fun onBackPressed() {
@ -249,6 +256,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
keepQueue = playerData.keepQueue keepQueue = playerData.keepQueue
timeStamp = playerData.timestamp timeStamp = playerData.timestamp
playerLayoutOrientation = resources.configuration.orientation
// broadcast receiver for PiP actions // broadcast receiver for PiP actions
context?.registerReceiver( context?.registerReceiver(
broadcastReceiver, broadcastReceiver,
@ -518,6 +527,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// set status bar icon color to white // set status bar icon color to white
windowInsetsControllerCompat.isAppearanceLightStatusBars = false windowInsetsControllerCompat.isAppearanceLightStatusBars = false
viewModel.isFullscreen.value = true
if (mainActivity.screenOrientationPref == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) { if (mainActivity.screenOrientationPref == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) {
val height = streams.videoStreams.firstOrNull()?.height ?: exoPlayer.videoSize.height val height = streams.videoStreams.firstOrNull()?.height ?: exoPlayer.videoSize.height
val width = streams.videoStreams.firstOrNull()?.width ?: exoPlayer.videoSize.width val width = streams.videoStreams.firstOrNull()?.width ?: exoPlayer.videoSize.width
@ -529,8 +540,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit) playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit)
playerBinding.exoTitle.isVisible = true playerBinding.exoTitle.isVisible = true
viewModel.isFullscreen.value = true
updateResolutionOnFullscreenChange(true) updateResolutionOnFullscreenChange(true)
openOrCloseFullscreenDialog(true) openOrCloseFullscreenDialog(true)
@ -546,6 +555,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
else -> true else -> true
} }
viewModel.isFullscreen.value = false
if (mainActivity.screenOrientationPref == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) { if (mainActivity.screenOrientationPref == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) {
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} }
@ -553,10 +564,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen)
playerBinding.exoTitle.isInvisible = true playerBinding.exoTitle.isInvisible = true
viewModel.isFullscreen.value = false
updateResolutionOnFullscreenChange(false) updateResolutionOnFullscreenChange(false)
openOrCloseFullscreenDialog(false) openOrCloseFullscreenDialog(false)
checkForNecessaryOrientationRestart()
} }
private fun openOrCloseFullscreenDialog(open: Boolean) { private fun openOrCloseFullscreenDialog(open: Boolean) {
@ -1578,6 +1590,24 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
onDestroy() onDestroy()
} }
/**
* Check if the activity needs to be recreated due to an orientation change
* If true, the activity will be automatically restarted
*/
private fun checkForNecessaryOrientationRestart() {
val lockedOrientations = listOf(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
if (mainActivity.screenOrientationPref in lockedOrientations) return
val orientation = resources.configuration.orientation
if (viewModel.isFullscreen.value != true && orientation != playerLayoutOrientation) {
if (this::exoPlayer.isInitialized) {
arguments?.putLong(IntentData.timeStamp, exoPlayer.currentPosition / 1000)
}
playerLayoutOrientation = orientation
activity?.recreate()
}
}
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
@ -1595,11 +1625,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// exit fullscreen if not landscape // exit fullscreen if not landscape
else -> unsetFullscreen() else -> unsetFullscreen()
} }
} else if (viewModel.isFullscreen.value != true) { } else {
if (this::exoPlayer.isInitialized) { checkForNecessaryOrientationRestart()
arguments?.putLong(IntentData.timeStamp, exoPlayer.currentPosition / 1000)
}
activity?.recreate()
} }
} }
} }