mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-03 08:13:55 +01:00
parent
5e6115d5de
commit
ba7ffc8051
1 changed files with 81 additions and 74 deletions
|
|
@ -1,74 +1,81 @@
|
||||||
package fr.free.nrw.commons.upload.worker
|
package fr.free.nrw.commons.upload.worker
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.work.BackoffPolicy
|
import androidx.work.BackoffPolicy
|
||||||
import androidx.work.Constraints
|
import androidx.work.Constraints
|
||||||
import androidx.work.ExistingWorkPolicy
|
import androidx.work.ExistingWorkPolicy
|
||||||
import androidx.work.NetworkType
|
import androidx.work.NetworkType
|
||||||
import androidx.work.OneTimeWorkRequest
|
import androidx.work.OneTimeWorkRequest
|
||||||
import androidx.work.WorkManager
|
import androidx.work.WorkManager
|
||||||
import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS
|
import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for all the one time work requests
|
* Helper class for all the one time work requests
|
||||||
*/
|
*/
|
||||||
class WorkRequestHelper {
|
class WorkRequestHelper {
|
||||||
companion object {
|
companion object {
|
||||||
private var isUploadWorkerRunning = false
|
private var isUploadWorkerRunning = false
|
||||||
private val lock = Object()
|
private val lock = Object()
|
||||||
|
|
||||||
fun makeOneTimeWorkRequest(
|
fun makeOneTimeWorkRequest(
|
||||||
context: Context,
|
context: Context,
|
||||||
existingWorkPolicy: ExistingWorkPolicy,
|
existingWorkPolicy: ExistingWorkPolicy,
|
||||||
) {
|
) {
|
||||||
synchronized(lock) {
|
synchronized(lock) {
|
||||||
if (isUploadWorkerRunning) {
|
if (isUploadWorkerRunning) {
|
||||||
Timber.e("UploadWorker is already running. Cannot start another instance.")
|
Timber.e("UploadWorker is already running. Cannot start another instance.")
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
Timber.e("Setting isUploadWorkerRunning to true")
|
Timber.e("Setting isUploadWorkerRunning to true")
|
||||||
isUploadWorkerRunning = true
|
isUploadWorkerRunning = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set backoff criteria for the work request
|
/* Set backoff criteria for the work request
|
||||||
The default backoff policy is EXPONENTIAL, but while testing we found that it
|
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
|
too long for the uploads to finish. So, set the backoff policy as LINEAR with the
|
||||||
minimum backoff delay value of 10 seconds.
|
minimum backoff delay value of 10 seconds.
|
||||||
|
|
||||||
More details on when exactly it is retried:
|
More details on when exactly it is retried:
|
||||||
https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff
|
https://developer.android.com/guide/background/persistent/getting-started/define-work#retries_backoff
|
||||||
*/
|
*/
|
||||||
val constraints: Constraints =
|
val constraints: Constraints =
|
||||||
Constraints
|
Constraints
|
||||||
.Builder()
|
.Builder()
|
||||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||||
.build()
|
.build()
|
||||||
val uploadRequest: OneTimeWorkRequest =
|
val uploadRequest: OneTimeWorkRequest =
|
||||||
OneTimeWorkRequest
|
OneTimeWorkRequest
|
||||||
.Builder(UploadWorker::class.java)
|
.Builder(UploadWorker::class.java)
|
||||||
.setBackoffCriteria(
|
.setBackoffCriteria(
|
||||||
BackoffPolicy.LINEAR,
|
BackoffPolicy.LINEAR,
|
||||||
MIN_BACKOFF_MILLIS,
|
MIN_BACKOFF_MILLIS,
|
||||||
TimeUnit.MILLISECONDS,
|
TimeUnit.MILLISECONDS,
|
||||||
).setConstraints(constraints)
|
).setConstraints(constraints)
|
||||||
.build()
|
.build()
|
||||||
WorkManager.getInstance(context).enqueueUniqueWork(
|
WorkManager.getInstance(context).enqueueUniqueWork(
|
||||||
UploadWorker::class.java.simpleName,
|
UploadWorker::class.java.simpleName,
|
||||||
existingWorkPolicy,
|
existingWorkPolicy,
|
||||||
uploadRequest,
|
uploadRequest,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started.
|
* Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started.
|
||||||
*/
|
*/
|
||||||
fun markUploadWorkerAsStopped() {
|
fun markUploadWorkerAsStopped() {
|
||||||
synchronized(lock) {
|
synchronized(lock) {
|
||||||
isUploadWorkerRunning = false
|
isUploadWorkerRunning = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* Provide a function for other class to get the flag isUploadWorkerRunning
|
||||||
|
*/
|
||||||
|
fun getisUploadWorkerRunning(): Boolean {
|
||||||
|
return isUploadWorkerRunning;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue