From d53feaa7634ee96af246d0f190ff57322da4af33 Mon Sep 17 00:00:00 2001 From: SkyD666 Date: Fri, 26 Apr 2024 21:12:11 +0800 Subject: [PATCH] [feature] Support showing crash information --- app/build.gradle.kts | 2 +- app/src/main/AndroidManifest.xml | 2 + app/src/main/java/com/skyd/rays/App.kt | 3 + .../main/java/com/skyd/rays/config/Config.kt | 1 + .../skyd/rays/ui/activity/CrashActivity.kt | 173 ++++++++++++++++++ .../java/com/skyd/rays/util/CrashHandler.kt | 64 +++++++ app/src/main/res/values-zh-rCN/strings.xml | 5 + app/src/main/res/values-zh-rTW/strings.xml | 5 + app/src/main/res/values/strings.xml | 5 + 9 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/skyd/rays/ui/activity/CrashActivity.kt create mode 100644 app/src/main/java/com/skyd/rays/util/CrashHandler.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b172e7e..8f5bc8c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,7 +22,7 @@ android { minSdk = 24 targetSdk = 34 versionCode = 65 - versionName = "2.2-rc03" + versionName = "2.2-rc04" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 857e5f7..7fca745 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -44,6 +44,8 @@ + + Unit, +) { + val snackbarHostState = remember { SnackbarHostState() } + val context = LocalContext.current + val scope = rememberCoroutineScope() + val clipboardManager = LocalClipboardManager.current + + Scaffold( + snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, + ) { + Column( + modifier = Modifier + .verticalScroll(rememberScrollState()) + .padding(it) + .padding(20.dp) + ) { + Spacer(modifier = Modifier.height(40.dp)) + Icon( + modifier = Modifier.size(40.dp), + imageVector = Icons.Default.BugReport, + contentDescription = null, + ) + + Spacer(modifier = Modifier.height(30.dp)) + Text( + text = stringResource(id = R.string.crashed), + style = MaterialTheme.typography.headlineLarge, + ) + + Spacer(modifier = Modifier.height(30.dp)) + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + TextButton(onClick = { + copyToClipboard(message, clipboardManager) + snackbarHostState.showSnackbar( + scope = scope, + message = context.getString(R.string.copied), + ) + }) { + Text(text = stringResource(id = R.string.crash_screen_copy_crash_log)) + } + + Spacer(modifier = Modifier.width(12.dp)) + + Button(onClick = { + copyToClipboard(message, clipboardManager) + onReport() + }) { + Text(text = stringResource(id = R.string.submit_an_issue_on_github)) + } + } + + Spacer(modifier = Modifier.height(20.dp)) + Text( + text = stringResource(R.string.crash_screen_crash_log), + style = MaterialTheme.typography.titleLarge, + ) + + Spacer(modifier = Modifier.height(10.dp)) + SelectionContainer { + Text(text = message, style = MaterialTheme.typography.bodyMedium) + } + } + } +} + +private fun copyToClipboard(text: String, clipboardManager: ClipboardManager) { + clipboardManager.setText(AnnotatedString(text)) +} \ No newline at end of file diff --git a/app/src/main/java/com/skyd/rays/util/CrashHandler.kt b/app/src/main/java/com/skyd/rays/util/CrashHandler.kt new file mode 100644 index 0000000..02cca2f --- /dev/null +++ b/app/src/main/java/com/skyd/rays/util/CrashHandler.kt @@ -0,0 +1,64 @@ +package com.skyd.rays.util + +import android.annotation.SuppressLint +import android.content.Context +import android.util.Log +import com.skyd.rays.ui.activity.CrashActivity +import java.io.PrintWriter +import java.io.StringWriter +import kotlin.system.exitProcess + + +class CrashHandler private constructor( + val context: Context +) : Thread.UncaughtExceptionHandler { + private val mDefaultHandler: Thread.UncaughtExceptionHandler? = + Thread.getDefaultUncaughtExceptionHandler() + + /** + * When UncaughtException occurs, this function will handle it. + */ + override fun uncaughtException(thread: Thread, ex: Throwable) { + try { + val stringWriter = StringWriter() + val printWriter = PrintWriter(stringWriter) + ex.printStackTrace(printWriter) + var cause = ex.cause + while (cause != null) { + cause.printStackTrace(printWriter) + cause = cause.cause + } + printWriter.close() + val unCaughtException = stringWriter.toString() + Log.e("Crash Info", unCaughtException) + CrashActivity.start(context, unCaughtException) + exitProcess(0) + } catch (e: Exception) { + e.printStackTrace() + } + mDefaultHandler?.uncaughtException(thread, ex) + } + + companion object { + @SuppressLint("StaticFieldLeak") + private var instance: CrashHandler? = null + + fun init(context: Context): CrashHandler? { + if (instance == null) { + synchronized(CrashHandler::class.java) { + if (instance == null) { + instance = CrashHandler(context) + } + } + } + return instance + } + } + + /** + * Only one CrashHandler can be initialized + */ + init { + Thread.setDefaultUncaughtExceptionHandler(this) + } +} \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 6e8c270..e112cb8 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -334,4 +334,9 @@ 删除 Mimetype 删除所有已缓存的表情包 Mimetype 数据 已删除 Mimetype 数据,花费 %.2f 秒 + + 在 GitHub 上报告 + 已复制 + 日志 + 复制日志 \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 6a19ab3..c8bb5ef 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -334,4 +334,9 @@ 刪除 Mimetype 刪除快取的 Mimetype 資料 所有 Mimetype 已刪除,花費了 %.2f 秒 + + 在 GitHub 上報告 + 已複製 + 日誌 + 複製日誌 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7a0c00f..ac7e461 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -347,4 +347,9 @@ Delete mimetypes Delete cached Mimetype data All Mimetype deleted, spent %.2fs + + Report on GitHub + Copied + Log + Copy log \ No newline at end of file