SystemUiManager.kt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.sduduzog.slimlauncher.utils
  2. import android.annotation.TargetApi
  3. import android.app.Activity
  4. import android.content.Context
  5. import android.content.SharedPreferences
  6. import android.content.res.Configuration
  7. import android.os.Build
  8. import android.util.TypedValue
  9. import android.view.View
  10. import android.view.Window
  11. import android.view.WindowInsets
  12. import android.view.WindowInsetsController
  13. import android.view.WindowManager
  14. import androidx.annotation.RequiresApi
  15. import androidx.appcompat.app.AppCompatActivity
  16. import com.sduduzog.slimlauncher.R
  17. import dagger.Module
  18. import dagger.Provides
  19. import dagger.hilt.InstallIn
  20. import dagger.hilt.android.components.ActivityComponent
  21. import dagger.hilt.android.qualifiers.ActivityContext
  22. @Module
  23. @InstallIn(ActivityComponent::class)
  24. open class SystemUiManager internal constructor(internal val context: Context) {
  25. internal val window: Window = (context as Activity).window
  26. internal val settings: SharedPreferences = context.getSharedPreferences(
  27. context.getString(R.string.prefs_settings),
  28. AppCompatActivity.MODE_PRIVATE
  29. )
  30. companion object {
  31. @Provides
  32. fun createInstance(@ActivityContext context: Context): SystemUiManager {
  33. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  34. (context as Activity).window.attributes.layoutInDisplayCutoutMode =
  35. WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
  36. }
  37. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  38. return LSystemUiManager(context)
  39. }
  40. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  41. return MSystemUiManager(context)
  42. }
  43. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
  44. return OSystemUiManager(context)
  45. }
  46. return SystemUiManager(context)
  47. }
  48. }
  49. @TargetApi(Build.VERSION_CODES.R)
  50. open fun setSystemUiVisibility() {
  51. val insetsController = window.insetsController
  52. if (isSystemUiHidden()) {
  53. insetsController?.hide(WindowInsets.Type.statusBars())
  54. } else {
  55. insetsController?.show(WindowInsets.Type.statusBars())
  56. }
  57. if (isLightModeTheme()) {
  58. insetsController?.setSystemBarsAppearance(
  59. WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
  60. WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
  61. )
  62. insetsController?.setSystemBarsAppearance(
  63. WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
  64. WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
  65. )
  66. } else {
  67. insetsController?.setSystemBarsAppearance(
  68. 0,
  69. WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
  70. )
  71. insetsController?.setSystemBarsAppearance(
  72. 0,
  73. WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
  74. )
  75. }
  76. }
  77. @TargetApi(Build.VERSION_CODES.O)
  78. open fun setSystemUiColors() {
  79. val primaryColor = getPrimaryColor()
  80. window.statusBarColor = primaryColor
  81. window.navigationBarColor = primaryColor
  82. }
  83. internal fun getPrimaryColor(): Int {
  84. val primaryColor = TypedValue()
  85. context.theme.resolveAttribute(R.attr.colorPrimary, primaryColor, true)
  86. return primaryColor.data
  87. }
  88. internal fun isSystemUiHidden(): Boolean {
  89. return settings.getBoolean(
  90. context.getString(R.string.prefs_settings_key_toggle_status_bar),
  91. false
  92. )
  93. }
  94. internal fun isLightModeTheme(): Boolean {
  95. val theme = settings.getInt(context.getString(R.string.prefs_settings_key_theme), 0)
  96. val uiMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
  97. return listOf(
  98. 6,
  99. 3,
  100. 5
  101. ).contains(theme) || (theme == 0 && uiMode == Configuration.UI_MODE_NIGHT_NO)
  102. }
  103. private open class OSystemUiManager(context: Context) : SystemUiManager(context) {
  104. @RequiresApi(Build.VERSION_CODES.O)
  105. override fun setSystemUiVisibility() {
  106. window.decorView.systemUiVisibility =
  107. getLightUiBarFlags() or getToggleStatusBarFlags()
  108. }
  109. @RequiresApi(Build.VERSION_CODES.O)
  110. open fun getLightUiBarFlags(): Int {
  111. return if (isLightModeTheme()) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR else 0
  112. }
  113. private fun getToggleStatusBarFlags(): Int {
  114. return View.SYSTEM_UI_FLAG_LAYOUT_STABLE or if (isSystemUiHidden()) View.SYSTEM_UI_FLAG_FULLSCREEN else 0
  115. }
  116. }
  117. private open class MSystemUiManager(context: Context) : OSystemUiManager(context) {
  118. @RequiresApi(Build.VERSION_CODES.M)
  119. override fun setSystemUiColors() {
  120. window.statusBarColor = getPrimaryColor()
  121. }
  122. @RequiresApi(Build.VERSION_CODES.M)
  123. override fun getLightUiBarFlags(): Int {
  124. return if (isLightModeTheme()) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
  125. }
  126. }
  127. private class LSystemUiManager(context: Context) : MSystemUiManager(context) {
  128. override fun setSystemUiColors() {}
  129. override fun getLightUiBarFlags(): Int {
  130. return 0
  131. }
  132. }
  133. }