Skip to content

Commit

Permalink
Android 14: work around sync always marked as pending by the system #953
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Nov 3, 2023
1 parent bcb7147 commit e642f37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Version 70
----------
*in development*

* 🔨 Android 14: fix automatic syncing.

#### 70.0.1 🧪
*2023-10-20*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.ContentResolver
import android.content.Context
import android.content.SharedPreferences
import android.content.SyncResult
import android.os.Build
import android.os.Bundle
import android.text.format.DateUtils
import android.widget.Toast
Expand Down Expand Up @@ -265,11 +266,28 @@ class SgSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, tru
fun requestSyncIfTime(context: Context) {
// guard against scheduling too many sync requests
val account = AccountUtils.getAccount(context)
if (account == null ||
ContentResolver.isSyncPending(account, SgApp.CONTENT_AUTHORITY)) {
if (account == null) {
Timber.d("Requesting sync: no account, skip.")
return
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// On Android 14 (API 34) the sync is always returned as pending
// after setSyncAutomatically true was called (and remains so even if a sync was
// completed or setSyncAutomatically false is called).
// The next best thing to avoid scheduling too many syncs is checking if one
// is active instead of pending.
if (ContentResolver.isSyncActive(account, SgApp.CONTENT_AUTHORITY)) {
Timber.d("Requesting sync: sync active, skip.")
return
}
} else {
if (ContentResolver.isSyncPending(account, SgApp.CONTENT_AUTHORITY)) {
Timber.d("Requesting sync: sync pending, skip.")
return
}
}
if (!isTimeForSync(context, System.currentTimeMillis())) {
Timber.d("Requesting sync: did sync recently, skip.")
return
}
requestSyncIfConnected(context, SyncType.DELTA, 0)
Expand All @@ -293,6 +311,7 @@ class SgSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, tru
private fun requestSyncIfConnected(context: Context, syncType: SyncType, showId: Long) {
if (!AndroidUtils.isNetworkConnected(context) || !isSyncAutomatically(context)) {
// offline or auto-sync disabled: abort
Timber.d(("Requesting sync: offline or disabled, skip."))
return
}
val args = Bundle()
Expand Down Expand Up @@ -375,6 +394,7 @@ class SgSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, tru
*/
private fun requestSync(context: Context, args: Bundle) {
val account = AccountUtils.getAccount(context) ?: return
Timber.d("Requesting sync: queueing request!")
ContentResolver.requestSync(account, SgApp.CONTENT_AUTHORITY, args)
}

Expand Down

0 comments on commit e642f37

Please sign in to comment.