-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Add unknown exception display page
- Loading branch information
Showing
8 changed files
with
169 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
91 changes: 91 additions & 0 deletions
91
app/src/main/java/com/skyd/anivu/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,91 @@ | ||
package com.skyd.anivu.ui.activity | ||
|
||
import android.content.ClipData | ||
import android.content.ClipboardManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.os.Process | ||
import android.widget.TextView | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.skyd.anivu.R | ||
import com.skyd.anivu.config.Const.GITHUB_NEW_ISSUE_URL | ||
import com.skyd.anivu.ext.getAppVersionName | ||
import com.skyd.anivu.ext.openBrowser | ||
import com.skyd.anivu.ext.sp | ||
import com.skyd.anivu.ui.component.showToast | ||
import kotlin.system.exitProcess | ||
|
||
|
||
/** | ||
* 调试用的异常activity,不要继承BaseActivity | ||
*/ | ||
class CrashActivity : AppCompatActivity() { | ||
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) | ||
} | ||
} | ||
|
||
private fun copyToClipboard(crashInfo: String?) { | ||
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | ||
cm.setPrimaryClip(ClipData.newPlainText("Exception trace stack", crashInfo)) | ||
} | ||
|
||
private fun exitApp() { | ||
finish() | ||
Process.killProcess(Process.myPid()) | ||
exitProcess(1) | ||
} | ||
|
||
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("Brand: ").append(Build.BRAND).append("\n") | ||
append("Model: ").append(Build.MODEL).append("\n") | ||
append("SDK Version: ").append(Build.VERSION.SDK_INT).append("\n\n") | ||
append("Crash Info: \n") | ||
append(crashInfo) | ||
} | ||
MaterialAlertDialogBuilder(this) | ||
.setIcon(R.drawable.ic_error_24) | ||
.setTitle(getString(R.string.crashed)) | ||
.setMessage(message) | ||
.setCancelable(false) | ||
.setPositiveButton(getString(R.string.submit_an_issue_on_github)) { _, _ -> | ||
copyToClipboard(crashInfo) | ||
Uri.parse(GITHUB_NEW_ISSUE_URL).openBrowser(this) | ||
exitApp() | ||
} | ||
.setNegativeButton(getString(R.string.close)) { _, _ -> | ||
exitApp() | ||
} | ||
.setNeutralButton(getString(android.R.string.copy)) { _, _ -> | ||
copyToClipboard(crashInfo) | ||
getString(R.string.copied).showToast() | ||
exitApp() | ||
} | ||
.show() | ||
.apply { | ||
window?.decorView?.findViewById<TextView>(android.R.id.message)?.apply { | ||
setTextIsSelectable(true) | ||
textSize = 3.5f.sp | ||
} | ||
} | ||
setFinishOnTouchOutside(false) | ||
} | ||
} |
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.anivu.util | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.util.Log | ||
import com.skyd.anivu.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() | ||
|
||
/** | ||
* 当UncaughtException发生时会转入该函数来处理 | ||
*/ | ||
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