298 lines
14 KiB
Java
298 lines
14 KiB
Java
package androidx.work.impl.utils;
|
|
|
|
import android.os.Build;
|
|
import android.text.TextUtils;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.work.Constraints;
|
|
import androidx.work.Data;
|
|
import androidx.work.ExistingWorkPolicy;
|
|
import androidx.work.Logger;
|
|
import androidx.work.Operation;
|
|
import androidx.work.WorkInfo;
|
|
import androidx.work.WorkRequest;
|
|
import androidx.work.impl.OperationImpl;
|
|
import androidx.work.impl.Scheduler;
|
|
import androidx.work.impl.Schedulers;
|
|
import androidx.work.impl.WorkContinuationImpl;
|
|
import androidx.work.impl.WorkDatabase;
|
|
import androidx.work.impl.WorkManagerImpl;
|
|
import androidx.work.impl.background.systemalarm.RescheduleReceiver;
|
|
import androidx.work.impl.model.Dependency;
|
|
import androidx.work.impl.model.DependencyDao;
|
|
import androidx.work.impl.model.WorkName;
|
|
import androidx.work.impl.model.WorkSpec;
|
|
import androidx.work.impl.model.WorkSpecDao;
|
|
import androidx.work.impl.model.WorkTag;
|
|
import androidx.work.impl.workers.ConstraintTrackingWorker;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public class EnqueueRunnable implements Runnable {
|
|
private static final String TAG = Logger.tagWithPrefix("EnqueueRunnable");
|
|
private final OperationImpl mOperation = new OperationImpl();
|
|
private final WorkContinuationImpl mWorkContinuation;
|
|
|
|
public EnqueueRunnable(@NonNull WorkContinuationImpl workContinuationImpl) {
|
|
this.mWorkContinuation = workContinuationImpl;
|
|
}
|
|
|
|
private static boolean enqueueContinuation(@NonNull WorkContinuationImpl workContinuationImpl) {
|
|
boolean enqueueWorkWithPrerequisites = enqueueWorkWithPrerequisites(workContinuationImpl.getWorkManagerImpl(), workContinuationImpl.getWork(), (String[]) WorkContinuationImpl.prerequisitesFor(workContinuationImpl).toArray(new String[0]), workContinuationImpl.getName(), workContinuationImpl.getExistingWorkPolicy());
|
|
workContinuationImpl.markEnqueued();
|
|
return enqueueWorkWithPrerequisites;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
/* JADX WARNING: Removed duplicated region for block: B:113:0x01ae */
|
|
/* JADX WARNING: Removed duplicated region for block: B:116:0x01b8 */
|
|
/* JADX WARNING: Removed duplicated region for block: B:122:0x01e1 A[LOOP:7: B:120:0x01db->B:122:0x01e1, LOOP_END] */
|
|
/* JADX WARNING: Removed duplicated region for block: B:124:0x01fa */
|
|
/* JADX WARNING: Removed duplicated region for block: B:142:0x020a A[SYNTHETIC] */
|
|
/* JADX WARNING: Removed duplicated region for block: B:42:0x0093 */
|
|
/* JADX WARNING: Removed duplicated region for block: B:88:0x0159 */
|
|
/* JADX WARNING: Unknown variable types count: 1 */
|
|
private static boolean enqueueWorkWithPrerequisites(WorkManagerImpl workManagerImpl, @NonNull List<? extends WorkRequest> list, String[] strArr, String str, ExistingWorkPolicy existingWorkPolicy) {
|
|
boolean z2;
|
|
boolean z3;
|
|
boolean z4;
|
|
boolean z5;
|
|
long j;
|
|
int i;
|
|
ArrayList arrayList;
|
|
boolean z6;
|
|
DependencyDao dependencyDao;
|
|
WorkManagerImpl workManagerImpl2 = workManagerImpl;
|
|
String[] strArr2 = strArr;
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
WorkDatabase workDatabase = workManagerImpl.getWorkDatabase();
|
|
boolean z7 = strArr2 != null && strArr2.length > 0;
|
|
if (z7) {
|
|
z4 = true;
|
|
z3 = false;
|
|
z2 = false;
|
|
for (String str2 : strArr2) {
|
|
WorkSpec workSpec = workDatabase.workSpecDao().getWorkSpec(str2);
|
|
if (workSpec == null) {
|
|
Logger.get().error(TAG, String.format("Prerequisite %s doesn't exist; not enqueuing", str2), new Throwable[0]);
|
|
return false;
|
|
}
|
|
WorkInfo.State state = workSpec.state;
|
|
z4 &= state == WorkInfo.State.SUCCEEDED;
|
|
if (state == WorkInfo.State.FAILED) {
|
|
z2 = true;
|
|
} else if (state == WorkInfo.State.CANCELLED) {
|
|
z3 = true;
|
|
}
|
|
}
|
|
} else {
|
|
z4 = true;
|
|
z3 = false;
|
|
z2 = false;
|
|
}
|
|
boolean z8 = !TextUtils.isEmpty(str);
|
|
if (z8 && !z7) {
|
|
List<WorkSpec.IdAndState> workSpecIdAndStatesForName = workDatabase.workSpecDao().getWorkSpecIdAndStatesForName(str);
|
|
if (!workSpecIdAndStatesForName.isEmpty()) {
|
|
if (existingWorkPolicy == ExistingWorkPolicy.APPEND || existingWorkPolicy == ExistingWorkPolicy.APPEND_OR_REPLACE) {
|
|
DependencyDao dependencyDao2 = workDatabase.dependencyDao();
|
|
ArrayList arrayList2 = new ArrayList();
|
|
for (WorkSpec.IdAndState idAndState : workSpecIdAndStatesForName) {
|
|
if (!dependencyDao2.hasDependents(idAndState.f30id)) {
|
|
WorkInfo.State state2 = idAndState.state;
|
|
dependencyDao = dependencyDao2;
|
|
boolean z9 = (state2 == WorkInfo.State.SUCCEEDED) & z4;
|
|
if (state2 == WorkInfo.State.FAILED) {
|
|
z2 = true;
|
|
} else if (state2 == WorkInfo.State.CANCELLED) {
|
|
z3 = true;
|
|
}
|
|
arrayList2.add(idAndState.f30id);
|
|
z4 = z9;
|
|
} else {
|
|
dependencyDao = dependencyDao2;
|
|
}
|
|
dependencyDao2 = dependencyDao;
|
|
}
|
|
if (existingWorkPolicy != ExistingWorkPolicy.APPEND_OR_REPLACE || (!z3 && !z2)) {
|
|
z6 = z2;
|
|
arrayList = arrayList2;
|
|
} else {
|
|
WorkSpecDao workSpecDao = workDatabase.workSpecDao();
|
|
for (WorkSpec.IdAndState idAndState2 : workSpecDao.getWorkSpecIdAndStatesForName(str)) {
|
|
workSpecDao.delete(idAndState2.f30id);
|
|
}
|
|
z6 = false;
|
|
z3 = false;
|
|
arrayList = Collections.emptyList();
|
|
}
|
|
strArr2 = (String[]) arrayList.toArray(strArr2);
|
|
z7 = strArr2.length > 0;
|
|
z2 = z6;
|
|
} else {
|
|
if (existingWorkPolicy == ExistingWorkPolicy.KEEP) {
|
|
for (WorkSpec.IdAndState idAndState3 : workSpecIdAndStatesForName) {
|
|
WorkInfo.State state3 = idAndState3.state;
|
|
if (state3 == WorkInfo.State.ENQUEUED || state3 == WorkInfo.State.RUNNING) {
|
|
return false;
|
|
}
|
|
while (r3.hasNext()) {
|
|
}
|
|
}
|
|
}
|
|
CancelWorkRunnable.forName(str, workManagerImpl2, false).run();
|
|
WorkSpecDao workSpecDao2 = workDatabase.workSpecDao();
|
|
for (WorkSpec.IdAndState idAndState4 : workSpecIdAndStatesForName) {
|
|
workSpecDao2.delete(idAndState4.f30id);
|
|
}
|
|
z5 = true;
|
|
for (WorkRequest workRequest : list) {
|
|
WorkSpec workSpec2 = workRequest.getWorkSpec();
|
|
if (!z7 || z4) {
|
|
if (!workSpec2.isPeriodic()) {
|
|
workSpec2.periodStartTime = currentTimeMillis;
|
|
} else {
|
|
j = currentTimeMillis;
|
|
workSpec2.periodStartTime = 0;
|
|
i = Build.VERSION.SDK_INT;
|
|
if (i < 23 && i <= 25) {
|
|
tryDelegateConstrainedWorkSpec(workSpec2);
|
|
} else if (i <= 22 && usesScheduler(workManagerImpl2, Schedulers.GCM_SCHEDULER)) {
|
|
tryDelegateConstrainedWorkSpec(workSpec2);
|
|
}
|
|
if (workSpec2.state == WorkInfo.State.ENQUEUED) {
|
|
z5 = true;
|
|
}
|
|
workDatabase.workSpecDao().insertWorkSpec(workSpec2);
|
|
if (z7) {
|
|
for (String str3 : strArr2) {
|
|
workDatabase.dependencyDao().insertDependency(new Dependency(workRequest.getStringId(), str3));
|
|
}
|
|
}
|
|
for (String str4 : workRequest.getTags()) {
|
|
workDatabase.workTagDao().insert(new WorkTag(str4, workRequest.getStringId()));
|
|
}
|
|
if (!z8) {
|
|
workDatabase.workNameDao().insert(new WorkName(str, workRequest.getStringId()));
|
|
}
|
|
workManagerImpl2 = workManagerImpl;
|
|
currentTimeMillis = j;
|
|
}
|
|
} else if (z2) {
|
|
workSpec2.state = WorkInfo.State.FAILED;
|
|
} else if (z3) {
|
|
workSpec2.state = WorkInfo.State.CANCELLED;
|
|
} else {
|
|
workSpec2.state = WorkInfo.State.BLOCKED;
|
|
}
|
|
j = currentTimeMillis;
|
|
i = Build.VERSION.SDK_INT;
|
|
if (i < 23) {
|
|
}
|
|
tryDelegateConstrainedWorkSpec(workSpec2);
|
|
if (workSpec2.state == WorkInfo.State.ENQUEUED) {
|
|
}
|
|
workDatabase.workSpecDao().insertWorkSpec(workSpec2);
|
|
if (z7) {
|
|
}
|
|
while (r0.hasNext()) {
|
|
}
|
|
if (!z8) {
|
|
}
|
|
workManagerImpl2 = workManagerImpl;
|
|
currentTimeMillis = j;
|
|
}
|
|
return z5;
|
|
}
|
|
}
|
|
}
|
|
z5 = false;
|
|
while (r7.hasNext()) {
|
|
}
|
|
return z5;
|
|
}
|
|
|
|
private static boolean processContinuation(@NonNull WorkContinuationImpl workContinuationImpl) {
|
|
List<WorkContinuationImpl> parents = workContinuationImpl.getParents();
|
|
boolean z2 = false;
|
|
if (parents != null) {
|
|
boolean z3 = false;
|
|
for (WorkContinuationImpl workContinuationImpl2 : parents) {
|
|
if (!workContinuationImpl2.isEnqueued()) {
|
|
z3 |= processContinuation(workContinuationImpl2);
|
|
} else {
|
|
Logger.get().warning(TAG, String.format("Already enqueued work ids (%s).", TextUtils.join(", ", workContinuationImpl2.getIds())), new Throwable[0]);
|
|
}
|
|
}
|
|
z2 = z3;
|
|
}
|
|
return enqueueContinuation(workContinuationImpl) | z2;
|
|
}
|
|
|
|
private static void tryDelegateConstrainedWorkSpec(WorkSpec workSpec) {
|
|
Constraints constraints = workSpec.constraints;
|
|
if (constraints.requiresBatteryNotLow() || constraints.requiresStorageNotLow()) {
|
|
String str = workSpec.workerClassName;
|
|
Data.Builder builder = new Data.Builder();
|
|
builder.putAll(workSpec.input).putString(ConstraintTrackingWorker.ARGUMENT_CLASS_NAME, str);
|
|
workSpec.workerClassName = ConstraintTrackingWorker.class.getName();
|
|
workSpec.input = builder.build();
|
|
}
|
|
}
|
|
|
|
private static boolean usesScheduler(@NonNull WorkManagerImpl workManagerImpl, @NonNull String str) {
|
|
try {
|
|
Class<?> cls = Class.forName(str);
|
|
for (Scheduler scheduler : workManagerImpl.getSchedulers()) {
|
|
if (cls.isAssignableFrom(scheduler.getClass())) {
|
|
return true;
|
|
}
|
|
}
|
|
} catch (ClassNotFoundException unused) {
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public boolean addToDatabase() {
|
|
WorkDatabase workDatabase = this.mWorkContinuation.getWorkManagerImpl().getWorkDatabase();
|
|
workDatabase.beginTransaction();
|
|
try {
|
|
boolean processContinuation = processContinuation(this.mWorkContinuation);
|
|
workDatabase.setTransactionSuccessful();
|
|
return processContinuation;
|
|
} finally {
|
|
workDatabase.endTransaction();
|
|
}
|
|
}
|
|
|
|
public Operation getOperation() {
|
|
return this.mOperation;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
try {
|
|
if (!this.mWorkContinuation.hasCycles()) {
|
|
if (addToDatabase()) {
|
|
PackageManagerHelper.setComponentEnabled(this.mWorkContinuation.getWorkManagerImpl().getApplicationContext(), RescheduleReceiver.class, true);
|
|
scheduleWorkInBackground();
|
|
}
|
|
this.mOperation.setState(Operation.SUCCESS);
|
|
return;
|
|
}
|
|
throw new IllegalStateException(String.format("WorkContinuation has cycles (%s)", this.mWorkContinuation));
|
|
} catch (Throwable th) {
|
|
this.mOperation.setState(new Operation.State.FAILURE(th));
|
|
}
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public void scheduleWorkInBackground() {
|
|
WorkManagerImpl workManagerImpl = this.mWorkContinuation.getWorkManagerImpl();
|
|
Schedulers.schedule(workManagerImpl.getConfiguration(), workManagerImpl.getWorkDatabase(), workManagerImpl.getSchedulers());
|
|
}
|
|
}
|