Skip to content

Commit

Permalink
Fix bugs with wallpaper not re-drawing when prefs change
Browse files Browse the repository at this point in the history
  • Loading branch information
zadeviggers committed Apr 26, 2023
1 parent 77fd3a6 commit f693f83
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/src/main/java/net/viggers/zade/wallpaper/WallpaperService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f693f83

Please sign in to comment.