Files
Meshtastic-Android/app/src/main/java/com/geeksville/mesh/MeshUtilApplication.kt
Mike Cumings 654a32c01c Introduce Hilt dependency injection
Uses Hilt to get the database initialization off of the
main thread.

The initial introduction always has a disproportionate
fan-out of boilerplate. In this case, all entry points which
were using UIViewModel needed to be annotated in order to let
the code gen know that they needed to support it.

The PacketRepository is injected into things via the main
thread (e.g., the MeshService) but due to the lazy declaration,
the database isn't hydrated until the DAO is access while on an
IO thread.
2022-02-08 13:57:04 -08:00

57 lines
2.3 KiB
Kotlin

package com.geeksville.mesh
import android.os.Debug
import com.geeksville.android.AppPrefs
import com.geeksville.android.BuildUtils.isEmulator
import com.geeksville.android.GeeksvilleApplication
import com.geeksville.android.Logging
import com.geeksville.util.Exceptions
import com.google.firebase.crashlytics.FirebaseCrashlytics
import dagger.hilt.android.HiltAndroidApp
// NOTE: This is a workaround since the Hilt Gradle plugin doesn't support constructors with default parameters
open class GeeksvilleApplicationWrapper : GeeksvilleApplication()
@HiltAndroidApp
class MeshUtilApplication : GeeksvilleApplicationWrapper() {
override fun onCreate() {
super.onCreate()
Logging.showLogs = BuildConfig.DEBUG
// We default to off in the manifest - we turn on here if the user approves
// leave off when running in the debugger
if (!isEmulator && (!BuildConfig.DEBUG || !Debug.isDebuggerConnected())) {
val crashlytics = FirebaseCrashlytics.getInstance()
crashlytics.setCrashlyticsCollectionEnabled(isAnalyticsAllowed)
crashlytics.setCustomKey("debug_build", BuildConfig.DEBUG)
val pref = AppPrefs(this)
crashlytics.setUserId(pref.getInstallId()) // be able to group all bugs per anonymous user
// We always send our log messages to the crashlytics lib, but they only get sent to the server if we report an exception
// This makes log messages work properly if someone turns on analytics just before they click report bug.
// send all log messages through crashyltics, so if we do crash we'll have those in the report
val standardLogger = Logging.printlog
Logging.printlog = { level, tag, message ->
crashlytics.log("$tag: $message")
standardLogger(level, tag, message)
}
fun sendCrashReports() {
if (isAnalyticsAllowed)
crashlytics.sendUnsentReports()
}
// Send any old reports if user approves
sendCrashReports()
// Attach to our exception wrapper
Exceptions.reporter = { exception, _, _ ->
crashlytics.recordException(exception)
sendCrashReports() // Send the new report
}
}
}
}