diff options
| -rw-r--r-- | android-app/app/src/main/AndroidManifest.xml | 1 | ||||
| -rw-r--r-- | android-app/app/src/main/kotlin/org/terst/nav/NavApplication.kt | 52 |
2 files changed, 53 insertions, 0 deletions
diff --git a/android-app/app/src/main/AndroidManifest.xml b/android-app/app/src/main/AndroidManifest.xml index 0b9fc05..e3d679b 100644 --- a/android-app/app/src/main/AndroidManifest.xml +++ b/android-app/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ <uses-permission android:name="android.permission.RECORD_AUDIO" /> <application + android:name=".NavApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" diff --git a/android-app/app/src/main/kotlin/org/terst/nav/NavApplication.kt b/android-app/app/src/main/kotlin/org/terst/nav/NavApplication.kt new file mode 100644 index 0000000..1075930 --- /dev/null +++ b/android-app/app/src/main/kotlin/org/terst/nav/NavApplication.kt @@ -0,0 +1,52 @@ +package org.terst.nav + +import android.app.Application +import android.os.Environment +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() { + + override fun onCreate() { + super.onCreate() + 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) + } +} |
