feat: log export to Download

This commit is contained in:
2026-08-17 19:25:43 +08:00
parent d9a0342271
commit 31107fddd5
4 changed files with 63 additions and 0 deletions
@@ -2,6 +2,7 @@ package top.yeij.hearth
import android.app.Activity
import android.app.WallpaperManager
import android.content.ContentValues
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Bitmap
@@ -10,6 +11,7 @@ import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.provider.Settings
import android.text.TextUtils
import android.util.Base64
@@ -215,6 +217,7 @@ class MainActivity : Activity() {
onShowTabPanel = { showNativeTabPanel() },
storage = webAppStorage,
onImportOfflinePackage = { id -> startImportOfflinePackage(id) },
onExportLog = { exportLog() },
)
root = FrameLayout(this)
setContentView(root)
@@ -436,6 +439,34 @@ class MainActivity : Activity() {
// dp to px
private fun dp(v: Int): Int = (v * resources.displayMetrics.density).toInt()
// 导出日志到 Download 目录(读 logcat 本进程 + 过滤 Hearth 相关行)
// Export the log to the Download directory (read logcat, filter Hearth lines)
private fun exportLog(): String {
return try {
val process = Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
val log = process.inputStream.bufferedReader().readText()
val filtered = log.lines()
.filter { it.contains("Hearth") }
.joinToString("\n")
val name = "hearth-log-" + System.currentTimeMillis() + ".txt"
val values = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME, name)
put(MediaStore.Downloads.MIME_TYPE, "text/plain")
put(MediaStore.Downloads.RELATIVE_PATH, "Download/")
}
val uri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values)
?: return "导出失败:无法创建文件"
contentResolver.openOutputStream(uri)?.use { out ->
out.write(filtered.toByteArray())
} ?: return "导出失败:无法写入"
Log.d(TAG, "exportLog: $name (${filtered.length} chars)")
"已导出:Download/$name"
} catch (e: Exception) {
Log.e(TAG, "exportLog failed: ${e.message}")
"导出失败:${e.message}"
}
}
// 触发 SAF 选择 zip 导入离线包
// Trigger SAF to pick a zip for offline-package import
private fun startImportOfflinePackage(id: String) {
@@ -41,6 +41,7 @@ class WebAppRepository(
fun fetchManifest(): List<WebApp> {
val body = http.get(manifestUrl)
Log.d(TAG, "fetchManifest: url=$manifestUrl bodyNull=${body == null}")
if (body != null) {
// 先解析、成功才缓存,避免畸形 body 或空结果被永久缓存
// Parse first, cache only on success (never cache malformed/empty bodies)
@@ -81,6 +81,9 @@ class JsBridge(
// 手动导入离线包回调:由 MainActivity 实现(SAF 选择 zip 后导入)
// Manual offline-package import callback implemented by MainActivity (SAF pick + import)
private val onImportOfflinePackage: ((String) -> Unit)? = null,
// 导出日志回调:由 MainActivity 实现,返回导出结果描述(文件名/错误)
// Export-log callback implemented by MainActivity, returns a result description
private val onExportLog: (() -> String)? = null,
) {
private val gson = com.google.gson.Gson()
@@ -185,6 +188,14 @@ class JsBridge(
@android.webkit.JavascriptInterface
fun hasLocalPackage(id: String): Boolean = storage?.hasLocalPackage(id) ?: false
// 导出日志到 Download 目录,返回结果描述(文件名或错误)
// Export the log to the Download directory, return a result description
@android.webkit.JavascriptInterface
fun exportLog(): String {
Log.d("HearthBridge", "exportLog called")
return onExportLog?.invoke() ?: "日志导出未实现"
}
// 本地离线包版本号(无则空字符串)
// Local offline-package version (empty string when absent)
@android.webkit.JavascriptInterface