discord-jadx/app/src/main/java/androidx/work/impl/WorkerWrapper.java

514 lines
23 KiB
Java

package androidx.work.impl;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.work.Configuration;
import androidx.work.Data;
import androidx.work.InputMerger;
import androidx.work.ListenableWorker;
import androidx.work.Logger;
import androidx.work.WorkInfo;
import androidx.work.WorkerParameters;
import androidx.work.impl.background.systemalarm.RescheduleReceiver;
import androidx.work.impl.foreground.ForegroundProcessor;
import androidx.work.impl.model.DependencyDao;
import androidx.work.impl.model.WorkSpec;
import androidx.work.impl.model.WorkSpecDao;
import androidx.work.impl.model.WorkTagDao;
import androidx.work.impl.utils.PackageManagerHelper;
import androidx.work.impl.utils.WorkForegroundUpdater;
import androidx.work.impl.utils.WorkProgressUpdater;
import androidx.work.impl.utils.futures.SettableFuture;
import androidx.work.impl.utils.taskexecutor.TaskExecutor;
import c.i.b.d.a.a;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
public class WorkerWrapper implements Runnable {
public static final String TAG = Logger.tagWithPrefix("WorkerWrapper");
public Context mAppContext;
private Configuration mConfiguration;
private DependencyDao mDependencyDao;
private ForegroundProcessor mForegroundProcessor;
@NonNull
public SettableFuture<Boolean> mFuture = SettableFuture.create();
@Nullable
public a<ListenableWorker.Result> mInnerFuture = null;
private volatile boolean mInterrupted;
@NonNull
public ListenableWorker.Result mResult = ListenableWorker.Result.failure();
private WorkerParameters.RuntimeExtras mRuntimeExtras;
private List<Scheduler> mSchedulers;
private List<String> mTags;
private WorkDatabase mWorkDatabase;
private String mWorkDescription;
public WorkSpec mWorkSpec;
private WorkSpecDao mWorkSpecDao;
private String mWorkSpecId;
private WorkTagDao mWorkTagDao;
private TaskExecutor mWorkTaskExecutor;
public ListenableWorker mWorker;
/* renamed from: androidx.work.impl.WorkerWrapper$1 reason: invalid class name */
public class AnonymousClass1 implements Runnable {
public final /* synthetic */ SettableFuture val$future;
public AnonymousClass1(SettableFuture settableFuture) {
this.val$future = settableFuture;
}
@Override // java.lang.Runnable
public void run() {
try {
Logger.get().debug(WorkerWrapper.TAG, String.format("Starting work for %s", WorkerWrapper.this.mWorkSpec.workerClassName), new Throwable[0]);
WorkerWrapper workerWrapper = WorkerWrapper.this;
workerWrapper.mInnerFuture = workerWrapper.mWorker.startWork();
this.val$future.setFuture(WorkerWrapper.this.mInnerFuture);
} catch (Throwable th) {
this.val$future.setException(th);
}
}
}
/* renamed from: androidx.work.impl.WorkerWrapper$2 reason: invalid class name */
public class AnonymousClass2 implements Runnable {
public final /* synthetic */ SettableFuture val$future;
public final /* synthetic */ String val$workDescription;
public AnonymousClass2(SettableFuture settableFuture, String str) {
this.val$future = settableFuture;
this.val$workDescription = str;
}
@Override // java.lang.Runnable
@SuppressLint({"SyntheticAccessor"})
public void run() {
try {
ListenableWorker.Result result = this.val$future.get();
if (result == null) {
Logger.get().error(WorkerWrapper.TAG, String.format("%s returned a null result. Treating it as a failure.", WorkerWrapper.this.mWorkSpec.workerClassName), new Throwable[0]);
} else {
Logger.get().debug(WorkerWrapper.TAG, String.format("%s returned a %s result.", WorkerWrapper.this.mWorkSpec.workerClassName, result), new Throwable[0]);
WorkerWrapper.this.mResult = result;
}
} catch (CancellationException e) {
Logger.get().info(WorkerWrapper.TAG, String.format("%s was cancelled", this.val$workDescription), e);
} catch (InterruptedException | ExecutionException e2) {
Logger.get().error(WorkerWrapper.TAG, String.format("%s failed because it threw an exception/error", this.val$workDescription), e2);
} catch (Throwable th) {
WorkerWrapper.this.onWorkFinished();
throw th;
}
WorkerWrapper.this.onWorkFinished();
}
}
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
public static class Builder {
@NonNull
public Context mAppContext;
@NonNull
public Configuration mConfiguration;
@NonNull
public ForegroundProcessor mForegroundProcessor;
@NonNull
public WorkerParameters.RuntimeExtras mRuntimeExtras = new WorkerParameters.RuntimeExtras();
public List<Scheduler> mSchedulers;
@NonNull
public WorkDatabase mWorkDatabase;
@NonNull
public String mWorkSpecId;
@NonNull
public TaskExecutor mWorkTaskExecutor;
@Nullable
public ListenableWorker mWorker;
public Builder(@NonNull Context context, @NonNull Configuration configuration, @NonNull TaskExecutor taskExecutor, @NonNull ForegroundProcessor foregroundProcessor, @NonNull WorkDatabase workDatabase, @NonNull String str) {
this.mAppContext = context.getApplicationContext();
this.mWorkTaskExecutor = taskExecutor;
this.mForegroundProcessor = foregroundProcessor;
this.mConfiguration = configuration;
this.mWorkDatabase = workDatabase;
this.mWorkSpecId = str;
}
public WorkerWrapper build() {
return new WorkerWrapper(this);
}
@NonNull
public Builder withRuntimeExtras(@Nullable WorkerParameters.RuntimeExtras runtimeExtras) {
if (runtimeExtras != null) {
this.mRuntimeExtras = runtimeExtras;
}
return this;
}
@NonNull
public Builder withSchedulers(@NonNull List<Scheduler> list) {
this.mSchedulers = list;
return this;
}
@NonNull
@VisibleForTesting
public Builder withWorker(@NonNull ListenableWorker listenableWorker) {
this.mWorker = listenableWorker;
return this;
}
}
public WorkerWrapper(@NonNull Builder builder) {
this.mAppContext = builder.mAppContext;
this.mWorkTaskExecutor = builder.mWorkTaskExecutor;
this.mForegroundProcessor = builder.mForegroundProcessor;
this.mWorkSpecId = builder.mWorkSpecId;
this.mSchedulers = builder.mSchedulers;
this.mRuntimeExtras = builder.mRuntimeExtras;
this.mWorker = builder.mWorker;
this.mConfiguration = builder.mConfiguration;
WorkDatabase workDatabase = builder.mWorkDatabase;
this.mWorkDatabase = workDatabase;
this.mWorkSpecDao = workDatabase.workSpecDao();
this.mDependencyDao = this.mWorkDatabase.dependencyDao();
this.mWorkTagDao = this.mWorkDatabase.workTagDao();
}
private String createWorkDescription(List<String> list) {
StringBuilder sb = new StringBuilder("Work [ id=");
sb.append(this.mWorkSpecId);
sb.append(", tags={ ");
boolean z2 = true;
for (String str : list) {
if (z2) {
z2 = false;
} else {
sb.append(", ");
}
sb.append(str);
}
sb.append(" } ]");
return sb.toString();
}
private void handleResult(ListenableWorker.Result result) {
if (result instanceof ListenableWorker.Result.Success) {
Logger.get().info(TAG, String.format("Worker result SUCCESS for %s", this.mWorkDescription), new Throwable[0]);
if (this.mWorkSpec.isPeriodic()) {
resetPeriodicAndResolve();
} else {
setSucceededAndResolve();
}
} else if (result instanceof ListenableWorker.Result.Retry) {
Logger.get().info(TAG, String.format("Worker result RETRY for %s", this.mWorkDescription), new Throwable[0]);
rescheduleAndResolve();
} else {
Logger.get().info(TAG, String.format("Worker result FAILURE for %s", this.mWorkDescription), new Throwable[0]);
if (this.mWorkSpec.isPeriodic()) {
resetPeriodicAndResolve();
} else {
setFailedAndResolve();
}
}
}
private void iterativelyFailWorkAndDependents(String str) {
LinkedList linkedList = new LinkedList();
linkedList.add(str);
while (!linkedList.isEmpty()) {
String str2 = (String) linkedList.remove();
if (this.mWorkSpecDao.getState(str2) != WorkInfo.State.CANCELLED) {
this.mWorkSpecDao.setState(WorkInfo.State.FAILED, str2);
}
linkedList.addAll(this.mDependencyDao.getDependentWorkIds(str2));
}
}
private void rescheduleAndResolve() {
this.mWorkDatabase.beginTransaction();
boolean z2 = true;
try {
this.mWorkSpecDao.setState(WorkInfo.State.ENQUEUED, this.mWorkSpecId);
this.mWorkSpecDao.setPeriodStartTime(this.mWorkSpecId, System.currentTimeMillis());
this.mWorkSpecDao.markWorkSpecScheduled(this.mWorkSpecId, -1);
this.mWorkDatabase.setTransactionSuccessful();
} finally {
this.mWorkDatabase.endTransaction();
resolve(z2);
}
}
private void resetPeriodicAndResolve() {
this.mWorkDatabase.beginTransaction();
boolean z2 = false;
try {
this.mWorkSpecDao.setPeriodStartTime(this.mWorkSpecId, System.currentTimeMillis());
this.mWorkSpecDao.setState(WorkInfo.State.ENQUEUED, this.mWorkSpecId);
this.mWorkSpecDao.resetWorkSpecRunAttemptCount(this.mWorkSpecId);
this.mWorkSpecDao.markWorkSpecScheduled(this.mWorkSpecId, -1);
this.mWorkDatabase.setTransactionSuccessful();
} finally {
this.mWorkDatabase.endTransaction();
resolve(z2);
}
}
/* JADX INFO: finally extract failed */
/* JADX WARNING: Removed duplicated region for block: B:10:0x001e A[Catch:{ all -> 0x0059 }] */
/* JADX WARNING: Removed duplicated region for block: B:12:0x0027 A[Catch:{ all -> 0x0059 }] */
private void resolve(boolean z2) {
boolean z3;
ListenableWorker listenableWorker;
this.mWorkDatabase.beginTransaction();
try {
List<String> allUnfinishedWork = this.mWorkDatabase.workSpecDao().getAllUnfinishedWork();
if (allUnfinishedWork != null) {
if (!allUnfinishedWork.isEmpty()) {
z3 = false;
if (z3) {
PackageManagerHelper.setComponentEnabled(this.mAppContext, RescheduleReceiver.class, false);
}
if (z2) {
this.mWorkSpecDao.markWorkSpecScheduled(this.mWorkSpecId, -1);
}
if (!(this.mWorkSpec == null || (listenableWorker = this.mWorker) == null || !listenableWorker.isRunInForeground())) {
this.mForegroundProcessor.stopForeground(this.mWorkSpecId);
}
this.mWorkDatabase.setTransactionSuccessful();
this.mWorkDatabase.endTransaction();
this.mFuture.set(Boolean.valueOf(z2));
}
}
z3 = true;
if (z3) {
}
if (z2) {
}
this.mForegroundProcessor.stopForeground(this.mWorkSpecId);
this.mWorkDatabase.setTransactionSuccessful();
this.mWorkDatabase.endTransaction();
this.mFuture.set(Boolean.valueOf(z2));
} catch (Throwable th) {
this.mWorkDatabase.endTransaction();
throw th;
}
}
private void resolveIncorrectStatus() {
WorkInfo.State state = this.mWorkSpecDao.getState(this.mWorkSpecId);
if (state == WorkInfo.State.RUNNING) {
Logger.get().debug(TAG, String.format("Status for %s is RUNNING;not doing any work and rescheduling for later execution", this.mWorkSpecId), new Throwable[0]);
resolve(true);
return;
}
Logger.get().debug(TAG, String.format("Status for %s is %s; not doing any work", this.mWorkSpecId, state), new Throwable[0]);
resolve(false);
}
private void runWorker() {
Data merge;
if (!tryCheckForInterruptionAndResolve()) {
this.mWorkDatabase.beginTransaction();
try {
WorkSpec workSpec = this.mWorkSpecDao.getWorkSpec(this.mWorkSpecId);
this.mWorkSpec = workSpec;
if (workSpec == null) {
Logger.get().error(TAG, String.format("Didn't find WorkSpec for id %s", this.mWorkSpecId), new Throwable[0]);
resolve(false);
} else if (workSpec.state != WorkInfo.State.ENQUEUED) {
resolveIncorrectStatus();
this.mWorkDatabase.setTransactionSuccessful();
Logger.get().debug(TAG, String.format("%s is not in ENQUEUED state. Nothing more to do.", this.mWorkSpec.workerClassName), new Throwable[0]);
this.mWorkDatabase.endTransaction();
} else {
if (workSpec.isPeriodic() || this.mWorkSpec.isBackedOff()) {
long currentTimeMillis = System.currentTimeMillis();
WorkSpec workSpec2 = this.mWorkSpec;
if (!(workSpec2.periodStartTime == 0) && currentTimeMillis < workSpec2.calculateNextRunTime()) {
Logger.get().debug(TAG, String.format("Delaying execution for %s because it is being executed before schedule.", this.mWorkSpec.workerClassName), new Throwable[0]);
resolve(true);
this.mWorkDatabase.endTransaction();
return;
}
}
this.mWorkDatabase.setTransactionSuccessful();
this.mWorkDatabase.endTransaction();
if (this.mWorkSpec.isPeriodic()) {
merge = this.mWorkSpec.input;
} else {
InputMerger createInputMergerWithDefaultFallback = this.mConfiguration.getInputMergerFactory().createInputMergerWithDefaultFallback(this.mWorkSpec.inputMergerClassName);
if (createInputMergerWithDefaultFallback == null) {
Logger.get().error(TAG, String.format("Could not create Input Merger %s", this.mWorkSpec.inputMergerClassName), new Throwable[0]);
setFailedAndResolve();
return;
}
ArrayList arrayList = new ArrayList();
arrayList.add(this.mWorkSpec.input);
arrayList.addAll(this.mWorkSpecDao.getInputsFromPrerequisites(this.mWorkSpecId));
merge = createInputMergerWithDefaultFallback.merge(arrayList);
}
WorkerParameters workerParameters = new WorkerParameters(UUID.fromString(this.mWorkSpecId), merge, this.mTags, this.mRuntimeExtras, this.mWorkSpec.runAttemptCount, this.mConfiguration.getExecutor(), this.mWorkTaskExecutor, this.mConfiguration.getWorkerFactory(), new WorkProgressUpdater(this.mWorkDatabase, this.mWorkTaskExecutor), new WorkForegroundUpdater(this.mWorkDatabase, this.mForegroundProcessor, this.mWorkTaskExecutor));
if (this.mWorker == null) {
this.mWorker = this.mConfiguration.getWorkerFactory().createWorkerWithDefaultFallback(this.mAppContext, this.mWorkSpec.workerClassName, workerParameters);
}
ListenableWorker listenableWorker = this.mWorker;
if (listenableWorker == null) {
Logger.get().error(TAG, String.format("Could not create Worker %s", this.mWorkSpec.workerClassName), new Throwable[0]);
setFailedAndResolve();
} else if (listenableWorker.isUsed()) {
Logger.get().error(TAG, String.format("Received an already-used Worker %s; WorkerFactory should return new instances", this.mWorkSpec.workerClassName), new Throwable[0]);
setFailedAndResolve();
} else {
this.mWorker.setUsed();
if (!trySetRunning()) {
resolveIncorrectStatus();
} else if (!tryCheckForInterruptionAndResolve()) {
SettableFuture create = SettableFuture.create();
this.mWorkTaskExecutor.getMainThreadExecutor().execute(new AnonymousClass1(create));
create.addListener(new AnonymousClass2(create, this.mWorkDescription), this.mWorkTaskExecutor.getBackgroundExecutor());
}
}
}
} finally {
this.mWorkDatabase.endTransaction();
}
}
}
private void setSucceededAndResolve() {
this.mWorkDatabase.beginTransaction();
boolean z2 = false;
try {
this.mWorkSpecDao.setState(WorkInfo.State.SUCCEEDED, this.mWorkSpecId);
this.mWorkSpecDao.setOutput(this.mWorkSpecId, ((ListenableWorker.Result.Success) this.mResult).getOutputData());
long currentTimeMillis = System.currentTimeMillis();
for (String str : this.mDependencyDao.getDependentWorkIds(this.mWorkSpecId)) {
if (this.mWorkSpecDao.getState(str) == WorkInfo.State.BLOCKED && this.mDependencyDao.hasCompletedAllPrerequisites(str)) {
Logger.get().info(TAG, String.format("Setting status to enqueued for %s", str), new Throwable[0]);
this.mWorkSpecDao.setState(WorkInfo.State.ENQUEUED, str);
this.mWorkSpecDao.setPeriodStartTime(str, currentTimeMillis);
}
}
this.mWorkDatabase.setTransactionSuccessful();
} finally {
this.mWorkDatabase.endTransaction();
resolve(z2);
}
}
private boolean tryCheckForInterruptionAndResolve() {
if (!this.mInterrupted) {
return false;
}
Logger.get().debug(TAG, String.format("Work interrupted for %s", this.mWorkDescription), new Throwable[0]);
WorkInfo.State state = this.mWorkSpecDao.getState(this.mWorkSpecId);
if (state == null) {
resolve(false);
} else {
resolve(!state.isFinished());
}
return true;
}
private boolean trySetRunning() {
this.mWorkDatabase.beginTransaction();
try {
boolean z2 = true;
if (this.mWorkSpecDao.getState(this.mWorkSpecId) == WorkInfo.State.ENQUEUED) {
this.mWorkSpecDao.setState(WorkInfo.State.RUNNING, this.mWorkSpecId);
this.mWorkSpecDao.incrementWorkSpecRunAttemptCount(this.mWorkSpecId);
} else {
z2 = false;
}
this.mWorkDatabase.setTransactionSuccessful();
return z2;
} finally {
this.mWorkDatabase.endTransaction();
}
}
@NonNull
public a<Boolean> getFuture() {
return this.mFuture;
}
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
public void interrupt() {
boolean z2;
this.mInterrupted = true;
tryCheckForInterruptionAndResolve();
a<ListenableWorker.Result> aVar = this.mInnerFuture;
if (aVar != null) {
z2 = aVar.isDone();
this.mInnerFuture.cancel(true);
} else {
z2 = false;
}
ListenableWorker listenableWorker = this.mWorker;
if (listenableWorker == null || z2) {
Logger.get().debug(TAG, String.format("WorkSpec %s is already done. Not interrupting.", this.mWorkSpec), new Throwable[0]);
} else {
listenableWorker.stop();
}
}
public void onWorkFinished() {
if (!tryCheckForInterruptionAndResolve()) {
this.mWorkDatabase.beginTransaction();
try {
WorkInfo.State state = this.mWorkSpecDao.getState(this.mWorkSpecId);
this.mWorkDatabase.workProgressDao().delete(this.mWorkSpecId);
if (state == null) {
resolve(false);
} else if (state == WorkInfo.State.RUNNING) {
handleResult(this.mResult);
} else if (!state.isFinished()) {
rescheduleAndResolve();
}
this.mWorkDatabase.setTransactionSuccessful();
} finally {
this.mWorkDatabase.endTransaction();
}
}
List<Scheduler> list = this.mSchedulers;
if (list != null) {
for (Scheduler scheduler : list) {
scheduler.cancel(this.mWorkSpecId);
}
Schedulers.schedule(this.mConfiguration, this.mWorkDatabase, this.mSchedulers);
}
}
@Override // java.lang.Runnable
@WorkerThread
public void run() {
List<String> tagsForWorkSpecId = this.mWorkTagDao.getTagsForWorkSpecId(this.mWorkSpecId);
this.mTags = tagsForWorkSpecId;
this.mWorkDescription = createWorkDescription(tagsForWorkSpecId);
runWorker();
}
@VisibleForTesting
public void setFailedAndResolve() {
this.mWorkDatabase.beginTransaction();
boolean z2 = false;
try {
iterativelyFailWorkAndDependents(this.mWorkSpecId);
this.mWorkSpecDao.setOutput(this.mWorkSpecId, ((ListenableWorker.Result.Failure) this.mResult).getOutputData());
this.mWorkDatabase.setTransactionSuccessful();
} finally {
this.mWorkDatabase.endTransaction();
resolve(z2);
}
}
}