SettingsActivity resync now uses SyncWorker (bitfireAT/davx5#226)

* Alter the SyncWorker sync request to allow passing sync arguments along

* Request sync in SettingsActivity via SyncWorker instead of ContentResolver

* Don't use catch-all arguments as worker arguments

---------

Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
Sunik Kupfer 2023-03-28 16:58:53 +02:00 committed by Ricki Hirner
parent 2f8340a646
commit fc17d0ed69
No known key found for this signature in database
GPG key ID: 79A019FCAAEDD3AA
2 changed files with 35 additions and 18 deletions

View file

@ -12,6 +12,7 @@ import android.content.SyncResult
import android.os.Bundle
import android.provider.CalendarContract
import android.provider.ContactsContract
import androidx.annotation.IntDef
import androidx.concurrent.futures.CallbackToFutureAdapter
import androidx.core.app.NotificationCompat
import androidx.hilt.work.HiltWorker
@ -44,6 +45,13 @@ class SyncWorker @AssistedInject constructor(
const val ARG_ACCOUNT_TYPE = "accountType"
const val ARG_AUTHORITY = "authority"
const val ARG_RESYNC = "resync"
@IntDef(NO_RESYNC, RESYNC, FULL_RESYNC)
annotation class ArgResync
const val NO_RESYNC = 0
const val RESYNC = 1
const val FULL_RESYNC = 2
fun workerName(account: Account, authority: String): String {
return "explicit-sync $authority ${account.type}/${account.name}"
}
@ -54,9 +62,9 @@ class SyncWorker @AssistedInject constructor(
*
* @param account account to sync
*/
fun requestSync(context: Context, account: Account) {
fun requestSync(context: Context, account: Account, @ArgResync resync: Int = NO_RESYNC) {
for (authority in SyncUtils.syncAuthorities(context))
requestSync(context, account, authority)
requestSync(context, account, authority, resync)
}
/**
@ -64,15 +72,17 @@ class SyncWorker @AssistedInject constructor(
*
* @param account account to sync
* @param authority authority to sync (for instance: [CalendarContract.AUTHORITY]])
* @param resync whether to request re-synchronization
*/
fun requestSync(context: Context, account: Account, authority: String) {
val arguments = Data.Builder()
fun requestSync(context: Context, account: Account, authority: String, @ArgResync resync: Int = NO_RESYNC) {
val argumentsBuilder = Data.Builder()
.putString(ARG_AUTHORITY, authority)
.putString(ARG_ACCOUNT_NAME, account.name)
.putString(ARG_ACCOUNT_TYPE, account.type)
.build()
if (resync != 0)
argumentsBuilder.putInt(ARG_RESYNC, resync)
val workRequest = OneTimeWorkRequestBuilder<SyncWorker>()
.setInputData(arguments)
.setInputData(argumentsBuilder.build())
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
@ -152,13 +162,19 @@ class SyncWorker @AssistedInject constructor(
throw IllegalArgumentException("Invalid authority $authority")
}
// Pass flags to the sync adapter. Note that these are sync framework flags, but they don't
// have anything to do with the sync framework anymore. They only exist because we still use
// the same sync code called from two locations (from the WorkManager and from the sync framework).
val extras = Bundle(2)
// Pass flags to the sync adapter. Note that these may be sync framework flags, but they
// don't have anything to do with the sync framework anymore. They only exist because we
// still use the same sync code called from two locations (from WorkManager and from the
// sync framework).
val extras = Bundle()
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true) // manual sync
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true) // run immediately (don't queue)
val result = SyncResult()
// pass flags which are used by the sync code
when (extras.getInt(ARG_RESYNC)) {
FULL_RESYNC -> extras.putBoolean(SyncAdapterService.SYNC_EXTRAS_FULL_RESYNC, true)
RESYNC -> extras.putBoolean(SyncAdapterService.SYNC_EXTRAS_RESYNC, true)
}
val provider: ContentProviderClient? =
try {
@ -172,6 +188,7 @@ class SyncWorker @AssistedInject constructor(
return Result.failure()
}
val result = SyncResult()
try {
syncThread = Thread.currentThread()
syncAdapter.onPerformSync(account, extras, authority, provider, result)

View file

@ -27,7 +27,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.preference.*
import at.bitfire.davdroid.InvalidAccountException
import at.bitfire.davdroid.util.PermissionUtils
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.log.Logger
@ -35,7 +34,9 @@ import at.bitfire.davdroid.resource.TaskUtils
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.syncadapter.SyncAdapterService
import at.bitfire.davdroid.syncadapter.SyncWorker
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.util.PermissionUtils
import at.bitfire.ical4android.TaskProvider
import at.bitfire.vcard4android.GroupMethod
import com.google.android.material.snackbar.Snackbar
@ -555,13 +556,12 @@ class SettingsActivity: AppCompatActivity() {
}
private fun resync(authority: String, fullResync: Boolean) {
val args = Bundle(1)
args.putBoolean(if (fullResync)
SyncAdapterService.SYNC_EXTRAS_FULL_RESYNC
val resync =
if (fullResync)
SyncWorker.FULL_RESYNC
else
SyncAdapterService.SYNC_EXTRAS_RESYNC, true)
ContentResolver.requestSync(account, authority, args)
SyncWorker.RESYNC
SyncWorker.requestSync(context, account, authority, resync)
}
}