add option for auto fullscreen

This commit is contained in:
Bnyro 2022-07-14 15:57:20 +02:00
parent 3c432aa49f
commit b5c4ec9e92
6 changed files with 92 additions and 41 deletions

View File

@ -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

View File

@ -141,6 +141,8 @@ class PlayerFragment : Fragment() {
private lateinit var chapters: List<ChapterSegment>
private val sponsorBlockPrefs = SponsorBlockPrefs()
private var autoRotationEnabled = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
@ -164,9 +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
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
onConfigurationChanged(resources.configuration)
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)
@ -343,20 +370,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
}
@ -375,8 +405,11 @@ class PlayerFragment : Fragment() {
scaleControls(1F)
val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
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
}
@ -499,10 +532,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
@ -1428,14 +1457,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)
}
unsetFullscreen()
Globals.isFullScreen = false
} else {
@ -1464,19 +1486,16 @@ class PlayerFragment : Fragment() {
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val orientation = newConfig.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// go to fullscreen mode
setFullscreen()
} else {
// leave fullscreen mode
val rotationPref = PreferenceHelper.getString(
requireContext(),
"fullscreen_orientation",
"ratio"
)
if (rotationPref!! != "portrait") unsetFullscreen()
if (autoRotationEnabled) {
val orientation = newConfig.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// go to fullscreen mode
setFullscreen()
} else {
// exit fullscreen if not landscape
unsetFullscreen()
}
}
}
}

View File

@ -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<ListPreference>("fullscreen_orientation")
val autoRotateToFullscreen = findPreference<SwitchPreferenceCompat>("auto_fullscreen")
// only show the player orientation option if auto fullscreen is disabled
playerOrientation?.isEnabled != PreferenceHelper.getBoolean(
requireContext(),
"auto_fullscreen",
true
)
autoRotateToFullscreen?.setOnPreferenceChangeListener { _, newValue ->
playerOrientation?.isEnabled = newValue != true
true
}
}
}

View File

@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="48"
android:viewportWidth="48" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="m24.1,38 l5.7,-5.65 -5.7,-5.65 -2.1,2.1 2.15,2.15q-1.4,0.05 -2.725,-0.45 -1.325,-0.5 -2.375,-1.55 -1,-1 -1.525,-2.3 -0.525,-1.3 -0.525,-2.6 0,-0.85 0.225,-1.7t0.625,-1.65l-2.2,-2.2q-0.85,1.25 -1.25,2.65T14,24q0,1.9 0.75,3.75t2.2,3.3q1.45,1.45 3.25,2.175 1.8,0.725 3.7,0.775L22,35.9ZM32.35,29.5q0.85,-1.25 1.25,-2.65T34,24q0,-1.9 -0.725,-3.775T31.1,16.9q-1.45,-1.45 -3.275,-2.15t-3.725,-0.7L26,12.1 23.9,10l-5.7,5.65 5.7,5.65 2.1,-2.1 -2.2,-2.2q1.35,0 2.75,0.525t2.4,1.525q1,1 1.525,2.3 0.525,1.3 0.525,2.6 0,0.85 -0.225,1.7t-0.625,1.65ZM24,44q-4.1,0 -7.75,-1.575 -3.65,-1.575 -6.375,-4.3 -2.725,-2.725 -4.3,-6.375Q4,28.1 4,24q0,-4.15 1.575,-7.8 1.575,-3.65 4.3,-6.35 2.725,-2.7 6.375,-4.275Q19.9,4 24,4q4.15,0 7.8,1.575 3.65,1.575 6.35,4.275 2.7,2.7 4.275,6.35Q44,19.85 44,24q0,4.1 -1.575,7.75 -1.575,3.65 -4.275,6.375t-6.35,4.3Q28.15,44 24,44Z"/>
</vector>

View File

@ -237,4 +237,6 @@
<string name="always">Always</string>
<string name="selected">Selected</string>
<string name="never">Never</string>
<string name="autoRotatePlayer">Auto fullscreen</string>
<string name="autoRotatePlayer_summary">Automatically switch to player fullscreen when the device gets turned.</string>
</resources>

View File

@ -60,16 +60,26 @@
<SwitchPreferenceCompat
android:icon="@drawable/ic_play_filled"
android:summary="@string/autoplay_summary"
app:defaultValue="true"
app:key="autoplay"
app:title="@string/player_autoplay" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_pause_filled"
android:summary="@string/pauseOnScreenOff_summary"
app:defaultValue="false"
app:key="pause_screen_off"
app:title="@string/pauseOnScreenOff" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_rotating_circle"
android:summary="@string/autoRotatePlayer_summary"
app:defaultValue="true"
app:key="auto_fullscreen"
app:title="@string/autoRotatePlayer" />
<ListPreference
android:enabled="false"
android:icon="@drawable/ic_flip"
app:defaultValue="ratio"
app:entries="@array/fullscreenOrientation"