From 0a53decb1c5348f498c075b6101ceb82666612a3 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 11 Nov 2022 17:43:05 +0100 Subject: [PATCH] fix auto fullscreen --- .../libretube/ui/activities/MainActivity.kt | 17 +++++--- .../libretube/ui/fragments/PlayerFragment.kt | 41 +++++++++---------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 6d772594f..65ddfd7a3 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -59,11 +59,7 @@ class MainActivity : BaseActivity() { autoRotationEnabled = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_ROTATION, false) // enable auto rotation if turned on - requestedOrientation = if (autoRotationEnabled) { - ActivityInfo.SCREEN_ORIENTATION_USER - } else { - ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - } + requestOrientationChange() // start service that gets called on closure try { @@ -160,6 +156,17 @@ class MainActivity : BaseActivity() { loadIntentData() } + /** + * Rotate according to the preference + */ + fun requestOrientationChange() { + requestedOrientation = if (autoRotationEnabled) { + ActivityInfo.SCREEN_ORIENTATION_USER + } else { + ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + } + } + /** * Show a break reminder when watched too long */ 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 2e6061698..748ad2608 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 @@ -154,7 +154,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { /** * user preferences */ - private var token = PreferenceHelper.getToken() + private val token = PreferenceHelper.getToken() private var videoShownInExternalPlayer = false /** @@ -186,7 +186,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { return binding.root } - @SuppressLint("SourceLockedOrientationActivity") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) context?.hideKeyboard(view) @@ -194,17 +193,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { // clear the playing queue PlayingQueue.clear() - setUserPrefs() - - val mainActivity = activity as MainActivity - if (PlayerHelper.autoRotationEnabled) { - // enable auto rotation - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR - onConfigurationChanged(resources.configuration) - } else { - // go to portrait mode - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - } + changeOrientationMode() createExoPlayer() initializeTransitionLayout() @@ -224,14 +213,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { Handler(Looper.getMainLooper()).postDelayed(this::showBottomBar, 100) } - private fun setUserPrefs() { - token = PreferenceHelper.getToken() - - // save whether auto rotation is enabled - - // save whether related streams are enabled - } - @SuppressLint("ClickableViewAccessibility") private fun initializeTransitionLayout() { val mainActivity = activity as MainActivity @@ -268,10 +249,12 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { viewModel.isMiniPlayerVisible.value = true exoPlayerView.useController = false mainMotionLayout.progress = 1F + (activity as MainActivity).requestOrientationChange() } else if (currentId == sId) { viewModel.isMiniPlayerVisible.value = false exoPlayerView.useController = true mainMotionLayout.progress = 0F + changeOrientationMode() } } @@ -1303,6 +1286,22 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } } + /** + * Use the sensor mode if auto fullscreen is enabled + */ + @SuppressLint("SourceLockedOrientationActivity") + private fun changeOrientationMode() { + val mainActivity = activity as MainActivity + if (PlayerHelper.autoRotationEnabled) { + // enable auto rotation + mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR + onConfigurationChanged(resources.configuration) + } else { + // go to portrait mode + mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + } + } + override fun onCaptionsClicked() { if (!this@PlayerFragment::streams.isInitialized || streams.subtitles == null ||