2018-06-18 12:27:04 -07:00
|
|
|
package org.thoughtcrime.securesms.jobmanager;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2018-06-18 12:27:04 -07:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
2018-08-09 10:15:43 -04:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.BackoffPolicy;
|
|
|
|
import androidx.work.Constraints;
|
|
|
|
import androidx.work.Data;
|
|
|
|
import androidx.work.ExistingWorkPolicy;
|
|
|
|
import androidx.work.NetworkType;
|
|
|
|
import androidx.work.OneTimeWorkRequest;
|
|
|
|
import androidx.work.WorkManager;
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
public class JobManager {
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final Constraints NETWORK_CONSTRAINT = new Constraints.Builder()
|
|
|
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
|
|
|
.build();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private final Executor executor = Executors.newSingleThreadExecutor();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private final WorkManager workManager;
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
public JobManager(@NonNull WorkManager workManager) {
|
|
|
|
this.workManager = workManager;
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
public void add(Job job) {
|
|
|
|
executor.execute(() -> {
|
|
|
|
workManager.synchronous().pruneWorkSync();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
JobParameters jobParameters = job.getJobParameters();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
if (jobParameters == null) {
|
|
|
|
throw new IllegalStateException("Jobs must have JobParameters at this stage.");
|
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
Data.Builder dataBuilder = new Data.Builder().putInt(Job.KEY_RETRY_COUNT, jobParameters.getRetryCount())
|
|
|
|
.putLong(Job.KEY_RETRY_UNTIL, jobParameters.getRetryUntil())
|
|
|
|
.putLong(Job.KEY_SUBMIT_TIME, System.currentTimeMillis())
|
2018-10-03 09:42:35 -07:00
|
|
|
.putBoolean(Job.KEY_REQUIRES_NETWORK, jobParameters.requiresNetwork())
|
2018-08-09 10:15:43 -04:00
|
|
|
.putBoolean(Job.KEY_REQUIRES_MASTER_SECRET, jobParameters.requiresMasterSecret())
|
|
|
|
.putBoolean(Job.KEY_REQUIRES_SQLCIPHER, jobParameters.requiresSqlCipher());
|
|
|
|
Data data = job.serialize(dataBuilder);
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
OneTimeWorkRequest.Builder requestBuilder = new OneTimeWorkRequest.Builder(job.getClass())
|
|
|
|
.setInputData(data)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS);
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
if (jobParameters.requiresNetwork()) {
|
|
|
|
requestBuilder.setConstraints(NETWORK_CONSTRAINT);
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
OneTimeWorkRequest request = requestBuilder.build();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
job.onSubmit(request.getId());
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
String groupId = jobParameters.getGroupId();
|
|
|
|
if (groupId != null) {
|
|
|
|
ExistingWorkPolicy policy = jobParameters.shouldIgnoreDuplicates() ? ExistingWorkPolicy.KEEP : ExistingWorkPolicy.APPEND;
|
|
|
|
workManager.beginUniqueWork(groupId, policy, request).enqueue();
|
|
|
|
} else {
|
|
|
|
workManager.beginWith(request).enqueue();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|