1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +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.accounts.Account
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.provider.CalendarContract import android.provider.CalendarContract
import at.bitfire.ical4android.TaskProvider.Companion.OPENTASK_PROVIDERS
import com.etesync.syncadapter.App import com.etesync.syncadapter.App
import com.etesync.syncadapter.utils.TaskProviderHandling
fun requestSync(account: Account?) { fun requestSync(context: Context, account: Account?) {
val authorities = arrayOf(App.addressBooksAuthority, CalendarContract.AUTHORITY) + val authorities = arrayOf(
OPENTASK_PROVIDERS.map { it.authority } App.addressBooksAuthority,
CalendarContract.AUTHORITY,
TaskProviderHandling.getWantedTaskSyncProvider(context)?.authority
)
for (authority in authorities) { for (authority in authorities.filterNotNull()) {
val extras = Bundle() val extras = Bundle()
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true) // manual sync extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true) // manual sync
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true) // run immediately (don't queue) 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() { private fun requestSync() {
requestSync(account) requestSync(applicationContext, account)
Snackbar.make(findViewById(R.id.parent), R.string.account_synchronizing_now, Snackbar.LENGTH_LONG).show() 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() this@ChangeEncryptionPasswordActivity.finish()
}.show() }.show()
requestSync(account) requestSync(applicationContext, account)
} }
} catch (e: Exception) { } catch (e: Exception) {
uiThread { uiThread {

View File

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

View File

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

View File

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

View File

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