diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dbc40491c..8ed717cba 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -37,7 +37,7 @@ android:exported="true" android:hardwareAccelerated="true" android:launchMode="singleTop" - android:screenOrientation="userPortrait" + android:screenOrientation="user" android:supportsPictureInPicture="true"> diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 1156f1efe..dc79c69b5 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -3,7 +3,6 @@ package com.github.libretube.activities import android.app.Activity import android.content.Context import android.content.Intent -import android.content.pm.ActivityInfo import android.content.res.Configuration import android.net.Uri import android.os.Build @@ -84,8 +83,6 @@ class MainActivity : AppCompatActivity() { binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - navController = findNavController(R.id.fragment) binding.bottomNav.setupWithNavController(navController) @@ -290,7 +287,6 @@ class MainActivity : AppCompatActivity() { // set the animation duration motionLayout.setTransitionDuration(250) motionLayout.transitionToEnd() - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT with(motionLayout) { getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) enableTransition(R.id.yt_transition, true) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index edcdda4c3..bde390d9b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -6,6 +6,7 @@ import android.app.PictureInPictureParams import android.content.Context import android.content.Intent import android.content.pm.ActivityInfo +import android.content.res.Configuration import android.graphics.Color import android.graphics.Rect import android.net.Uri @@ -140,6 +141,8 @@ class PlayerFragment : Fragment() { private lateinit var chapters: List private val sponsorBlockPrefs = SponsorBlockPrefs() + private var autoRotationEnabled = true + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { @@ -163,6 +166,34 @@ class PlayerFragment : Fragment() { super.onViewCreated(view, savedInstanceState) hideKeyboard() + // save whether auto rotation is enabled + autoRotationEnabled = PreferenceHelper.getBoolean( + requireContext(), + "auto_fullscreen", + true + ) + val mainActivity = activity as MainActivity + if (autoRotationEnabled) { + // enable auto rotation + mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER + onConfigurationChanged(resources.configuration) + } else { + // go to portrait mode + mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + } + + // save whether related streams and autoplay are enabled + autoplay = PreferenceHelper.getBoolean( + requireContext(), + "autoplay", + false + ) + relatedStreamsEnabled = PreferenceHelper.getBoolean( + requireContext(), + "related_streams_toggle", + true + ) + setSponsorBlockPrefs() createExoPlayer(view) initializeTransitionLayout(view) @@ -232,7 +263,6 @@ class PlayerFragment : Fragment() { Globals.isMiniPlayerVisible = false binding.playerMotionLayout.transitionToEnd() val mainActivity = activity as MainActivity - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.supportFragmentManager.beginTransaction() .remove(this) .commit() @@ -241,7 +271,6 @@ class PlayerFragment : Fragment() { Globals.isMiniPlayerVisible = false binding.playerMotionLayout.transitionToEnd() val mainActivity = activity as MainActivity - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.supportFragmentManager.beginTransaction() .remove(this) .commit() @@ -268,6 +297,8 @@ class PlayerFragment : Fragment() { } // FullScreen button trigger + // hide fullscreen button if auto rotation enabled + playerBinding.fullscreen.visibility = if (autoRotationEnabled) View.GONE else View.VISIBLE playerBinding.fullscreen.setOnClickListener { // hide player controller exoPlayerView.hideController() @@ -341,20 +372,23 @@ class PlayerFragment : Fragment() { scaleControls(1.3F) - val orientation = when (fullscreenOrientationPref) { - "ratio" -> { - val videoSize = exoPlayer.videoSize - // probably a youtube shorts video - if (videoSize.height > videoSize.width) ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - // a video with normal aspect ratio - else ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE + if (!autoRotationEnabled) { + // different orientations of the video are only available when auto rotation is disabled + val orientation = when (fullscreenOrientationPref) { + "ratio" -> { + val videoSize = exoPlayer.videoSize + // probably a youtube shorts video + if (videoSize.height > videoSize.width) ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + // a video with normal aspect ratio + else ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE + } + "auto" -> ActivityInfo.SCREEN_ORIENTATION_USER + "landscape" -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE + "portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + else -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE } - "auto" -> ActivityInfo.SCREEN_ORIENTATION_USER - "landscape" -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE - "portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - else -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE + mainActivity.requestedOrientation = orientation } - mainActivity.requestedOrientation = orientation Globals.isFullScreen = true } @@ -373,8 +407,11 @@ class PlayerFragment : Fragment() { scaleControls(1F) - val mainActivity = activity as MainActivity - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + if (!autoRotationEnabled) { + // switch back to portrait mode if auto rotation disabled + val mainActivity = activity as MainActivity + mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + } Globals.isFullScreen = false } @@ -497,10 +534,6 @@ class PlayerFragment : Fragment() { uploader = response.uploader!! thumbnailUrl = response.thumbnailUrl!! - // save whether related streams and autoplay are enabled - autoplay = PreferenceHelper.getBoolean(requireContext(), "autoplay", false) - relatedStreamsEnabled = - PreferenceHelper.getBoolean(requireContext(), "related_streams_toggle", true) // save related streams for autoplay relatedStreams = response.relatedStreams @@ -1426,17 +1459,7 @@ class PlayerFragment : Fragment() { exoPlayerView.hideController() exoPlayerView.useController = false - // hide anything but the player - binding.linLayout.visibility = View.GONE - binding.mainContainer.isClickable = true - - with(binding.playerMotionLayout) { - getConstraintSet(R.id.start).constrainHeight(R.id.player, -1) - enableTransition(R.id.yt_transition, false) - } - - val mainActivity = activity as MainActivity - mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + unsetFullscreen() Globals.isFullScreen = false } else { @@ -1462,4 +1485,19 @@ class PlayerFragment : Fragment() { private fun updatePipParams() = PictureInPictureParams.Builder() .setActions(emptyList()) .build() + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + + if (autoRotationEnabled) { + val orientation = newConfig.orientation + if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + // go to fullscreen mode + setFullscreen() + } else { + // exit fullscreen if not landscape + unsetFullscreen() + } + } + } } diff --git a/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt b/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt index f2fdc3fca..b3428f4f7 100644 --- a/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/PlayerSettings.kt @@ -1,7 +1,9 @@ package com.github.libretube.preferences import android.os.Bundle +import androidx.preference.ListPreference import androidx.preference.PreferenceFragmentCompat +import androidx.preference.SwitchPreferenceCompat import com.github.libretube.R import com.github.libretube.activities.SettingsActivity @@ -13,5 +15,20 @@ class PlayerSettings : PreferenceFragmentCompat() { val settingsActivity = activity as SettingsActivity settingsActivity.changeTopBarText(getString(R.string.audio_video)) + + val playerOrientation = findPreference("fullscreen_orientation") + val autoRotateToFullscreen = findPreference("auto_fullscreen") + + // only show the player orientation option if auto fullscreen is disabled + playerOrientation?.isEnabled != PreferenceHelper.getBoolean( + requireContext(), + "auto_fullscreen", + false + ) + + autoRotateToFullscreen?.setOnPreferenceChangeListener { _, newValue -> + playerOrientation?.isEnabled = newValue != true + true + } } } diff --git a/app/src/main/res/drawable/ic_rotating_circle.xml b/app/src/main/res/drawable/ic_rotating_circle.xml new file mode 100644 index 000000000..7339373fe --- /dev/null +++ b/app/src/main/res/drawable/ic_rotating_circle.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 141527a8f..ac64401d1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -237,4 +237,6 @@ Always Selected Never + Auto fullscreen + Automatically switch to player fullscreen when the device gets turned. \ No newline at end of file diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index dc0efc7b1..10563cdc6 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -60,15 +60,24 @@ + +