From e30a95ee2e40fa1e1c569e5bb8dc363c30e41d0c Mon Sep 17 00:00:00 2001
From: Kolya Opahle <k.opahle@sap.com>
Date: Fri, 22 Jan 2021 09:39:39 +0100
Subject: [PATCH] Notification to open app from time to time comes back every
 few minutes (EXPOSUREAPP-4623) #2128

* changed ExistingPeriodicWorkPolicy to KEEP for deadman check task to stop notification loop on every app open

Signed-off-by: Kolya Opahle <k.opahle@sap.com>

* fixed tests

Signed-off-by: Kolya Opahle <k.opahle@sap.com>

* Added info text to the schedulePeriodic doc ...

... to explain the change to ExistingPeriodicWorkPolicy

* Fixed linting

Co-authored-by: Ralf Gehrer <ralfgehrer@users.noreply.github.com>
Co-authored-by: harambasicluka <64483219+harambasicluka@users.noreply.github.com>
---
 .../coronawarnapp/deadman/DeadmanNotificationScheduler.kt | 8 ++++++--
 .../deadman/DeadmanNotificationSchedulerTest.kt           | 4 ++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/deadman/DeadmanNotificationScheduler.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/deadman/DeadmanNotificationScheduler.kt
index d3b1523be..7e14369e7 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/deadman/DeadmanNotificationScheduler.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/deadman/DeadmanNotificationScheduler.kt
@@ -35,13 +35,17 @@ class DeadmanNotificationScheduler @Inject constructor(
 
     /**
      * Enqueue background deadman notification onetime work
-     * Replace with new if older work exists.
+     * Do -NOT- Replace with new if older work exists.
+     * As this will result in the notification spam seen in the past
+     * because running a new check every time the app is opened
+     * spawns a notification every time the last check
+     * was more than 35 Hours ago.
      */
     fun schedulePeriodic() {
         // Create unique work and enqueue
         workManager.enqueueUniquePeriodicWork(
             PERIODIC_WORK_NAME,
-            ExistingPeriodicWorkPolicy.REPLACE,
+            ExistingPeriodicWorkPolicy.KEEP,
             workBuilder.buildPeriodicWork()
         )
     }
diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/deadman/DeadmanNotificationSchedulerTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/deadman/DeadmanNotificationSchedulerTest.kt
index 26e170849..1ec45a093 100644
--- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/deadman/DeadmanNotificationSchedulerTest.kt
+++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/deadman/DeadmanNotificationSchedulerTest.kt
@@ -36,7 +36,7 @@ class DeadmanNotificationSchedulerTest : BaseTest() {
         every {
             workManager.enqueueUniquePeriodicWork(
                 DeadmanNotificationScheduler.PERIODIC_WORK_NAME,
-                ExistingPeriodicWorkPolicy.REPLACE,
+                ExistingPeriodicWorkPolicy.KEEP,
                 any()
             )
         } returns operation
@@ -104,7 +104,7 @@ class DeadmanNotificationSchedulerTest : BaseTest() {
         verifySequence {
             workManager.enqueueUniquePeriodicWork(
                 DeadmanNotificationScheduler.PERIODIC_WORK_NAME,
-                ExistingPeriodicWorkPolicy.REPLACE,
+                ExistingPeriodicWorkPolicy.KEEP,
                 periodicWorkRequest
             )
         }
-- 
GitLab