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

Fix: Installation time (EXPOSUREAPP-14249) #5665

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package de.rki.coronawarnapp.installTime

import android.content.Context
import de.rki.coronawarnapp.util.di.AppContext
import de.rki.coronawarnapp.util.TimeStamper
import de.rki.coronawarnapp.util.di.AppInstallTime
import de.rki.coronawarnapp.util.toLocalDateUserTz
import java.time.Instant
import java.time.LocalDate
import java.time.Period
import java.time.temporal.ChronoUnit

import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class InstallTimeProvider @Inject constructor(
@AppContext private val context: Context
@AppInstallTime private val installTime: Instant,
private val timeStamper: TimeStamper
) {
private val dayOfInstallation: LocalDate = Instant.ofEpochMilli(
context
.packageManager
.getPackageInfo(context.packageName, 0)
.firstInstallTime
)
.toLocalDateUserTz()
private val dayOfInstallation = installTime.toLocalDateUserTz()

val today: LocalDate
get() = Instant.now().toLocalDateUserTz()
get() = timeStamper.nowUTC.toLocalDateUserTz()

val daysSinceInstallation: Int
get() = Period.between(dayOfInstallation, today).days
get() = ChronoUnit.DAYS.between(dayOfInstallation, today).toInt()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ class AndroidModule {
@Provides
@AppInstallTime
fun installTime(@AppContext context: Context): Instant =
context.packageManager.getPackageInfo(context.packageName, 0).firstInstallTime.run {
Instant.ofEpochMilli(this)
}
context
.packageManager
.getPackageInfo(context.packageName, 0)
.firstInstallTime.run {
Instant.ofEpochMilli(this)
}

@Suppress("DEPRECATION")
@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.rki.coronawarnapp.installTime

import de.rki.coronawarnapp.util.TimeStamper
import io.kotest.matchers.shouldBe
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import testhelpers.BaseTest
import java.time.Instant

internal class InstallTimeProviderTest : BaseTest() {
@MockK lateinit var timeStamper: TimeStamper

@BeforeEach
fun setup() {
MockKAnnotations.init(this)
}

@Test
fun `Days since installation across years`() {
every { timeStamper.nowUTC } returns Instant.parse("2022-11-03T05:35:16.000Z")

InstallTimeProvider(
installTime = Instant.parse("2020-11-03T05:35:16.000Z"),
timeStamper = timeStamper
).daysSinceInstallation shouldBe 730
}

@Test
fun `Days since installation across months`() {
every { timeStamper.nowUTC } returns Instant.parse("2020-12-03T05:35:16.000Z")

InstallTimeProvider(
installTime = Instant.parse("2020-11-03T05:35:16.000Z"),
timeStamper = timeStamper
).daysSinceInstallation shouldBe 30
}

@Test
fun `Days since installation across days`() {
every { timeStamper.nowUTC } returns Instant.parse("2022-11-03T05:35:16.000Z")

InstallTimeProvider(
installTime = Instant.parse("2022-10-24T05:35:16.000Z"),
timeStamper = timeStamper
).daysSinceInstallation shouldBe 10
}

@Test
fun `Days since installation same day`() {
every { timeStamper.nowUTC } returns Instant.parse("2022-11-03T18:35:16.000Z")

InstallTimeProvider(
installTime = Instant.parse("2022-11-03T05:35:16.000Z"),
timeStamper = timeStamper
).daysSinceInstallation shouldBe 0
}
}