[UI] Using WindowInsetControllerCompat for setting Light Status bar, Light navigation bar & Immersive mode and removed deprecated code

This commit is contained in:
Prathamesh More 2021-11-28 22:18:17 +05:30
parent 1e85bcf388
commit 3df5a77ee8
2 changed files with 22 additions and 25 deletions

View file

@ -25,6 +25,8 @@ import android.view.WindowManager
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode
import androidx.core.os.ConfigurationCompat import androidx.core.os.ConfigurationCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import code.name.monkey.appthemehelper.ATH import code.name.monkey.appthemehelper.ATH
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.common.ATHToolbarActivity import code.name.monkey.appthemehelper.common.ATHToolbarActivity
@ -192,16 +194,20 @@ abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
} }
private fun setImmersiveFullscreen() { private fun setImmersiveFullscreen() {
val flags =
(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)
if (PreferenceUtil.isFullScreenMode) { if (PreferenceUtil.isFullScreenMode) {
window.decorView.systemUiVisibility = flags WindowInsetsControllerCompat(window, window.decorView).apply {
systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.systemBars())
}
} }
} }
private fun exitFullscreen() { private fun exitFullscreen() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE WindowInsetsControllerCompat(window, window.decorView).apply {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
show(WindowInsetsCompat.Type.systemBars())
}
} }
override fun run() { override fun run() {

View file

@ -6,9 +6,9 @@ import android.app.ActivityManager
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import android.view.View import android.view.View
import android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.core.view.WindowInsetsControllerCompat
import code.name.monkey.appthemehelper.util.ColorUtil import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.TintHelper import code.name.monkey.appthemehelper.util.TintHelper
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
@ -27,29 +27,20 @@ object ATH {
} }
fun setLightStatusbar(activity: Activity, enabled: Boolean) { fun setLightStatusbar(activity: Activity, enabled: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.window.apply {
val decorView = activity.window.decorView WindowInsetsControllerCompat(
val systemUiVisibility = decorView.systemUiVisibility this,
if (enabled) { decorView
decorView.systemUiVisibility = ).isAppearanceLightStatusBars = enabled
systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
decorView.systemUiVisibility =
systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
} }
} }
fun setLightNavigationbar(activity: Activity, enabled: Boolean) { fun setLightNavigationbar(activity: Activity, enabled: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { activity.window?.apply {
val decorView = activity.window.decorView WindowInsetsControllerCompat(
var systemUiVisibility = decorView.systemUiVisibility this,
systemUiVisibility = if (enabled) { decorView
systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR ).isAppearanceLightNavigationBars = enabled
} else {
systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
}
decorView.systemUiVisibility = systemUiVisibility
} }
} }