From 7c72c4bea146bad5b66ca5040030ae5a739f5250 Mon Sep 17 00:00:00 2001 From: anilbeesetti Date: Wed, 29 Mar 2023 09:24:39 +0530 Subject: [PATCH 1/5] Fix two taps required for interacting with player controls --- .../github/libretube/helpers/WindowHelper.kt | 33 +++++++++---------- .../libretube/ui/views/CustomExoPlayerView.kt | 19 +++++++++++ 2 files changed, 35 insertions(+), 17 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 8fc0a1cb1..fe9604c65 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -20,25 +20,24 @@ object WindowHelper { WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen) - val controller = WindowCompat.getInsetsController(window, window.decorView) - val flags = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars() if (isFullscreen) { - controller.hide(flags) + activity.hideSystemBars() } else { - controller.show(flags) - } - - controller.systemBarsBehavior = if (isFullscreen) { - WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - } else { - WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH - } - - val layoutFlag = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS - if (isFullscreen) { - window.setFlags(layoutFlag, layoutFlag) - } else { - window.clearFlags(layoutFlag) + activity.showSystemBars() } } } + +fun Activity.hideSystemBars() { + WindowCompat.getInsetsController(window, window.decorView).apply { + systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + hide(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + } +} + +fun Activity.showSystemBars() { + WindowCompat.getInsetsController(window, window.decorView).apply { + systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + show(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + } +} 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 2716dbb72..ef8c1b870 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 @@ -32,6 +32,8 @@ import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.BrightnessHelper import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.WindowHelper +import com.github.libretube.helpers.hideSystemBars +import com.github.libretube.helpers.showSystemBars import com.github.libretube.obj.BottomSheetItem import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.interfaces.OnlinePlayerOptions @@ -202,6 +204,23 @@ internal class CustomExoPlayerView( override fun onScrubStop(timeBar: TimeBar, position: Long, canceled: Boolean) {} }) + setControllerVisibilityListener( + ControllerVisibilityListener { visibility -> + playerViewModel?.isFullscreen?.value?.let { isFullscreen -> + if (isFullscreen) { + when (visibility) { + View.VISIBLE -> { + activity.showSystemBars() + } + View.GONE -> { + activity.hideSystemBars() + } + } + } + } + } + ) + playerViewModel?.isFullscreen?.observe(viewLifecycleOwner!!) { isFullscreen -> WindowHelper.toggleFullscreen(activity, isFullscreen) } From 7f68deb33bd0c694a43acf48d9aec3509aa1f4c6 Mon Sep 17 00:00:00 2001 From: anilbeesetti Date: Thu, 30 Mar 2023 06:53:37 +0530 Subject: [PATCH 2/5] move extension functions to extensions package --- .../github/libretube/helpers/WindowHelper.kt | 16 ++------------- .../libretube/ui/extensions/Activity.kt | 20 +++++++++++++++++++ .../libretube/ui/views/CustomExoPlayerView.kt | 4 ++-- 3 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/extensions/Activity.kt 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 fe9604c65..5d86f88c7 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -6,6 +6,8 @@ import android.view.WindowManager import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat +import com.github.libretube.ui.extensions.hideSystemBars +import com.github.libretube.ui.extensions.showSystemBars object WindowHelper { fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) { @@ -27,17 +29,3 @@ object WindowHelper { } } } - -fun Activity.hideSystemBars() { - WindowCompat.getInsetsController(window, window.decorView).apply { - systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - hide(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) - } -} - -fun Activity.showSystemBars() { - WindowCompat.getInsetsController(window, window.decorView).apply { - systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - show(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) - } -} diff --git a/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt new file mode 100644 index 000000000..445a95af0 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt @@ -0,0 +1,20 @@ +package com.github.libretube.ui.extensions + +import android.app.Activity +import androidx.core.view.WindowCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.WindowInsetsControllerCompat + +fun Activity.hideSystemBars() { + WindowCompat.getInsetsController(window, window.decorView).apply { + systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + hide(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + } +} + +fun Activity.showSystemBars() { + WindowCompat.getInsetsController(window, window.decorView).apply { + systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + show(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + } +} 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 ef8c1b870..497b88159 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 @@ -32,10 +32,10 @@ import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.BrightnessHelper import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.WindowHelper -import com.github.libretube.helpers.hideSystemBars -import com.github.libretube.helpers.showSystemBars import com.github.libretube.obj.BottomSheetItem import com.github.libretube.ui.base.BaseActivity +import com.github.libretube.ui.extensions.hideSystemBars +import com.github.libretube.ui.extensions.showSystemBars import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.interfaces.PlayerGestureOptions import com.github.libretube.ui.interfaces.PlayerOptions From 8e24cc878b9ad9b76398997fc4f8670b4085093e Mon Sep 17 00:00:00 2001 From: anilbeesetti Date: Fri, 31 Mar 2023 07:01:49 +0530 Subject: [PATCH 3/5] fix navigation bars showing when controller is visible --- .../com/github/libretube/helpers/WindowHelper.kt | 5 ++--- .../com/github/libretube/ui/extensions/Activity.kt | 10 +++++----- .../libretube/ui/views/CustomExoPlayerView.kt | 14 +++++--------- 3 files changed, 12 insertions(+), 17 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 5d86f88c7..b591f2335 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -5,7 +5,6 @@ import android.os.Build import android.view.WindowManager import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat -import androidx.core.view.WindowInsetsControllerCompat import com.github.libretube.ui.extensions.hideSystemBars import com.github.libretube.ui.extensions.showSystemBars @@ -23,9 +22,9 @@ object WindowHelper { WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen) if (isFullscreen) { - activity.hideSystemBars() + activity.hideSystemBars(WindowInsetsCompat.Type.systemBars()) } else { - activity.showSystemBars() + activity.showSystemBars(WindowInsetsCompat.Type.systemBars()) } } } diff --git a/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt index 445a95af0..782755fb5 100644 --- a/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt +++ b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt @@ -2,19 +2,19 @@ package com.github.libretube.ui.extensions import android.app.Activity import androidx.core.view.WindowCompat -import androidx.core.view.WindowInsetsCompat +import androidx.core.view.WindowInsetsCompat.Type.InsetsType import androidx.core.view.WindowInsetsControllerCompat -fun Activity.hideSystemBars() { +fun Activity.hideSystemBars(@InsetsType types: Int) { WindowCompat.getInsetsController(window, window.decorView).apply { systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - hide(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + hide(types) } } -fun Activity.showSystemBars() { +fun Activity.showSystemBars(@InsetsType types: Int) { WindowCompat.getInsetsController(window, window.decorView).apply { systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - show(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()) + show(types) } } 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 497b88159..64e5d4fb9 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,6 +17,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.os.postDelayed import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import androidx.core.view.isVisible import androidx.core.view.marginStart import androidx.core.view.updateLayoutParams @@ -207,15 +208,10 @@ internal class CustomExoPlayerView( setControllerVisibilityListener( ControllerVisibilityListener { visibility -> playerViewModel?.isFullscreen?.value?.let { isFullscreen -> - if (isFullscreen) { - when (visibility) { - View.VISIBLE -> { - activity.showSystemBars() - } - View.GONE -> { - activity.hideSystemBars() - } - } + if (!isFullscreen) return@let + when (visibility) { + View.VISIBLE -> activity.showSystemBars(WindowInsetsCompat.Type.statusBars()) + View.GONE -> activity.hideSystemBars(WindowInsetsCompat.Type.statusBars()) } } } From 6e452ad156f60364b42be468e4d9c031f0e0cb34 Mon Sep 17 00:00:00 2001 From: anilbeesetti Date: Fri, 31 Mar 2023 07:05:04 +0530 Subject: [PATCH 4/5] fix lint issues --- .../com/github/libretube/ui/views/CustomExoPlayerView.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 64e5d4fb9..cf3513655 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 @@ -210,8 +210,12 @@ internal class CustomExoPlayerView( playerViewModel?.isFullscreen?.value?.let { isFullscreen -> if (!isFullscreen) return@let when (visibility) { - View.VISIBLE -> activity.showSystemBars(WindowInsetsCompat.Type.statusBars()) - View.GONE -> activity.hideSystemBars(WindowInsetsCompat.Type.statusBars()) + View.VISIBLE -> { + activity.showSystemBars(WindowInsetsCompat.Type.statusBars()) + } + View.GONE -> { + activity.hideSystemBars(WindowInsetsCompat.Type.statusBars()) + } } } } From fb8145752c422c9a52c0e1c727841f1b6c1b7f7d Mon Sep 17 00:00:00 2001 From: anilbeesetti Date: Sat, 1 Apr 2023 18:51:55 +0530 Subject: [PATCH 5/5] refactor show and hide system bars function --- .../com/github/libretube/helpers/WindowHelper.kt | 15 ++++++++------- .../github/libretube/ui/extensions/Activity.kt | 15 ++++++--------- .../libretube/ui/views/CustomExoPlayerView.kt | 16 ++++++---------- 3 files changed, 20 insertions(+), 26 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 b591f2335..6316ac699 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -5,8 +5,7 @@ import android.os.Build import android.view.WindowManager import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat -import com.github.libretube.ui.extensions.hideSystemBars -import com.github.libretube.ui.extensions.showSystemBars +import com.github.libretube.ui.extensions.toggleSystemBars object WindowHelper { fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) { @@ -21,10 +20,12 @@ object WindowHelper { WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen) - if (isFullscreen) { - activity.hideSystemBars(WindowInsetsCompat.Type.systemBars()) - } else { - activity.showSystemBars(WindowInsetsCompat.Type.systemBars()) - } + // Show the system bars when it is not fullscreen and hide them when it is fullscreen + // System bars means status bar and the navigation bar + // See: https://developer.android.com/training/system-ui/immersive#kotlin + activity.toggleSystemBars( + types = WindowInsetsCompat.Type.systemBars(), + showBars = !isFullscreen + ) } } diff --git a/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt index 782755fb5..da8f0679f 100644 --- a/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt +++ b/app/src/main/java/com/github/libretube/ui/extensions/Activity.kt @@ -5,16 +5,13 @@ import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat.Type.InsetsType import androidx.core.view.WindowInsetsControllerCompat -fun Activity.hideSystemBars(@InsetsType types: Int) { +fun Activity.toggleSystemBars(@InsetsType types: Int, showBars: Boolean) { WindowCompat.getInsetsController(window, window.decorView).apply { systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - hide(types) - } -} - -fun Activity.showSystemBars(@InsetsType types: Int) { - WindowCompat.getInsetsController(window, window.decorView).apply { - systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - show(types) + if (showBars) { + show(types) + } else { + hide(types) + } } } 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 cf3513655..a38ba52d4 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 @@ -35,8 +35,7 @@ import com.github.libretube.helpers.PlayerHelper 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.hideSystemBars -import com.github.libretube.ui.extensions.showSystemBars +import com.github.libretube.ui.extensions.toggleSystemBars import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.interfaces.PlayerGestureOptions import com.github.libretube.ui.interfaces.PlayerOptions @@ -209,14 +208,11 @@ internal class CustomExoPlayerView( ControllerVisibilityListener { visibility -> playerViewModel?.isFullscreen?.value?.let { isFullscreen -> if (!isFullscreen) return@let - when (visibility) { - View.VISIBLE -> { - activity.showSystemBars(WindowInsetsCompat.Type.statusBars()) - } - View.GONE -> { - activity.hideSystemBars(WindowInsetsCompat.Type.statusBars()) - } - } + // Show status bar only not navigation bar if the player controls are visible and hide it otherwise + activity.toggleSystemBars( + types = WindowInsetsCompat.Type.statusBars(), + showBars = visibility == View.VISIBLE + ) } } )