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 650d807a6..e5059634d 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 @@ -6,15 +6,10 @@ import android.content.pm.ActivityInfo import android.content.res.Configuration import android.os.Build import android.os.Bundle -import android.os.Handler -import android.os.Looper import android.view.Menu import android.view.MenuItem import android.view.View import android.view.ViewGroup -import android.view.WindowInsets -import android.view.WindowInsetsController -import android.view.WindowManager import android.widget.ScrollView import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView @@ -46,6 +41,7 @@ import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NetworkHelper import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.ThemeHelper +import com.github.libretube.util.WindowHelper import com.google.android.material.elevation.SurfaceColors class MainActivity : BaseActivity() { @@ -60,7 +56,7 @@ class MainActivity : BaseActivity() { lateinit var searchView: SearchView private lateinit var searchItem: MenuItem - private val handler = Handler(Looper.getMainLooper()) + val windowHelper = WindowHelper(this) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -468,68 +464,8 @@ class MainActivity : BaseActivity() { super.onConfigurationChanged(newConfig) when (newConfig.orientation) { - Configuration.ORIENTATION_PORTRAIT -> unsetFullscreen() - Configuration.ORIENTATION_LANDSCAPE -> setFullscreen() - } - } - - fun setFullscreen() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - window.attributes.layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - window.setDecorFitsSystemWindows(false) - window.insetsController?.apply { - hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()) - systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - } - } else { - @Suppress("DEPRECATION") - window.decorView.systemUiVisibility = ( - View.SYSTEM_UI_FLAG_FULLSCREEN - or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - or View.SYSTEM_UI_FLAG_IMMERSIVE - or View.SYSTEM_UI_FLAG_LAYOUT_STABLE - or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - ) - } - - window.setFlags( - WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, - WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS - ) - } - - private fun unsetFullscreen() { - window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { - @Suppress("DEPRECATION") - window.clearFlags( - WindowManager.LayoutParams.FLAG_FULLSCREEN - ) - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - window.attributes.layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - window.setDecorFitsSystemWindows(true) - window.insetsController?.apply { - show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT - } - } - } else { - @Suppress("DEPRECATION") - window.decorView.systemUiVisibility = - (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE) + Configuration.ORIENTATION_PORTRAIT -> windowHelper.unsetFullscreen() + Configuration.ORIENTATION_LANDSCAPE -> windowHelper.setFullscreen() } } 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 4a4762fb8..253e6a662 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 @@ -2,18 +2,13 @@ package com.github.libretube.ui.activities import android.app.PictureInPictureParams import android.content.pm.ActivityInfo -import android.graphics.Color import android.media.session.PlaybackState import android.net.Uri import android.os.Build import android.os.Bundle import android.text.format.DateUtils import android.view.View -import android.view.WindowManager import androidx.activity.viewModels -import androidx.core.view.WindowCompat -import androidx.core.view.WindowInsetsCompat -import androidx.core.view.WindowInsetsControllerCompat import com.github.libretube.constants.IntentData import com.github.libretube.databinding.ActivityOfflinePlayerBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding @@ -25,6 +20,7 @@ import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.extensions.setAspectRatio import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.PlayerHelper +import com.github.libretube.util.WindowHelper import com.google.android.exoplayer2.C import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem @@ -49,7 +45,7 @@ class OfflinePlayerActivity : BaseActivity() { private val playerViewModel: PlayerViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { - hideSystemBars() + WindowHelper(this).setFullscreen() requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE @@ -179,32 +175,6 @@ class OfflinePlayerActivity : BaseActivity() { } } - @Suppress("DEPRECATION") - private fun hideSystemBars() { - window?.decorView?.systemUiVisibility = ( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - ) - window.statusBarColor = Color.TRANSPARENT - - window.setFlags( - WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, - WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN - ) - - val windowInsetsController = - WindowCompat.getInsetsController(window, window.decorView) - windowInsetsController.systemBarsBehavior = - WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - windowInsetsController.hide(WindowInsetsCompat.Type.statusBars()) - - supportActionBar?.hide() - - windowInsetsController.systemBarsBehavior = - WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) - } - override fun onResume() { playerViewModel.isFullscreen.value = true super.onResume() 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 9b33f4354..05f9a1bd8 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 @@ -189,7 +189,7 @@ internal class CustomExoPlayerView( override fun hideController() { if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) { // hide all the navigation bars that potentially could have been reopened manually ba the user - (context as? MainActivity)?.setFullscreen() + (context as? MainActivity)?.windowHelper?.setFullscreen() } super.hideController() } diff --git a/app/src/main/java/com/github/libretube/util/WindowHelper.kt b/app/src/main/java/com/github/libretube/util/WindowHelper.kt new file mode 100644 index 000000000..e75c6855d --- /dev/null +++ b/app/src/main/java/com/github/libretube/util/WindowHelper.kt @@ -0,0 +1,49 @@ +package com.github.libretube.util + +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.base.BaseActivity + +class WindowHelper(private val activity: BaseActivity) { + fun setFullscreen() = activity.apply { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + window.attributes.layoutInDisplayCutoutMode = + 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 = + 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 + } + + window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) + } +}