Skip to content

Commit

Permalink
Jelly: Refactor immersive mode
Browse files Browse the repository at this point in the history
Also supports insets for edge to edge

Change-Id: I95690601fa5f28ef34d30bcc09909ecd015e95e1
  • Loading branch information
luca020400 committed Nov 2, 2024
1 parent 948becb commit d2a8b78
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
56 changes: 33 additions & 23 deletions app/src/main/java/org/lineageos/jelly/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.preference.PreferenceManager
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar
Expand Down Expand Up @@ -598,35 +601,42 @@ class MainActivity : WebViewExtActivity(), SharedPreferences.OnSharedPreferenceC
historyViewModel.insertOrUpdate(title, url)
}

@Suppress("DEPRECATION")
private fun setImmersiveMode(enable: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(!enable)
window.insetsController?.let {
val flags = WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()
val behavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
if (enable) {
it.hide(flags)
it.systemBarsBehavior = behavior
} else {
it.show(flags)
it.systemBarsBehavior = behavior.inv()
val decorView = window.decorView

if (enable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.let { controller ->
controller.hide(WindowInsets.Type.systemBars())
controller.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
@Suppress("DEPRECATION")
decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
)
}
} else {
var flags = window.decorView.systemUiVisibility
val immersiveModeFlags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
flags = if (enable) {
flags or immersiveModeFlags
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.show(WindowInsets.Type.systemBars())
} else {
flags and immersiveModeFlags.inv()
@Suppress("DEPRECATION")
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
window.decorView.systemUiVisibility = flags
}

ViewCompat.setOnApplyWindowInsetsListener(decorView) { v, insets ->
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.updatePadding(
top = if (enable) systemBarsInsets.top else 0,
bottom = if (enable) systemBarsInsets.bottom else 0,
left = if (enable) systemBarsInsets.left else 0,
right = if (enable) systemBarsInsets.right else 0
)
insets
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2017 The LineageOS Project
SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
Expand Down

0 comments on commit d2a8b78

Please sign in to comment.