feat: Add crash saving (#12)

This commit is contained in:
2023-08-24 15:54:55 +02:00
committed by GitHub
parent 1703479865
commit acbb524a7e
16 changed files with 383 additions and 130 deletions

View File

@ -3,6 +3,7 @@ package com.dzeio.crashhandlertest
import androidx.preference.PreferenceManager
import com.dzeio.crashhandler.CrashHandler
import com.dzeio.crashhandlertest.ui.ErrorActivity
import java.io.File
class Application : android.app.Application() {
override fun onCreate() {
@ -15,17 +16,20 @@ class Application : android.app.Application() {
CrashHandler.Builder()
// need the application context to run
.withContext(this)
// every other items below are optionnal
// define a custom activity to use
.withActivity(ErrorActivity::class.java)
// define the preferenceManager to be able to handle crash in a custom Activity and to have the previous crash date in the logs
// define the preferenceManager to have the previous crash date in the logs
.withPrefs(prefs)
.withPrefsKey("com.dzeio.crashhandler.key")
// a Prefix to add at the beginning the crash message
.withPrefix(
"Pokémon"
)
.withPrefix("Prefix")
// a Suffix to add at the end of the crash message
.withSuffix("Suffix")
// add a location where the crash logs are also exported (can be recovered as a zip ByteArray by calling {CrashHandler.getInstance().export()})
.withExportLocation(
File(this.getExternalFilesDir(null) ?: this.filesDir, "crash-logs")
)
// build & start the module
.build().setup()
}

View File

@ -1,16 +1,41 @@
package com.dzeio.crashhandlertest.ui
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.dzeio.crashhandler.CrashHandler
import com.dzeio.crashhandlertest.R
import com.dzeio.crashhandlertest.databinding.ActivityMainBinding
/**
*
*/
class MainActivity : AppCompatActivity() {
/**
* Response to the export button event
*/
private val writeResult = registerForActivityResult(
ActivityResultContracts.CreateDocument("application/zip")
) {
val zipFile = CrashHandler.getInstance().export()
if (zipFile == null) {
return@registerForActivityResult
}
// write file to location
this.contentResolver.openOutputStream(it!!)?.apply {
write(zipFile)
close()
}
Toast.makeText(
this,
R.string.export_complete,
Toast.LENGTH_LONG
).show()
}
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
@ -24,5 +49,15 @@ class MainActivity : AppCompatActivity() {
// DIE
throw Exception(getString(R.string.error_message))
}
binding.buttonExport.setOnClickListener {
// launch the popin to select where to save the file
writeResult.launch("output.zip")
}
binding.buttonClearExports.setOnClickListener {
// clear the handler exports
CrashHandler.getInstance().clearExports()
}
}
}

View File

@ -8,14 +8,35 @@
tools:context=".ui.MainActivity"
android:padding="16dp">
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crash_app"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_app"
android:layout_marginBottom="8dp" />
<Button
android:id="@+id/button_export"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/export_logs" />
<Button
android:id="@+id/button_clear_exports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear_export_logs" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -9,4 +9,7 @@
<string name="no_email_client_found">Aucun client E-Mail trouvé!</string>
<string name="send_email_report">Envoyer le rapport de crash par E-Mail</string>
<string name="error_report_application_crash">Rapport d\'erreur pour le crash d\'application</string>
<string name="export_logs">Exporter les logs de crash sauvegardé</string>
<string name="export_complete">Logs de crash exporté!</string>
<string name="clear_export_logs">Nettoyer les crashs sauvegardé</string>
</resources>

View File

@ -10,4 +10,7 @@
<string name="no_email_client_found">No E-Mail client found!</string>
<string name="send_email_report">Send a Report E-Mail</string>
<string name="error_report_application_crash">Error report for the application crash</string>
<string name="export_logs">Export saved crash logs</string>
<string name="export_complete">Exported crash logs!</string>
<string name="clear_export_logs">Cleanup saved crash logs</string>
</resources>