Skip to content

Commit ef361a4

Browse files
authored
fix: Missing required view (#1)
* misc: Renamed `app` to `sample` * misc: Renamed crashhandler to library * fix: Missing required view
1 parent 7dfb454 commit ef361a4

33 files changed

+233
-21
lines changed
File renamed without changes.

crashhandler/src/main/java/com/dzeio/crashhandler/CrashHandler.kt renamed to library/src/main/java/com/dzeio/crashhandler/CrashHandler.kt

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dzeio.crashhandler
22

33
import android.app.Application
4+
import android.content.Context
45
import android.content.Intent
56
import android.content.SharedPreferences
67
import 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
*/
1920
class 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)

crashhandler/src/main/java/com/dzeio/crashhandler/ui/ErrorActivity.kt renamed to library/src/main/java/com/dzeio/crashhandler/ui/ErrorActivity.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import android.content.ClipboardManager
55
import android.content.Context
66
import android.os.Bundle
77
import androidx.appcompat.app.AppCompatActivity
8-
import com.dzeio.crashhandler.databinding.ActivityErrorBinding
8+
import com.dzeio.crashhandler.databinding.CrashHandlerActivityErrorBinding
99

1010
class ErrorActivity : AppCompatActivity() {
1111

12-
private lateinit var binding: ActivityErrorBinding
12+
private lateinit var binding: CrashHandlerActivityErrorBinding
1313

1414
override fun onCreate(savedInstanceState: Bundle?) {
1515
super.onCreate(savedInstanceState)
1616

17-
binding = ActivityErrorBinding.inflate(layoutInflater)
17+
binding = CrashHandlerActivityErrorBinding.inflate(layoutInflater)
1818

1919
setContentView(binding.root)
2020

2121
val data = intent.getStringExtra("error")
2222

2323
// put it in the textView
24-
binding.errorText.text = data
25-
binding.errorText.setTextIsSelectable(true)
24+
binding.errorText.apply {
25+
text = data
26+
setTextIsSelectable(true)
27+
}
2628

2729
// Handle the Quit button
2830
binding.errorQuit.setOnClickListener {

crashhandler/src/main/res/layout/activity_error.xml renamed to library/src/main/res/layout/crash_handler_activity_error.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5-
5+
tools:context=".ui.ErrorActivity"
6+
android:fitsSystemWindows="true"
67
android:layout_width="match_parent"
78
android:layout_height="match_parent">
89

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)