Skip to content

Instantly share code, notes, and snippets.

View amsterdatech's full-sized avatar
💭
I may be slow to respond.

William Gouvea amsterdatech

💭
I may be slow to respond.
View GitHub Profile
fun Fragment.isGranted(permission: AppPermission) = run {
context?.let {
(PermissionChecker.checkSelfPermission(it, permission.permissionName
) == PermissionChecker.PERMISSION_GRANTED)
} ?: false
}
fun Fragment.shouldShowRationale(permission: AppPermission) = run {
shouldShowRequestPermissionRationale(permission.permissionName)
}
package com.flyingdutchman.material.components
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.Window
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.flyingdutchman.material.components.databinding.CustomDialogComponentBinding
val showDialogButton = findViewById<Button>(R.id.show_dialog_button)
showDialogButton?.let {
it.setOnClickListener {
dialog {
title = resources.getString(R.string.custom_dialog_title)
contentText = resources.getString(R.string.custom_dialog_body)
negativeText = resources.getString(R.string.custom_dialog_negative_text)
positiveText = resources.getString(R.string.custom_dialog_positive_text)
positiveAction = { toast("Positive Action!!!") }
negativeAction = { toast("Negative Action!!!") }
// Call inside a button on debug drawer or through a kinda of easter egg backdoor inside your app
button.setOnClickListener { analytics(it.context).simulateCrash() }
/**
* You can report it using trackEvent or do your own logic, but remember, you are running in the Main Thread.
* Besides, you shouldn't rely on heavy libraries like RxJava, but Coroutines or Executors are easy to follow as well
*/
override fun uncaughtException(thread: Thread, throwable: Throwable) {
try {
analytics(context)
.track {
name = EventName.EXCEPTION
EventName.STACK_TRACE to extractStackTraceInfo(throwable)
--------- beginning of crash
2021-01-26 22:25:18.607 8665-8665/com.flyingdutchman.simple_analytics E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flyingdutchman.simple_analytics, PID: 8665
java.lang.RuntimeException: Simulate Crash: Boom!!
at com.flyingdutchman.simple_analytics.AndroidCrashLogger.simulateCrash(AndroidCrashLogger.kt:35)
at com.flyingdutchman.simple_analytics.AnalyticsImpl.simulateCrash(Analytics.kt:46)
at com.flyingdutchman.simple_analytics.Main2Activity$onCreate$1.onClick(Main2Activity.kt:14)
at android.view.View.performClick(View.java:6256)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View$PerformClick.run(View.java:24701)
override fun simulateCrash() {
//Here you can throw a RuntimeException or use a related method of your crash report SDK
throw RuntimeException("Simulate Crash: Boom!!")
}
class App : Application() {
override fun onCreate() {
super.onCreate()
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
abstract class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
log("onCreate()")
super.onCreate(savedInstanceState)
}
override fun onStart() {
log("onStart()")
super.onStart()
private var defaultHandler: Thread.UncaughtExceptionHandler? =
Thread.getDefaultUncaughtExceptionHandler()