blob: 0b507d2570c2ffb8321dfa9fcff52042f2069eb6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package org.terst.nav
import android.app.Application
import com.google.firebase.crashlytics.FirebaseCrashlytics
import java.io.File
import java.io.PrintWriter
import java.io.StringWriter
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class NavApplication : Application() {
companion object {
var isTesting: Boolean = false
}
override fun onCreate() {
super.onCreate()
FirebaseCrashlytics.getInstance().sendUnsentReports()
installCrashLogger()
}
private fun installCrashLogger() {
val default = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
try {
writeCrashLog(thread, throwable)
} catch (_: Exception) {
// never suppress the original crash
}
default?.uncaughtException(thread, throwable)
}
}
private fun writeCrashLog(thread: Thread, throwable: Throwable) {
val sw = StringWriter()
throwable.printStackTrace(PrintWriter(sw))
val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
val body = buildString {
appendLine("=== Nav Crash Log ===")
appendLine("Time : $timestamp")
appendLine("Thread : ${thread.name}")
appendLine("Device : ${android.os.Build.MODEL} (API ${android.os.Build.VERSION.SDK_INT})")
appendLine()
append(sw)
}
// Try external storage first (readable without root)
val dir = getExternalFilesDir(null) ?: filesDir
val file = File(dir, "crash_${timestamp}.txt")
file.writeText(body)
// Also overwrite a fixed-name "latest" file for easy access
File(dir, "crash_latest.txt").writeText(body)
}
}
|