11package com.dzeio.crashhandler
22
33import android.app.Application
4+ import android.content.Context
45import android.content.Intent
56import android.content.SharedPreferences
67import android.os.Build
@@ -17,13 +18,14 @@ import kotlin.system.exitProcess
1718 * the Crash Handler class, you can get an instance by using it's [Builder]
1819 */
1920class CrashHandler private constructor(
20- private val activity : Any ,
21+ private val application : Application ? ,
22+ private val activity : Class <* >,
2123 private val prefs : SharedPreferences ? ,
2224 private val prefsKey : String? ,
2325 @StringRes
2426 private val errorReporterCrashKey : Int? ,
25- private var prefix : String? = null ,
26- private var suffix : String? = null
27+ private val prefix : String? = null ,
28+ private val suffix : String? = null
2729) {
2830
2931 private companion object {
@@ -34,10 +36,11 @@ class CrashHandler private constructor(
3436 * Builder for the crash handler
3537 */
3638 class Builder () {
39+ private var application: Application ? = null
3740 private var prefs: SharedPreferences ? = null
3841 private var prefsKey: String? = null
3942 private var errorReporterCrashKey: Int? = null
40- private var activity: Any ? = ErrorActivity ::class .java
43+ private var activity: Class < * > ? = ErrorActivity ::class .java
4144 private var prefix: String? = null
4245 private var suffix: String? = null
4346
@@ -48,7 +51,19 @@ class CrashHandler private constructor(
4851 *
4952 * @param activity the activity class to use
5053 */
51- fun withActivity (activity : Any ): Builder {
54+ fun withContext (context : Context ): Builder {
55+ this .application = context.applicationContext as Application ?
56+ return this
57+ }
58+
59+ /* *
60+ * Change the Crash activity to with your own
61+ *
62+ * note: you can get the backtrace text by using `intent.getStringExtra("error")`
63+ *
64+ * @param activity the activity class to use
65+ */
66+ fun withActivity (activity : Class <* >): Builder {
5267 this .activity = activity
5368 return this
5469 }
@@ -114,7 +129,24 @@ class CrashHandler private constructor(
114129 * build the Crash Handler
115130 */
116131 fun build (): CrashHandler {
117- return CrashHandler (activity!! , prefs, prefsKey, errorReporterCrashKey, prefix, suffix)
132+ return CrashHandler (application, activity!! , prefs, prefsKey, errorReporterCrashKey, prefix, suffix)
133+ }
134+ }
135+
136+ private var oldHandler: Thread .UncaughtExceptionHandler ? = null
137+
138+ fun setup () {
139+ if (application != null ) {
140+ this .setup(application)
141+ }
142+ }
143+
144+ /* *
145+ * Destroy the handler
146+ */
147+ fun destroy () {
148+ if (oldHandler != null ) {
149+ Thread .setDefaultUncaughtExceptionHandler(oldHandler)
118150 }
119151 }
120152
@@ -126,7 +158,7 @@ class CrashHandler private constructor(
126158 */
127159 fun setup (application : Application ) {
128160 // Application Error Handling
129- val oldHandler = Thread .getDefaultUncaughtExceptionHandler()
161+ oldHandler = Thread .getDefaultUncaughtExceptionHandler()
130162 Thread .setDefaultUncaughtExceptionHandler { paramThread, paramThrowable ->
131163
132164 // Log error to logcat if it wasn't done before has it can not be logged depending on the version
@@ -199,13 +231,13 @@ class CrashHandler private constructor(
199231
200232 data + = suffix ? : " "
201233
202- Log .i(TAG , " Starting ${( activity as Class < * >) .name} " )
234+ Log .i(TAG , " Starting ${activity.name} " )
203235
204236 // prepare the activity
205- val intent = Intent (application.applicationContext , activity)
237+ val intent = Intent (application, activity)
206238
207239 // add flags so that it don't use the current Application context
208- intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS )
240+ intent.addFlags(Intent .FLAG_ACTIVITY_CLEAR_TASK or Intent . FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS )
209241
210242 // add the Data String
211243 intent.putExtra(" error" , data)
0 commit comments