package com.google.firebase.crashlytics.internal.common; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public class CrashlyticsBackgroundWorker { private final ExecutorService executorService; private Task tail = Tasks.forResult(null); private final Object tailLock = new Object(); private ThreadLocal isExecutorThread = new ThreadLocal<>(); public CrashlyticsBackgroundWorker(ExecutorService executorService) { this.executorService = executorService; executorService.submit(new Runnable(this) { // from class: com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker.1 final CrashlyticsBackgroundWorker this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.isExecutorThread.set(Boolean.TRUE); } }); } private boolean isRunningOnThread() { return Boolean.TRUE.equals(this.isExecutorThread.get()); } public void checkRunningOnThread() { if (!isRunningOnThread()) { throw new IllegalStateException("Not running on background worker thread as intended."); } } /* JADX INFO: Access modifiers changed from: package-private */ public Task submit(Runnable runnable) { return submit(new Callable(this, runnable) { // from class: com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker.2 final CrashlyticsBackgroundWorker this$0; final Runnable val$runnable; { this.this$0 = this; this.val$runnable = runnable; } @Override // java.util.concurrent.Callable public Void call() throws Exception { this.val$runnable.run(); return null; } }); } private Continuation newContinuation(Callable callable) { return new Continuation(this, callable) { // from class: com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker.3 final CrashlyticsBackgroundWorker this$0; final Callable val$callable; { this.this$0 = this; this.val$callable = callable; } @Override // com.google.android.gms.tasks.Continuation public T then(Task task) throws Exception { return (T) this.val$callable.call(); } }; } private Task ignoreResult(Task task) { return task.continueWith(this.executorService, new Continuation(this) { // from class: com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker.4 final CrashlyticsBackgroundWorker this$0; @Override // com.google.android.gms.tasks.Continuation public Void then(Task task2) throws Exception { return null; } { this.this$0 = this; } }); } public Task submit(Callable callable) { Task continueWith; synchronized (this.tailLock) { continueWith = this.tail.continueWith(this.executorService, newContinuation(callable)); this.tail = ignoreResult(continueWith); } return continueWith; } public Task submitTask(Callable> callable) { Task continueWithTask; synchronized (this.tailLock) { continueWithTask = this.tail.continueWithTask(this.executorService, newContinuation(callable)); this.tail = ignoreResult(continueWithTask); } return continueWithTask; } public Executor getExecutor() { return this.executorService; } }