From be9384c3c65c77233b50b3e9723ec00b454716e2 Mon Sep 17 00:00:00 2001
From: Philipp Woessner <64482866+pwoessner@users.noreply.github.com>
Date: Wed, 1 Jul 2020 10:10:08 +0200
Subject: [PATCH] Prevent automatic risk calculation in manual mode on app
 start (#795)

* added a check for manual mode when updating on app start

* added logging for conditions

* added comments
---
 .../ui/viewmodel/TracingViewModel.kt          | 35 ++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt
index d28c0f0b5..9ec236574 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/viewmodel/TracingViewModel.kt
@@ -62,28 +62,55 @@ class TracingViewModel : ViewModel() {
     fun refreshRiskLevel() {
         viewModelScope.launch {
             try {
+
+                // get the current date and the date the diagnosis keys were fetched the last time
                 val currentDate = DateTime(Instant.now(), DateTimeZone.getDefault())
                 val lastFetch = DateTime(
                     LocalData.lastTimeDiagnosisKeysFromServerFetch(),
                     DateTimeZone.getDefault()
                 )
-                val keysWereNotRetrievedToday = LocalData.lastTimeDiagnosisKeysFromServerFetch() == null ||
-                        currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
-                val isNetworkEnabled = ConnectivityHelper.isNetworkEnabled(CoronaWarnApplication.getAppContext())
-                if (keysWereNotRetrievedToday && isNetworkEnabled) {
+
+                // check if the keys were not already retrieved today
+                val keysWereNotRetrievedToday =
+                    LocalData.lastTimeDiagnosisKeysFromServerFetch() == null ||
+                            currentDate.withTimeAtStartOfDay() != lastFetch.withTimeAtStartOfDay()
+
+                // check if the network is enabled to make the server fetch
+                val isNetworkEnabled =
+                    ConnectivityHelper.isNetworkEnabled(CoronaWarnApplication.getAppContext())
+
+                // only fetch the diagnosis keys if background jobs are enabled, so that in manual
+                // model the keys are only fetched on button press of the user
+                val isBackgroundJobEnabled =
+                    ConnectivityHelper.isBackgroundJobEnabled(CoronaWarnApplication.getAppContext())
+
+                Timber.v("Keys were not retrieved today $keysWereNotRetrievedToday")
+                Timber.v("Network is enabled $isNetworkEnabled")
+                Timber.v("Background jobs are enabled $isBackgroundJobEnabled")
+
+                if (keysWereNotRetrievedToday && isNetworkEnabled && isBackgroundJobEnabled) {
                     TracingRepository.isRefreshing.value = true
+
+                    // start the fetching and submitting of the diagnosis keys
                     RetrieveDiagnosisKeysTransaction.start()
                     refreshLastTimeDiagnosisKeysFetchedDate()
                     TimerHelper.checkManualKeyRetrievalTimer()
                 }
             } catch (e: TransactionException) {
                 e.cause?.report(INTERNAL)
+            } catch (e: Exception) {
+                e.report(INTERNAL)
             }
+
+            // refresh the risk level
             try {
                 RiskLevelTransaction.start()
             } catch (e: TransactionException) {
                 e.cause?.report(INTERNAL)
+            } catch (e: Exception) {
+                e.report(INTERNAL)
             }
+
             TracingRepository.isRefreshing.value = false
         }
     }
-- 
GitLab