EN: Ensure correct handling of new provideDiagnosisKeys()

Fixes #1300
This commit is contained in:
Marvin W 2020-12-03 10:15:18 +01:00
parent c0324710f3
commit 2b09a5e530
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
4 changed files with 36 additions and 10 deletions

View File

@ -30,4 +30,6 @@ description = 'microG API for play-services-nearby'
dependencies { dependencies {
api project(':play-services-basement') api project(':play-services-basement')
api project(':play-services-base-api') api project(':play-services-base-api')
implementation "androidx.annotation:annotation:$annotationVersion"
} }

View File

@ -7,6 +7,8 @@ package com.google.android.gms.nearby.exposurenotification.internal;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import androidx.annotation.Nullable;
import com.google.android.gms.common.api.internal.IStatusCallback; import com.google.android.gms.common.api.internal.IStatusCallback;
import com.google.android.gms.nearby.exposurenotification.ExposureConfiguration; import com.google.android.gms.nearby.exposurenotification.ExposureConfiguration;
import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey; import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey;
@ -17,16 +19,21 @@ import java.util.List;
public class ProvideDiagnosisKeysParams extends AutoSafeParcelable { public class ProvideDiagnosisKeysParams extends AutoSafeParcelable {
@Field(1) @Field(1)
@Nullable
public List<TemporaryExposureKey> keys; public List<TemporaryExposureKey> keys;
@Field(2) @Field(2)
public IStatusCallback callback; public IStatusCallback callback;
@Field(3) @Field(3)
@Nullable
public List<ParcelFileDescriptor> keyFiles; public List<ParcelFileDescriptor> keyFiles;
@Field(4) @Field(4)
@Nullable
public ExposureConfiguration configuration; public ExposureConfiguration configuration;
@Field(5) @Field(5)
@Nullable
public String token; public String token;
@Field(6) @Field(6)
@Nullable
public IDiagnosisKeyFileSupplier keyFileSupplier; public IDiagnosisKeyFileSupplier keyFileSupplier;
private ProvideDiagnosisKeysParams() { private ProvideDiagnosisKeysParams() {

View File

@ -22,6 +22,7 @@ import com.google.android.gms.nearby.exposurenotification.TemporaryExposureKey
import kotlinx.coroutines.* import kotlinx.coroutines.*
import okio.ByteString import okio.ByteString
import org.json.JSONObject import org.json.JSONObject
import org.microg.gms.nearby.exposurenotification.Constants.TOKEN_A
import java.io.File import java.io.File
import java.lang.Runnable import java.lang.Runnable
import java.nio.ByteBuffer 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 { private fun storeSingleDiagnosisKey(tid: Long, key: TemporaryExposureKey, database: SQLiteDatabase = writableDatabase) = database.run {
val tcsid = getTekCheckSingleId(key, true, database) val tcsid = getTekCheckSingleId(key, true, database)
insert(TABLE_TEK_CHECK_SINGLE_TOKEN, "NULL", ContentValues().apply { insert(TABLE_TEK_CHECK_SINGLE_TOKEN, "NULL", ContentValues().apply {

View File

@ -215,17 +215,19 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
} }
override fun provideDiagnosisKeys(params: ProvideDiagnosisKeysParams) { 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 { lifecycleScope.launchWhenStarted {
val tid = ExposureDatabase.with(context) { database -> val tid = ExposureDatabase.with(context) { database ->
if (params.configuration != null) { val configuration = params.configuration
database.storeConfiguration(packageName, params.token, params.configuration) if (configuration != null) {
database.storeConfiguration(packageName, token, configuration)
} else { } else {
database.getTokenId(packageName, params.token) database.getOrCreateTokenId(packageName, token)
} }
} }
if (tid == null) { if (tid == null) {
Log.w(TAG, "Unknown token without configuration: $packageName/${params.token}") Log.w(TAG, "Unknown token without configuration: $packageName/$token")
try { try {
params.callback.onResult(Status.INTERNAL_ERROR) params.callback.onResult(Status.INTERNAL_ERROR)
} catch (e: Exception) { } catch (e: Exception) {
@ -281,7 +283,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
if (todoKeyFiles.size > 0) { if (todoKeyFiles.size > 0) {
val time = (System.currentTimeMillis() - start).coerceAtLeast(1).toDouble() / 1000.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 { 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 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 { database.noteAppAction(packageName, "provideDiagnosisKeys", JSONObject().apply {
put("request_token", params.token) put("request_token", token)
put("request_keys_size", params.keys?.size) put("request_keys_size", params.keys?.size)
put("request_keyFiles_size", params.keyFiles?.size) put("request_keyFiles_size", params.keyFiles?.size)
put("request_keys_count", keys) put("request_keys_count", keys)
}.toString()) }.toString())
val exposureSummary = buildExposureSummary(params.token) val exposureSummary = buildExposureSummary(token)
try { try {
val intent = if (exposureSummary.matchedKeyCount > 0) { val intent = if (exposureSummary.matchedKeyCount > 0) {
@ -340,7 +342,7 @@ class ExposureNotificationServiceImpl(private val context: Context, private val
} else { } else {
Intent(ACTION_EXPOSURE_NOT_FOUND) Intent(ACTION_EXPOSURE_NOT_FOUND)
} }
intent.putExtra(EXTRA_TOKEN, params.token) intent.putExtra(EXTRA_TOKEN, token)
intent.`package` = packageName intent.`package` = packageName
Log.d(TAG, "Sending $intent") Log.d(TAG, "Sending $intent")
context.sendOrderedBroadcast(intent, null) context.sendOrderedBroadcast(intent, null)