1
0
mirror of https://github.com/etesync/android synced 2024-12-22 22:58:29 +00:00

Use getWantedTaskProvider

This commit is contained in:
Alex Baker 2020-08-06 14:20:15 -05:00 committed by Tom Hacohen
parent 52bf0b615c
commit a36cfb8c9b
7 changed files with 37 additions and 29 deletions

View File

@ -2,17 +2,21 @@ package com.etesync.syncadapter.syncadapter
import android.accounts.Account
import android.content.ContentResolver
import android.content.Context
import android.os.Bundle
import android.provider.CalendarContract
import at.bitfire.ical4android.TaskProvider.Companion.OPENTASK_PROVIDERS
import com.etesync.syncadapter.App
import com.etesync.syncadapter.utils.TaskProviderHandling
fun requestSync(account: Account?) {
val authorities = arrayOf(App.addressBooksAuthority, CalendarContract.AUTHORITY) +
OPENTASK_PROVIDERS.map { it.authority }
fun requestSync(context: Context, account: Account?) {
val authorities = arrayOf(
App.addressBooksAuthority,
CalendarContract.AUTHORITY,
TaskProviderHandling.getWantedTaskSyncProvider(context)?.authority
)
for (authority in authorities) {
for (authority in authorities.filterNotNull()) {
val extras = Bundle()
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true) // manual sync
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true) // run immediately (don't queue)

View File

@ -473,7 +473,7 @@ class AccountActivity : BaseActivity(), Toolbar.OnMenuItemClickListener, PopupMe
private fun requestSync() {
requestSync(account)
requestSync(applicationContext, account)
Snackbar.make(findViewById(R.id.parent), R.string.account_synchronizing_now, Snackbar.LENGTH_LONG).show()
}

View File

@ -134,7 +134,7 @@ open class ChangeEncryptionPasswordActivity : BaseActivity() {
this@ChangeEncryptionPasswordActivity.finish()
}.show()
requestSync(account)
requestSync(applicationContext, account)
}
} catch (e: Exception) {
uiThread {

View File

@ -19,8 +19,6 @@ import androidx.fragment.app.DialogFragment
import androidx.loader.app.LoaderManager
import androidx.loader.content.AsyncTaskLoader
import androidx.loader.content.Loader
import at.bitfire.ical4android.TaskProvider
import at.bitfire.ical4android.TaskProvider.Companion.OPENTASK_PROVIDERS
import com.etesync.syncadapter.*
import com.etesync.journalmanager.Crypto
import com.etesync.journalmanager.Exceptions
@ -28,6 +26,7 @@ import com.etesync.journalmanager.JournalManager
import com.etesync.syncadapter.model.CollectionInfo
import com.etesync.syncadapter.model.JournalEntity
import com.etesync.syncadapter.model.JournalModel
import com.etesync.syncadapter.utils.TaskProviderHandling
import okhttp3.HttpUrl
class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks<Exception> {
@ -85,14 +84,16 @@ class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks
override fun loadInBackground(): Exception? {
try {
val data = (context.applicationContext as App).data
val context = context.applicationContext
val data = (context as App).data
// 1. find service ID
val authorities = when (info.enumType){
CollectionInfo.Type.ADDRESS_BOOK -> listOf(App.addressBooksAuthority)
CollectionInfo.Type.CALENDAR -> listOf(CalendarContract.AUTHORITY)
CollectionInfo.Type.TASKS -> OPENTASK_PROVIDERS.map { it.authority }
else -> emptyList()
val authority = when (info.enumType){
CollectionInfo.Type.ADDRESS_BOOK -> App.addressBooksAuthority
CollectionInfo.Type.CALENDAR -> CalendarContract.AUTHORITY
CollectionInfo.Type.TASKS ->
TaskProviderHandling.getWantedTaskSyncProvider(context)?.authority
else -> null
}
val serviceEntity = JournalModel.Service.fetchOrCreate(data, account.name, info.enumType)
@ -127,7 +128,7 @@ class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks
journalManager.update(journal)
}
authorities.forEach { requestSync(it) }
authority?.let { requestSync(it) }
} catch (e: IllegalStateException) {
return e
} catch (e: Exceptions.HttpException) {

View File

@ -19,7 +19,6 @@ import at.bitfire.ical4android.Event
import at.bitfire.ical4android.InvalidCalendarException
import at.bitfire.ical4android.Task
import at.bitfire.ical4android.TaskProvider
import at.bitfire.ical4android.TaskProvider.Companion.OPENTASK_PROVIDERS
import at.bitfire.vcard4android.Contact
import com.etesync.syncadapter.App
import com.etesync.syncadapter.Constants
@ -30,6 +29,7 @@ import com.etesync.journalmanager.model.SyncEntry
import com.etesync.syncadapter.resource.*
import com.etesync.syncadapter.ui.journalviewer.ListEntriesFragment.Companion.setJournalEntryView
import com.etesync.syncadapter.utils.EventEmailInvitation
import com.etesync.syncadapter.utils.TaskProviderHandling
import com.google.android.material.tabs.TabLayout
import ezvcard.util.PartialDate
import org.jetbrains.anko.doAsync
@ -117,7 +117,7 @@ class JournalItemActivity : BaseActivity(), Refreshable {
}
}
CollectionInfo.Type.TASKS -> {
OPENTASK_PROVIDERS.forEach {
TaskProviderHandling.getWantedTaskSyncProvider(applicationContext)?.let {
val provider = TaskProvider.acquire(this, it)!!
val localTaskList = LocalTaskList.findByName(account, provider, LocalTaskList.Factory, info.uid!!)!!
val task = Task.tasksFromReader(StringReader(syncEntry.content))[0]

View File

@ -24,7 +24,6 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import at.bitfire.ical4android.CalendarStorageException
import at.bitfire.ical4android.TaskProvider
import at.bitfire.ical4android.TaskProvider.Companion.OPENTASK_PROVIDERS
import at.bitfire.vcard4android.ContactsStorageException
import com.etesync.syncadapter.App
import com.etesync.syncadapter.Constants
@ -39,6 +38,7 @@ import com.etesync.syncadapter.ui.importlocal.ImportActivity
import com.etesync.syncadapter.ui.journalviewer.ListEntriesFragment
import com.etesync.syncadapter.utils.HintManager
import com.etesync.syncadapter.utils.ShowcaseBuilder
import com.etesync.syncadapter.utils.TaskProviderHandling
import com.google.android.material.floatingactionbutton.FloatingActionButton
import tourguide.tourguide.ToolTip
import java.io.FileNotFoundException
@ -213,9 +213,9 @@ class ViewCollectionActivity : BaseActivity(), Refreshable {
}
CollectionInfo.Type.TASKS -> {
try {
val providerClient = OPENTASK_PROVIDERS.mapNotNull {
val providerClient = TaskProviderHandling.getWantedTaskSyncProvider(this@ViewCollectionActivity)?.let {
TaskProvider.acquire(this@ViewCollectionActivity, it)
}.firstOrNull()
}
if (providerClient == null) {
return null
}

View File

@ -28,6 +28,7 @@ import com.etesync.syncadapter.resource.*
import com.etesync.syncadapter.syncadapter.ContactsSyncManager
import com.etesync.syncadapter.ui.Refreshable
import com.etesync.syncadapter.ui.importlocal.ResultFragment.ImportResult
import com.etesync.syncadapter.utils.TaskProviderHandling
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
@ -283,15 +284,17 @@ class ImportFragment : DialogFragment() {
finishParsingFile(tasks.size)
val providers = OPENTASK_PROVIDERS.mapNotNull {
TaskProvider.acquire(context, it)
}
if (providers.isEmpty()) {
result.e = Exception("Failed to acquire tasks content provider.")
return result
}
val provider = TaskProviderHandling.getWantedTaskSyncProvider(requireContext())
.let {
if (it == null) {
result.e = Exception("Failed to acquire tasks content provider.")
null
} else {
TaskProvider.acquire(context, it)
}
}
providers.forEach {
provider?.let {
val localTaskList: LocalTaskList?
try {
localTaskList = LocalTaskList.findByName(account, it, LocalTaskList.Factory, info.uid!!)