Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Prevent statistics from being downloaded when EOL on (EXPOSUREAPP-149…
Browse files Browse the repository at this point in the history
…74) (#5905)

* Prevent statistics download on EOL.

* Lint.

* Fix test.
  • Loading branch information
SamuraiKek committed Mar 21, 2023
1 parent 35dedbe commit 7eefe6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
@@ -1,5 +1,6 @@
package de.rki.coronawarnapp.statistics.source

import de.rki.coronawarnapp.eol.AppEol
import de.rki.coronawarnapp.statistics.StatisticsData
import de.rki.coronawarnapp.util.HashExtensions.toHexString
import de.rki.coronawarnapp.util.coroutine.AppScope
Expand All @@ -10,6 +11,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
Expand All @@ -24,7 +26,8 @@ class StatisticsProvider @Inject constructor(
private val localCache: StatisticsCache,
private val parser: StatisticsParser,
foregroundState: ForegroundState,
dispatcherProvider: DispatcherProvider
dispatcherProvider: DispatcherProvider,
appEol: AppEol
) {

private val statisticsData = HotDataFlow(
Expand All @@ -36,16 +39,21 @@ class StatisticsProvider @Inject constructor(
replayExpirationMillis = 0
)
) {
try {
val cachedValues = fromCache()
if (cachedValues == null) {
triggerUpdate()
if (!appEol.isEol.first()) {
try {
val cachedValues = fromCache()
if (cachedValues == null) {
triggerUpdate()
StatisticsData.DEFAULT
} else {
cachedValues
}
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Failed to get data from server.")
StatisticsData.DEFAULT
} else {
cachedValues
}
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Failed to get data from server.")
} else {
Timber.tag(TAG).e("Canceled statistics update, app is in EOL mode.")
StatisticsData.DEFAULT
}
}
Expand All @@ -55,12 +63,12 @@ class StatisticsProvider @Inject constructor(
init {
foregroundState.isInForeground
.onEach {
if (it) {
if (it && !appEol.isEol.first()) {
Timber.tag(TAG).d("App moved to foreground triggering statistics update.")
triggerUpdate()
}
}
.catch { Timber.tag(TAG).e("Failed to trigger statistics update.") }
.catch { Timber.tag(TAG).e("Failed to trigger statistics update or EOL has started.") }
.launchIn(scope)
}

Expand Down
@@ -1,5 +1,6 @@
package de.rki.coronawarnapp.statistics.source

import de.rki.coronawarnapp.eol.AppEol
import de.rki.coronawarnapp.statistics.StatisticsData
import de.rki.coronawarnapp.util.device.ForegroundState
import io.kotest.matchers.shouldBe
Expand All @@ -13,6 +14,7 @@ import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.jupiter.api.BeforeEach
Expand All @@ -29,6 +31,7 @@ class StatisticsProviderTest : BaseTest() {
@MockK lateinit var parser: StatisticsParser
@MockK lateinit var foregroundState: ForegroundState
@MockK lateinit var statisticsData: StatisticsData
@MockK lateinit var appEol: AppEol

private val testData = "ABC".encodeToByteArray()

Expand All @@ -45,6 +48,7 @@ class StatisticsProviderTest : BaseTest() {
every { parser.parse(testData) } returns statisticsData
every { foregroundState.isInForeground } returns testForegroundState
every { statisticsData.isDataAvailable } returns true
every { appEol.isEol } returns flowOf(false)

localCache.apply {
var testLocalCache: ByteArray? = null
Expand All @@ -59,7 +63,8 @@ class StatisticsProviderTest : BaseTest() {
localCache = localCache,
parser = parser,
foregroundState = foregroundState,
dispatcherProvider = scope.asDispatcherProvider()
dispatcherProvider = scope.asDispatcherProvider(),
appEol = appEol
)

@Test
Expand Down

0 comments on commit 7eefe6d

Please sign in to comment.