From 420cbfac937ba209174e96fae771dea4be7b748d Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 6 Aug 2020 14:20:15 -0500 Subject: [PATCH] Use getWantedTaskProvider --- .../syncadapter/syncadapter/RequestSync.kt | 14 +++++++++----- .../etesync/syncadapter/ui/AccountActivity.kt | 2 +- .../ui/ChangeEncryptionPasswordActivity.kt | 2 +- .../ui/CreateCollectionFragment.kt | 19 ++++++++++--------- .../syncadapter/ui/JournalItemActivity.kt | 4 ++-- .../syncadapter/ui/ViewCollectionActivity.kt | 6 +++--- .../ui/importlocal/ImportFragment.kt | 19 +++++++++++-------- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/RequestSync.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/RequestSync.kt index 7c65faa5..e5312a08 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/RequestSync.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/RequestSync.kt @@ -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) diff --git a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt index 9c9e2c24..65df29bf 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.kt @@ -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() } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/ChangeEncryptionPasswordActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/ChangeEncryptionPasswordActivity.kt index 0899c259..e43b7809 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ChangeEncryptionPasswordActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/ChangeEncryptionPasswordActivity.kt @@ -134,7 +134,7 @@ open class ChangeEncryptionPasswordActivity : BaseActivity() { this@ChangeEncryptionPasswordActivity.finish() }.show() - requestSync(account) + requestSync(applicationContext, account) } } catch (e: Exception) { uiThread { diff --git a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt index 7a5fcda8..d543edb7 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt @@ -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 { @@ -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) { diff --git a/app/src/main/java/com/etesync/syncadapter/ui/JournalItemActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/JournalItemActivity.kt index 3500ed6e..2f4a18bc 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/JournalItemActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/JournalItemActivity.kt @@ -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] diff --git a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt index 8a2713f7..1f90ddca 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt @@ -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 } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt index ca761517..94d12318 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt @@ -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!!)