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) {