Commit a85ec59d authored by Lucio Maciel's avatar Lucio Maciel

Actually we don't need a whole object, just use a top-level fun

parent 0816dd5f
...@@ -21,6 +21,7 @@ import chat.rocket.android.dagger.qualifier.ForMessages ...@@ -21,6 +21,7 @@ import chat.rocket.android.dagger.qualifier.ForMessages
import chat.rocket.android.helper.CrashlyticsTree import chat.rocket.android.helper.CrashlyticsTree
import chat.rocket.android.infrastructure.CrashlyticsWrapper import chat.rocket.android.infrastructure.CrashlyticsWrapper
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.installCrashlyticsWrapper
import chat.rocket.android.server.domain.AccountsRepository import chat.rocket.android.server.domain.AccountsRepository
import chat.rocket.android.server.domain.GetCurrentServerInteractor import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetSettingsInteractor import chat.rocket.android.server.domain.GetSettingsInteractor
...@@ -268,7 +269,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -268,7 +269,7 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
val core = CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build() val core = CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()
Fabric.with(this, Crashlytics.Builder().core(core).build()) Fabric.with(this, Crashlytics.Builder().core(core).build())
CrashlyticsWrapper.install(this@RocketChatApplication, installCrashlyticsWrapper(this@RocketChatApplication,
getCurrentServerInteractor, settingsInteractor, getCurrentServerInteractor, settingsInteractor,
accountRepository, localRepository) accountRepository, localRepository)
} }
......
...@@ -9,61 +9,59 @@ import chat.rocket.android.server.domain.SITE_URL ...@@ -9,61 +9,59 @@ import chat.rocket.android.server.domain.SITE_URL
import com.crashlytics.android.Crashlytics import com.crashlytics.android.Crashlytics
import kotlinx.coroutines.experimental.runBlocking import kotlinx.coroutines.experimental.runBlocking
object CrashlyticsWrapper { fun installCrashlyticsWrapper(context: Application,
currentServerInteractor: GetCurrentServerInteractor,
private const val KEY_CURRENT_SERVER = "CURRENT_SERVER" settingsInteractor: GetSettingsInteractor,
private const val KEY_CURRENT_USER = "CURRENT_USER" accountRepository: AccountsRepository,
private const val KEY_CURRENT_USERNAME = "CURRENT_USERNAME" localRepository: LocalRepository) {
private const val KEY_ACCOUNTS = "ACCOUNTS" if (isCrashlyticsEnabled()) {
private const val KEY_SETTINGS_SIZE = "SETTINGS_SIZE" Thread.setDefaultUncaughtExceptionHandler(RocketChatUncaughtExceptionHandler(currentServerInteractor,
private const val KEY_SETTINGS_BASE_URL = "SETTINGS_BASE_URL" settingsInteractor, accountRepository, localRepository))
fun install(context: Application,
currentServerInteractor: GetCurrentServerInteractor,
settingsInteractor: GetSettingsInteractor,
accountRepository: AccountsRepository,
localRepository: LocalRepository) {
if (isEnabled()) {
Thread.setDefaultUncaughtExceptionHandler(RocketChatUncaughtExceptionHandler(currentServerInteractor,
settingsInteractor, accountRepository, localRepository))
}
} }
}
private fun isEnabled(): Boolean { private fun isCrashlyticsEnabled(): Boolean {
return !BuildConfig.DEBUG return !BuildConfig.DEBUG
} }
private class RocketChatUncaughtExceptionHandler(
val currentServerInteractor: GetCurrentServerInteractor,
val settingsInteractor: GetSettingsInteractor,
val accountRepository: AccountsRepository,
val localRepository: LocalRepository)
: Thread.UncaughtExceptionHandler {
val crashlyticsHandler: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler() private class RocketChatUncaughtExceptionHandler(
val currentServerInteractor: GetCurrentServerInteractor,
val settingsInteractor: GetSettingsInteractor,
val accountRepository: AccountsRepository,
val localRepository: LocalRepository)
: Thread.UncaughtExceptionHandler {
override fun uncaughtException(t: Thread, e: Throwable) { val crashlyticsHandler: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler()
val currentServer = currentServerInteractor.get() ?: "<unknown>"
Crashlytics.setString(KEY_CURRENT_SERVER, currentServer)
runBlocking {
val accounts = accountRepository.load()
Crashlytics.setString(KEY_ACCOUNTS, accounts.toString())
}
val settings = settingsInteractor.get(currentServer) override fun uncaughtException(t: Thread, e: Throwable) {
Crashlytics.setInt(KEY_SETTINGS_SIZE, settings.size) val currentServer = currentServerInteractor.get() ?: "<unknown>"
val baseUrl = settings[SITE_URL]?.toString() Crashlytics.setString(KEY_CURRENT_SERVER, currentServer)
Crashlytics.setString(KEY_SETTINGS_BASE_URL, baseUrl) runBlocking {
val accounts = accountRepository.load()
Crashlytics.setString(KEY_ACCOUNTS, accounts.toString())
}
val settings = settingsInteractor.get(currentServer)
Crashlytics.setInt(KEY_SETTINGS_SIZE, settings.size)
val baseUrl = settings[SITE_URL]?.toString()
Crashlytics.setString(KEY_SETTINGS_BASE_URL, baseUrl)
val user = localRepository.getCurrentUser(currentServer) val user = localRepository.getCurrentUser(currentServer)
Crashlytics.setString(KEY_CURRENT_USER, user?.toString()) Crashlytics.setString(KEY_CURRENT_USER, user?.toString())
Crashlytics.setString(KEY_CURRENT_USERNAME, localRepository.username()) Crashlytics.setString(KEY_CURRENT_USERNAME, localRepository.username())
if (crashlyticsHandler != null) { if (crashlyticsHandler != null) {
crashlyticsHandler.uncaughtException(t, e) crashlyticsHandler.uncaughtException(t, e)
} else { } else {
throw RuntimeException("Missing default exception handler") throw RuntimeException("Missing default exception handler")
}
} }
} }
} }
\ No newline at end of file
private const val KEY_CURRENT_SERVER = "CURRENT_SERVER"
private const val KEY_CURRENT_USER = "CURRENT_USER"
private const val KEY_CURRENT_USERNAME = "CURRENT_USERNAME"
private const val KEY_ACCOUNTS = "ACCOUNTS"
private const val KEY_SETTINGS_SIZE = "SETTINGS_SIZE"
private const val KEY_SETTINGS_BASE_URL = "SETTINGS_BASE_URL"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment