|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.app.attributed.metrics.impl |
| 18 | + |
| 19 | +import androidx.lifecycle.LifecycleOwner |
| 20 | +import com.duckduckgo.app.attributed.metrics.AttributedMetricsConfigFeature |
| 21 | +import com.duckduckgo.app.attributed.metrics.store.AttributedMetricsDataStore |
| 22 | +import com.duckduckgo.app.attributed.metrics.store.DateProvider |
| 23 | +import com.duckduckgo.app.di.AppCoroutineScope |
| 24 | +import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver |
| 25 | +import com.duckduckgo.app.statistics.api.AtbLifecyclePlugin |
| 26 | +import com.duckduckgo.appbuildconfig.api.AppBuildConfig |
| 27 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 28 | +import com.duckduckgo.di.scopes.AppScope |
| 29 | +import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin |
| 30 | +import com.squareup.anvil.annotations.ContributesBinding |
| 31 | +import com.squareup.anvil.annotations.ContributesMultibinding |
| 32 | +import dagger.SingleInstanceIn |
| 33 | +import kotlinx.coroutines.CoroutineScope |
| 34 | +import kotlinx.coroutines.launch |
| 35 | +import logcat.logcat |
| 36 | +import java.time.LocalDate |
| 37 | +import java.time.format.DateTimeFormatter |
| 38 | +import java.time.temporal.ChronoUnit |
| 39 | +import javax.inject.Inject |
| 40 | + |
| 41 | +/** |
| 42 | + * Interface for checking if attributed metrics are active. |
| 43 | + * The active state is determined by: |
| 44 | + * 1. Having an initialization date |
| 45 | + * 2. Being within the collection period (6 months = 24 weeks) |
| 46 | + * 3. Being enabled in remote config |
| 47 | + */ |
| 48 | +interface AttributedMetricsState { |
| 49 | + suspend fun isActive(): Boolean |
| 50 | +} |
| 51 | + |
| 52 | +@ContributesBinding( |
| 53 | + scope = AppScope::class, |
| 54 | + boundType = AttributedMetricsState::class, |
| 55 | +) |
| 56 | +@ContributesMultibinding( |
| 57 | + scope = AppScope::class, |
| 58 | + boundType = MainProcessLifecycleObserver::class, |
| 59 | +) |
| 60 | +@ContributesMultibinding( |
| 61 | + scope = AppScope::class, |
| 62 | + boundType = AtbLifecyclePlugin::class, |
| 63 | +) |
| 64 | +@ContributesMultibinding( |
| 65 | + scope = AppScope::class, |
| 66 | + boundType = PrivacyConfigCallbackPlugin::class, |
| 67 | +) |
| 68 | +@SingleInstanceIn(AppScope::class) |
| 69 | +class RealAttributedMetricsState @Inject constructor( |
| 70 | + @AppCoroutineScope private val appCoroutineScope: CoroutineScope, |
| 71 | + private val dispatcherProvider: DispatcherProvider, |
| 72 | + private val dataStore: AttributedMetricsDataStore, |
| 73 | + private val attributedMetricsConfigFeature: AttributedMetricsConfigFeature, |
| 74 | + private val appBuildConfig: AppBuildConfig, |
| 75 | + private val dateProvider: DateProvider, |
| 76 | +) : AttributedMetricsState, MainProcessLifecycleObserver, AtbLifecyclePlugin, PrivacyConfigCallbackPlugin { |
| 77 | + |
| 78 | + override fun onCreate(owner: LifecycleOwner) { |
| 79 | + appCoroutineScope.launch(dispatcherProvider.io()) { |
| 80 | + checkCollectionPeriodAndUpdateState() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // this is called when the ATB is initialized after privacy config is downloaded, only once after app is installed |
| 85 | + override fun onAppAtbInitialized() { |
| 86 | + appCoroutineScope.launch(dispatcherProvider.io()) { |
| 87 | + logcat(tag = "AttributedMetrics") { |
| 88 | + "Detected New Install, try to initialize Attributed Metrics" |
| 89 | + } |
| 90 | + if (attributedMetricsConfigFeature.self().isEnabled().not()) { |
| 91 | + logcat(tag = "AttributedMetrics") { |
| 92 | + "Client disabled from remote config, skipping initialization" |
| 93 | + } |
| 94 | + return@launch |
| 95 | + } |
| 96 | + |
| 97 | + val initDate = dataStore.getInitializationDate() |
| 98 | + if (initDate == null) { |
| 99 | + logcat(tag = "AttributedMetrics") { |
| 100 | + "Setting initialization date for Attributed Metrics" |
| 101 | + } |
| 102 | + val currentDate = dateProvider.getCurrentDate() |
| 103 | + dataStore.setInitializationDate(currentDate) |
| 104 | + if (appBuildConfig.isAppReinstall()) { |
| 105 | + logcat(tag = "AttributedMetrics") { |
| 106 | + "App reinstall detected, attributed metrics will not be active" |
| 107 | + } |
| 108 | + // Do not start metrics for returning users |
| 109 | + dataStore.setActive(false) |
| 110 | + } else { |
| 111 | + logcat(tag = "AttributedMetrics") { |
| 112 | + "New install detected, attributed metrics active" |
| 113 | + } |
| 114 | + dataStore.setActive(true) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + override fun onPrivacyConfigDownloaded() { |
| 121 | + appCoroutineScope.launch(dispatcherProvider.io()) { |
| 122 | + val toggleEnabledState = attributedMetricsConfigFeature.self().isEnabled() |
| 123 | + logcat(tag = "AttributedMetrics") { |
| 124 | + "Privacy config downloaded, attributed metrics enabled: $toggleEnabledState," + |
| 125 | + " client state: ${dataStore.isActive()}-${dataStore.getInitializationDate()}" |
| 126 | + } |
| 127 | + dataStore.setEnabled(toggleEnabledState) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + override suspend fun isActive(): Boolean = dataStore.isActive() && dataStore.isEnabled() && dataStore.getInitializationDate() != null |
| 132 | + |
| 133 | + private suspend fun checkCollectionPeriodAndUpdateState() { |
| 134 | + val initDate = dataStore.getInitializationDate() |
| 135 | + |
| 136 | + if (initDate == null) { |
| 137 | + logcat(tag = "AttributedMetrics") { |
| 138 | + "Client not initialized, skipping state check" |
| 139 | + } |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + val initLocalDate = LocalDate.parse(initDate, DATE_FORMATTER) |
| 144 | + val currentDate = LocalDate.now() |
| 145 | + |
| 146 | + val daysSinceInit = ChronoUnit.DAYS.between(initLocalDate, currentDate) |
| 147 | + val isWithinPeriod = daysSinceInit <= COLLECTION_PERIOD_DAYS |
| 148 | + val isClientActive = isWithinPeriod && dataStore.isActive() |
| 149 | + |
| 150 | + logcat(tag = "AttributedMetrics") { |
| 151 | + "Updating client state to $isClientActive, within period? $isWithinPeriod, is client active? ${dataStore.isActive()}" |
| 152 | + } |
| 153 | + dataStore.setActive(isClientActive) |
| 154 | + } |
| 155 | + |
| 156 | + companion object { |
| 157 | + private val DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
| 158 | + private const val COLLECTION_PERIOD_DAYS = 168 // 24 weeks * 7 days (6 months in weeks) |
| 159 | + } |
| 160 | +} |
0 commit comments