mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
feat: desktop and scrolling wall live wallpapers (#2012)
* feat: desktop and scrolling wall live wallpapers selectable from Android settings * fix: render the wall wallpaper off the main thread and give it its own thumbnail * fix: serialize wall wallpaper visibility and wait out in-flight draws on surface destroy * fix: restart the wall loop on surface recreation and keep queued visibility changes * fix: keep the wall loop alive across a failed canvas lock and finish redraws before returning * fix: post the wall redraw asynchronously on surface change * fix: redraw the desktop wallpaper logo from the real mark and show game covers on the wall * chore: mount the git dir in build containers and seed new worktrees from the main checkout * fix: decode wall covers as RGB_565 and seed worktree assets only into empty dirs * feat: drop the desktop wallpaper and add frame rate and speed settings to the wall * feat: wall settings over the live wallpaper with a match-screen rate, and drop the center vignette * perf: pace the wall on vsync and draw it on a hardware canvas * fix: stage worktree seed copies and publish them only after they succeed * fix: point the wall settings link at the activity class so the picker shows the gear * fix: dedupe wall covers by app and space repeats like the in-app wall, keep the frame loop alive across surface gaps * fix: show the picker preview behind wall settings and drop the display-rate lookup * fix: stop wall frame callbacks when the surface goes and only replace an empty seed destination * fix: skip a seed when the destination reappears before the staged copy is moved in
This commit is contained in:
1 parent
0110dc3c0f
commit
430af5e22d
11 files changed
+406
-3
No files matched your search
@@ -0,0 +1,223 @@
|
||||
package com.droiddeck.launcher.wallpaper
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Path
|
||||
import android.graphics.RectF
|
||||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import android.os.SystemClock
|
||||
import android.service.wallpaper.WallpaperService
|
||||
import android.view.Choreographer
|
||||
import android.view.SurfaceHolder
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import com.droiddeck.launcher.frontend.Library
|
||||
import com.droiddeck.launcher.ui.Themes
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
|
||||
class SteamWallService : WallpaperService() {
|
||||
override fun onCreateEngine(): Engine = WallEngine()
|
||||
|
||||
private inner class WallEngine : Engine() {
|
||||
private val thread = HandlerThread("steam-wall").also { it.start() }
|
||||
private val handler = Handler(thread.looper)
|
||||
private val fill = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE }
|
||||
private val rect = RectF()
|
||||
private val clip = Path()
|
||||
private val art = Paint(Paint.FILTER_BITMAP_FLAG or Paint.ANTI_ALIAS_FLAG)
|
||||
@Volatile private var covers: List<Bitmap> = emptyList()
|
||||
private val lock = Any()
|
||||
private val redraw = Runnable { draw() }
|
||||
private var visible = false
|
||||
private var surfaceLive = false
|
||||
private var lastFrame = 0L
|
||||
private var phase = 0f
|
||||
private var choreographer: Choreographer? = null
|
||||
private var lastDrawNanos = 0L
|
||||
private val tick = object : Choreographer.FrameCallback {
|
||||
override fun doFrame(frameTimeNanos: Long) {
|
||||
if (!visible) return
|
||||
val interval = if (WallPrefs.fps(applicationContext) == WallPrefs.MATCH_SCREEN) 0L else 1_000_000_000L / framesPerSecond()
|
||||
if (frameTimeNanos - lastDrawNanos >= interval - SLACK_NANOS) {
|
||||
lastDrawNanos = frameTimeNanos
|
||||
runCatching { draw() }
|
||||
}
|
||||
if (synchronized(lock) { surfaceLive }) choreographer?.postFrameCallback(this)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTicking() {
|
||||
val c = choreographer ?: Choreographer.getInstance().also { choreographer = it }
|
||||
c.removeFrameCallback(tick)
|
||||
c.postFrameCallback(tick)
|
||||
}
|
||||
|
||||
private fun stopTicking() {
|
||||
choreographer?.removeFrameCallback(tick)
|
||||
}
|
||||
|
||||
private fun framesPerSecond(): Int = WallPrefs.fps(applicationContext)
|
||||
|
||||
override fun onCreate(surfaceHolder: SurfaceHolder) {
|
||||
handler.post { covers = loadCovers() }
|
||||
}
|
||||
|
||||
private fun loadCovers(): List<Bitmap> {
|
||||
val d = resources.displayMetrics.density
|
||||
val targetW = (112f * d).toInt()
|
||||
val files = runCatching { Library.steamLibraryArt(applicationContext).distinctBy { it.first }.shuffled().take(MAX_COVERS) }
|
||||
.getOrDefault(emptyList())
|
||||
return files.mapNotNull { (_, file) ->
|
||||
runCatching {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(file.path, bounds)
|
||||
var sample = 1
|
||||
while (bounds.outWidth / (sample * 2) >= targetW) sample *= 2
|
||||
BitmapFactory.decodeFile(file.path, BitmapFactory.Options().apply {
|
||||
inSampleSize = sample
|
||||
inPreferredConfig = Bitmap.Config.RGB_565
|
||||
})
|
||||
}.getOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onVisibilityChanged(visible: Boolean) {
|
||||
handler.post {
|
||||
this.visible = visible
|
||||
stopTicking()
|
||||
if (visible) {
|
||||
lastFrame = SystemClock.uptimeMillis()
|
||||
startTicking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSurfaceCreated(holder: SurfaceHolder) {
|
||||
synchronized(lock) { surfaceLive = true }
|
||||
handler.post {
|
||||
stopTicking()
|
||||
if (visible) startTicking()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSurfaceRedrawNeeded(holder: SurfaceHolder) {
|
||||
redrawNow()
|
||||
}
|
||||
|
||||
override fun onSurfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
||||
handler.removeCallbacks(redraw)
|
||||
handler.post(redraw)
|
||||
}
|
||||
|
||||
override fun onSurfaceDestroyed(holder: SurfaceHolder) {
|
||||
stopTicking()
|
||||
handler.removeCallbacks(redraw)
|
||||
synchronized(lock) { surfaceLive = false }
|
||||
handler.post { stopTicking() }
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
synchronized(lock) { surfaceLive = false }
|
||||
handler.post {
|
||||
visible = false
|
||||
stopTicking()
|
||||
handler.removeCallbacks(redraw)
|
||||
}
|
||||
thread.quitSafely()
|
||||
covers = emptyList()
|
||||
}
|
||||
|
||||
private fun redrawNow() {
|
||||
val done = CountDownLatch(1)
|
||||
handler.removeCallbacks(redraw)
|
||||
handler.post(redraw)
|
||||
handler.post { done.countDown() }
|
||||
done.await(REDRAW_WAIT_MS, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
|
||||
private fun draw() {
|
||||
synchronized(lock) {
|
||||
if (!surfaceLive || !surfaceHolder.surface.isValid) return
|
||||
val canvas = surfaceHolder.surface.lockHardwareCanvas() ?: return
|
||||
try {
|
||||
render(canvas, canvas.width.toFloat(), canvas.height.toFloat())
|
||||
} finally {
|
||||
surfaceHolder.surface.unlockCanvasAndPost(canvas)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun render(canvas: Canvas, w: Float, h: Float) {
|
||||
val pal = Themes.current(applicationContext)
|
||||
val bg = pal.background.toArgb()
|
||||
val d = resources.displayMetrics.density
|
||||
val capW = 112f * d
|
||||
val capH = 168f * d
|
||||
val gap = 14f * d
|
||||
val radius = 10f * d
|
||||
val tilt = Math.toRadians(TILT.toDouble())
|
||||
val cs = cos(tilt).toFloat()
|
||||
val sn = sin(tilt).toFloat()
|
||||
val wallW = (w * cs + h * sn) * 1.1f
|
||||
val wallH = maxOf(h * 2.4f, (w * sn + h * cs) * 1.1f)
|
||||
val columns = ((wallW + gap) / (capW + gap)).toInt() + 1
|
||||
val perRun = ((wallH + gap) / (capH + gap)).toInt() + 1
|
||||
val run = (capH + gap) * perRun
|
||||
val now = SystemClock.uptimeMillis()
|
||||
if (lastFrame != 0L) phase = (phase + (now - lastFrame) * WallPrefs.speed(applicationContext) / DRIFT_MS) % 1f
|
||||
lastFrame = now
|
||||
val shift = run * phase
|
||||
val px = w / 2f
|
||||
val py = h / 2f
|
||||
val left = px - wallW / 2f
|
||||
val top = py - wallH / 2f
|
||||
|
||||
val deck = covers
|
||||
canvas.drawColor(bg)
|
||||
fill.color = pal.surface.toArgb()
|
||||
stroke.color = pal.line.toArgb()
|
||||
stroke.strokeWidth = d
|
||||
canvas.save()
|
||||
canvas.rotate(-TILT, px, py)
|
||||
for (c in 0 until columns) {
|
||||
val x = left + c * (capW + gap)
|
||||
val dy = if (c % 2 == 0) -shift else shift - run
|
||||
for (i in 0 until perRun * 2) {
|
||||
val y = top + i * (capH + gap) + dy
|
||||
val lx = x + capW / 2 - px
|
||||
val ly = y + capH / 2 - py
|
||||
val sx = px + lx * cs + ly * sn
|
||||
val sy = py - lx * sn + ly * cs
|
||||
if (sx < -capH || sx > w + capH || sy < -capH || sy > h + capH) continue
|
||||
rect.set(x, y, x + capW, y + capH)
|
||||
canvas.drawRoundRect(rect, radius, radius, fill)
|
||||
if (deck.isNotEmpty()) {
|
||||
val cover = deck[(c * 5 + (i % perRun) * 3) % deck.size]
|
||||
clip.rewind()
|
||||
clip.addRoundRect(rect, radius, radius, Path.Direction.CW)
|
||||
canvas.save()
|
||||
canvas.clipPath(clip)
|
||||
canvas.drawBitmap(cover, null, rect, art)
|
||||
canvas.restore()
|
||||
}
|
||||
canvas.drawRoundRect(rect, radius, radius, stroke)
|
||||
}
|
||||
}
|
||||
canvas.restore()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val MAX_COVERS = 48
|
||||
const val TILT = 13f
|
||||
const val DRIFT_MS = 50_000L
|
||||
const val REDRAW_WAIT_MS = 200L
|
||||
const val SLACK_NANOS = 2_000_000L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.droiddeck.launcher.wallpaper
|
||||
|
||||
import android.content.Context
|
||||
|
||||
object WallPrefs {
|
||||
val FPS_CHOICES = listOf(15, 30, 60, MATCH_SCREEN)
|
||||
val SPEED_CHOICES = listOf(0.5f, 1f, 2f)
|
||||
const val MATCH_SCREEN = 0
|
||||
private const val FILE = "wall_wallpaper"
|
||||
private const val KEY_FPS = "fps"
|
||||
private const val KEY_SPEED = "speed"
|
||||
|
||||
private fun prefs(context: Context) = context.getSharedPreferences(FILE, Context.MODE_PRIVATE)
|
||||
|
||||
fun fps(context: Context): Int = prefs(context).getInt(KEY_FPS, 30).let { if (it == MATCH_SCREEN) it else it.coerceIn(5, 120) }
|
||||
|
||||
fun speed(context: Context): Float = prefs(context).getFloat(KEY_SPEED, 1f).coerceIn(0.1f, 4f)
|
||||
|
||||
fun setFps(context: Context, value: Int) = prefs(context).edit().putInt(KEY_FPS, value).apply()
|
||||
|
||||
fun setSpeed(context: Context, value: Float) = prefs(context).edit().putFloat(KEY_SPEED, value).apply()
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.droiddeck.launcher.wallpaper
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableFloatStateOf
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.core.AppUiPrefs
|
||||
import com.droiddeck.launcher.ui.DroidDeckTheme
|
||||
|
||||
class WallpaperSettingsActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
DroidDeckTheme(appScale = AppUiPrefs.scale(this)) {
|
||||
val ink = MaterialTheme.colorScheme.onBackground
|
||||
var fps by remember { mutableIntStateOf(WallPrefs.fps(this)) }
|
||||
var speed by remember { mutableFloatStateOf(WallPrefs.speed(this)) }
|
||||
Column(Modifier.fillMaxSize().padding(24.dp), verticalArrangement = Arrangement.Bottom) {
|
||||
Column(
|
||||
Modifier.background(MaterialTheme.colorScheme.background.copy(alpha = 0.88f), RoundedCornerShape(20.dp))
|
||||
.verticalScroll(rememberScrollState()).padding(20.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
Text(getString(R.string.steam_wall_label), style = MaterialTheme.typography.titleLarge, color = ink)
|
||||
Text(getString(R.string.wall_settings_fps), style = MaterialTheme.typography.titleMedium, color = ink)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
WallPrefs.FPS_CHOICES.forEach { choice ->
|
||||
FilterChip(
|
||||
selected = fps == choice,
|
||||
onClick = { fps = choice; WallPrefs.setFps(this@WallpaperSettingsActivity, choice) },
|
||||
label = { Text(if (choice == WallPrefs.MATCH_SCREEN) getString(R.string.wall_settings_match) else "$choice") },
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(getString(R.string.wall_settings_speed), style = MaterialTheme.typography.titleMedium, color = ink)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
WallPrefs.SPEED_CHOICES.forEach { choice ->
|
||||
FilterChip(
|
||||
selected = speed == choice,
|
||||
onClick = { speed = choice; WallPrefs.setSpeed(this@WallpaperSettingsActivity, choice) },
|
||||
label = { Text(if (choice == 1f) "1x" else "${choice.toString().removeSuffix(".0")}x") },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user