2018-06-18 12:27:04 -07:00
|
|
|
package org.thoughtcrime.securesms.jobmanager;
|
|
|
|
|
2018-10-20 22:52:14 -07:00
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2018-11-27 12:34:42 -08:00
|
|
|
import java.util.concurrent.ExecutionException;
|
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;
|
2018-12-07 12:16:37 -08:00
|
|
|
import androidx.work.WorkContinuation;
|
2018-08-09 10:15:43 -04:00
|
|
|
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-11-27 12:34:42 -08:00
|
|
|
private static final String TAG = JobManager.class.getSimpleName();
|
|
|
|
|
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-10-20 22:52:14 -07:00
|
|
|
private final Context context;
|
2018-08-09 10:15:43 -04:00
|
|
|
private final WorkManager workManager;
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-10-20 22:52:14 -07:00
|
|
|
public JobManager(@NonNull Context context, @NonNull WorkManager workManager) {
|
|
|
|
this.context = context;
|
|
|
|
this.workManager = workManager;
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
public Chain startChain(@NonNull Job job) {
|
|
|
|
return startChain(Collections.singletonList(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Chain startChain(@NonNull List<? extends Job> jobs) {
|
|
|
|
return new Chain(jobs);
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
public void add(Job job) {
|
2018-12-07 12:16:37 -08:00
|
|
|
JobParameters jobParameters = job.getJobParameters();
|
|
|
|
|
|
|
|
if (jobParameters == null) {
|
|
|
|
throw new IllegalStateException("Jobs must have JobParameters at this stage. (" + job.getClass().getSimpleName() + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
startChain(job).enqueue(jobParameters.getSoloChainParameters());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enqueueChain(@NonNull Chain chain, @NonNull ChainParameters chainParameters) {
|
2018-08-09 10:15:43 -04:00
|
|
|
executor.execute(() -> {
|
2018-11-27 12:34:42 -08:00
|
|
|
try {
|
|
|
|
workManager.pruneWork().getResult().get();
|
|
|
|
} catch (ExecutionException | InterruptedException e) {
|
|
|
|
Log.w(TAG, "Failed to prune work.", e);
|
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
List<List<Job>> jobListChain = chain.getJobListChain();
|
2018-12-07 18:31:39 -08:00
|
|
|
List<List<OneTimeWorkRequest>> requestListChain = Stream.of(jobListChain)
|
|
|
|
.filter(jobList -> !jobList.isEmpty())
|
|
|
|
.map(jobList -> Stream.of(jobList).map(this::toWorkRequest).toList())
|
|
|
|
.toList();
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
if (jobListChain.isEmpty()) {
|
|
|
|
throw new IllegalStateException("Enqueued an empty chain.");
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
for (int i = 0; i < jobListChain.size(); i++) {
|
|
|
|
for (int j = 0; j < jobListChain.get(i).size(); j++) {
|
|
|
|
jobListChain.get(i).get(j).onSubmit(context, requestListChain.get(i).get(j).getId());
|
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
WorkContinuation continuation;
|
2018-06-18 12:27:04 -07:00
|
|
|
|
2018-12-07 12:16:37 -08:00
|
|
|
if (chainParameters.getGroupId().isPresent()) {
|
|
|
|
ExistingWorkPolicy policy = chainParameters.shouldIgnoreDuplicates() ? ExistingWorkPolicy.KEEP : ExistingWorkPolicy.APPEND;
|
|
|
|
continuation = workManager.beginUniqueWork(chainParameters.getGroupId().get(), policy, requestListChain.get(0));
|
2018-08-09 10:15:43 -04:00
|
|
|
} else {
|
2018-12-07 12:16:37 -08:00
|
|
|
continuation = workManager.beginWith(requestListChain.get(0));
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2018-12-07 12:16:37 -08:00
|
|
|
|
|
|
|
for (int i = 1; i < requestListChain.size(); i++) {
|
|
|
|
continuation = continuation.then(requestListChain.get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
continuation.enqueue();
|
2018-08-09 10:15:43 -04:00
|
|
|
});
|
2018-12-07 12:16:37 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private OneTimeWorkRequest toWorkRequest(@NonNull Job job) {
|
|
|
|
JobParameters jobParameters = job.getJobParameters();
|
|
|
|
|
|
|
|
if (jobParameters == null) {
|
|
|
|
throw new IllegalStateException("Jobs must have JobParameters at this stage. (" + job.getClass().getSimpleName() + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
.putBoolean(Job.KEY_REQUIRES_NETWORK, jobParameters.requiresNetwork())
|
|
|
|
.putBoolean(Job.KEY_REQUIRES_SQLCIPHER, jobParameters.requiresSqlCipher());
|
|
|
|
Data data = job.serialize(dataBuilder);
|
|
|
|
|
|
|
|
OneTimeWorkRequest.Builder requestBuilder = new OneTimeWorkRequest.Builder(job.getClass())
|
|
|
|
.setInputData(data)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
if (jobParameters.requiresNetwork()) {
|
|
|
|
requestBuilder.setConstraints(NETWORK_CONSTRAINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return requestBuilder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Chain {
|
|
|
|
|
|
|
|
private final List<List<Job>> jobs = new LinkedList<>();
|
|
|
|
|
|
|
|
private Chain(@NonNull List<? extends Job> jobs) {
|
|
|
|
this.jobs.add(new ArrayList<>(jobs));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Chain then(@NonNull Job job) {
|
|
|
|
return then(Collections.singletonList(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Chain then(@NonNull List<Job> jobs) {
|
|
|
|
this.jobs.add(new ArrayList<>(jobs));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void enqueue(@NonNull ChainParameters chainParameters) {
|
|
|
|
enqueueChain(this, chainParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<List<Job>> getJobListChain() {
|
|
|
|
return jobs;
|
|
|
|
}
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2018-06-18 12:27:04 -07:00
|
|
|
}
|