fix: resolve relative manifest paths, base64 demo icon

This commit is contained in:
2026-08-17 19:44:53 +08:00
parent a35a5018b7
commit 5498c8ed0b
@@ -1,5 +1,6 @@
package top.yeij.hearth.webapp
import android.util.Base64
import android.util.Log
import com.google.gson.Gson
import top.yeij.hearth.cache.CacheManager
@@ -69,14 +70,16 @@ class WebAppRepository(
// 内置示例 webapp(打包在 assets 内,无需服务器)
// Builtin demo webapp (bundled in assets, no server required)
private fun builtinWebApps(): List<WebApp> {
val svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'>" +
"<rect width='24' height='24' rx='6' fill='#ff6900'/>" +
"<text x='12' y='17' font-size='13' text-anchor='middle' fill='white'>H</text></svg>"
val icon = "data:image/svg+xml;base64," +
Base64.encodeToString(svg.toByteArray(Charsets.UTF_8), Base64.NO_WRAP)
return listOf(
WebApp(
id = "demo",
name = "示例应用",
icon = "data:image/svg+xml;utf8," +
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'>" +
"<rect width='24' height='24' rx='6' fill='%23ff6900'/>" +
"<text x='12' y='17' font-size='13' text-anchor='middle' fill='white'>H</text></svg>",
icon = icon,
url = "file:///android_asset/webapps/demo/index.html",
)
)
@@ -103,7 +106,13 @@ class WebAppRepository(
val offlineMap = map["offline"] as? Map<*, *>
val offline = offlineMap?.get("package") as? String
val offlineVersion = offlineMap?.get("version") as? String
WebApp(id, name, icon, url, offline, offlineVersion)
WebApp(
id, name,
resolvePath(icon),
resolvePath(url),
offline?.let { resolvePath(it) },
offlineVersion,
)
}
}
@@ -111,4 +120,16 @@ class WebAppRepository(
private const val TAG = "HearthWebApp"
private const val KEY = "webapp-manifest"
}
// 相对路径拼服务器根;绝对 URL / data URI / file URI 直接返回
// Resolve a relative path against the server root; return absolute URL / data
// URI / file URI as-is
private fun resolvePath(path: String): String {
if (path.startsWith("http://") || path.startsWith("https://") ||
path.startsWith("data:") || path.startsWith("file:")
) {
return path
}
return serverUrl.trimEnd('/') + "/" + path.trimStart('/')
}
}