From f693f831a53b327532aeb92e079573e27db1fa78 Mon Sep 17 00:00:00 2001 From: Zade Viggers Date: Wed, 26 Apr 2023 22:48:50 +1200 Subject: [PATCH] Fix bugs with wallpaper not re-drawing when prefs change --- .../viggers/zade/wallpaper/WallpaperService.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/viggers/zade/wallpaper/WallpaperService.kt b/app/src/main/java/net/viggers/zade/wallpaper/WallpaperService.kt index bfb89e4..c27b367 100644 --- a/app/src/main/java/net/viggers/zade/wallpaper/WallpaperService.kt +++ b/app/src/main/java/net/viggers/zade/wallpaper/WallpaperService.kt @@ -31,8 +31,10 @@ class WallpaperService : WallpaperService() { // Preferences private val onSharedPreferenceChanged: OnSharedPreferenceChangeListener = OnSharedPreferenceChangeListener { newPrefs, _ -> - loadPreferences(newPrefs) Log.v("ZV-Wallpaper:Engine", "Preferences changed") + loadPreferences(newPrefs) + // Re-render wallpaper, in case thing such as background colour were changed. + drawWallpaper() } // By default we just want all the shape types. @@ -148,7 +150,8 @@ class WallpaperService : WallpaperService() { fun clearAllShapes() { shapes.clear() - drawShapes() + // Re-draw the wallpaper with the shapes gone + drawWallpaper() } private fun loadPreferences(prefs: SharedPreferences) { @@ -215,17 +218,21 @@ class WallpaperService : WallpaperService() { visible = isVisible if (isVisible) { handler.post(drawRunner) + // Re-draw everything when it becomes visible + drawWallpaper() } else { handler.removeCallbacks(drawRunner) } } + override fun onSurfaceDestroyed(holder: SurfaceHolder) { super.onSurfaceDestroyed(holder) visible = false handler.removeCallbacks(drawRunner) } + // Getting the surface to stick the canvas to draw on override fun onSurfaceChanged( holder: SurfaceHolder, format: Int, width: Int, height: Int @@ -235,6 +242,7 @@ class WallpaperService : WallpaperService() { super.onSurfaceChanged(holder, format, width, height) } + // Shape drawing override fun onTouchEvent(event: MotionEvent) { if (enableTouchInteraction) { val x = event.x @@ -271,9 +279,11 @@ class WallpaperService : WallpaperService() { } shapes.add(shape) - drawShapes() + // Re-draw the wallpaper + drawWallpaper() } + // Ticks for random shape spawning private fun drawTick() { if (randomShapeSpawningEnabled) { var shouldAddShape = true @@ -319,7 +329,7 @@ class WallpaperService : WallpaperService() { } // Surface view requires that all elements are drawn completely - private fun drawShapes() { + private fun drawWallpaper() { var canvas: Canvas? = null val holder = surfaceHolder try {