Convert WindowHelper to an object.

This commit is contained in:
Isira Seneviratne 2023-03-06 10:56:42 +05:30
parent 1520e46930
commit 4388743b6f
4 changed files with 37 additions and 52 deletions

View File

@ -1,54 +1,44 @@
package com.github.libretube.helpers package com.github.libretube.helpers
import android.app.Activity
import android.os.Build import android.os.Build
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 androidx.core.view.WindowInsetsControllerCompat import androidx.core.view.WindowInsetsControllerCompat
import com.github.libretube.ui.base.BaseActivity
class WindowHelper(private val activity: BaseActivity) { object WindowHelper {
fun setFullscreen() = activity.apply { fun toggleFullscreen(activity: Activity, isFullscreen: Boolean) {
val window = activity.window
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { 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 WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
} } else {
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 =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
}
} }
WindowCompat.setDecorFitsSystemWindows(window, true) WindowCompat.setDecorFitsSystemWindows(window, !isFullscreen)
WindowInsetsControllerCompat(window, window.decorView).let { controller ->
controller.show( val controller = WindowCompat.getInsetsController(window, window.decorView)
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars() val flags = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.navigationBars()
) if (isFullscreen) {
controller.systemBarsBehavior = controller.hide(flags)
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH } 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 { val layoutFlag = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
return ViewCompat.getRootWindowInsets(activity.window.decorView)?.displayCutout != null if (isFullscreen) {
window.setFlags(layoutFlag, layoutFlag)
} else {
window.clearFlags(layoutFlag)
}
} }
} }

View File

@ -59,8 +59,6 @@ class MainActivity : BaseActivity() {
lateinit var searchView: SearchView lateinit var searchView: SearchView
private lateinit var searchItem: MenuItem private lateinit var searchItem: MenuItem
val windowHelper = WindowHelper(this)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -465,8 +463,8 @@ class MainActivity : BaseActivity() {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
when (newConfig.orientation) { when (newConfig.orientation) {
Configuration.ORIENTATION_PORTRAIT -> windowHelper.unsetFullscreen() Configuration.ORIENTATION_PORTRAIT -> WindowHelper.toggleFullscreen(this, false)
Configuration.ORIENTATION_LANDSCAPE -> windowHelper.setFullscreen() Configuration.ORIENTATION_LANDSCAPE -> WindowHelper.toggleFullscreen(this, true)
} }
} }

View File

@ -49,7 +49,7 @@ class OfflinePlayerActivity : BaseActivity() {
private val playerViewModel: PlayerViewModel by viewModels() private val playerViewModel: PlayerViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
WindowHelper(this).setFullscreen() WindowHelper.toggleFullscreen(this, true)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE

View File

@ -16,6 +16,7 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
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.updateLayoutParams import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import com.github.libretube.R 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.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.WindowHelper
import com.github.libretube.obj.BottomSheetItem 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.base.BaseActivity
import com.github.libretube.ui.interfaces.OnlinePlayerOptions import com.github.libretube.ui.interfaces.OnlinePlayerOptions
import com.github.libretube.ui.interfaces.PlayerGestureOptions import com.github.libretube.ui.interfaces.PlayerGestureOptions
@ -84,11 +85,11 @@ internal class CustomExoPlayerView(
private var resizeModePref = PlayerHelper.resizeModePref private var resizeModePref = PlayerHelper.resizeModePref
private val windowHelper private val activity
get() = (context as? MainActivity)?.windowHelper get() = context as BaseActivity
private val supportFragmentManager private val supportFragmentManager
get() = (context as BaseActivity).supportFragmentManager get() = activity.supportFragmentManager
private fun toggleController() { private fun toggleController() {
if (isControllerFullyVisible) hideController() else showController() if (isControllerFullyVisible) hideController() else showController()
@ -200,11 +201,7 @@ internal class CustomExoPlayerView(
}) })
playerViewModel?.isFullscreen?.observe(viewLifecycleOwner!!) { isFullscreen -> playerViewModel?.isFullscreen?.observe(viewLifecycleOwner!!) { isFullscreen ->
if (isFullscreen) { WindowHelper.toggleFullscreen(activity, isFullscreen)
windowHelper?.setFullscreen()
} else {
windowHelper?.unsetFullscreen()
}
} }
} }
@ -230,7 +227,7 @@ internal class CustomExoPlayerView(
// hide system bars if in fullscreen // hide system bars if in fullscreen
playerViewModel?.let { playerViewModel?.let {
if (it.isFullscreen.value == true) { if (it.isFullscreen.value == true) {
windowHelper?.setFullscreen() WindowHelper.toggleFullscreen(activity, true)
} }
updateTopBarMargin() updateTopBarMargin()
} }
@ -592,7 +589,7 @@ internal class CustomExoPlayerView(
updateTopBarMargin() updateTopBarMargin()
// don't add extra padding if there's no cutout // 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 // add a margin to the top and the bottom bar in landscape mode for notches
val newMargin = when (newConfig?.orientation) { val newMargin = when (newConfig?.orientation) {