From e54d3d568ad988b92885689fe17b018859437be3 Mon Sep 17 00:00:00 2001 From: Armin Date: Thu, 26 Oct 2023 15:05:57 +0200 Subject: [PATCH] [LEIP-182] Add file counts to meta data --- build.gradle | 4 +- .../de/cyface/persistence/dao/FileDao.kt | 7 ++ .../de/cyface/synchronization/SyncAdapter.kt | 64 +++++++++++-------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/build.gradle b/build.gradle index 7681e747e..2557d7e35 100644 --- a/build.gradle +++ b/build.gradle @@ -51,8 +51,8 @@ ext { // Cyface dependencies cyfaceUtilsVersion = "4.0.4" - cyfaceSerializationVersion = "3.0.0" - cyfaceUploaderVersion = "1.0.0" + cyfaceSerializationVersion = "3.2.0-alpha1" + cyfaceUploaderVersion = "1.2.0-alpha2" // Android SDK versions minSdkVersion = 21 // device support diff --git a/persistence/src/main/kotlin/de/cyface/persistence/dao/FileDao.kt b/persistence/src/main/kotlin/de/cyface/persistence/dao/FileDao.kt index 582ad3470..fef28fd57 100644 --- a/persistence/src/main/kotlin/de/cyface/persistence/dao/FileDao.kt +++ b/persistence/src/main/kotlin/de/cyface/persistence/dao/FileDao.kt @@ -25,6 +25,7 @@ import de.cyface.persistence.content.BaseColumns import de.cyface.persistence.content.FileTable import de.cyface.persistence.model.File import de.cyface.persistence.model.FileStatus +import de.cyface.protos.model.File.FileType /** * Data access object which provides the API to interact with the [File] database table. @@ -65,6 +66,12 @@ interface FileDao { @Query("SELECT COUNT(*) FROM ${FileTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId") fun countByMeasurementId(measurementId: Long): Int + /** + * Returns the number of files found for a specific [measurementId] and [type]. + */ + @Query("SELECT COUNT(*) FROM ${FileTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId AND ${FileTable.COLUMN_TYPE} = :type") + fun countByMeasurementIdAndType(measurementId: Long, type: FileType): Int + @Query("DELETE FROM ${FileTable.URI_PATH} WHERE ${BaseColumns.MEASUREMENT_ID} = :measurementId") fun deleteItemByMeasurementId(measurementId: Long): Int diff --git a/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt b/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt index 1ef7c300a..ab89c5506 100644 --- a/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt +++ b/synchronization/src/main/kotlin/de/cyface/synchronization/SyncAdapter.kt @@ -235,16 +235,16 @@ class SyncAdapter private constructor( authority: String ) { val measurementCount = measurements.size - var error = false + var error: Boolean for (index in 0 until measurementCount) { val measurement = measurements[index] - val fileCount = persistence.fileDao!!.countByMeasurementId(measurement.id) - Log.d(TAG, "Preparing to upload Measurement (id ${measurement.id}) with $fileCount attachments.") validateMeasurementFormat(measurement) - val metaData = loadMetaData(measurement, persistence, deviceId, context) + val fileCount = metaData.logCount + metaData.imageCount + metaData.videoCount + + Log.d(TAG, "Preparing to upload Measurement (id ${measurement.id}) with $fileCount attachments.") // Upload measurement binary first if (measurement.status === MeasurementStatus.FINISHED) { @@ -612,29 +612,41 @@ class SyncAdapter private constructor( endLocation = RequestMetaData.GeoLocation(l!!.timestamp, l.lat, l.lon) } - // Non location meta data - val deviceType = Build.MODEL - val osVersion = "Android " + Build.VERSION.RELEASE - val appVersion: String - val packageManager = context.packageManager - appVersion = try { - packageManager.getPackageInfo(context.packageName, 0).versionName - } catch (e: PackageManager.NameNotFoundException) { - throw IllegalStateException(e) + // Attachments + return runBlocking { + val logCount = persistence.fileDao!!.countByMeasurementIdAndType(measurement.id, FileType.CSV) + val imageCount = persistence.fileDao!!.countByMeasurementIdAndType(measurement.id, FileType.JPG) + val otherFiles = persistence.fileDao!!.countByMeasurementId(measurement.id) + val unsupportedFiles = otherFiles - logCount - imageCount + require(unsupportedFiles == 0) { "Number of unsupported files: $unsupportedFiles" } + + // Non location meta data + val deviceType = Build.MODEL + val osVersion = "Android " + Build.VERSION.RELEASE + val appVersion: String + val packageManager = context.packageManager + appVersion = try { + packageManager.getPackageInfo(context.packageName, 0).versionName + } catch (e: PackageManager.NameNotFoundException) { + throw IllegalStateException(e) + } + return@runBlocking RequestMetaData( + deviceId, + measurement.id.toString(), + osVersion, + deviceType, + appVersion, + measurement.distance, + locationCount.toLong(), + startLocation, + endLocation, + measurement.modality.databaseIdentifier, + RequestMetaData.CURRENT_TRANSFER_FILE_FORMAT_VERSION, + logCount, + imageCount, + 0 + ) } - return RequestMetaData( - deviceId, - measurement.id.toString(), - osVersion, - deviceType, - appVersion, - measurement.distance, - locationCount.toLong(), - startLocation, - endLocation, - measurement.modality.databaseIdentifier, - RequestMetaData.CURRENT_TRANSFER_FILE_FORMAT_VERSION - ) } /**