Skip to content

Commit

Permalink
Add Init Configuration With Callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazan98 committed Sep 26, 2021
1 parent bc4ee5e commit 5babd89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,63 @@ import java.util.*

object ShakeReporter {

const val CRASH_FILE_PATH = "crashes"
private const val FILES_ROOT_PATH = "ShakeReporter"
private const val CRASH_FILE_PATH = "crashes"
private const val LOGGING_PREFIX = "[ShakeReporter]:"

/**
* Init The Library With Storage Path and Generated File in Private Storage for Application
* Used To Save All Exceptions and Network Calls To See The History Via Notification
* Here The Default Notification Channel Will Be Created
*/
@JvmStatic
fun init(context: Context) {
ReporterNotifications.createNotificationChannel(context)
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
val reporterFilesPath = File(context.filesDir, FILES_ROOT_PATH)
if (!reporterFilesPath.exists()) {
reporterFilesPath.mkdir()
printLogs("Create Root Crashes Path : ${reporterFilesPath.absoluteFile}")
}
onCrashTriggered(thread, throwable, context)
}
}

/**
* If You Want to Submit Crash in Custom Callback Use This Method
* You want To Add Extra Params Like Printing Non Fatal Crashes
* Print it on Callback then Call this Method to report the Crash Internally
*/
@JvmStatic
fun onCrashTriggered(thread: Thread, throwable: Throwable, context: Context) {
val reporterFilesPath = File(context.filesDir, FILES_ROOT_PATH)
if (!reporterFilesPath.exists()) {
reporterFilesPath.mkdir()
printLogs("Create Root Crashes Path : ${reporterFilesPath.absoluteFile}")
}

createExceptionReport(thread, throwable, reporterFilesPath)
ReporterNotifications.showNotification(
getNotificationTitle(),
getNotificationMessage(thread, throwable),
context
)
createExceptionReport(thread, throwable, reporterFilesPath)
ReporterNotifications.showNotification(
getNotificationTitle(),
getNotificationMessage(thread, throwable),
context
)
}

/**
* Use this Method when you want to Submit Any Callback
* to do Custom Work on The Exceptions Then Will Call the Default
* Handler Automatic
*/
@JvmStatic
fun init(context: Context, callback: ShakeReporterCallback) {
init(context)
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
callback.onExceptionTriggered(throwable)
onCrashTriggered(thread, throwable, context)
}
}

/**
* Each Crash Will be Saved in Application Private Storage to Access It From Screens
* This Method Don't need Storage Permission
* Each Exception Record is Saved in One File with Name : CRASH_FILE_PATH Variable
*/
private fun createExceptionReport(thread: Thread, throwable: Throwable, reporterFilesPath: File?) {
Thread {
reporterFilesPath?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yazantarifi.android.reporter

interface ShakeReporterCallback {

fun onExceptionTriggered(throwable: Throwable)

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class CrashesFragment: Fragment(R.layout.fragment_crashes) {
val currentCrashesFiles = currentFiles[0].listFiles()
for (item in currentCrashesFiles) {
try {
items.add(getCrashItemFromFile(item))
if (item.absolutePath.contains(ShakeReporter.CRASH_FILE_PATH)) {
items.add(getCrashItemFromFile(item))
}
} catch (ex: Exception) {
ShakeReporter.printLogs(ex.message ?: "", true)
}
Expand Down

0 comments on commit 5babd89

Please sign in to comment.