Improve handling of background dark mode changes

The wallpaper doesn't currently update if the dark mode setting changes while the device is asleep. I'm hoping this workaround solves that problem but we'll see.
This commit is contained in:
William Brawner 2020-12-21 19:42:02 -07:00
parent 94149bf59b
commit 4a7de80164

View file

@ -6,7 +6,6 @@ import android.graphics.Canvas
import android.graphics.Rect import android.graphics.Rect
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.service.wallpaper.WallpaperService import android.service.wallpaper.WallpaperService
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.SurfaceHolder import android.view.SurfaceHolder
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -20,6 +19,8 @@ class DesertLandscapeService : WallpaperService() {
inner class Engine : WallpaperService.Engine() { inner class Engine : WallpaperService.Engine() {
private val random = Random(24849010328) private val random = Random(24849010328)
private val stars = mutableListOf<Drawable>() private val stars = mutableListOf<Drawable>()
private var lastKnownUiModeConfig: Int? = null
private var holder: SurfaceHolder? = null
override fun onCreate(surfaceHolder: SurfaceHolder?) { override fun onCreate(surfaceHolder: SurfaceHolder?) {
super.onCreate(surfaceHolder) super.onCreate(surfaceHolder)
@ -34,6 +35,8 @@ class DesertLandscapeService : WallpaperService() {
override fun onSurfaceRedrawNeeded(holder: SurfaceHolder?) { override fun onSurfaceRedrawNeeded(holder: SurfaceHolder?) {
super.onSurfaceRedrawNeeded(holder) super.onSurfaceRedrawNeeded(holder)
this.holder = holder
lastKnownUiModeConfig = applicationContext?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)
holder?.draw { canvas -> holder?.draw { canvas ->
val canvasRect = Rect(0, 0, canvas.width, canvas.height) val canvasRect = Rect(0, 0, canvas.width, canvas.height)
ContextCompat.getDrawable(applicationContext, R.drawable.wallpaper_sky) ContextCompat.getDrawable(applicationContext, R.drawable.wallpaper_sky)
@ -58,6 +61,13 @@ class DesertLandscapeService : WallpaperService() {
} }
} }
override fun onVisibilityChanged(visible: Boolean) {
super.onVisibilityChanged(visible)
if (applicationContext?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) != lastKnownUiModeConfig) {
onSurfaceRedrawNeeded(this.holder)
}
}
private fun drawStars(canvas: Canvas) { private fun drawStars(canvas: Canvas) {
canvas.save() canvas.save()
var totalY = 0f var totalY = 0f