BarInsta/app/src/main/java/awaisomereport/CrashReporter.kt

40 lines
1.2 KiB
Kotlin
Raw Normal View History

2021-07-11 12:24:35 +00:00
package awaisomereport
2020-07-01 17:08:28 +00:00
2021-07-11 12:24:35 +00:00
import android.app.Application
2020-07-01 17:08:28 +00:00
2021-07-11 12:24:35 +00:00
class CrashReporter private constructor(application: Application) : Thread.UncaughtExceptionHandler {
2020-07-01 17:08:28 +00:00
2021-07-11 12:24:35 +00:00
private val crashHandler: CrashHandler?
private var startAttempted = false
private var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
2021-07-11 12:24:35 +00:00
init {
crashHandler = CrashHandler(application)
2020-07-01 17:08:28 +00:00
}
2021-07-11 12:24:35 +00:00
fun start() {
if (startAttempted) return
2021-07-11 12:35:17 +00:00
startAttempted = true
2021-07-11 12:24:35 +00:00
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(this)
2020-07-01 17:08:28 +00:00
}
2021-07-11 12:24:35 +00:00
override fun uncaughtException(t: Thread, exception: Throwable) {
if (crashHandler == null) {
defaultExceptionHandler?.uncaughtException(t, exception)
return
2020-07-01 17:08:28 +00:00
}
2021-07-11 12:24:35 +00:00
crashHandler.uncaughtException(t, exception, defaultExceptionHandler ?: return)
2020-07-01 17:08:28 +00:00
}
2021-07-11 12:24:35 +00:00
companion object {
@Volatile
private var INSTANCE: CrashReporter? = null
fun getInstance(application: Application): CrashReporter {
return INSTANCE ?: synchronized(this) {
CrashReporter(application).also { INSTANCE = it }
}
2020-07-01 17:08:28 +00:00
}
}
2021-07-11 12:24:35 +00:00
}