-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Support showing crash information
- Loading branch information
Showing
9 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
app/src/main/java/com/skyd/rays/ui/activity/CrashActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package com.skyd.rays.ui.activity | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.text.selection.SelectionContainer | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.BugReport | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ClipboardManager | ||
import androidx.compose.ui.platform.LocalClipboardManager | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.unit.dp | ||
import com.skyd.rays.R | ||
import com.skyd.rays.config.GITHUB_NEW_ISSUE_URL | ||
import com.skyd.rays.ext.showSnackbar | ||
import com.skyd.rays.model.preference.SettingsProvider | ||
import com.skyd.rays.ui.local.LocalDarkMode | ||
import com.skyd.rays.ui.local.LocalWindowSizeClass | ||
import com.skyd.rays.ui.theme.RaysTheme | ||
import com.skyd.rays.util.CommonUtil.getAppVersionCode | ||
import com.skyd.rays.util.CommonUtil.getAppVersionName | ||
import com.skyd.rays.util.CommonUtil.openBrowser | ||
|
||
/** | ||
* CrashActivity | ||
*/ | ||
class CrashActivity : ComponentActivity() { | ||
companion object { | ||
const val CRASH_INFO = "crashInfo" | ||
|
||
fun start(context: Context, crashInfo: String) { | ||
val intent = Intent(context, CrashActivity::class.java) | ||
intent.putExtra(CRASH_INFO, crashInfo) | ||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
context.startActivity(intent) | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
enableEdgeToEdge() | ||
super.onCreate(savedInstanceState) | ||
|
||
val crashInfo = intent.getStringExtra(CRASH_INFO) | ||
val message = buildString { | ||
append("VersionName: ").append(getAppVersionName()).append("\n") | ||
append("VersionCode: ").append(getAppVersionCode()).append("\n") | ||
append("Brand: ").append(Build.BRAND).append("\n") | ||
append("Model: ").append(Build.MODEL).append("\n") | ||
append("SDK Version: ").append(Build.VERSION.SDK_INT).append("\n") | ||
append("ABI: ").append(Build.SUPPORTED_ABIS.firstOrNull().orEmpty()).append("\n\n") | ||
append("Crash Info: \n") | ||
append(crashInfo) | ||
} | ||
|
||
setContent { | ||
CompositionLocalProvider( | ||
LocalWindowSizeClass provides calculateWindowSizeClass(this) | ||
) { | ||
SettingsProvider { | ||
RaysTheme(darkTheme = LocalDarkMode.current) { | ||
CrashScreen( | ||
message = message, | ||
onReport = { openBrowser(GITHUB_NEW_ISSUE_URL) } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun CrashScreen( | ||
message: String, | ||
onReport: () -> 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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters