From 4a7de80164ce6ec60dde5ba99d6ac84aa147d8d4 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Mon, 21 Dec 2020 19:42:02 -0700 Subject: [PATCH] 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. --- .../wbrawner/flatscapes/DesertLandscapeService.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/wbrawner/flatscapes/DesertLandscapeService.kt b/app/src/main/java/com/wbrawner/flatscapes/DesertLandscapeService.kt index 8d47711..987804d 100644 --- a/app/src/main/java/com/wbrawner/flatscapes/DesertLandscapeService.kt +++ b/app/src/main/java/com/wbrawner/flatscapes/DesertLandscapeService.kt @@ -6,7 +6,6 @@ import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable import android.service.wallpaper.WallpaperService -import android.util.Log import android.util.TypedValue import android.view.SurfaceHolder import androidx.core.content.ContextCompat @@ -20,6 +19,8 @@ class DesertLandscapeService : WallpaperService() { inner class Engine : WallpaperService.Engine() { private val random = Random(24849010328) private val stars = mutableListOf() + private var lastKnownUiModeConfig: Int? = null + private var holder: SurfaceHolder? = null override fun onCreate(surfaceHolder: SurfaceHolder?) { super.onCreate(surfaceHolder) @@ -34,6 +35,8 @@ class DesertLandscapeService : WallpaperService() { override fun onSurfaceRedrawNeeded(holder: SurfaceHolder?) { super.onSurfaceRedrawNeeded(holder) + this.holder = holder + lastKnownUiModeConfig = applicationContext?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) holder?.draw { canvas -> val canvasRect = Rect(0, 0, canvas.width, canvas.height) 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) { canvas.save() var totalY = 0f