|
1 |
| -package fr.free.nrw.commons.upload.worker |
2 |
| - |
3 |
| -import android.content.Context |
4 |
| -import androidx.work.BackoffPolicy |
5 |
| -import androidx.work.Constraints |
6 |
| -import androidx.work.ExistingWorkPolicy |
7 |
| -import androidx.work.NetworkType |
8 |
| -import androidx.work.OneTimeWorkRequest |
9 |
| -import androidx.work.WorkManager |
10 |
| -import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS |
11 |
| -import timber.log.Timber |
12 |
| -import java.util.concurrent.TimeUnit |
13 |
| - |
14 |
| -/** |
15 |
| - * Helper class for all the one time work requests |
16 |
| - */ |
17 |
| -class WorkRequestHelper { |
18 |
| - companion object { |
19 |
| - private var isUploadWorkerRunning = false |
20 |
| - private val lock = Object() |
21 |
| - |
22 |
| - fun makeOneTimeWorkRequest( |
23 |
| - context: Context, |
24 |
| - existingWorkPolicy: ExistingWorkPolicy, |
25 |
| - ) { |
26 |
| - synchronized(lock) { |
27 |
| - if (isUploadWorkerRunning) { |
28 |
| - Timber.e("UploadWorker is already running. Cannot start another instance.") |
29 |
| - return |
30 |
| - } else { |
31 |
| - Timber.e("Setting isUploadWorkerRunning to true") |
32 |
| - isUploadWorkerRunning = true |
33 |
| - } |
34 |
| - } |
35 |
| - |
36 |
| - /* Set backoff criteria for the work request |
37 |
| - The default backoff policy is EXPONENTIAL, but while testing we found that it |
38 |
| - too long for the uploads to finish. So, set the backoff policy as LINEAR with the |
39 |
| - minimum backoff delay value of 10 seconds. |
40 |
| -
|
41 |
| - More details on when exactly it is retried: |
42 |
| - https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff |
43 |
| - */ |
44 |
| - val constraints: Constraints = |
45 |
| - Constraints |
46 |
| - .Builder() |
47 |
| - .setRequiredNetworkType(NetworkType.CONNECTED) |
48 |
| - .build() |
49 |
| - val uploadRequest: OneTimeWorkRequest = |
50 |
| - OneTimeWorkRequest |
51 |
| - .Builder(UploadWorker::class.java) |
52 |
| - .setBackoffCriteria( |
53 |
| - BackoffPolicy.LINEAR, |
54 |
| - MIN_BACKOFF_MILLIS, |
55 |
| - TimeUnit.MILLISECONDS, |
56 |
| - ).setConstraints(constraints) |
57 |
| - .build() |
58 |
| - WorkManager.getInstance(context).enqueueUniqueWork( |
59 |
| - UploadWorker::class.java.simpleName, |
60 |
| - existingWorkPolicy, |
61 |
| - uploadRequest, |
62 |
| - ) |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started. |
67 |
| - */ |
68 |
| - fun markUploadWorkerAsStopped() { |
69 |
| - synchronized(lock) { |
70 |
| - isUploadWorkerRunning = false |
71 |
| - } |
72 |
| - } |
73 |
| - } |
74 |
| -} |
| 1 | +package fr.free.nrw.commons.upload.worker |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.work.BackoffPolicy |
| 5 | +import androidx.work.Constraints |
| 6 | +import androidx.work.ExistingWorkPolicy |
| 7 | +import androidx.work.NetworkType |
| 8 | +import androidx.work.OneTimeWorkRequest |
| 9 | +import androidx.work.WorkManager |
| 10 | +import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS |
| 11 | +import timber.log.Timber |
| 12 | +import java.util.concurrent.TimeUnit |
| 13 | + |
| 14 | +/** |
| 15 | + * Helper class for all the one time work requests |
| 16 | + */ |
| 17 | +class WorkRequestHelper { |
| 18 | + companion object { |
| 19 | + private var isUploadWorkerRunning = false |
| 20 | + private val lock = Object() |
| 21 | + |
| 22 | + fun makeOneTimeWorkRequest( |
| 23 | + context: Context, |
| 24 | + existingWorkPolicy: ExistingWorkPolicy, |
| 25 | + ) { |
| 26 | + synchronized(lock) { |
| 27 | + if (isUploadWorkerRunning) { |
| 28 | + Timber.e("UploadWorker is already running. Cannot start another instance.") |
| 29 | + return |
| 30 | + } else { |
| 31 | + Timber.e("Setting isUploadWorkerRunning to true") |
| 32 | + isUploadWorkerRunning = true |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /* Set backoff criteria for the work request |
| 37 | + The default backoff policy is EXPONENTIAL, but while testing we found that it |
| 38 | + too long for the uploads to finish. So, set the backoff policy as LINEAR with the |
| 39 | + minimum backoff delay value of 10 seconds. |
| 40 | +
|
| 41 | + More details on when exactly it is retried: |
| 42 | + https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff |
| 43 | + */ |
| 44 | + val constraints: Constraints = |
| 45 | + Constraints |
| 46 | + .Builder() |
| 47 | + .setRequiredNetworkType(NetworkType.CONNECTED) |
| 48 | + .build() |
| 49 | + val uploadRequest: OneTimeWorkRequest = |
| 50 | + OneTimeWorkRequest |
| 51 | + .Builder(UploadWorker::class.java) |
| 52 | + .setBackoffCriteria( |
| 53 | + BackoffPolicy.LINEAR, |
| 54 | + MIN_BACKOFF_MILLIS, |
| 55 | + TimeUnit.MILLISECONDS, |
| 56 | + ).setConstraints(constraints) |
| 57 | + .build() |
| 58 | + WorkManager.getInstance(context).enqueueUniqueWork( |
| 59 | + UploadWorker::class.java.simpleName, |
| 60 | + existingWorkPolicy, |
| 61 | + uploadRequest, |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started. |
| 67 | + */ |
| 68 | + fun markUploadWorkerAsStopped() { |
| 69 | + synchronized(lock) { |
| 70 | + isUploadWorkerRunning = false |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Provide a function for other class to get the flag isUploadWorkerRunning |
| 76 | + */ |
| 77 | + fun getisUploadWorkerRunning(): Boolean { |
| 78 | + return isUploadWorkerRunning; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments