Use user agent template from firebase remote config (#2139)

* Use user agent template from firebase remote config

* Improve base class usage, activation refactor
This commit is contained in:
Mikołaj Pich 2023-03-07 18:10:20 +01:00 committed by GitHub
parent a495fcbc5f
commit 5331bf90cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 1 deletions

View File

@ -246,6 +246,7 @@ dependencies {
playImplementation 'com.google.firebase:firebase-analytics-ktx'
playImplementation 'com.google.firebase:firebase-messaging:'
playImplementation 'com.google.firebase:firebase-crashlytics:'
playImplementation 'com.google.firebase:firebase-config-ktx'
playImplementation 'com.google.android.play:core:1.10.3'
playImplementation 'com.google.android.play:core-ktx:1.8.1'
playImplementation 'com.google.android.gms:play-services-ads:21.4.0'

View File

@ -0,0 +1,7 @@
package io.github.wulkanowy.utils
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class RemoteConfigHelper @Inject constructor() : BaseRemoteConfigHelper()

View File

@ -0,0 +1,7 @@
package io.github.wulkanowy.utils
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class RemoteConfigHelper @Inject constructor() : BaseRemoteConfigHelper()

View File

@ -34,11 +34,15 @@ class WulkanowyApp : Application(), Configuration.Provider {
@Inject
lateinit var adsHelper: AdsHelper
@Inject
lateinit var remoteConfigHelper: RemoteConfigHelper
override fun onCreate() {
super.onCreate()
initializeAppLanguage()
themeManager.applyDefaultTheme()
adsHelper.initialize()
remoteConfigHelper.initialize()
initLogging()
}

View File

@ -19,6 +19,7 @@ import io.github.wulkanowy.data.db.SharedPrefProvider
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.AppInfo
import io.github.wulkanowy.utils.RemoteConfigHelper
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
@ -36,10 +37,11 @@ internal class DataModule {
@Singleton
@Provides
fun provideSdk(chuckerInterceptor: ChuckerInterceptor) =
fun provideSdk(chuckerInterceptor: ChuckerInterceptor, remoteConfig: RemoteConfigHelper) =
Sdk().apply {
androidVersion = android.os.Build.VERSION.RELEASE
buildTag = android.os.Build.MODEL
userAgentTemplate = remoteConfig.userAgentTemplate
setSimpleHttpLogger { Timber.d(it) }
// for debug only

View File

@ -0,0 +1,9 @@
package io.github.wulkanowy.utils
abstract class BaseRemoteConfigHelper {
open fun initialize() = Unit
open val userAgentTemplate: String
get() = RemoteConfigDefaults.USER_AGENT_TEMPLATE.value as String
}

View File

@ -0,0 +1,8 @@
package io.github.wulkanowy.utils
enum class RemoteConfigDefaults(val value: Any) {
USER_AGENT_TEMPLATE(""),
;
val key get() = name.lowercase()
}

View File

@ -0,0 +1,35 @@
package io.github.wulkanowy.utils
import android.content.Context
import com.google.firebase.FirebaseApp
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class RemoteConfigHelper @Inject constructor(
@ApplicationContext private val context: Context,
private val appInfo: AppInfo,
) : BaseRemoteConfigHelper() {
fun initialize() {
FirebaseApp.initializeApp(context)
Firebase.remoteConfig.setConfigSettingsAsync(remoteConfigSettings {
fetchTimeoutInSeconds = 3
if (appInfo.isDebug) {
minimumFetchIntervalInSeconds = 0
}
})
Firebase.remoteConfig.setDefaultsAsync(RemoteConfigDefaults.values().associate {
it.key to it.value
})
Firebase.remoteConfig.fetchAndActivate()
}
override val userAgentTemplate: String
get() = Firebase.remoteConfig.getString(RemoteConfigDefaults.USER_AGENT_TEMPLATE.key)
}