From 110157dab8097bc6c6589c950483731f4175320d Mon Sep 17 00:00:00 2001 From: Marvin W Date: Fri, 14 Jan 2022 10:46:59 +0100 Subject: [PATCH] ENF: Handle issue with negative or 0 durations due to system time changes With time synchronisation and can happen that timestamps in database are newer than reported system time, which then can lead to reported exposure with multiple measurements but negative or zero reported duration, resulting in divide-by-zero and other issues in average RSSI calculation. This fixes the issue, by ignoring new measurements of the same RPI when they are seemingly older than a previous measurement. --- .../microg/gms/nearby/exposurenotification/ExposureDatabase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureDatabase.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureDatabase.kt index a4323a2a..3166fb9a 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureDatabase.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureDatabase.kt @@ -184,7 +184,7 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit } fun noteAdvertisement(rpi: ByteArray, aem: ByteArray, rssi: Int, timestamp: Long = Date().time) = writableDatabase.run { - val update = compileStatement("UPDATE $TABLE_ADVERTISEMENTS SET rssi = ((rssi * duration) + (? * (? - timestamp - duration))) / (? - timestamp), duration = (? - timestamp) WHERE rpi = ? AND timestamp > ? AND timestamp < ?").run { + val update = compileStatement("UPDATE $TABLE_ADVERTISEMENTS SET rssi = IFNULL(((rssi * duration) + (? * MIN(0, ? - timestamp - duration))) / MAX(duration, ? - timestamp), -100), duration = MAX(duration, ? - timestamp) WHERE rpi = ? AND timestamp > ? AND timestamp < ?").run { bindLong(1, rssi.toLong()) bindLong(2, timestamp) bindLong(3, timestamp)