|
1 | 1 | package com.github.odaridavid.designpatterns.base |
2 | 2 |
|
| 3 | +import android.content.Context |
| 4 | +import android.content.res.Configuration |
3 | 5 | import android.os.Build |
4 | | -import android.os.Bundle |
| 6 | +import android.os.PowerManager |
5 | 7 | import android.view.View |
| 8 | +import androidx.annotation.RequiresApi |
6 | 9 | import androidx.appcompat.app.AppCompatActivity |
| 10 | +import androidx.preference.PreferenceManager |
7 | 11 | import com.github.odaridavid.designpatterns.R |
8 | | -import com.github.odaridavid.designpatterns.helpers.SdkUtils |
| 12 | +import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionFrom |
| 13 | +import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionUntil |
| 14 | +import com.github.odaridavid.designpatterns.helpers.ThemeUtils |
9 | 15 |
|
10 | 16 | abstract class BaseActivity : AppCompatActivity() { |
11 | 17 |
|
12 | | - override fun onCreate(savedInstanceState: Bundle?) { |
13 | | - matchStatusBarWithBackground() |
14 | | - super.onCreate(savedInstanceState) |
| 18 | + private val powerManager: PowerManager by lazy { |
| 19 | + getSystemService(Context.POWER_SERVICE) as PowerManager |
15 | 20 | } |
16 | 21 |
|
17 | | - private fun matchStatusBarWithBackground() { |
18 | | - if (SdkUtils.versionFrom(Build.VERSION_CODES.M)) { |
| 22 | + override fun onResume() { |
| 23 | + matchSystemBarsWithBackground() |
| 24 | + super.onResume() |
| 25 | + } |
| 26 | + |
| 27 | + private fun matchSystemBarsWithBackground() { |
| 28 | + val sp = PreferenceManager.getDefaultSharedPreferences(baseContext) |
| 29 | + val theme = sp.getString(getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT) |
| 30 | + if (versionFrom(Build.VERSION_CODES.M)) |
| 31 | + handleSystemBars(theme) |
| 32 | + } |
| 33 | + |
| 34 | + @RequiresApi(Build.VERSION_CODES.M) |
| 35 | + private fun handleSystemBars(theme: String?) { |
| 36 | + when (theme) { |
| 37 | + ThemeUtils.THEME_LIGHT -> setLightSystemBars() |
| 38 | + ThemeUtils.THEME_DARK -> setDarkSystemBars() |
| 39 | + ThemeUtils.THEME_SYSTEM -> { |
| 40 | + if (versionUntil(Build.VERSION_CODES.P)) { |
| 41 | + onPowerSaverModeChange() |
| 42 | + } else { |
| 43 | + onUiModeConfigChange() |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @RequiresApi(Build.VERSION_CODES.M) |
| 50 | + private fun onPowerSaverModeChange() { |
| 51 | + if (powerManager.isPowerSaveMode) |
| 52 | + setDarkSystemBars() |
| 53 | + else |
| 54 | + setLightSystemBars() |
| 55 | + } |
| 56 | + |
| 57 | + @RequiresApi(Build.VERSION_CODES.M) |
| 58 | + private fun onUiModeConfigChange() { |
| 59 | + when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { |
| 60 | + Configuration.UI_MODE_NIGHT_NO -> { |
| 61 | + setLightSystemBars() |
| 62 | + } |
| 63 | + Configuration.UI_MODE_NIGHT_YES -> { |
| 64 | + setDarkSystemBars() |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @RequiresApi(Build.VERSION_CODES.M) |
| 70 | + private fun setLightSystemBars() { |
| 71 | + if (versionFrom(Build.VERSION_CODES.O)) { |
| 72 | + window.decorView.systemUiVisibility = |
| 73 | + View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
| 74 | + } else { |
19 | 75 | window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR |
20 | | - window.statusBarColor = getColor(R.color.colorPrimary) |
21 | 76 | } |
| 77 | + window.statusBarColor = getColor(android.R.color.background_light) |
| 78 | + window.navigationBarColor = getColor(android.R.color.background_light) |
22 | 79 | } |
23 | 80 |
|
| 81 | + @RequiresApi(Build.VERSION_CODES.M) |
| 82 | + private fun setDarkSystemBars() { |
| 83 | + window.statusBarColor = getColor(android.R.color.background_dark) |
| 84 | + window.navigationBarColor = getColor(android.R.color.background_dark) |
| 85 | + } |
24 | 86 | } |
0 commit comments