Migrate to open source firebase-crashlytics

This commit is contained in:
Mygod
2020-01-24 22:22:48 +08:00
parent b55ad234ac
commit 3cee5a4782
18 changed files with 67 additions and 125 deletions

View File

@@ -8,6 +8,9 @@ import android.content.res.Configuration
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.Size
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
@@ -20,6 +23,9 @@ import be.mygod.vpnhotspot.net.DhcpWorkaround
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.util.DeviceStorageApp
import be.mygod.vpnhotspot.util.RootSession
import com.google.firebase.FirebaseApp
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber
@@ -40,7 +46,18 @@ class App : Application() {
deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager(this).sharedPreferencesName)
deviceStorage.moveDatabaseFrom(this, AppDatabase.DB_NAME)
} else deviceStorage = this
DebugHelper.init()
FirebaseApp.initializeApp(deviceStorage)
Timber.plant(object : Timber.DebugTree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (t == null) {
if (priority != Log.DEBUG || BuildConfig.DEBUG) Log.println(priority, tag, message)
crashlytics.log("${"XXVDIWEF".getOrElse(priority) { 'X' }}/$tag: $message")
} else {
if (priority >= Log.WARN || priority == Log.DEBUG) Log.println(priority, tag, message)
if (priority >= Log.INFO) crashlytics.recordException(t)
}
}
})
ServiceNotification.updateNotificationChannels()
EmojiCompat.init(FontRequestEmojiCompatConfig(deviceStorage, FontRequest(
"com.google.android.gms.fonts",
@@ -49,7 +66,7 @@ class App : Application() {
R.array.com_google_android_gms_fonts_certs)).apply {
setEmojiSpanIndicatorEnabled(BuildConfig.DEBUG)
registerInitCallback(object : EmojiCompat.InitCallback() {
override fun onInitialized() = DebugHelper.log("EmojiCompat", "Initialized")
override fun onInitialized() = Timber.d("EmojiCompat initialized")
override fun onFailed(throwable: Throwable?) = Timber.d(throwable)
})
})
@@ -67,7 +84,18 @@ class App : Application() {
if (level >= TRIM_MEMORY_RUNNING_CRITICAL) GlobalScope.launch { RootSession.trimMemory() }
}
/**
* This method is used to log "expected" and well-handled errors, i.e. we care less about logs, etc.
* logException is inappropriate sometimes because it flushes all logs that could be used to investigate other bugs.
*/
fun logEvent(@Size(min = 1L, max = 40L) event: String, extras: Bundle? = null) {
Timber.i(if (extras == null) event else "$event, extras: $extras")
analytics.logEvent(event, extras)
}
lateinit var deviceStorage: Application
private val analytics by lazy { FirebaseAnalytics.getInstance(app.deviceStorage) }
val crashlytics by lazy { FirebaseCrashlytics.getInstance() }
val english by lazy {
createConfigurationContext(Configuration(resources.configuration).apply {
setLocale(Locale.ENGLISH)