117 lines
4.1 KiB
Java
117 lines
4.1 KiB
Java
|
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<Void> tail = Tasks.forResult(null);
|
||
|
private final Object tailLock = new Object();
|
||
|
private ThreadLocal<Boolean> 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<Void> submit(Runnable runnable) {
|
||
|
return submit(new Callable<Void>(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 <T> Continuation<Void, T> newContinuation(Callable<T> callable) {
|
||
|
return new Continuation<Void, T>(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<Void> task) throws Exception {
|
||
|
return (T) this.val$callable.call();
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private <T> Task<Void> ignoreResult(Task<T> task) {
|
||
|
return task.continueWith(this.executorService, new Continuation<T, Void>(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<T> task2) throws Exception {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
{
|
||
|
this.this$0 = this;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public <T> Task<T> submit(Callable<T> callable) {
|
||
|
Task<T> continueWith;
|
||
|
synchronized (this.tailLock) {
|
||
|
continueWith = this.tail.continueWith(this.executorService, newContinuation(callable));
|
||
|
this.tail = ignoreResult(continueWith);
|
||
|
}
|
||
|
return continueWith;
|
||
|
}
|
||
|
|
||
|
public <T> Task<T> submitTask(Callable<Task<T>> callable) {
|
||
|
Task<T> continueWithTask;
|
||
|
synchronized (this.tailLock) {
|
||
|
continueWithTask = this.tail.continueWithTask(this.executorService, newContinuation(callable));
|
||
|
this.tail = ignoreResult(continueWithTask);
|
||
|
}
|
||
|
return continueWithTask;
|
||
|
}
|
||
|
|
||
|
public Executor getExecutor() {
|
||
|
return this.executorService;
|
||
|
}
|
||
|
}
|