From 50e97b333ad2874105a7f0b3243183b7e1e4c7e3 Mon Sep 17 00:00:00 2001 From: Cristian Monforte Date: Fri, 22 Nov 2024 20:06:48 +0100 Subject: [PATCH] Fixes remote config not refreshed until app restart --- .../autoconsent/impl/RealAutoconsent.kt | 18 ++++++++++++++-- .../store/AutoconsentSettingsDataStore.kt | 21 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt index 772565dd45ef..88bc3f62516a 100644 --- a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt +++ b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/RealAutoconsent.kt @@ -28,11 +28,21 @@ import com.duckduckgo.autoconsent.impl.remoteconfig.AutoconsentFeature import com.duckduckgo.autoconsent.impl.store.AutoconsentSettingsRepository import com.duckduckgo.common.utils.plugins.PluginPoint import com.duckduckgo.di.scopes.AppScope +import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin import com.duckduckgo.privacy.config.api.UnprotectedTemporary import com.squareup.anvil.annotations.ContributesBinding +import com.squareup.anvil.annotations.ContributesMultibinding +import timber.log.Timber import javax.inject.Inject -@ContributesBinding(AppScope::class) +@ContributesBinding( + scope = AppScope::class, + boundType = Autoconsent::class, +) +@ContributesMultibinding( + scope = AppScope::class, + boundType = PrivacyConfigCallbackPlugin::class, +) class RealAutoconsent @Inject constructor( private val messageHandlerPlugins: PluginPoint, private val settingsRepository: AutoconsentSettingsRepository, @@ -40,7 +50,7 @@ class RealAutoconsent @Inject constructor( private val autoconsent: AutoconsentFeature, private val userAllowlistRepository: UserAllowListRepository, private val unprotectedTemporary: UnprotectedTemporary, -) : Autoconsent { +) : Autoconsent, PrivacyConfigCallbackPlugin { private lateinit var autoconsentJs: String @@ -108,4 +118,8 @@ class RealAutoconsent @Inject constructor( } return autoconsentJs } + + override fun onPrivacyConfigDownloaded() { + settingsRepository.refresh() + } } diff --git a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt index a58f5aa57a3d..bc92757738b7 100644 --- a/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt +++ b/autoconsent/autoconsent-impl/src/main/java/com/duckduckgo/autoconsent/impl/store/AutoconsentSettingsDataStore.kt @@ -21,12 +21,15 @@ import android.content.SharedPreferences import androidx.core.content.edit import com.duckduckgo.autoconsent.impl.remoteconfig.AutoconsentFeature import com.duckduckgo.common.utils.DispatcherProvider +import com.duckduckgo.common.utils.checkMainThread import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch +import timber.log.Timber interface AutoconsentSettingsDataStore { var userSetting: Boolean var firstPopupHandled: Boolean + fun refresh() } class RealAutoconsentSettingsDataStore constructor( @@ -38,7 +41,16 @@ class RealAutoconsentSettingsDataStore constructor( private val preferences: SharedPreferences by lazy { context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE) } private var cachedInternalUserSetting: Boolean? = null - private val defaultValue: Boolean by lazy { autoconsentFeature.onByDefault().isEnabled() } + + private var _defaultValue: Boolean? = null + private val defaultValue: Boolean + get() { + if (_defaultValue == null) { + checkMainThread() + _defaultValue = autoconsentFeature.onByDefault().isEnabled() + } + return _defaultValue!! + } init { appCoroutineScope.launch(dispatcherProvider.io()) { @@ -68,6 +80,13 @@ class RealAutoconsentSettingsDataStore constructor( } } + override fun refresh() { + appCoroutineScope.launch(dispatcherProvider.io()) { + _defaultValue = autoconsentFeature.onByDefault().isEnabled() + cachedInternalUserSetting = null // invalidate cache + } + } + companion object { private const val FILENAME = "com.duckduckgo.autoconsent.store.settings" private const val AUTOCONSENT_USER_SETTING = "AutoconsentUserSetting"