|
|
@ -3,8 +3,14 @@ package org.thoughtcrime.securesms.jobmanager;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
@ -16,6 +22,7 @@ import androidx.work.Data;
|
|
|
|
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.WorkContinuation;
|
|
|
|
import androidx.work.WorkManager;
|
|
|
|
import androidx.work.WorkManager;
|
|
|
|
|
|
|
|
|
|
|
|
public class JobManager {
|
|
|
|
public class JobManager {
|
|
|
@ -36,7 +43,25 @@ public class JobManager {
|
|
|
|
this.workManager = workManager;
|
|
|
|
this.workManager = workManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Chain startChain(@NonNull Job job) {
|
|
|
|
|
|
|
|
return startChain(Collections.singletonList(job));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Chain startChain(@NonNull List<? extends Job> jobs) {
|
|
|
|
|
|
|
|
return new Chain(jobs);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void add(Job job) {
|
|
|
|
public void add(Job job) {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
executor.execute(() -> {
|
|
|
|
executor.execute(() -> {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
workManager.pruneWork().getResult().get();
|
|
|
|
workManager.pruneWork().getResult().get();
|
|
|
@ -44,6 +69,38 @@ public class JobManager {
|
|
|
|
Log.w(TAG, "Failed to prune work.", e);
|
|
|
|
Log.w(TAG, "Failed to prune work.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<List<Job>> jobListChain = chain.getJobListChain();
|
|
|
|
|
|
|
|
List<List<OneTimeWorkRequest>> requestListChain = Stream.of(jobListChain).map(jl -> Stream.of(jl).map(this::toWorkRequest).toList()).toList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (jobListChain.isEmpty()) {
|
|
|
|
|
|
|
|
throw new IllegalStateException("Enqueued an empty chain.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WorkContinuation continuation;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (chainParameters.getGroupId().isPresent()) {
|
|
|
|
|
|
|
|
ExistingWorkPolicy policy = chainParameters.shouldIgnoreDuplicates() ? ExistingWorkPolicy.KEEP : ExistingWorkPolicy.APPEND;
|
|
|
|
|
|
|
|
continuation = workManager.beginUniqueWork(chainParameters.getGroupId().get(), policy, requestListChain.get(0));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
continuation = workManager.beginWith(requestListChain.get(0));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < requestListChain.size(); i++) {
|
|
|
|
|
|
|
|
continuation = continuation.then(requestListChain.get(i));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
continuation.enqueue();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OneTimeWorkRequest toWorkRequest(@NonNull Job job) {
|
|
|
|
JobParameters jobParameters = job.getJobParameters();
|
|
|
|
JobParameters jobParameters = job.getJobParameters();
|
|
|
|
|
|
|
|
|
|
|
|
if (jobParameters == null) {
|
|
|
|
if (jobParameters == null) {
|
|
|
@ -65,17 +122,32 @@ public class JobManager {
|
|
|
|
requestBuilder.setConstraints(NETWORK_CONSTRAINT);
|
|
|
|
requestBuilder.setConstraints(NETWORK_CONSTRAINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OneTimeWorkRequest request = requestBuilder.build();
|
|
|
|
return requestBuilder.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
job.onSubmit(context, request.getId());
|
|
|
|
public class Chain {
|
|
|
|
|
|
|
|
|
|
|
|
String groupId = jobParameters.getGroupId();
|
|
|
|
private final List<List<Job>> jobs = new LinkedList<>();
|
|
|
|
if (groupId != null) {
|
|
|
|
|
|
|
|
ExistingWorkPolicy policy = jobParameters.shouldIgnoreDuplicates() ? ExistingWorkPolicy.KEEP : ExistingWorkPolicy.APPEND;
|
|
|
|
private Chain(@NonNull List<? extends Job> jobs) {
|
|
|
|
workManager.beginUniqueWork(groupId, policy, request).enqueue();
|
|
|
|
this.jobs.add(new ArrayList<>(jobs));
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
workManager.beginWith(request).enqueue();
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|