Skip to content

Commit

Permalink
feat: simplified sample app code to better understand how it work (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviortheking authored Mar 17, 2023
1 parent 71993d0 commit 64d9b25
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 124 deletions.
14 changes: 12 additions & 2 deletions sample/src/main/java/com/dzeio/crashhandlertest/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ class Application : android.app.Application() {
override fun onCreate() {
super.onCreate()

// get the device Preference store
val prefs = PreferenceManager.getDefaultSharedPreferences(this)

// create the Crash Handler
CrashHandler.Builder()
.withActivity(ErrorActivity::class.java)
// need the application context to run
.withContext(this)
// 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
.withPrefs(prefs)
.withPrefsKey("com.dzeio.crashhandler.key")
.withPrefix("Pouet :D")
// a Prefix to add at the beginning the crash message
.withPrefix(
"POKEMON"
)
// a Suffix to add at the end of the crash message
.withSuffix("WHYYYYY")
// build & start the module
.build().setup()
}
}
35 changes: 15 additions & 20 deletions sample/src/main/java/com/dzeio/crashhandlertest/ui/ErrorActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.dzeio.crashhandlertest.ui
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Process
import android.widget.Toast
Expand All @@ -12,37 +11,30 @@ import androidx.core.view.WindowCompat
import com.dzeio.crashhandlertest.databinding.ActivityErrorBinding
import kotlin.system.exitProcess

/**
* Example Activity for a custom ErrorActivity
*
* note: try to keep the complexity of this class as low as possible
* to make sure this will always load
*/
class ErrorActivity : AppCompatActivity() {

// the view binding
private lateinit var binding: ActivityErrorBinding

override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)

// inflate the view
binding = ActivityErrorBinding.inflate(layoutInflater)
setContentView(binding.root)

// get the error string from the library
val data = intent.getStringExtra("error")

// Get Application datas
val deviceToReport =
if (Build.DEVICE.contains(Build.MANUFACTURER)) {
Build.DEVICE
} else {
"${Build.MANUFACTURER} ${Build.DEVICE}"
}

val reportText = """
Crash Report (Thread: ${intent?.getLongExtra("threadId", -1) ?: "unknown"})
on $deviceToReport (${Build.MODEL}) running Android ${Build.VERSION.RELEASE} (${Build.VERSION.SDK_INT})
backtrace:
""".trimIndent() + data

// put it in the textView
binding.errorText.text = reportText
binding.errorText.text = data

// Handle the Quit button
binding.errorQuit.setOnClickListener {
Expand All @@ -57,8 +49,9 @@ class ErrorActivity : AppCompatActivity() {
intent.setDataAndType(Uri.parse("mailto:"), "text/plain")
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("report.openhealth@dzeio.com"))
intent.putExtra(Intent.EXTRA_SUBJECT, "Error report for application crash")
intent.putExtra(Intent.EXTRA_TEXT, "Send Report Email\n$reportText")
intent.putExtra(Intent.EXTRA_TEXT, "Send Report Email\n$data")

// send intent
try {
startActivity(Intent.createChooser(intent, "Send Report Email..."))
} catch (e: ActivityNotFoundException) {
Expand All @@ -69,8 +62,10 @@ class ErrorActivity : AppCompatActivity() {
// Handle the GitHub Button
binding.errorSubmitGithub.setOnClickListener {
// Build URL
val url = "https://github.com/dzeiocom/OpenHealth/issues/new?title=Application Error&body=$reportText"
val title = "Application Error"
val url = "https://github.com/dzeiocom/OpenHealth/issues/new?title=$title&body=$data"

// send intent
try {
startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse(url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package com.dzeio.crashhandlertest.ui
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.dzeio.crashhandlertest.R
import com.dzeio.crashhandlertest.databinding.ActivityMainBinding

/**
*
*/
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
Expand All @@ -15,5 +19,10 @@ class MainActivity : AppCompatActivity() {

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.buttonFirst.setOnClickListener {
// DIE
throw Exception(getString(R.string.error_message))
}
}
}
}
43 changes: 0 additions & 43 deletions sample/src/main/java/com/dzeio/crashhandlertest/ui/MainFragment.kt

This file was deleted.

16 changes: 9 additions & 7 deletions sample/src/main/res/layout/activity_error.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
Expand All @@ -12,7 +11,8 @@
style="?textAppearanceHeadline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blablabla"
android:textAlignment="center"
android:text="@string/crash_handler_page_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -22,7 +22,8 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="blablabla2"
android:textAlignment="center"
android:text="@string/crash_handler_page_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
Expand All @@ -42,7 +43,8 @@
android:id="@+id/error_text"
android:layout_width="match_parent"
style="?textAppearanceCaption"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:textIsSelectable="true" />

</ScrollView>

Expand All @@ -52,7 +54,7 @@
android:id="@+id/error_submit_github"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Github"
android:text="@string/github"
android:layout_marginStart="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/error_submit_email"
app:layout_constraintStart_toStartOf="parent" />
Expand All @@ -63,7 +65,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="E-Mail"
android:text="@string/e_mail"
app:layout_constraintBottom_toTopOf="@+id/error_quit"
app:layout_constraintEnd_toEndOf="parent" />

Expand All @@ -72,7 +74,7 @@
android:id="@+id/error_quit"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Quit"
android:text="@string/quit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Expand Down
22 changes: 10 additions & 12 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.MainActivity">
tools:context=".ui.MainActivity"
android:padding="16dp">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_app"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
app:layout_constraintTop_toTopOf="parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
21 changes: 0 additions & 21 deletions sample/src/main/res/layout/fragment_main.xml

This file was deleted.

13 changes: 0 additions & 13 deletions sample/src/main/res/navigation/nav_graph.xml

This file was deleted.

11 changes: 6 additions & 5 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<resources>
<string name="app_name" translatable="false">Crash Handler</string>
<string name="title_activity_main" translatable="false">MainActivity</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label" translatable="false">First Fragment</string>
<string name="second_fragment_label" translatable="false">Second Fragment</string>

<string name="crash_app" translatable="false">Crash app</string>
<string name="error_message" translatable="false">I am an error</string>
<string name="crash_handler_page_title" translatable="false">Crash Handler page title</string>
<string name="crash_handler_page_subtitle" translatable="false">crash handler page subtitle</string>
<string name="github" translatable="false">Github</string>
<string name="e_mail" translatable="false">E-Mail</string>
<string name="quit" translatable="false">Quit</string>
</resources>

0 comments on commit 64d9b25

Please sign in to comment.