mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 21:15:43 +00:00
Use Android app icons for Steam shortcut artwork
This commit is contained in:
1 parent
ce5cd4feae
commit
d662dca21c
2 files changed
+86
-6
No files matched your search
@@ -1,7 +1,17 @@
|
||||
package com.droiddeck.launcher.frontend
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.LinearGradient
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Shader
|
||||
import android.os.Build
|
||||
import android.util.AtomicFile
|
||||
import android.util.Log
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import com.droiddeck.launcher.agent.AgentAdb
|
||||
import com.droiddeck.launcher.core.WirelessAdbFix
|
||||
import org.json.JSONArray
|
||||
@@ -11,6 +21,7 @@ import java.util.zip.CRC32
|
||||
|
||||
internal object AndroidSteamApps {
|
||||
const val MIN_ANDROID = 35
|
||||
private const val TAG = "AndroidSteamApps"
|
||||
private const val LAUNCHER = "/usr/local/bin/droiddeck-android-app"
|
||||
private val packagePattern = Regex("[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)+")
|
||||
|
||||
@@ -28,6 +39,62 @@ 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 val artwork = listOf(
|
||||
Artwork("p", 600, 900, 300f),
|
||||
Artwork("header", 460, 215, 126f),
|
||||
Artwork("hero", 1920, 620, 360f, 0.77f),
|
||||
)
|
||||
|
||||
private fun savePng(file: File, bitmap: Bitmap) {
|
||||
val atomic = AtomicFile(file)
|
||||
val out = atomic.startWrite()
|
||||
try {
|
||||
check(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out))
|
||||
atomic.finishWrite(out)
|
||||
} catch (e: Exception) {
|
||||
atomic.failWrite(out)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun exportArtwork(context: Context, id: Long, activity: ActivityInfo): JSONObject {
|
||||
val pm = context.packageManager
|
||||
val dir = File(context.filesDir, "session/android-steam-art/$id").apply { mkdirs() }
|
||||
val iconFile = File(dir, "icon.png")
|
||||
val covers = artwork.associateWith { File(dir, "${it.slot}.png") }
|
||||
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}"
|
||||
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)
|
||||
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)
|
||||
savePng(file, bitmap)
|
||||
} finally {
|
||||
bitmap.recycle()
|
||||
}
|
||||
}
|
||||
stamp.writeText(version)
|
||||
return art
|
||||
}
|
||||
|
||||
fun sessionEnvironment(context: Context, sessionDir: File): List<String> {
|
||||
if (Build.VERSION.SDK_INT < MIN_ANDROID) return emptyList()
|
||||
val packages = prefs(context).all.keys.sorted()
|
||||
@@ -38,13 +105,16 @@ internal object AndroidSteamApps {
|
||||
val pm = context.packageManager
|
||||
val launch = pm.getLaunchIntentForPackage(pkg) ?: continue
|
||||
val component = launch.component ?: continue
|
||||
val label = runCatching {
|
||||
pm.getActivityInfo(component, 0).loadLabel(pm).toString()
|
||||
}.getOrDefault(pkg).ifBlank { pkg }
|
||||
val activity = runCatching { pm.getActivityInfo(component, 0) }.getOrNull()
|
||||
val label = runCatching { activity?.loadLabel(pm)?.toString() }.getOrNull().orEmpty().ifBlank { pkg }
|
||||
val id = CRC32().apply { update("droiddeck-android-app:$pkg".toByteArray()) }.value or 0x80000000L
|
||||
appIds.add(id)
|
||||
apps.put(JSONObject().put("name", label).put("appid", id)
|
||||
.put("exe", LAUNCHER).put("dir", "/usr/local/bin").put("launch", pkg).put("native", true))
|
||||
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))
|
||||
}.onFailure { Log.w(TAG, "Steam artwork for $pkg", it) }
|
||||
apps.put(entry)
|
||||
}
|
||||
if (apps.length() == 0) return emptyList()
|
||||
val listing = File(sessionDir, "android-apps.json").apply { writeText(apps.toString()) }
|
||||
|
||||
Reference in new issue
Block a user