Adds NIP-17 crash report.

This commit is contained in:
Vitor Pamplona
2025-08-28 16:59:16 -04:00
parent 8493c95e07
commit 624bef21b1
7 changed files with 325 additions and 0 deletions

View File

@@ -28,6 +28,8 @@ import coil3.disk.DiskCache
import coil3.memory.MemoryCache
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.connectivity.ConnectivityManager
import com.vitorpamplona.amethyst.service.crashreports.CrashReportCache
import com.vitorpamplona.amethyst.service.crashreports.UnexpectedCrashSaver
import com.vitorpamplona.amethyst.service.eventCache.MemoryTrimmingService
import com.vitorpamplona.amethyst.service.images.ImageCacheFactory
import com.vitorpamplona.amethyst.service.images.ImageLoaderSetup
@@ -163,6 +165,8 @@ class Amethyst : Application() {
// image cache in memory for coil
val memoryCache: MemoryCache by lazy { ImageCacheFactory.newMemory(this) }
val crashReportCache: CrashReportCache by lazy { CrashReportCache(this) }
// Application-wide ots verification cache
val otsVerifCache by lazy { VerificationStateCache() }
@@ -173,6 +177,8 @@ class Amethyst : Application() {
super.onCreate()
Log.d("AmethystApp", "onCreate $this")
Thread.setDefaultUncaughtExceptionHandler(UnexpectedCrashSaver(crashReportCache, applicationIOScope))
instance = this
if (isDebug) {

View File

@@ -0,0 +1,59 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.service.crashreports
import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okio.FileNotFoundException
import java.io.FileInputStream
import java.io.InputStreamReader
class CrashReportCache(
val appContext: Context,
) {
private fun outputStream() = appContext.openFileOutput("stack.trace", Context.MODE_PRIVATE)
private fun deleteReport() = appContext.deleteFile("stack.trace")
private fun inputStreamOrNull(): FileInputStream? =
try {
appContext.openFileInput("stack.trace")
} catch (_: FileNotFoundException) {
null
}
suspend fun writeReport(report: String) {
val trace = outputStream()
trace.write(report.toByteArray())
trace.close()
}
suspend fun loadAndDelete(): String? =
withContext(Dispatchers.IO) {
val stack =
inputStreamOrNull()?.let { inStream ->
InputStreamReader(inStream).readText()
}
deleteReport()
stack
}
}

View File

@@ -0,0 +1,125 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.service.crashreports
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Done
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.navigation.routes.routeToMessage
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.Size16dp
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun DisplayCrashMessages(
accountViewModel: AccountViewModel,
nav: INav,
) {
val stackTrace = remember { mutableStateOf<String?>(null) }
LaunchedEffect(accountViewModel) {
withContext(Dispatchers.IO) {
stackTrace.value = Amethyst.instance.crashReportCache.loadAndDelete()
}
}
stackTrace.value?.let { stack ->
AlertDialog(
onDismissRequest = { stackTrace.value = null },
title = { Text(stringResource(R.string.crashreport_found)) },
text = {
SelectionContainer {
Text(stringResource(R.string.would_you_like_to_send_the_recent_crash_report_to_amethyst_in_a_dm_no_personal_information_will_be_shared))
}
},
confirmButton = {
Row(
modifier =
Modifier
.padding(all = 8.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
) {
val clipboardManager = LocalClipboardManager.current
TextButton(onClick = {
clipboardManager.setText(AnnotatedString(stack))
}) {
Text(stringRes(R.string.copy_stack_to_clipboard))
}
Button(
onClick = {
nav.nav {
routeToMessage(
user = LocalCache.getOrCreateUser("aa9047325603dacd4f8142093567973566de3b1e20a89557b728c3be4c6a844b"),
draftMessage = stack,
accountViewModel = accountViewModel,
)
}
stackTrace.value = null
},
contentPadding = PaddingValues(horizontal = Size16dp),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = Icons.Outlined.Done,
contentDescription = stringRes(R.string.crashreport_found_send),
)
Spacer(StdHorzSpacer)
Text(stringRes(R.string.crashreport_found_send))
}
}
}
},
)
}
}

View File

@@ -0,0 +1,88 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.service.crashreports
import android.os.Build
import com.vitorpamplona.amethyst.BuildConfig
class ReportAssembler {
fun buildReport(e: Throwable): String =
buildString {
// Device and Product Information
append("Amethyst Version: ")
appendLine(BuildConfig.VERSION_NAME + "-" + BuildConfig.FLAVOR.uppercase())
appendLine()
// Device and Product Information
append("Manufacturer: ")
appendLine(Build.MANUFACTURER)
append("Model: ")
appendLine(Build.MODEL)
append("Product: ")
appendLine(Build.PRODUCT)
appendLine()
// OS Information
append("Android Version: ")
appendLine(Build.VERSION.RELEASE)
append("SDK Int: ")
appendLine(Build.VERSION.SDK_INT.toString())
append("Build ID: ")
appendLine(Build.ID)
appendLine()
// Hardware Information
append("Brand: ")
appendLine(Build.BRAND)
append("Hardware: ")
appendLine(Build.HARDWARE)
appendLine()
// Other Useful Information
append("Device: ")
appendLine(Build.DEVICE)
append("Host: ")
appendLine(Build.HOST)
append("User: ")
appendLine(Build.USER)
appendLine()
append(e.toString())
append("\n")
e.stackTrace.forEach {
append(" ")
append(it.toString())
append("\n")
}
val cause = e.cause
if (cause != null) {
append("\n\nCause:\n")
append(" ")
append(cause.toString())
append("\n")
cause.stackTrace.forEach {
append(" ")
append(it.toString())
append("\n")
}
}
}
}

View File

@@ -0,0 +1,41 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.service.crashreports
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class UnexpectedCrashSaver(
val cache: CrashReportCache,
val scope: CoroutineScope,
) : Thread.UncaughtExceptionHandler {
private val defaultUEH: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler()
override fun uncaughtException(
t: Thread,
e: Throwable,
) {
scope.launch {
cache.writeReport(ReportAssembler().buildReport(e))
}
defaultUEH!!.uncaughtException(t, e)
}
}

View File

@@ -40,6 +40,7 @@ import androidx.core.util.Consumer
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.service.crashreports.DisplayCrashMessages
import com.vitorpamplona.amethyst.service.relayClient.notifyCommand.compose.DisplayNotifyMessages
import com.vitorpamplona.amethyst.ui.actions.NewUserMetadataScreen
import com.vitorpamplona.amethyst.ui.actions.mediaServers.AllMediaServersScreen
@@ -260,6 +261,7 @@ fun AppNavigation(
DisplayErrorMessages(accountViewModel.toastManager, accountViewModel, nav)
DisplayNotifyMessages(accountViewModel, nav)
DisplayCrashMessages(accountViewModel, nav)
}
@Composable

View File

@@ -1245,4 +1245,8 @@
<string name="option_of">Option %1$s of %2$s</string>
<string name="feed_filter_selected">Feed filter, %1$s selected</string>
<string name="feed_filter_select_an_option">Feed filter, %1$s</string>
<string name="crashreport_found">Crash Report found</string>
<string name="would_you_like_to_send_the_recent_crash_report_to_amethyst_in_a_dm_no_personal_information_will_be_shared">Would you like to send the recent crash report to Amethyst in a DM? No personal information will be shared</string>
<string name="crashreport_found_send">Send it</string>
</resources>