From b596a87ffe73a189f1d63d905960bb4a61fd4f42 Mon Sep 17 00:00:00 2001
From: Philipp Woessner <64482866+pwoessner@users.noreply.github.com>
Date: Sat, 6 Jun 2020 18:57:41 +0200
Subject: [PATCH] Possibility to see exposure info on test fragment (#230)

* added possibility to see exposure info on test fragment

* fixed string concatenation
---
 .../rki/coronawarnapp/TestForAPIFragment.kt   |  4 ++--
 .../coronawarnapp/TestRiskLevelCalculation.kt | 23 ++++++++++++++++++-
 .../InternalExposureNotificationClient.kt     | 12 ++++++++++
 .../fragment_test_risk_level_calculation.xml  | 13 +++++++++++
 4 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestForAPIFragment.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestForAPIFragment.kt
index 8a2247446..d1740c85e 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestForAPIFragment.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestForAPIFragment.kt
@@ -533,13 +533,13 @@ class TestForAPIFragment : Fragment(), InternalExposureNotificationPermissionHel
         }
 
         private fun bitmapForImage(key: TemporaryExposureKey): Bitmap {
-            val key = AppleLegacyKeyExchange.Key.newBuilder()
+            val legacyKey = AppleLegacyKeyExchange.Key.newBuilder()
                 .setKeyData(ByteString.copyFrom(key.keyData))
                 .setRollingPeriod(key.rollingPeriod)
                 .setRollingStartNumber(key.rollingStartIntervalNumber)
                 .build().toByteArray()
             val bMatrix = QRCodeWriter().encode(
-                Base64.encodeToString(key, Base64.DEFAULT),
+                Base64.encodeToString(legacyKey, Base64.DEFAULT),
                 BarcodeFormat.QR_CODE,
                 300,
                 300
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestRiskLevelCalculation.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestRiskLevelCalculation.kt
index ef572b3a2..602f7322f 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestRiskLevelCalculation.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/TestRiskLevelCalculation.kt
@@ -44,7 +44,7 @@ import java.io.File
 import java.util.UUID
 import java.util.concurrent.TimeUnit
 
-@Suppress("MagicNumber")
+@Suppress("MagicNumber", "LongMethod")
 class TestRiskLevelCalculation : Fragment() {
     companion object {
         val TAG: String? = TestRiskLevelCalculation::class.simpleName
@@ -305,6 +305,27 @@ class TestRiskLevelCalculation : Fragment() {
                     binding.labelFormula.text = formulaString
 
                     binding.labelFullConfig.text = it.appConfig?.toString()
+
+                    val token = LocalData.googleApiToken()
+                    if (token != null) {
+                        val exposureInformation =
+                            InternalExposureNotificationClient.asyncGetExposureInformation(token)
+
+                        var infoString = ""
+                        exposureInformation.forEach {
+                            infoString += "Attenuation duration in min.: " +
+                                    "[${it.attenuationDurationsInMinutes?.get(0)}, " +
+                                    "${it.attenuationDurationsInMinutes?.get(1)}," +
+                                    "${it.attenuationDurationsInMinutes?.get(2)}]\n" +
+                                    "Attenuation value: ${it.attenuationValue}\n" +
+                                    "Duration in min.: ${it.durationMinutes}\n" +
+                                    "Risk Score: ${it.totalRiskScore}\n" +
+                                    "Transmission Risk Level: ${it.transmissionRiskLevel}\n" +
+                                    "Date Millis Since Epoch: ${it.dateMillisSinceEpoch}\n\n"
+                        }
+
+                        binding.labelExposureInfo.text = infoString
+                    }
                 }
             })
     }
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/nearby/InternalExposureNotificationClient.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/nearby/InternalExposureNotificationClient.kt
index 314b8d4c5..47c1ea0df 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/nearby/InternalExposureNotificationClient.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/nearby/InternalExposureNotificationClient.kt
@@ -3,6 +3,7 @@ package de.rki.coronawarnapp.nearby
 import com.google.android.gms.nearby.Nearby
 import com.google.android.gms.nearby.exposurenotification.ExposureConfiguration
 import com.google.android.gms.nearby.exposurenotification.ExposureConfiguration.ExposureConfigurationBuilder
+import com.google.android.gms.nearby.exposurenotification.ExposureInformation
 import com.google.android.gms.nearby.exposurenotification.ExposureSummary
 import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey
 import de.rki.coronawarnapp.CoronaWarnApplication
@@ -156,4 +157,15 @@ object InternalExposureNotificationClient {
                     cont.resumeWithException(it)
                 }
         }
+
+    // todo remove before release - not used in prod setup - only for testing
+    suspend fun asyncGetExposureInformation(token: String): List<ExposureInformation> =
+        suspendCoroutine { cont ->
+            exposureNotificationClient.getExposureInformation(token)
+                .addOnSuccessListener {
+                    cont.resume(it)
+                }.addOnFailureListener {
+                    cont.resumeWithException(it)
+                }
+        }
 }
diff --git a/Corona-Warn-App/src/main/res/layout/fragment_test_risk_level_calculation.xml b/Corona-Warn-App/src/main/res/layout/fragment_test_risk_level_calculation.xml
index c98bea241..8e030a1f9 100644
--- a/Corona-Warn-App/src/main/res/layout/fragment_test_risk_level_calculation.xml
+++ b/Corona-Warn-App/src/main/res/layout/fragment_test_risk_level_calculation.xml
@@ -154,6 +154,19 @@
                 android:layout_height="wrap_content"
                 android:text="-" />
 
+            <TextView
+                android:id="@+id/label_exposure_info_title"
+                style="@style/headline6"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="Exposure Information" />
+
+            <TextView
+                android:id="@+id/label_exposure_info"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="-" />
+
             <TextView
                 android:id="@+id/label_full_config_title"
                 style="@style/headline6"
-- 
GitLab