From 73b2e70e99461e2ec058032640dc3ec0d9bf93e2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 3 Nov 2023 15:19:21 +0100 Subject: [PATCH] feat: show navigation bar when showing controller if gesture navigation enabled --- .../github/libretube/helpers/WindowHelper.kt | 20 +++++++++++ .../libretube/ui/views/CustomExoPlayerView.kt | 30 +++++++++------- .../libretube/ui/views/OfflinePlayerView.kt | 6 ++-- .../libretube/ui/views/OnlinePlayerView.kt | 36 +++++++++---------- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt index a77156cc2..030773a49 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -1,13 +1,19 @@ package com.github.libretube.helpers +import android.content.Context import android.os.Build +import android.provider.Settings +import android.view.View import android.view.Window import android.view.WindowManager +import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import com.github.libretube.ui.extensions.toggleSystemBars object WindowHelper { + private const val NAVIGATION_MODE = "navigation_mode" + fun toggleFullscreen(window: Window, isFullscreen: Boolean) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { window.attributes.layoutInDisplayCutoutMode = if (isFullscreen) { @@ -34,4 +40,18 @@ object WindowHelper { showBars = !isFullscreen ) } + + /** + * Returns [WindowInsetsCompat.Type.systemBars] if the user uses gesture navigation + * Otherwise returns [WindowInsetsCompat.Type.statusBars] + */ + fun getGestureControlledBars(context: Context): Int { + if (Settings.Secure.getInt(context.contentResolver, NAVIGATION_MODE, 0) == 2) { + return WindowInsetsCompat.Type.systemBars() + } + + return WindowInsetsCompat.Type.statusBars() + } + + fun hasCutout(view: View) = ViewCompat.getRootWindowInsets(view)?.displayCutout != null } 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 353e18f31..42145d2ee 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 @@ -17,7 +17,6 @@ import android.widget.ImageView import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.os.postDelayed -import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.isGone import androidx.core.view.isVisible @@ -46,6 +45,7 @@ import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.BrightnessHelper import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PreferenceHelper +import com.github.libretube.helpers.WindowHelper import com.github.libretube.obj.BottomSheetItem import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.extensions.toggleSystemBars @@ -88,12 +88,6 @@ open class CustomExoPlayerView( updateCurrentPosition() } - /** - * The window that needs to be addressed for showing and hiding the system bars - * If null, the activity's default/main window will be used - */ - var currentWindow: Window? = null - /** * Preferences */ @@ -104,6 +98,10 @@ 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() } @@ -150,10 +148,7 @@ open class CustomExoPlayerView( // change locked status isPlayerLocked = !isPlayerLocked - (currentWindow ?: activity.window).toggleSystemBars( - types = WindowInsetsCompat.Type.statusBars(), - showBars = !isPlayerLocked - ) + toggleSystemBars(!isPlayerLocked) } resizeMode = when (resizeModePref) { @@ -215,6 +210,14 @@ open class CustomExoPlayerView( updateCurrentPosition() } + fun toggleSystemBars(showBars: Boolean) { + getWindow().toggleSystemBars( + types = if (showBars) WindowHelper.getGestureControlledBars(context) + else WindowInsetsCompat.Type.systemBars(), + showBars = showBars + ) + } + open fun onPlayerEvent(player: Player, playerEvents: Player.Events) = Unit private fun updatePlayPauseButton() { @@ -563,7 +566,6 @@ open class CustomExoPlayerView( updateTopBarMargin() // don't add extra padding if there's no cutout - val hasCutout = ViewCompat.getRootWindowInsets(this)?.displayCutout != null if (!hasCutout && binding.topBar.marginStart == 0) return // add a margin to the top and the bottom bar in landscape mode for notches @@ -595,7 +597,7 @@ open class CustomExoPlayerView( } /** - * Add extra margin to the top bar to not overlap the status bar + * Add extra margin to the top bar to not overlap the status bar. */ fun updateTopBarMargin() { binding.topBar.updateLayoutParams { @@ -722,6 +724,8 @@ open class CustomExoPlayerView( open fun minimizeOrExitPlayer() = Unit + open fun getWindow(): Window = activity.window + companion object { private const val HIDE_CONTROLLER_TOKEN = "hideController" private const val HIDE_FORWARD_BUTTON_TOKEN = "hideForwardButton" 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 d41544f39..fedd7b05a 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 @@ -3,8 +3,6 @@ package com.github.libretube.ui.views import android.content.Context import android.util.AttributeSet import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.WindowInsetsCompat -import com.github.libretube.ui.extensions.toggleSystemBars class OfflinePlayerView( context: Context, @@ -13,13 +11,13 @@ class OfflinePlayerView( override fun hideController() { super.hideController() // hide the status bars when continuing to watch video - activity.window.toggleSystemBars(WindowInsetsCompat.Type.systemBars(), false) + toggleSystemBars(false) } override fun showController() { super.showController() // show status bar when showing player options - activity.window.toggleSystemBars(WindowInsetsCompat.Type.statusBars(), true) + toggleSystemBars(true) } override fun getTopBarMarginDp(): Int { 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 29544f4b8..ad4290528 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 @@ -3,14 +3,12 @@ package com.github.libretube.ui.views import android.content.Context import android.content.res.Configuration import android.util.AttributeSet -import android.view.View +import android.view.Window import androidx.core.os.bundleOf -import androidx.core.view.WindowInsetsCompat import androidx.core.view.isVisible import androidx.lifecycle.LifecycleOwner import androidx.media3.common.C import androidx.media3.exoplayer.trackselection.TrackSelector -import androidx.media3.ui.PlayerView.ControllerVisibilityListener import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys @@ -21,7 +19,6 @@ import com.github.libretube.helpers.WindowHelper import com.github.libretube.obj.BottomSheetItem import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.dialogs.SubmitSegmentDialog -import com.github.libretube.ui.extensions.toggleSystemBars import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.PlayingQueue @@ -35,6 +32,12 @@ class OnlinePlayerView( private var trackSelector: TrackSelector? = null private var viewLifecycleOwner: LifecycleOwner? = null + /** + * The window that needs to be addressed for showing and hiding the system bars + * If null, the activity's default/main window will be used + */ + var currentWindow: Window? = null + @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) override fun getOptionsMenuItems(): List { return super.getOptionsMenuItems() + @@ -149,19 +152,6 @@ class OnlinePlayerView( updateTopBarMargin() } - setControllerVisibilityListener( - ControllerVisibilityListener { visibility -> - playerViewModel.isFullscreen.value?.let { isFullscreen -> - if (!isFullscreen) return@let - // Show status bar only not navigation bar if the player controls are visible and hide it otherwise - activity.window.toggleSystemBars( - types = WindowInsetsCompat.Type.statusBars(), - showBars = visibility == View.VISIBLE && !isPlayerLocked - ) - } - } - ) - binding.autoPlay.isChecked = PlayerHelper.autoPlayEnabled binding.autoPlay.setOnCheckedChangeListener { _, isChecked -> @@ -185,15 +175,25 @@ class OnlinePlayerView( } } + override fun getWindow(): Window = currentWindow ?: activity.window + override fun hideController() { super.hideController() if (playerViewModel?.isFullscreen?.value == true) { - WindowHelper.toggleFullscreen(activity.window, true) + toggleSystemBars(true) } updateTopBarMargin() } + override fun showController() { + super.showController() + + if (playerViewModel?.isFullscreen?.value == true && !isPlayerLocked) { + toggleSystemBars(true) + } + } + override fun getTopBarMarginDp(): Int { return when { resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE -> 15