mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Style Android shortcut artwork using app colors
This commit is contained in:
1 parent
1bb03d3c74
commit
fb2a1445b1
1 file changed
+61
-17
@@ -4,11 +4,16 @@ import android.content.Context
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.LinearGradient
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RadialGradient
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Shader
|
||||
import android.graphics.Typeface
|
||||
import android.os.Build
|
||||
import android.text.TextPaint
|
||||
import android.text.TextUtils
|
||||
import android.util.AtomicFile
|
||||
import android.util.Log
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
@@ -39,14 +44,61 @@ internal object AndroidSteamApps {
|
||||
|
||||
fun supports(pkg: String): Boolean = Build.VERSION.SDK_INT >= MIN_ANDROID && packagePattern.matches(pkg)
|
||||
|
||||
private data class Artwork(val slot: String, val width: Int, val height: Int, val iconSize: Float, val centerX: Float = 0.5f)
|
||||
private data class Artwork(val slot: String, val width: Int, val height: Int, val iconSize: Float = 0f)
|
||||
|
||||
private val artwork = listOf(
|
||||
Artwork("p", 600, 900, 300f),
|
||||
Artwork("header", 460, 215, 126f),
|
||||
Artwork("hero", 1920, 620, 360f, 0.77f),
|
||||
Artwork("p", 600, 900, 236f),
|
||||
Artwork("header", 460, 215, 104f),
|
||||
Artwork("hero", 1920, 620),
|
||||
)
|
||||
|
||||
private fun iconHue(icon: Bitmap): Float {
|
||||
val bins = FloatArray(12)
|
||||
val hsv = FloatArray(3)
|
||||
for (y in 8 until icon.height step 16) for (x in 8 until icon.width step 16) {
|
||||
val pixel = icon.getPixel(x, y)
|
||||
if (Color.alpha(pixel) < 128) continue
|
||||
Color.colorToHSV(pixel, hsv)
|
||||
if (hsv[1] >= 0.25f && hsv[2] >= 0.3f) {
|
||||
bins[(hsv[0] / 30f).toInt()] += hsv[1] * hsv[1] * hsv[2]
|
||||
}
|
||||
}
|
||||
val strongest = bins.indices.maxByOrNull { bins[it] } ?: return 210f
|
||||
return if (bins[strongest] == 0f) 210f else strongest * 30f + 15f
|
||||
}
|
||||
|
||||
private fun renderArtwork(canvas: Canvas, slot: Artwork, icon: Bitmap, hue: Float, label: String) {
|
||||
val w = slot.width.toFloat()
|
||||
val h = slot.height.toFloat()
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
paint.shader = LinearGradient(0f, h, w, 0f, 0xff111721.toInt(),
|
||||
Color.HSVToColor(floatArrayOf((hue + 335f) % 360f, 0.45f, 0.26f)), Shader.TileMode.CLAMP)
|
||||
canvas.drawPaint(paint)
|
||||
val glow = Color.HSVToColor(floatArrayOf(hue, 0.65f, 0.53f))
|
||||
val wide = w > h
|
||||
paint.shader = RadialGradient(if (wide) w * 0.83f else w * 0.49f, h * 0.36f,
|
||||
if (wide) w * 0.58f else h * 0.62f, glow, glow and 0x00ffffff, Shader.TileMode.CLAMP)
|
||||
canvas.drawPaint(paint)
|
||||
if (slot.iconSize == 0f) return
|
||||
|
||||
paint.shader = null
|
||||
val cx = if (wide) w * 0.19f else w * 0.5f
|
||||
val cy = if (wide) h * 0.5f else h * 0.42f
|
||||
val radius = slot.iconSize * 0.5f
|
||||
canvas.drawBitmap(icon, null, RectF(cx - radius, cy - radius, cx + radius, cy + radius), paint)
|
||||
|
||||
val title = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
color = Color.WHITE
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
textSize = if (wide) 30f else 46f
|
||||
textAlign = if (wide) Paint.Align.LEFT else Paint.Align.CENTER
|
||||
}
|
||||
val titleWidth = w * if (wide) 0.60f else 0.82f
|
||||
val text = TextUtils.ellipsize(label, title, titleWidth, TextUtils.TruncateAt.END).toString()
|
||||
val baseline = if (wide) cy - (title.ascent() + title.descent()) / 2f else h * 0.66f
|
||||
canvas.drawText(text, if (wide) w * 0.35f else w * 0.5f, baseline, title)
|
||||
}
|
||||
|
||||
private fun savePng(file: File, bitmap: Bitmap) {
|
||||
val atomic = AtomicFile(file)
|
||||
val out = atomic.startWrite()
|
||||
@@ -60,7 +112,7 @@ internal object AndroidSteamApps {
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun exportArtwork(context: Context, id: Long, activity: ActivityInfo): JSONObject {
|
||||
private fun exportArtwork(context: Context, id: Long, activity: ActivityInfo, label: String): JSONObject {
|
||||
val pm = context.packageManager
|
||||
val dir = File(context.filesDir, "session/android-steam-art/$id").apply { mkdirs() }
|
||||
val iconFile = File(dir, "icon.png")
|
||||
@@ -68,24 +120,16 @@ internal object AndroidSteamApps {
|
||||
val art = JSONObject().put("icon", iconFile.absolutePath)
|
||||
covers.forEach { (slot, file) -> art.put(slot.slot, file.absolutePath) }
|
||||
val stamp = File(dir, "version")
|
||||
val version = "1:${pm.getPackageInfo(activity.packageName, 0).lastUpdateTime}"
|
||||
val version = "2:${pm.getPackageInfo(activity.packageName, 0).lastUpdateTime}:$label"
|
||||
if (stamp.isFile && stamp.readText() == version && iconFile.isFile && covers.values.all(File::isFile)) return art
|
||||
|
||||
val icon = activity.loadIcon(pm).toBitmap(384, 384, Bitmap.Config.ARGB_8888)
|
||||
savePng(iconFile, icon)
|
||||
val hue = iconHue(icon)
|
||||
for ((slot, file) in covers) {
|
||||
val bitmap = Bitmap.createBitmap(slot.width, slot.height, Bitmap.Config.ARGB_8888)
|
||||
try {
|
||||
val canvas = Canvas(bitmap)
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
paint.shader = LinearGradient(0f, 0f, slot.width.toFloat(), slot.height.toFloat(),
|
||||
0xff202e41.toInt(), 0xff111821.toInt(), Shader.TileMode.CLAMP)
|
||||
canvas.drawPaint(paint)
|
||||
paint.shader = null
|
||||
val cx = slot.width * slot.centerX
|
||||
val cy = slot.height * 0.5f
|
||||
val radius = slot.iconSize * 0.5f
|
||||
canvas.drawBitmap(icon, null, RectF(cx - radius, cy - radius, cx + radius, cy + radius), paint)
|
||||
renderArtwork(Canvas(bitmap), slot, icon, hue, label)
|
||||
savePng(file, bitmap)
|
||||
} finally {
|
||||
bitmap.recycle()
|
||||
@@ -112,7 +156,7 @@ internal object AndroidSteamApps {
|
||||
val entry = JSONObject().put("name", label).put("appid", id)
|
||||
.put("exe", LAUNCHER).put("dir", "/usr/local/bin").put("launch", pkg).put("native", true)
|
||||
if (activity != null) runCatching {
|
||||
entry.put("art", exportArtwork(context, id, activity))
|
||||
entry.put("art", exportArtwork(context, id, activity, label))
|
||||
}.onFailure { Log.w(TAG, "Steam artwork for $pkg", it) }
|
||||
apps.put(entry)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user