From 4388743b6fe80d4de2243309272d59c84d3e0ab9 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 6 Mar 2023 10:56:42 +0530 Subject: [PATCH] Convert WindowHelper to an object. --- .../github/libretube/helpers/WindowHelper.kt | 62 ++++++++----------- .../libretube/ui/activities/MainActivity.kt | 6 +- .../ui/activities/OfflinePlayerActivity.kt | 2 +- .../libretube/ui/views/CustomExoPlayerView.kt | 19 +++--- 4 files changed, 37 insertions(+), 52 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 3220320c8..8fc0a1cb1 100644 --- a/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/WindowHelper.kt @@ -1,54 +1,44 @@ package com.github.libretube.helpers +import android.app.Activity import android.os.Build import android.view.WindowManager -import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat -import com.github.libretube.ui.base.BaseActivity -class WindowHelper(private val activity: BaseActivity) { - fun setFullscreen() = activity.apply { +object WindowHelper { + fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) { + val window = activity.window if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - window.attributes.layoutInDisplayCutoutMode = + window.attributes.layoutInDisplayCutoutMode = if (isFullscreen) { WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - } - - WindowCompat.setDecorFitsSystemWindows(window, false) - WindowInsetsControllerCompat(window, window.decorView).let { controller -> - controller.hide( - WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars() - ) - controller.systemBarsBehavior = - WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - } - - window.setFlags( - WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, - WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS - ) - } - - fun unsetFullscreen() = activity.apply { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - window.attributes.layoutInDisplayCutoutMode = + } else { WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT + } } - WindowCompat.setDecorFitsSystemWindows(window, true) - WindowInsetsControllerCompat(window, window.decorView).let { controller -> - controller.show( - WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars() - ) - controller.systemBarsBehavior = - WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH + 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) + } else { + controller.show(flags) } - window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) - } + controller.systemBarsBehavior = if (isFullscreen) { + WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + } else { + WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH + } - fun hasCutout(): Boolean { - return ViewCompat.getRootWindowInsets(activity.window.decorView)?.displayCutout != null + val layoutFlag = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS + if (isFullscreen) { + window.setFlags(layoutFlag, layoutFlag) + } else { + window.clearFlags(layoutFlag) + } } } 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 1dfd36d86..ebb151182 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 @@ -59,8 +59,6 @@ class MainActivity : BaseActivity() { lateinit var searchView: SearchView private lateinit var searchItem: MenuItem - val windowHelper = WindowHelper(this) - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -465,8 +463,8 @@ class MainActivity : BaseActivity() { super.onConfigurationChanged(newConfig) when (newConfig.orientation) { - Configuration.ORIENTATION_PORTRAIT -> windowHelper.unsetFullscreen() - Configuration.ORIENTATION_LANDSCAPE -> windowHelper.setFullscreen() + Configuration.ORIENTATION_PORTRAIT -> WindowHelper.toggleFullscreen(this, false) + Configuration.ORIENTATION_LANDSCAPE -> WindowHelper.toggleFullscreen(this, true) } } diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 987ab0f99..58801d97b 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -49,7 +49,7 @@ class OfflinePlayerActivity : BaseActivity() { private val playerViewModel: PlayerViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { - WindowHelper(this).setFullscreen() + WindowHelper.toggleFullscreen(this, true) requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE 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 5e9e968a6..b634d84d2 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 @@ -16,6 +16,7 @@ import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.os.postDelayed +import androidx.core.view.ViewCompat import androidx.core.view.updateLayoutParams import androidx.lifecycle.LifecycleOwner import com.github.libretube.R @@ -28,8 +29,8 @@ import com.github.libretube.extensions.round 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.obj.BottomSheetItem -import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.interfaces.PlayerGestureOptions @@ -84,11 +85,11 @@ internal class CustomExoPlayerView( private var resizeModePref = PlayerHelper.resizeModePref - private val windowHelper - get() = (context as? MainActivity)?.windowHelper + private val activity + get() = context as BaseActivity private val supportFragmentManager - get() = (context as BaseActivity).supportFragmentManager + get() = activity.supportFragmentManager private fun toggleController() { if (isControllerFullyVisible) hideController() else showController() @@ -200,11 +201,7 @@ internal class CustomExoPlayerView( }) playerViewModel?.isFullscreen?.observe(viewLifecycleOwner!!) { isFullscreen -> - if (isFullscreen) { - windowHelper?.setFullscreen() - } else { - windowHelper?.unsetFullscreen() - } + WindowHelper.toggleFullscreen(activity, isFullscreen) } } @@ -230,7 +227,7 @@ internal class CustomExoPlayerView( // hide system bars if in fullscreen playerViewModel?.let { if (it.isFullscreen.value == true) { - windowHelper?.setFullscreen() + WindowHelper.toggleFullscreen(activity, true) } updateTopBarMargin() } @@ -592,7 +589,7 @@ internal class CustomExoPlayerView( updateTopBarMargin() // don't add extra padding if there's no cutout - if ((context as? MainActivity)?.windowHelper?.hasCutout() == false) return + if (ViewCompat.getRootWindowInsets(this)?.displayCutout == null) return // add a margin to the top and the bottom bar in landscape mode for notches val newMargin = when (newConfig?.orientation) {