-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCrashHandler.kt
More file actions
55 lines (46 loc) · 1.96 KB
/
CrashHandler.kt
File metadata and controls
55 lines (46 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.greybox.projectmesh.debug
import android.content.Context
import android.content.Intent
import com.google.gson.Gson
import timber.log.Timber
import java.lang.Exception
import java.lang.Thread.UncaughtExceptionHandler
import kotlin.system.exitProcess
class CrashHandler(private val context: Context, private val defaultHandler: UncaughtExceptionHandler, private val activityToBeLaunched: Class<*>) : Thread.UncaughtExceptionHandler {
override fun uncaughtException(thread: Thread, throwable: Throwable) {
try {
launchActivity(context,activityToBeLaunched,throwable)
exitProcess(status = 1)
} catch (e: Exception)
{
defaultHandler.uncaughtException(thread,throwable)
}
}
private fun launchActivity(applicationContext: Context, activity: Class<*>, exception: Throwable)
{
val crashIntent = Intent(applicationContext, activity).also {
it.putExtra("CrashData", Gson().toJson(exception))
Timber.tag("Project Mesh Error").e(exception, "Error: ");
}
crashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
crashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
applicationContext.startActivity(crashIntent)
}
companion object {
fun init(applicationContext: Context, activityToBeLaunched: Class<*>)
{
val handler = CrashHandler(applicationContext,Thread.getDefaultUncaughtExceptionHandler() as UncaughtExceptionHandler, activityToBeLaunched)
Thread.setDefaultUncaughtExceptionHandler(handler)
}
fun getThrowableFromIntent(intent: Intent): Throwable?
{
return try {
Gson().fromJson(intent.getStringExtra("CrashData"), Throwable::class.java)
}
catch (e: Exception) {
Timber.tag("CrashHandler",).e(e,"getThrowableFromIntent: ");
null
}
}
}
}