Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cnrture committed Apr 19, 2023
1 parent c3a9524 commit 12a5ec1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/canerture/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.canerture.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.canerture.exceptionreport.ExceptionHandler
import com.canerture.exceptionreport.ExceptionReport
import com.canerture.sample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
Expand All @@ -14,7 +14,7 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

ExceptionHandler().init(this, R.color.teal_700)
ExceptionReport(this, R.color.teal_700)

binding.btnException.setOnClickListener {
throw NullPointerException("Null data!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlin.system.exitProcess

class ExceptionHandler : Thread.UncaughtExceptionHandler {
class ExceptionReport(
private val activity: Activity,
@ColorRes private val themeColor: Int? = null
) : Thread.UncaughtExceptionHandler {

private var onExceptionReceived: (String) -> Unit = {}

fun init(
activity: Activity,
@ColorRes themeColor: Int? = null
) {
init {
Thread.setDefaultUncaughtExceptionHandler(this)

onExceptionReceived = { exceptionText ->
Expand All @@ -37,13 +37,16 @@ class ExceptionHandler : Thread.UncaughtExceptionHandler {
val stackTrace = StringWriter()
exception.printStackTrace(PrintWriter(stackTrace))

StringBuilder().apply {
append("Android Version: ${Build.VERSION.RELEASE}\n")
append("Device: ${Build.BRAND.uppercase()} - ${Build.DEVICE.uppercase()}\n")
append("Date: ${getDate()}\n\n")
append("Error\n")
append("$stackTrace\n")
onExceptionReceived(this.toString())
with(activity) {
StringBuilder().apply {
append("${getString(R.string.android_version)} ${Build.VERSION.RELEASE}\n")
append("${getString(R.string.device)} ${Build.BRAND.uppercase()} - ${Build.DEVICE.uppercase()}\n")
append("${getString(R.string.date)} ${getDate()}\n\n")
append("${getString(R.string.error)}\n")
append("$stackTrace\n")

onExceptionReceived(this.toString())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.canerture.exceptionreport
import android.content.ClipData
import android.content.ClipboardManager
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.Typeface.BOLD
import android.os.Bundle
import android.text.Layout
import android.text.Layout.Alignment.ALIGN_CENTER
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
Expand Down Expand Up @@ -52,7 +52,7 @@ class ExceptionReportActivity : AppCompatActivity() {

btnCopy.setOnClickListener {
(getSystemService(CLIPBOARD_SERVICE) as ClipboardManager).apply {
setPrimaryClip(ClipData.newPlainText("text", exceptionText))
setPrimaryClip(ClipData.newPlainText(getString(R.string.text), exceptionText))
Snackbar.make(it, getString(R.string.text_copied), DURATION).show()
}
}
Expand All @@ -65,12 +65,19 @@ class ExceptionReportActivity : AppCompatActivity() {

private fun TextView.setExceptionText(exceptionText: String, color: Int) {
SpannableString(exceptionText).apply {
listOf("Android Version:", "Device:", "Date:", "Error").forEach {
listOf(
getString(R.string.android_version),
getString(R.string.device),
getString(R.string.date),
getString(R.string.error)
).forEach {
setSpan(ForegroundColorSpan(color), exceptionText, it)
setSpan(StyleSpan(Typeface.BOLD), exceptionText, it)
if (it != "Error")
setSpan(Standard(Layout.Alignment.ALIGN_CENTER), exceptionText, it)
setSpan(StyleSpan(BOLD), exceptionText, it)

if (it != getString(R.string.error))
setSpan(Standard(ALIGN_CENTER), exceptionText, it)
}

text = this
movementMethod = LinkMovementMethod.getInstance()
highlightColor = Color.TRANSPARENT
Expand Down
6 changes: 6 additions & 0 deletions exceptionreport/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<string name="title_exception">An unexpected error has occurred!</string>
<string name="text_exception_button">Exception Button</string>
<string name="text_copied">Text copied</string>

<string name="android_version">Android Version:</string>
<string name="device">Device:</string>
<string name="date">Date:</string>
<string name="error">Error:</string>
<string name="text">text</string>
</resources>

0 comments on commit 12a5ec1

Please sign in to comment.