From 1e22fab499bdca5d0432ee49b6afb9735bb82d09 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 6 Aug 2023 12:39:52 +0200 Subject: [PATCH] feat: preference to force landscape as orientation --- .../libretube/constants/PreferenceKeys.kt | 2 +- .../github/libretube/helpers/PlayerHelper.kt | 2 +- .../libretube/ui/activities/MainActivity.kt | 24 +---------------- .../github/libretube/ui/base/BaseActivity.kt | 26 +++++++++++++++++++ .../libretube/ui/fragments/PlayerFragment.kt | 17 +++++------- .../ui/preferences/GeneralSettings.kt | 2 +- app/src/main/res/values-television/config.xml | 2 +- app/src/main/res/values/array.xml | 12 +++++++++ app/src/main/res/values/config.xml | 2 +- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/general_settings.xml | 11 +++++--- 11 files changed, 58 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 4d4e19582..70dbb1d48 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -17,7 +17,7 @@ object PreferenceKeys { */ const val LANGUAGE = "language" const val REGION = "region" - const val AUTO_ROTATION = "auto_rotation" + const val ORIENTATION = "orientation" const val SLEEP_TIMER = "sleep_timer_toggle" const val SLEEP_TIMER_DELAY = "sleep_timer_delay" const val SAVE_FEED = "save_feed" diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index da5d52bfa..4ca477fa3 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -114,7 +114,7 @@ object PlayerHelper { } } - val autoRotationEnabled: Boolean + val autoFullscreenEnabled: Boolean get() = PreferenceHelper.getBoolean( PreferenceKeys.AUTO_FULLSCREEN, false 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 269244159..14c579a2f 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 @@ -60,13 +60,6 @@ class MainActivity : BaseActivity() { private var savedSearchQuery: String? = null - val autoRotationEnabled by lazy { - PreferenceHelper.getBoolean( - PreferenceKeys.AUTO_ROTATION, - resources.getBoolean(R.bool.config_default_auto_rotation_pref) - ) - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -230,17 +223,6 @@ class MainActivity : BaseActivity() { } } - /** - * Rotate according to the preference - */ - fun requestOrientationChange() { - requestedOrientation = if (autoRotationEnabled) { - ActivityInfo.SCREEN_ORIENTATION_USER - } else { - ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - } - } - /** * Initialize the notification badge showing the amount of new videos */ @@ -501,11 +483,7 @@ class MainActivity : BaseActivity() { } playerViewModel.isFullscreen.value = false - requestedOrientation = if (autoRotationEnabled) { - ActivityInfo.SCREEN_ORIENTATION_USER - } else { - ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - } + requestOrientationChange() } @SuppressLint("SwitchIntDef") diff --git a/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt b/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt index c56566e63..96168fa3b 100644 --- a/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt @@ -1,14 +1,31 @@ package com.github.libretube.ui.base +import android.content.pm.ActivityInfo import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import com.github.libretube.R +import com.github.libretube.constants.PreferenceKeys import com.github.libretube.helpers.LocaleHelper +import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ThemeHelper /** * Activity that applies the LibreTube theme and the in-app language */ open class BaseActivity : AppCompatActivity() { + private val screenOrientationPref by lazy { + val orientationPref = PreferenceHelper.getString( + PreferenceKeys.ORIENTATION, + resources.getString(R.string.config_default_orientation_pref) + ) + when (orientationPref) { + "portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + "landscape" -> ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE + "auto" -> ActivityInfo.SCREEN_ORIENTATION_USER + else -> throw IllegalArgumentException() + } + } + override fun onCreate(savedInstanceState: Bundle?) { // set the app theme (e.g. Material You) ThemeHelper.updateTheme(this) @@ -16,6 +33,15 @@ open class BaseActivity : AppCompatActivity() { // set the apps language LocaleHelper.updateLanguage(this) + requestOrientationChange() + super.onCreate(savedInstanceState) } + + /** + * Rotate according to the preference + */ + fun requestOrientationChange() { + requestedOrientation = screenOrientationPref + } } 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 f6273e85d..9b245f4f8 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 @@ -415,7 +415,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // FullScreen button trigger // hide fullscreen button if autorotation enabled - playerBinding.fullscreen.isInvisible = PlayerHelper.autoRotationEnabled + playerBinding.fullscreen.isInvisible = PlayerHelper.autoFullscreenEnabled playerBinding.fullscreen.setOnClickListener { // hide player controller binding.player.hideController() @@ -513,7 +513,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { * Expected behavior: Portrait for shorts, Landscape for normal videos */ private fun updateFullscreenOrientation() { - if (!PlayerHelper.autoRotationEnabled) { + if (!PlayerHelper.autoFullscreenEnabled) { val height = streams.videoStreams.firstOrNull()?.height ?: exoPlayer.videoSize.height val width = streams.videoStreams.firstOrNull()?.width ?: exoPlayer.videoSize.width @@ -563,7 +563,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.exoTitle.isInvisible = true - if (!PlayerHelper.autoRotationEnabled) { + if (!PlayerHelper.autoFullscreenEnabled) { // switch back to portrait mode if autorotation disabled mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT } @@ -641,12 +641,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { nowPlayingNotification.destroySelfAndPlayer() - activity?.requestedOrientation = - if ((activity as MainActivity).autoRotationEnabled) { - ActivityInfo.SCREEN_ORIENTATION_USER - } else { - ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT - } + (context as MainActivity).requestOrientationChange() } catch (e: Exception) { e.printStackTrace() } @@ -1400,7 +1395,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { */ @SuppressLint("SourceLockedOrientationActivity") private fun changeOrientationMode() { - if (PlayerHelper.autoRotationEnabled) { + if (PlayerHelper.autoFullscreenEnabled) { // enable auto rotation mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR onConfigurationChanged(resources.configuration) @@ -1612,7 +1607,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - if (!PlayerHelper.autoRotationEnabled || _binding == null || + if (!PlayerHelper.autoFullscreenEnabled || _binding == null || // If in PiP mode, orientation is given as landscape. PictureInPictureCompat.isInPictureInPictureMode(requireActivity()) ) { diff --git a/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt index d9e9e3c1b..ed30b1acb 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/GeneralSettings.kt @@ -27,7 +27,7 @@ class GeneralSettings : BasePreferenceFragment() { val region = findPreference("region") region?.let { setupRegionPref(it) } - val autoRotation = findPreference(PreferenceKeys.AUTO_ROTATION) + val autoRotation = findPreference(PreferenceKeys.ORIENTATION) autoRotation?.setOnPreferenceChangeListener { _, _ -> RequireRestartDialog().show(childFragmentManager, RequireRestartDialog::class.java.name) true diff --git a/app/src/main/res/values-television/config.xml b/app/src/main/res/values-television/config.xml index 861d394b3..2a908c7e4 100644 --- a/app/src/main/res/values-television/config.xml +++ b/app/src/main/res/values-television/config.xml @@ -1,3 +1,3 @@ - true + landscape diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 5726f3e30..5c3e34ace 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -455,4 +455,16 @@ automatic + + @string/portrait + @string/landscape + @string/auto_rotation + + + + portrait + landscape + auto + + diff --git a/app/src/main/res/values/config.xml b/app/src/main/res/values/config.xml index 5dfa9b145..11f4a1a83 100644 --- a/app/src/main/res/values/config.xml +++ b/app/src/main/res/values/config.xml @@ -1,3 +1,3 @@ - false + portrait diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8970e1b78..53de2cd0a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -461,6 +461,7 @@ unknown or no audio Continue watching No chapter + Screen orientation Download Service diff --git a/app/src/main/res/xml/general_settings.xml b/app/src/main/res/xml/general_settings.xml index 1f73c9dab..32c5522d2 100644 --- a/app/src/main/res/xml/general_settings.xml +++ b/app/src/main/res/xml/general_settings.xml @@ -30,11 +30,14 @@ android:title="@string/audio_only_mode" app:key="audio_only_mode" /> - + app:key="orientation" + app:title="@string/screen_orientation" + app:useSimpleSummaryProvider="true" />