wulkanowy-mod/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt

105 lines
3.2 KiB
Kotlin
Raw Normal View History

2018-07-18 14:13:57 -05:00
package io.github.wulkanowy
2021-03-02 17:34:25 -06:00
import android.annotation.SuppressLint
2020-08-02 11:29:41 -05:00
import android.app.Application
2018-08-22 15:31:17 -05:00
import android.content.Context
2020-02-22 14:24:06 -06:00
import android.util.Log.DEBUG
import android.util.Log.INFO
import android.util.Log.VERBOSE
import android.webkit.WebView
2021-03-02 17:34:25 -06:00
import androidx.fragment.app.FragmentManager
2020-08-02 11:29:41 -05:00
import androidx.hilt.work.HiltWorkerFactory
2018-10-20 13:59:46 -05:00
import androidx.multidex.MultiDex
import androidx.work.Configuration
2019-11-03 05:37:03 -06:00
import com.yariksoffice.lingver.Lingver
2020-08-02 11:29:41 -05:00
import dagger.hilt.android.HiltAndroidApp
2020-02-22 14:24:06 -06:00
import fr.bipi.tressence.file.FileLoggerTree
import io.github.wulkanowy.data.repositories.PreferencesRepository
2019-08-26 13:54:20 -05:00
import io.github.wulkanowy.ui.base.ThemeManager
import io.github.wulkanowy.utils.ActivityLifecycleLogger
import io.github.wulkanowy.utils.AnalyticsHelper
import io.github.wulkanowy.utils.AppInfo
2020-10-29 07:58:56 -05:00
import io.github.wulkanowy.utils.CrashLogExceptionTree
import io.github.wulkanowy.utils.CrashLogTree
2018-10-03 14:28:23 -05:00
import io.github.wulkanowy.utils.DebugLogTree
2018-07-18 14:13:57 -05:00
import timber.log.Timber
import javax.inject.Inject
2018-07-18 14:13:57 -05:00
2020-08-02 11:29:41 -05:00
@HiltAndroidApp
class WulkanowyApp : Application(), Configuration.Provider {
2018-07-18 14:13:57 -05:00
@Inject
2020-08-02 11:29:41 -05:00
lateinit var workerFactory: HiltWorkerFactory
2019-08-26 13:54:20 -05:00
@Inject
lateinit var themeManager: ThemeManager
@Inject
lateinit var appInfo: AppInfo
@Inject
lateinit var preferencesRepository: PreferencesRepository
@Inject
lateinit var analyticsHelper: AnalyticsHelper
2018-08-22 15:31:17 -05:00
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
MultiDex.install(this)
}
2018-07-18 14:13:57 -05:00
2021-03-02 17:34:25 -06:00
@SuppressLint("UnsafeOptInUsageWarning")
2018-07-18 14:13:57 -05:00
override fun onCreate() {
super.onCreate()
2021-03-02 17:34:25 -06:00
FragmentManager.enableNewStateManager(false)
2019-04-19 16:52:34 -05:00
initializeAppLanguage()
themeManager.applyDefaultTheme()
2019-04-19 16:52:34 -05:00
initLogging()
fixWebViewLocale()
}
2019-04-19 16:52:34 -05:00
private fun initLogging() {
if (appInfo.isDebug) {
2020-02-22 14:24:06 -06:00
Timber.plant(DebugLogTree())
Timber.plant(
FileLoggerTree.Builder()
.withFileName("wulkanowy.%g.log")
.withDirName(applicationContext.filesDir.absolutePath)
.withFileLimit(10)
.withMinPriority(DEBUG)
.build()
2020-02-22 14:24:06 -06:00
)
2019-04-19 16:52:34 -05:00
} else {
2020-10-29 07:58:56 -05:00
Timber.plant(CrashLogExceptionTree())
Timber.plant(CrashLogTree())
2019-04-19 16:52:34 -05:00
}
registerActivityLifecycleCallbacks(ActivityLifecycleLogger())
2018-07-18 14:13:57 -05:00
}
private fun initializeAppLanguage() {
Lingver.init(this)
if (preferencesRepository.appLanguage == "system") {
Lingver.getInstance().setFollowSystemLocale(this)
analyticsHelper.logEvent("language", "startup" to appInfo.systemLanguage)
} else {
analyticsHelper.logEvent("language", "startup" to preferencesRepository.appLanguage)
}
}
private fun fixWebViewLocale() {
//https://stackoverflow.com/questions/40398528/android-webview-language-changes-abruptly-on-android-7-0-and-above
2021-02-03 13:59:24 -06:00
try {
WebView(this).destroy()
} catch (e: Exception) {
//Ignore exceptions
}
}
2019-09-01 06:24:21 -05:00
override fun getWorkManagerConfiguration() = Configuration.Builder()
.setWorkerFactory(workerFactory)
.setMinimumLoggingLevel(if (appInfo.isDebug) VERBOSE else INFO)
.build()
2018-07-18 14:13:57 -05:00
}