@@ -63,13 +63,17 @@ import java.util.logging.Level
63
63
*
64
64
* By enqueuing this worker ([SyncWorker.enqueue]) a sync will be started immediately (as soon as
65
65
* possible). Currently, there are three scenarios starting a sync:
66
+ *
66
67
* 1) *manual sync*: User presses an in-app sync button and enqueues this worker directly.
67
68
* 2) *periodic sync*: User defines time interval to sync in app settings. The [PeriodicSyncWorker] runs
68
69
* in the background and enqueues this worker when due.
69
70
* 3) *content-triggered sync*: User changes a calendar event, task or contact, or presses a sync
70
71
* button in one of the responsible apps. The [SyncAdapterService] is notified of this and enqueues
71
72
* this worker.
72
73
*
74
+ * Expedited: when run manually
75
+ *
76
+ * Long-running: no
73
77
*/
74
78
@HiltWorker
75
79
class SyncWorker @AssistedInject constructor(
@@ -84,6 +88,9 @@ class SyncWorker @AssistedInject constructor(
84
88
internal const val ARG_ACCOUNT_TYPE = " accountType"
85
89
internal const val ARG_AUTHORITY = " authority"
86
90
91
+ /* * Boolean. Set to `true` when the job was requested as expedited job. */
92
+ private const val ARG_EXPEDITED = " expedited"
93
+
87
94
private const val ARG_UPLOAD = " upload"
88
95
89
96
private const val ARG_RESYNC = " resync"
@@ -129,7 +136,7 @@ class SyncWorker @AssistedInject constructor(
129
136
upload : Boolean = false
130
137
) {
131
138
for (authority in SyncUtils .syncAuthorities(context))
132
- enqueue(context, account, authority, resync, upload)
139
+ enqueue(context, account, authority, expedited = true , resync = resync, upload = upload)
133
140
}
134
141
135
142
/* *
@@ -146,6 +153,7 @@ class SyncWorker @AssistedInject constructor(
146
153
context : Context ,
147
154
account : Account ,
148
155
authority : String ,
156
+ expedited : Boolean ,
149
157
@ArgResync resync : Int = NO_RESYNC ,
150
158
upload : Boolean = false
151
159
): String {
@@ -154,6 +162,8 @@ class SyncWorker @AssistedInject constructor(
154
162
.putString(ARG_AUTHORITY , authority)
155
163
.putString(ARG_ACCOUNT_NAME , account.name)
156
164
.putString(ARG_ACCOUNT_TYPE , account.type)
165
+ if (expedited)
166
+ argumentsBuilder.putBoolean(ARG_EXPEDITED , true )
157
167
if (resync != NO_RESYNC )
158
168
argumentsBuilder.putInt(ARG_RESYNC , resync)
159
169
argumentsBuilder.putBoolean(ARG_UPLOAD , upload)
@@ -165,7 +175,6 @@ class SyncWorker @AssistedInject constructor(
165
175
val workRequest = OneTimeWorkRequestBuilder <SyncWorker >()
166
176
.addTag(workerName(account, authority))
167
177
.setInputData(argumentsBuilder.build())
168
- .setExpedited(OutOfQuotaPolicy .RUN_AS_NON_EXPEDITED_WORK_REQUEST )
169
178
.setBackoffCriteria(
170
179
BackoffPolicy .EXPONENTIAL ,
171
180
WorkRequest .DEFAULT_BACKOFF_DELAY_MILLIS , // 30 sec
@@ -179,17 +188,20 @@ class SyncWorker @AssistedInject constructor(
179
188
addTag(workerName(mainAccount, authority))
180
189
}
181
190
}
182
- .build()
191
+
192
+ if (expedited)
193
+ workRequest.setExpedited(OutOfQuotaPolicy .RUN_AS_NON_EXPEDITED_WORK_REQUEST )
183
194
184
195
// enqueue and start syncing
185
196
val name = workerName(account, authority)
186
- Logger .log.log(Level .INFO , " Enqueueing unique worker: $name , with tags: ${workRequest.tags} " )
197
+ val request = workRequest.build()
198
+ Logger .log.log(Level .INFO , " Enqueueing unique worker: $name , expedited = $expedited , tags = ${request.tags} " )
187
199
WorkManager .getInstance(context).enqueueUniqueWork(
188
200
name,
189
201
ExistingWorkPolicy .KEEP , // If sync is already running, just continue.
190
202
// Existing retried work will not be replaced (for instance when
191
203
// PeriodicSyncWorker enqueues another scheduled sync).
192
- workRequest
204
+ request
193
205
)
194
206
return name
195
207
}
@@ -305,8 +317,9 @@ class SyncWorker @AssistedInject constructor(
305
317
inputData.getString(ARG_ACCOUNT_TYPE ) ? : throw IllegalArgumentException (" $ARG_ACCOUNT_TYPE required" )
306
318
)
307
319
val authority = inputData.getString(ARG_AUTHORITY ) ? : throw IllegalArgumentException (" $ARG_AUTHORITY required" )
320
+ val expedited = inputData.getBoolean(ARG_EXPEDITED , false )
308
321
309
- // Check internet connection
322
+ // check internet connection
310
323
val ignoreVpns = AccountSettings (applicationContext, account).getIgnoreVpns()
311
324
val connectivityManager = applicationContext.getSystemService<ConnectivityManager >()!!
312
325
if (! internetAvailable(connectivityManager, ignoreVpns)) {
@@ -319,7 +332,7 @@ class SyncWorker @AssistedInject constructor(
319
332
// What are we going to sync? Select syncer based on authority
320
333
val syncer: Syncer = when (authority) {
321
334
applicationContext.getString(R .string.address_books_authority) ->
322
- AddressBookSyncer (applicationContext)
335
+ AddressBookSyncer (applicationContext, expedited )
323
336
CalendarContract .AUTHORITY ->
324
337
CalendarSyncer (applicationContext)
325
338
ContactsContract .AUTHORITY ->
@@ -438,6 +451,7 @@ class SyncWorker @AssistedInject constructor(
438
451
.setCategory(NotificationCompat .CATEGORY_STATUS )
439
452
.setOngoing(true )
440
453
.setPriority(NotificationCompat .PRIORITY_LOW )
454
+ .setForegroundServiceBehavior(NotificationCompat .FOREGROUND_SERVICE_DEFERRED )
441
455
.build()
442
456
return ForegroundInfo (NotificationUtils .NOTIFY_SYNC_EXPEDITED , notification)
443
457
}
0 commit comments