fix: "Jetzt senden" schickt immer einen Bericht (auch ohne neue Eintraege)

Nach frischer Installation ist das Diagnose-Log leer; der Knopf meldete dann nur
"Keine neuen Eintraege". Jetzt geht ein Bericht mit Vermerk "manuell gesendet" raus.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-12 23:01:57 +01:00
co-authored by Claude Opus 5.5
parent 3b8918d45d
commit 448b4b8edf
4 changed files with 27 additions and 5 deletions
@@ -72,6 +72,13 @@ object DiagReport {
return listOf("absturz", "exception", "error", "fehler", "fehlgeschlagen", "failed").any { it in lower } return listOf("absturz", "exception", "error", "fehler", "fehlgeschlagen", "failed").any { it in lower }
} }
/** Vermerk zum Bericht: manuell gesendet (ggf. ohne neue Einträge) oder leer. */
fun note(manual: Boolean, noNewLines: Boolean): String = when {
manual && noNewLines -> "manuell gesendet (keine neuen Einträge)"
manual -> "manuell gesendet"
else -> ""
}
fun build(installId: String, version: String, androidVersion: String, device: String, lines: List<String>, note: String = ""): JSONObject = fun build(installId: String, version: String, androidVersion: String, device: String, lines: List<String>, note: String = ""): JSONObject =
JSONObject() JSONObject()
.put("app", "android") .put("app", "android")
@@ -85,17 +85,21 @@ object Diagnostics {
if (!isEnabled(context)) return if (!isEnabled(context)) return
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
if (now - prefs(context).getLong(KEY_LAST_TRY, 0) < INTERVAL_MS) return if (now - prefs(context).getLong(KEY_LAST_TRY, 0) < INTERVAL_MS) return
send(context) { } send(context, manual = false) { }
} }
/** Senden; [done] bekommt (null = nichts Neues, true/false = Erfolg) auf dem Hauptthread. */ /**
fun send(context: Context, done: (Boolean?) -> Unit) { * Senden; [done] bekommt (null = nichts Neues, true/false = Erfolg) auf dem Hauptthread.
* [manual] ("Jetzt senden"): geht auch ohne neue Logzeilen raus, dann mit dem Vermerk
* "manuell gesendet" (Version, Gerät und Kennung reichen zum Prüfen).
*/
fun send(context: Context, manual: Boolean, done: (Boolean?) -> Unit) {
val ctx = context.applicationContext val ctx = context.applicationContext
Thread { Thread {
val prefs = prefs(ctx) val prefs = prefs(ctx)
val text = runCatching { logFile(ctx).readText() }.getOrDefault("") val text = runCatching { logFile(ctx).readText() }.getOrDefault("")
val collected = DiagReport.collect(text, prefs.getLong(KEY_LAST_SENT, 0)) val collected = DiagReport.collect(text, prefs.getLong(KEY_LAST_SENT, 0))
if (collected.lines.isEmpty()) { if (collected.lines.isEmpty() && !manual) {
main.post { done(null) } main.post { done(null) }
return@Thread return@Thread
} }
@@ -106,6 +110,7 @@ object Diagnostics {
Build.VERSION.RELEASE ?: "?", Build.VERSION.RELEASE ?: "?",
"${Build.MANUFACTURER} ${Build.MODEL}".take(80), "${Build.MANUFACTURER} ${Build.MODEL}".take(80),
collected.lines, collected.lines,
DiagReport.note(manual, collected.lines.isEmpty()),
) )
val ok = post("${BuildConfig.JAM_SERVER}/api/diag", body.toString()) val ok = post("${BuildConfig.JAM_SERVER}/api/diag", body.toString())
if (ok) prefs.edit().putLong(KEY_LAST_SENT, collected.latest).apply() if (ok) prefs.edit().putLong(KEY_LAST_SENT, collected.latest).apply()
@@ -276,7 +276,7 @@ class MainActivity : AppCompatActivity() {
.setView(content) .setView(content)
.setPositiveButton(R.string.diag_done, null) .setPositiveButton(R.string.diag_done, null)
.setNeutralButton(R.string.diag_send_now) { _, _ -> .setNeutralButton(R.string.diag_send_now) { _, _ ->
Diagnostics.send(this) { ok -> Diagnostics.send(this, manual = true) { ok ->
val text = when (ok) { val text = when (ok) {
null -> R.string.diag_nothing null -> R.string.diag_nothing
true -> R.string.diag_sent true -> R.string.diag_sent
@@ -47,6 +47,16 @@ class DiagReportTest {
assertEquals("[701] Zeile 701", lines.first()) assertEquals("[701] Zeile 701", lines.first())
} }
@Test
fun manualReportWithoutLinesHasNote() {
assertEquals("manuell gesendet (keine neuen Einträge)", DiagReport.note(manual = true, noNewLines = true))
assertEquals("manuell gesendet", DiagReport.note(manual = true, noNewLines = false))
assertEquals("", DiagReport.note(manual = false, noNewLines = true))
val report = DiagReport.build("abc123abc123", "1.6.1", "15", "Google Pixel 9", emptyList(), DiagReport.note(true, true))
assertEquals(0, report.getJSONArray("lines").length())
assertTrue(report.getString("note").isNotEmpty()) // sonst lehnt der Server den Bericht als leer ab
}
@Test @Test
fun buildsReport() { fun buildsReport() {
val lines = listOf("[1] Absturz in main: java.lang.IllegalStateException", "[2] ok") val lines = listOf("[1] Absturz in main: java.lang.IllegalStateException", "[2] ok")