1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +00:00

TaskListSyncAdapter: implement syncing etebase task lists

This commit is contained in:
Tom Hacohen 2020-08-26 07:49:00 +03:00
parent 1c284bce91
commit d6a0958d16
2 changed files with 76 additions and 6 deletions

View File

@ -11,14 +11,17 @@ package com.etesync.syncadapter.resource
import android.accounts.Account import android.accounts.Account
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.RemoteException import android.os.RemoteException
import android.provider.CalendarContract
import at.bitfire.ical4android.AndroidTaskList import at.bitfire.ical4android.AndroidTaskList
import at.bitfire.ical4android.AndroidTaskListFactory import at.bitfire.ical4android.AndroidTaskListFactory
import at.bitfire.ical4android.CalendarStorageException import at.bitfire.ical4android.CalendarStorageException
import at.bitfire.ical4android.TaskProvider import at.bitfire.ical4android.TaskProvider
import at.bitfire.ical4android.TaskProvider.ProviderName import at.bitfire.ical4android.TaskProvider.ProviderName
import com.etesync.syncadapter.CachedCollection
import com.etesync.syncadapter.model.JournalEntity import com.etesync.syncadapter.model.JournalEntity
import org.dmfs.tasks.contract.TaskContract.TaskLists import org.dmfs.tasks.contract.TaskContract.TaskLists
import org.dmfs.tasks.contract.TaskContract.Tasks import org.dmfs.tasks.contract.TaskContract.Tasks
@ -54,6 +57,14 @@ class LocalTaskList private constructor(
return create(account, provider, values) return create(account, provider, values)
} }
fun create(account: Account, provider: TaskProvider, cachedCollection: CachedCollection): Uri {
val values = valuesFromCachedCollection(cachedCollection, true)
values.put(TaskLists.OWNER, account.name)
values.put(TaskLists.SYNC_ENABLED, 1)
values.put(TaskLists.VISIBLE, 1)
return create(account, provider, values)
}
fun findByName(account: Account, provider: TaskProvider, factory: Factory, name: String): LocalTaskList? fun findByName(account: Account, provider: TaskProvider, factory: Factory, name: String): LocalTaskList?
= AndroidTaskList.find(account, provider, factory, TaskLists._SYNC_ID + "==?", arrayOf(name)).firstOrNull() = AndroidTaskList.find(account, provider, factory, TaskLists._SYNC_ID + "==?", arrayOf(name)).firstOrNull()
@ -70,6 +81,18 @@ class LocalTaskList private constructor(
return values return values
} }
private fun valuesFromCachedCollection(cachedCollection: CachedCollection, withColor: Boolean): ContentValues {
val col = cachedCollection.col
val meta = cachedCollection.meta
val values = ContentValues(3)
values.put(TaskLists._SYNC_ID, col.uid)
values.put(TaskLists.LIST_NAME, meta.name)
if (withColor)
values.put(TaskLists.LIST_COLOR, if (!meta.color.isNullOrBlank()) Color.parseColor(meta.color) else defaultColor)
return values
}
} }
override val url: String? override val url: String?
@ -78,6 +101,9 @@ class LocalTaskList private constructor(
fun update(journalEntity: JournalEntity, updateColor: Boolean) = fun update(journalEntity: JournalEntity, updateColor: Boolean) =
update(valuesFromCollectionInfo(journalEntity, updateColor)) update(valuesFromCollectionInfo(journalEntity, updateColor))
fun update(cachedCollection: CachedCollection, updateColor: Boolean) =
update(valuesFromCachedCollection(cachedCollection, updateColor))
override fun findDeleted() = queryTasks("${Tasks._DELETED}!=0", null) override fun findDeleted() = queryTasks("${Tasks._DELETED}!=0", null)
override fun findDirty(limit: Int?): List<LocalTask> { override fun findDirty(limit: Int?): List<LocalTask> {

View File

@ -18,10 +18,7 @@ import android.os.Bundle
import at.bitfire.ical4android.AndroidTaskList import at.bitfire.ical4android.AndroidTaskList
import at.bitfire.ical4android.TaskProvider import at.bitfire.ical4android.TaskProvider
import at.bitfire.ical4android.TaskProvider.ProviderName import at.bitfire.ical4android.TaskProvider.ProviderName
import com.etesync.syncadapter.AccountSettings import com.etesync.syncadapter.*
import com.etesync.syncadapter.App
import com.etesync.syncadapter.Constants
import com.etesync.syncadapter.R
import com.etesync.syncadapter.log.Logger import com.etesync.syncadapter.log.Logger
import com.etesync.syncadapter.model.CollectionInfo import com.etesync.syncadapter.model.CollectionInfo
import com.etesync.syncadapter.model.JournalEntity import com.etesync.syncadapter.model.JournalEntity
@ -63,8 +60,11 @@ class TasksSyncAdapterService: SyncAdapterService() {
RefreshCollections(account, CollectionInfo.Type.TASKS).run() RefreshCollections(account, CollectionInfo.Type.TASKS).run()
updateLocalTaskLists(taskProvider, account, accountSettings) if (accountSettings.isLegacy) {
legacyUpdateLocalTaskLists(taskProvider, account, accountSettings)
} else {
updateLocalTaskLists(taskProvider, account, accountSettings)
}
val principal = accountSettings.uri?.toHttpUrlOrNull()!! val principal = accountSettings.uri?.toHttpUrlOrNull()!!
for (taskList in AndroidTaskList.find(account, taskProvider, LocalTaskList.Factory, "${TaskContract.TaskLists.SYNC_ENABLED}!=0", null)) { for (taskList in AndroidTaskList.find(account, taskProvider, LocalTaskList.Factory, "${TaskContract.TaskLists.SYNC_ENABLED}!=0", null)) {
@ -78,6 +78,50 @@ class TasksSyncAdapterService: SyncAdapterService() {
} }
private fun updateLocalTaskLists(provider: TaskProvider, account: Account, settings: AccountSettings) { private fun updateLocalTaskLists(provider: TaskProvider, account: Account, settings: AccountSettings) {
val remote = HashMap<String, CachedCollection>()
val etebaseLocalCache = EtebaseLocalCache.getInstance(context, account.name)
val collections: List<CachedCollection>
synchronized(etebaseLocalCache) {
val httpClient = HttpClient.Builder(context, settings).setForeground(false).build()
val etebase = EtebaseLocalCache.getEtebase(context, httpClient.okHttpClient, settings)
val colMgr = etebase.collectionManager
collections = etebaseLocalCache.collectionList(colMgr).filter { it.meta.collectionType == Constants.ETEBASE_TYPE_TASKS }
}
for (collection in collections) {
remote[collection.col.uid] = collection
}
val local = AndroidTaskList.find(account, provider, LocalTaskList.Factory, null, null)
val updateColors = settings.manageCalendarColors
// delete obsolete local calendar
for (taskList in local) {
val url = taskList.name
val collection = remote[url]
if (collection == null) {
Logger.log.fine("Deleting obsolete local taskList $url")
taskList.delete()
} else {
// remote CollectionInfo found for this local collection, update data
Logger.log.fine("Updating local taskList $url")
taskList.update(collection, updateColors)
// we already have a local taskList for this remote collection, don't take into consideration anymore
remote.remove(url)
}
}
// create new local calendars
for (url in remote.keys) {
val cachedCollection = remote[url]!!
Logger.log.info("Adding local calendar list $cachedCollection")
LocalTaskList.create(account, provider, cachedCollection)
}
}
private fun legacyUpdateLocalTaskLists(provider: TaskProvider, account: Account, settings: AccountSettings) {
val data = (context.applicationContext as App).data val data = (context.applicationContext as App).data
var service = JournalModel.Service.fetchOrCreate(data, account.name, CollectionInfo.Type.TASKS) var service = JournalModel.Service.fetchOrCreate(data, account.name, CollectionInfo.Type.TASKS)