From c8749888e96b712d8730e918ad61ec5e547ff11c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 15 Nov 2023 16:51:15 +0100 Subject: [PATCH] fix: horizontal margin of fullscreen player not updated --- .../github/libretube/ui/base/BaseActivity.kt | 8 +++++ .../libretube/ui/fragments/PlayerFragment.kt | 4 +++ .../libretube/ui/views/CustomExoPlayerView.kt | 35 +++++++++---------- .../libretube/ui/views/OfflinePlayerView.kt | 5 --- .../libretube/ui/views/OnlinePlayerView.kt | 9 ----- 5 files changed, 29 insertions(+), 32 deletions(-) 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 cc03b8dee..db7f010db 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 @@ -8,6 +8,7 @@ import com.github.libretube.constants.PreferenceKeys import com.github.libretube.helpers.LocaleHelper import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ThemeHelper +import com.github.libretube.helpers.WindowHelper /** * Activity that applies the LibreTube theme and the in-app language @@ -26,6 +27,11 @@ open class BaseActivity : AppCompatActivity() { } } + /** + * Whether the phone of the user has a cutout like a notch or not + */ + var hasCutout = false + override fun onCreate(savedInstanceState: Bundle?) { // set the app theme (e.g. Material You) ThemeHelper.updateTheme(this) @@ -35,6 +41,8 @@ open class BaseActivity : AppCompatActivity() { requestOrientationChange() + hasCutout = WindowHelper.hasCutout(window.decorView) + super.onCreate(savedInstanceState) } 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 efeb2afb7..5f6176c94 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 @@ -690,6 +690,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { updateResolutionOnFullscreenChange(true) openOrCloseFullscreenDialog(true) + + binding.player.updateMarginsByFullscreenMode() } @SuppressLint("SourceLockedOrientationActivity") @@ -716,6 +718,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { openOrCloseFullscreenDialog(false) checkForNecessaryOrientationRestart() + + binding.player.updateMarginsByFullscreenMode() } private fun openOrCloseFullscreenDialog(open: Boolean) { diff --git a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt index cccbafcd0..e0d86bc71 100644 --- a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt @@ -3,6 +3,7 @@ package com.github.libretube.ui.views import android.annotation.SuppressLint import android.app.Activity import android.content.Context +import android.content.pm.ActivityInfo import android.content.res.Configuration import android.graphics.Color import android.os.Handler @@ -98,10 +99,6 @@ open class CustomExoPlayerView( private val supportFragmentManager get() = activity.supportFragmentManager - private val hasCutout by lazy { - WindowHelper.hasCutout(this) - } - private fun toggleController() { if (isControllerFullyVisible) hideController() else showController() } @@ -553,9 +550,16 @@ open class CustomExoPlayerView( open fun isFullscreen() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE - override fun onConfigurationChanged(newConfig: Configuration) { + override fun onConfigurationChanged(newConfig: Configuration?) { super.onConfigurationChanged(newConfig) + updateMarginsByFullscreenMode() + } + + /** + * Updates the margins according to the current orientation and fullscreen mode + */ + fun updateMarginsByFullscreenMode() { // add a larger bottom margin to the time bar in landscape mode binding.progressBar.updateLayoutParams { bottomMargin = (if (isFullscreen()) 20f else 10f).dpToPx() @@ -563,19 +567,17 @@ open class CustomExoPlayerView( updateTopBarMargin() - // don't add extra padding if there's no cutout - if (!hasCutout && binding.topBar.marginStart == 0) return + // don't add extra padding if there's no cutout and no margin set that would need to be undone + if (!(context as BaseActivity).hasCutout && binding.topBar.marginStart == LANDSCAPE_MARGIN_HORIZONTAL_NONE) return // add a margin to the top and the bottom bar in landscape mode for notches - val newMargin = when (newConfig.orientation) { - Configuration.ORIENTATION_LANDSCAPE -> LANDSCAPE_MARGIN_HORIZONTAL - else -> 0 - } + val isForcedPortrait = activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT + val horizontalMargin = if (isFullscreen() && !isForcedPortrait) LANDSCAPE_MARGIN_HORIZONTAL else LANDSCAPE_MARGIN_HORIZONTAL_NONE listOf(binding.topBar, binding.bottomBar).forEach { it.updateLayoutParams { - marginStart = newMargin - marginEnd = newMargin + marginStart = horizontalMargin + marginEnd = horizontalMargin } } } @@ -599,7 +601,7 @@ open class CustomExoPlayerView( */ fun updateTopBarMargin() { binding.topBar.updateLayoutParams { - topMargin = getTopBarMarginDp().toFloat().dpToPx() + topMargin = (if (isFullscreen()) 18f else 0f).dpToPx() } } @@ -616,10 +618,6 @@ open class CustomExoPlayerView( runnableHandler.postDelayed(100, UPDATE_POSITION_TOKEN, this::updateCurrentPosition) } - open fun getTopBarMarginDp(): Int { - return if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) 10 else 0 - } - override fun onSingleTap() { toggleController() } @@ -728,5 +726,6 @@ open class CustomExoPlayerView( private const val ANIMATION_DURATION = 100L private const val AUTO_HIDE_CONTROLLER_DELAY = 2000L private val LANDSCAPE_MARGIN_HORIZONTAL = 20f.dpToPx() + private val LANDSCAPE_MARGIN_HORIZONTAL_NONE = 0f.dpToPx() } } diff --git a/app/src/main/java/com/github/libretube/ui/views/OfflinePlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/OfflinePlayerView.kt index fedd7b05a..31b94952a 100644 --- a/app/src/main/java/com/github/libretube/ui/views/OfflinePlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/OfflinePlayerView.kt @@ -20,11 +20,6 @@ class OfflinePlayerView( toggleSystemBars(true) } - override fun getTopBarMarginDp(): Int { - // the offline player requires a bigger top bar margin - return if (isFullscreen()) 18 else super.getTopBarMarginDp() - } - override fun minimizeOrExitPlayer() { (context as AppCompatActivity).onBackPressedDispatcher.onBackPressed() } diff --git a/app/src/main/java/com/github/libretube/ui/views/OnlinePlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/OnlinePlayerView.kt index 3d4c39824..8cd6ad954 100644 --- a/app/src/main/java/com/github/libretube/ui/views/OnlinePlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/OnlinePlayerView.kt @@ -1,7 +1,6 @@ package com.github.libretube.ui.views import android.content.Context -import android.content.res.Configuration import android.util.AttributeSet import android.view.Window import androidx.core.os.bundleOf @@ -194,14 +193,6 @@ class OnlinePlayerView( } } - override fun getTopBarMarginDp(): Int { - return when { - resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE -> 15 - playerViewModel?.isFullscreen?.value == true -> 20 - else -> super.getTopBarMarginDp() - } - } - override fun isFullscreen(): Boolean { return playerViewModel?.isFullscreen?.value ?: super.isFullscreen() }