feat: show navigation bar when showing controller if gesture navigation enabled

This commit is contained in:
Bnyro 2023-11-03 15:19:21 +01:00
parent 259cb3b8d9
commit 73b2e70e99
4 changed files with 57 additions and 35 deletions

View File

@ -1,13 +1,19 @@
package com.github.libretube.helpers package com.github.libretube.helpers
import android.content.Context
import android.os.Build import android.os.Build
import android.provider.Settings
import android.view.View
import android.view.Window import android.view.Window
import android.view.WindowManager import android.view.WindowManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import com.github.libretube.ui.extensions.toggleSystemBars import com.github.libretube.ui.extensions.toggleSystemBars
object WindowHelper { object WindowHelper {
private const val NAVIGATION_MODE = "navigation_mode"
fun toggleFullscreen(window: Window, isFullscreen: Boolean) { fun toggleFullscreen(window: Window, isFullscreen: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode = if (isFullscreen) { window.attributes.layoutInDisplayCutoutMode = if (isFullscreen) {
@ -34,4 +40,18 @@ object WindowHelper {
showBars = !isFullscreen 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
} }

View File

@ -17,7 +17,6 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible 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.BrightnessHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.WindowHelper
import com.github.libretube.obj.BottomSheetItem import com.github.libretube.obj.BottomSheetItem
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.extensions.toggleSystemBars import com.github.libretube.ui.extensions.toggleSystemBars
@ -88,12 +88,6 @@ open class CustomExoPlayerView(
updateCurrentPosition() 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 * Preferences
*/ */
@ -104,6 +98,10 @@ open class CustomExoPlayerView(
private val supportFragmentManager private val supportFragmentManager
get() = activity.supportFragmentManager get() = activity.supportFragmentManager
private val hasCutout by lazy {
WindowHelper.hasCutout(this)
}
private fun toggleController() { private fun toggleController() {
if (isControllerFullyVisible) hideController() else showController() if (isControllerFullyVisible) hideController() else showController()
} }
@ -150,10 +148,7 @@ open class CustomExoPlayerView(
// change locked status // change locked status
isPlayerLocked = !isPlayerLocked isPlayerLocked = !isPlayerLocked
(currentWindow ?: activity.window).toggleSystemBars( toggleSystemBars(!isPlayerLocked)
types = WindowInsetsCompat.Type.statusBars(),
showBars = !isPlayerLocked
)
} }
resizeMode = when (resizeModePref) { resizeMode = when (resizeModePref) {
@ -215,6 +210,14 @@ open class CustomExoPlayerView(
updateCurrentPosition() 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 open fun onPlayerEvent(player: Player, playerEvents: Player.Events) = Unit
private fun updatePlayPauseButton() { private fun updatePlayPauseButton() {
@ -563,7 +566,6 @@ open class CustomExoPlayerView(
updateTopBarMargin() updateTopBarMargin()
// don't add extra padding if there's no cutout // don't add extra padding if there's no cutout
val hasCutout = ViewCompat.getRootWindowInsets(this)?.displayCutout != null
if (!hasCutout && binding.topBar.marginStart == 0) return if (!hasCutout && binding.topBar.marginStart == 0) return
// add a margin to the top and the bottom bar in landscape mode for notches // 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() { fun updateTopBarMargin() {
binding.topBar.updateLayoutParams<MarginLayoutParams> { binding.topBar.updateLayoutParams<MarginLayoutParams> {
@ -722,6 +724,8 @@ open class CustomExoPlayerView(
open fun minimizeOrExitPlayer() = Unit open fun minimizeOrExitPlayer() = Unit
open fun getWindow(): Window = activity.window
companion object { companion object {
private const val HIDE_CONTROLLER_TOKEN = "hideController" private const val HIDE_CONTROLLER_TOKEN = "hideController"
private const val HIDE_FORWARD_BUTTON_TOKEN = "hideForwardButton" private const val HIDE_FORWARD_BUTTON_TOKEN = "hideForwardButton"

View File

@ -3,8 +3,6 @@ package com.github.libretube.ui.views
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowInsetsCompat
import com.github.libretube.ui.extensions.toggleSystemBars
class OfflinePlayerView( class OfflinePlayerView(
context: Context, context: Context,
@ -13,13 +11,13 @@ class OfflinePlayerView(
override fun hideController() { override fun hideController() {
super.hideController() super.hideController()
// hide the status bars when continuing to watch video // hide the status bars when continuing to watch video
activity.window.toggleSystemBars(WindowInsetsCompat.Type.systemBars(), false) toggleSystemBars(false)
} }
override fun showController() { override fun showController() {
super.showController() super.showController()
// show status bar when showing player options // show status bar when showing player options
activity.window.toggleSystemBars(WindowInsetsCompat.Type.statusBars(), true) toggleSystemBars(true)
} }
override fun getTopBarMarginDp(): Int { override fun getTopBarMarginDp(): Int {

View File

@ -3,14 +3,12 @@ package com.github.libretube.ui.views
import android.content.Context import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.Window
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.media3.common.C import androidx.media3.common.C
import androidx.media3.exoplayer.trackselection.TrackSelector import androidx.media3.exoplayer.trackselection.TrackSelector
import androidx.media3.ui.PlayerView.ControllerVisibilityListener
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys 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.obj.BottomSheetItem
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.SubmitSegmentDialog 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.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
@ -35,6 +32,12 @@ class OnlinePlayerView(
private var trackSelector: TrackSelector? = null private var trackSelector: TrackSelector? = null
private var viewLifecycleOwner: LifecycleOwner? = 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) @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
override fun getOptionsMenuItems(): List<BottomSheetItem> { override fun getOptionsMenuItems(): List<BottomSheetItem> {
return super.getOptionsMenuItems() + return super.getOptionsMenuItems() +
@ -149,19 +152,6 @@ class OnlinePlayerView(
updateTopBarMargin() 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.isChecked = PlayerHelper.autoPlayEnabled
binding.autoPlay.setOnCheckedChangeListener { _, isChecked -> binding.autoPlay.setOnCheckedChangeListener { _, isChecked ->
@ -185,15 +175,25 @@ class OnlinePlayerView(
} }
} }
override fun getWindow(): Window = currentWindow ?: activity.window
override fun hideController() { override fun hideController() {
super.hideController() super.hideController()
if (playerViewModel?.isFullscreen?.value == true) { if (playerViewModel?.isFullscreen?.value == true) {
WindowHelper.toggleFullscreen(activity.window, true) toggleSystemBars(true)
} }
updateTopBarMargin() updateTopBarMargin()
} }
override fun showController() {
super.showController()
if (playerViewModel?.isFullscreen?.value == true && !isPlayerLocked) {
toggleSystemBars(true)
}
}
override fun getTopBarMarginDp(): Int { override fun getTopBarMarginDp(): Int {
return when { return when {
resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE -> 15 resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE -> 15