-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new checking for the display of pause button
- Loading branch information
Showing
1 changed file
with
81 additions
and
74 deletions.
There are no files selected for viewing
155 changes: 81 additions & 74 deletions
155
app/src/main/java/fr/free/nrw/commons/upload/worker/WorkRequestHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,81 @@ | ||
package fr.free.nrw.commons.upload.worker | ||
|
||
import android.content.Context | ||
import androidx.work.BackoffPolicy | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.NetworkType | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS | ||
import timber.log.Timber | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Helper class for all the one time work requests | ||
*/ | ||
class WorkRequestHelper { | ||
companion object { | ||
private var isUploadWorkerRunning = false | ||
private val lock = Object() | ||
|
||
fun makeOneTimeWorkRequest( | ||
context: Context, | ||
existingWorkPolicy: ExistingWorkPolicy, | ||
) { | ||
synchronized(lock) { | ||
if (isUploadWorkerRunning) { | ||
Timber.e("UploadWorker is already running. Cannot start another instance.") | ||
return | ||
} else { | ||
Timber.e("Setting isUploadWorkerRunning to true") | ||
isUploadWorkerRunning = true | ||
} | ||
} | ||
|
||
/* Set backoff criteria for the work request | ||
The default backoff policy is EXPONENTIAL, but while testing we found that it | ||
too long for the uploads to finish. So, set the backoff policy as LINEAR with the | ||
minimum backoff delay value of 10 seconds. | ||
More details on when exactly it is retried: | ||
https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff | ||
*/ | ||
val constraints: Constraints = | ||
Constraints | ||
.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
val uploadRequest: OneTimeWorkRequest = | ||
OneTimeWorkRequest | ||
.Builder(UploadWorker::class.java) | ||
.setBackoffCriteria( | ||
BackoffPolicy.LINEAR, | ||
MIN_BACKOFF_MILLIS, | ||
TimeUnit.MILLISECONDS, | ||
).setConstraints(constraints) | ||
.build() | ||
WorkManager.getInstance(context).enqueueUniqueWork( | ||
UploadWorker::class.java.simpleName, | ||
existingWorkPolicy, | ||
uploadRequest, | ||
) | ||
} | ||
|
||
/** | ||
* Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started. | ||
*/ | ||
fun markUploadWorkerAsStopped() { | ||
synchronized(lock) { | ||
isUploadWorkerRunning = false | ||
} | ||
} | ||
} | ||
} | ||
package fr.free.nrw.commons.upload.worker | ||
|
||
import android.content.Context | ||
import androidx.work.BackoffPolicy | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.NetworkType | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS | ||
import timber.log.Timber | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Helper class for all the one time work requests | ||
*/ | ||
class WorkRequestHelper { | ||
companion object { | ||
private var isUploadWorkerRunning = false | ||
private val lock = Object() | ||
|
||
fun makeOneTimeWorkRequest( | ||
context: Context, | ||
existingWorkPolicy: ExistingWorkPolicy, | ||
) { | ||
synchronized(lock) { | ||
if (isUploadWorkerRunning) { | ||
Timber.e("UploadWorker is already running. Cannot start another instance.") | ||
return | ||
} else { | ||
Timber.e("Setting isUploadWorkerRunning to true") | ||
isUploadWorkerRunning = true | ||
} | ||
} | ||
|
||
/* Set backoff criteria for the work request | ||
The default backoff policy is EXPONENTIAL, but while testing we found that it | ||
too long for the uploads to finish. So, set the backoff policy as LINEAR with the | ||
minimum backoff delay value of 10 seconds. | ||
More details on when exactly it is retried: | ||
https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff | ||
*/ | ||
val constraints: Constraints = | ||
Constraints | ||
.Builder() | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
val uploadRequest: OneTimeWorkRequest = | ||
OneTimeWorkRequest | ||
.Builder(UploadWorker::class.java) | ||
.setBackoffCriteria( | ||
BackoffPolicy.LINEAR, | ||
MIN_BACKOFF_MILLIS, | ||
TimeUnit.MILLISECONDS, | ||
).setConstraints(constraints) | ||
.build() | ||
WorkManager.getInstance(context).enqueueUniqueWork( | ||
UploadWorker::class.java.simpleName, | ||
existingWorkPolicy, | ||
uploadRequest, | ||
) | ||
} | ||
|
||
/** | ||
* Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started. | ||
*/ | ||
fun markUploadWorkerAsStopped() { | ||
synchronized(lock) { | ||
isUploadWorkerRunning = false | ||
} | ||
} | ||
|
||
/** | ||
* Provide a function for other class to get the flag isUploadWorkerRunning | ||
*/ | ||
fun getisUploadWorkerRunning(): Boolean { | ||
return isUploadWorkerRunning; | ||
} | ||
} | ||
} |