From 2b09a5e530fc9733b41063ce8fcf88007a896d84 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 3 Dec 2020 10:15:18 +0100 Subject: [PATCH] EN: Ensure correct handling of new provideDiagnosisKeys() Fixes #1300 --- play-services-nearby-api/build.gradle | 2 ++ .../internal/ProvideDiagnosisKeysParams.java | 7 ++++++ .../exposurenotification/ExposureDatabase.kt | 15 +++++++++++++ .../ExposureNotificationServiceImpl.kt | 22 ++++++++++--------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/play-services-nearby-api/build.gradle b/play-services-nearby-api/build.gradle index 903c8331d4..132747d26f 100644 --- a/play-services-nearby-api/build.gradle +++ b/play-services-nearby-api/build.gradle @@ -30,4 +30,6 @@ description = 'microG API for play-services-nearby' dependencies { api project(':play-services-basement') api project(':play-services-base-api') + + implementation "androidx.annotation:annotation:$annotationVersion" } diff --git a/play-services-nearby-api/src/main/java/com/google/android/gms/nearby/exposurenotification/internal/ProvideDiagnosisKeysParams.java b/play-services-nearby-api/src/main/java/com/google/android/gms/nearby/exposurenotification/internal/ProvideDiagnosisKeysParams.java index a38a8c88d1..fc044c2c6e 100644 --- a/play-services-nearby-api/src/main/java/com/google/android/gms/nearby/exposurenotification/internal/ProvideDiagnosisKeysParams.java +++ b/play-services-nearby-api/src/main/java/com/google/android/gms/nearby/exposurenotification/internal/ProvideDiagnosisKeysParams.java @@ -7,6 +7,8 @@ import android.os.ParcelFileDescriptor; +import androidx.annotation.Nullable; + import com.google.android.gms.common.api.internal.IStatusCallback; import com.google.android.gms.nearby.exposurenotification.ExposureConfiguration; import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey; @@ -17,16 +19,21 @@ public class ProvideDiagnosisKeysParams extends AutoSafeParcelable { @Field(1) + @Nullable public List keys; @Field(2) public IStatusCallback callback; @Field(3) + @Nullable public List keyFiles; @Field(4) + @Nullable public ExposureConfiguration configuration; @Field(5) + @Nullable public String token; @Field(6) + @Nullable public IDiagnosisKeyFileSupplier keyFileSupplier; private ProvideDiagnosisKeysParams() { 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 1f29eecd27..4fcf3d01fa 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 @@ -22,6 +22,7 @@ import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey import kotlinx.coroutines.* import okio.ByteString import org.json.JSONObject +import org.microg.gms.nearby.exposurenotification.Constants.TOKEN_A import java.io.File import java.lang.Runnable import java.nio.ByteBuffer @@ -223,6 +224,20 @@ class ExposureDatabase private constructor(private val context: Context) : SQLit } } + fun getOrCreateTokenId(packageName: String, token: String, database: SQLiteDatabase = writableDatabase) = database.run { + val tid = getTokenId(packageName, token, this) + if (tid != null) { + tid + } else { + insert(TABLE_TOKENS, "NULL", ContentValues().apply { + put("package", packageName) + put("token", token) + put("timestamp", System.currentTimeMillis()) + }) + getTokenId(packageName, token, this) + } + } + private fun storeSingleDiagnosisKey(tid: Long, key: TemporaryExposureKey, database: SQLiteDatabase = writableDatabase) = database.run { val tcsid = getTekCheckSingleId(key, true, database) insert(TABLE_TEK_CHECK_SINGLE_TOKEN, "NULL", ContentValues().apply { diff --git a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureNotificationServiceImpl.kt b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureNotificationServiceImpl.kt index 6dbe9719b6..42e7a65729 100644 --- a/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureNotificationServiceImpl.kt +++ b/play-services-nearby-core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ExposureNotificationServiceImpl.kt @@ -215,17 +215,19 @@ class ExposureNotificationServiceImpl(private val context: Context, private val } override fun provideDiagnosisKeys(params: ProvideDiagnosisKeysParams) { - Log.w(TAG, "provideDiagnosisKeys() with $packageName/${params.token}") + val token = params.token ?: TOKEN_A + Log.w(TAG, "provideDiagnosisKeys() with $packageName/$token") lifecycleScope.launchWhenStarted { val tid = ExposureDatabase.with(context) { database -> - if (params.configuration != null) { - database.storeConfiguration(packageName, params.token, params.configuration) + val configuration = params.configuration + if (configuration != null) { + database.storeConfiguration(packageName, token, configuration) } else { - database.getTokenId(packageName, params.token) + database.getOrCreateTokenId(packageName, token) } } if (tid == null) { - Log.w(TAG, "Unknown token without configuration: $packageName/${params.token}") + Log.w(TAG, "Unknown token without configuration: $packageName/$token") try { params.callback.onResult(Status.INTERNAL_ERROR) } catch (e: Exception) { @@ -281,7 +283,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val if (todoKeyFiles.size > 0) { val time = (System.currentTimeMillis() - start).coerceAtLeast(1).toDouble() / 1000.0 - Log.d(TAG, "$packageName/${params.token} processed $keys keys (${todoKeyFiles.size} files pending) in ${time}s -> ${(keys.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s") + Log.d(TAG, "$packageName/$token processed $keys keys (${todoKeyFiles.size} files pending) in ${time}s -> ${(keys.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s") } Handler(Looper.getMainLooper()).post { @@ -323,16 +325,16 @@ class ExposureNotificationServiceImpl(private val context: Context, private val } val time = (System.currentTimeMillis() - start).coerceAtLeast(1).toDouble() / 1000.0 - Log.d(TAG, "$packageName/${params.token} processed $keys keys ($newKeys new) in ${time}s -> ${(keys.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s") + Log.d(TAG, "$packageName/$token processed $keys keys ($newKeys new) in ${time}s -> ${(keys.toDouble() / time * 1000).roundToInt().toDouble() / 1000.0} keys/s") database.noteAppAction(packageName, "provideDiagnosisKeys", JSONObject().apply { - put("request_token", params.token) + put("request_token", token) put("request_keys_size", params.keys?.size) put("request_keyFiles_size", params.keyFiles?.size) put("request_keys_count", keys) }.toString()) - val exposureSummary = buildExposureSummary(params.token) + val exposureSummary = buildExposureSummary(token) try { val intent = if (exposureSummary.matchedKeyCount > 0) { @@ -340,7 +342,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val } else { Intent(ACTION_EXPOSURE_NOT_FOUND) } - intent.putExtra(EXTRA_TOKEN, params.token) + intent.putExtra(EXTRA_TOKEN, token) intent.`package` = packageName Log.d(TAG, "Sending $intent") context.sendOrderedBroadcast(intent, null)