From b4e58eeb44bbdf3e08d686deaa4e76be9a40c987 Mon Sep 17 00:00:00 2001 From: Sunik Kupfer Date: Tue, 14 May 2024 21:28:18 +0200 Subject: [PATCH] Log last sync time per collection service and URL (#702) * Log last sync time per collection service and URL * Remove unused deprecated method * Include tasks and other services as caldav type --- .../at/bitfire/davdroid/db/CollectionDao.kt | 4 --- .../davdroid/syncadapter/SyncManager.kt | 30 ++++++++++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/at/bitfire/davdroid/db/CollectionDao.kt b/app/src/main/kotlin/at/bitfire/davdroid/db/CollectionDao.kt index 01a07b56..edd4623b 100644 --- a/app/src/main/kotlin/at/bitfire/davdroid/db/CollectionDao.kt +++ b/app/src/main/kotlin/at/bitfire/davdroid/db/CollectionDao.kt @@ -50,10 +50,6 @@ interface CollectionDao { @Query("SELECT collection.* FROM collection, homeset WHERE collection.serviceId=:serviceId AND type=:type AND homeSetId=homeset.id AND homeset.personal ORDER BY collection.displayName COLLATE NOCASE, collection.url COLLATE NOCASE") fun pagePersonalByServiceAndType(serviceId: Long, type: String): PagingSource - @Deprecated("Use getByServiceAndUrl instead") - @Query("SELECT * FROM collection WHERE url=:url") - fun getByUrl(url: String): Collection? - @Query("SELECT * FROM collection WHERE serviceId=:serviceId AND url=:url") fun getByServiceAndUrl(serviceId: Long, url: String): Collection? diff --git a/app/src/main/kotlin/at/bitfire/davdroid/syncadapter/SyncManager.kt b/app/src/main/kotlin/at/bitfire/davdroid/syncadapter/SyncManager.kt index 4b28b37d..d4643184 100644 --- a/app/src/main/kotlin/at/bitfire/davdroid/syncadapter/SyncManager.kt +++ b/app/src/main/kotlin/at/bitfire/davdroid/syncadapter/SyncManager.kt @@ -38,6 +38,7 @@ import at.bitfire.dav4jvm.property.webdav.SyncToken import at.bitfire.davdroid.InvalidAccountException import at.bitfire.davdroid.R import at.bitfire.davdroid.db.AppDatabase +import at.bitfire.davdroid.db.Service import at.bitfire.davdroid.db.SyncState import at.bitfire.davdroid.db.SyncStats import at.bitfire.davdroid.log.Logger @@ -185,14 +186,7 @@ abstract class SyncManager, out CollectionType: L } // log sync time - val db = EntryPointAccessors.fromApplication(context, SyncManagerEntryPoint::class.java).appDatabase() - db.runInTransaction { - db.collectionDao().getByUrl(collectionURL.toString())?.let { collection -> - db.syncStatsDao().insertOrReplace( - SyncStats(0, collection.id, authority, System.currentTimeMillis()) - ) - } - } + logSyncTime() Logger.log.info("Querying server capabilities") var remoteSyncState = queryCapabilities() @@ -332,6 +326,26 @@ abstract class SyncManager, out CollectionType: L }) } + private fun logSyncTime() { + val serviceType = when (authority) { + ContactsContract.AUTHORITY, // Contacts + context.getString(R.string.address_books_authority) -> // Address books + Service.TYPE_CARDDAV + else -> // Calendars + other (ie. tasks) + Service.TYPE_CALDAV + } + val db = EntryPointAccessors.fromApplication(context, SyncManagerEntryPoint::class.java).appDatabase() + db.runInTransaction { + val service = db.serviceDao().getByAccountAndType(account.name, serviceType) + ?: return@runInTransaction + val collection = db.collectionDao().getByServiceAndUrl(service.id, collectionURL.toString()) + ?: return@runInTransaction + db.syncStatsDao().insertOrReplace( + SyncStats(0, collection.id, authority, System.currentTimeMillis()) + ) + } + } + /** * Prepares synchronization. Sets the lateinit properties [collectionURL] and [davCollection].