package com.facebook.bolts; import com.facebook.bolts.BoltsExecutors; import com.facebook.bolts.Task; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; import kotlin.Unit; import kotlin.collections.CollectionsKt__CollectionsKt; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.StringCompanionObject; /* loaded from: classes2.dex */ public final class Task { public static final ExecutorService BACKGROUND_EXECUTOR; public static final Companion Companion = new Companion(null); private static final Executor IMMEDIATE_EXECUTOR; private static final Task TASK_CANCELLED; private static final Task TASK_FALSE; private static final Task TASK_NULL; private static final Task TASK_TRUE; public static final Executor UI_THREAD_EXECUTOR; private static volatile UnobservedExceptionHandler unobservedExceptionHandler; private boolean cancelledField; private boolean completeField; private final Condition condition; private List> continuations; private Exception errorField; private boolean errorHasBeenObserved; private final ReentrantLock lock; private TResult resultField; private UnobservedErrorNotifier unobservedErrorNotifier; public interface UnobservedExceptionHandler { void unobservedException(Task task, UnobservedTaskException unobservedTaskException); } public static final Task call(Callable callable) { return Companion.call(callable); } public static final Task call(Callable callable, CancellationToken cancellationToken) { return Companion.call(callable, cancellationToken); } public static final Task call(Callable callable, Executor executor) { return Companion.call(callable, executor); } public static final Task call(Callable callable, Executor executor, CancellationToken cancellationToken) { return Companion.call(callable, executor, cancellationToken); } public static final Task callInBackground(Callable callable) { return Companion.callInBackground(callable); } public static final Task callInBackground(Callable callable, CancellationToken cancellationToken) { return Companion.callInBackground(callable, cancellationToken); } public static final Task cancelled() { return Companion.cancelled(); } public static final Task delay(long j) { return Companion.delay(j); } public static final Task delay(long j, CancellationToken cancellationToken) { return Companion.delay(j, cancellationToken); } public static final Task forError(Exception exc) { return Companion.forError(exc); } public static final Task forResult(TResult tresult) { return Companion.forResult(tresult); } public static final UnobservedExceptionHandler getUnobservedExceptionHandler() { return Companion.getUnobservedExceptionHandler(); } public static final void setUnobservedExceptionHandler(UnobservedExceptionHandler unobservedExceptionHandler2) { Companion.setUnobservedExceptionHandler(unobservedExceptionHandler2); } public static final Task whenAll(Collection> collection) { return Companion.whenAll(collection); } public static final Task> whenAllResult(Collection> collection) { return Companion.whenAllResult(collection); } public static final Task> whenAny(Collection> collection) { return Companion.whenAny(collection); } public static final Task> whenAnyResult(Collection> collection) { return Companion.whenAnyResult(collection); } /* JADX WARN: Multi-variable type inference failed */ public final Task cast() { return this; } public Task() { ReentrantLock reentrantLock = new ReentrantLock(); this.lock = reentrantLock; this.condition = reentrantLock.newCondition(); this.continuations = new ArrayList(); } private Task(TResult tresult) { ReentrantLock reentrantLock = new ReentrantLock(); this.lock = reentrantLock; this.condition = reentrantLock.newCondition(); this.continuations = new ArrayList(); trySetResult(tresult); } private Task(boolean z) { ReentrantLock reentrantLock = new ReentrantLock(); this.lock = reentrantLock; this.condition = reentrantLock.newCondition(); this.continuations = new ArrayList(); if (z) { trySetCancelled(); } else { trySetResult(null); } } public final boolean isCompleted() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { return this.completeField; } finally { reentrantLock.unlock(); } } public final boolean isCancelled() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { return this.cancelledField; } finally { reentrantLock.unlock(); } } public final boolean isFaulted() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { return this.errorField != null; } finally { reentrantLock.unlock(); } } public final TResult getResult() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { return this.resultField; } finally { reentrantLock.unlock(); } } public final Exception getError() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (this.errorField != null) { this.errorHasBeenObserved = true; UnobservedErrorNotifier unobservedErrorNotifier = this.unobservedErrorNotifier; if (unobservedErrorNotifier != null) { unobservedErrorNotifier.setObserved(); this.unobservedErrorNotifier = null; } } Exception exc = this.errorField; reentrantLock.unlock(); return exc; } catch (Throwable th) { reentrantLock.unlock(); throw th; } } public final void waitForCompletion() throws InterruptedException { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (!isCompleted()) { this.condition.await(); } Unit unit = Unit.INSTANCE; reentrantLock.unlock(); } catch (Throwable th) { reentrantLock.unlock(); throw th; } } public final boolean waitForCompletion(long j, TimeUnit timeUnit) throws InterruptedException { Intrinsics.checkNotNullParameter(timeUnit, "timeUnit"); ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (!isCompleted()) { this.condition.await(j, timeUnit); } boolean isCompleted = isCompleted(); reentrantLock.unlock(); return isCompleted; } catch (Throwable th) { reentrantLock.unlock(); throw th; } } public final Task makeVoid() { return continueWithTask(new Continuation() { // from class: com.facebook.bolts.Task$$ExternalSyntheticLambda3 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Task m528makeVoid$lambda8; m528makeVoid$lambda8 = Task.m528makeVoid$lambda8(task); return m528makeVoid$lambda8; } }); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: makeVoid$lambda-8, reason: not valid java name */ public static final Task m528makeVoid$lambda8(Task task) { Intrinsics.checkNotNullParameter(task, "task"); if (task.isCancelled()) { return Companion.cancelled(); } if (task.isFaulted()) { return Companion.forError(task.getError()); } return Companion.forResult(null); } public final Task continueWhile(Callable predicate, Continuation> continuation) { Intrinsics.checkNotNullParameter(predicate, "predicate"); Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWhile(predicate, continuation, IMMEDIATE_EXECUTOR, null); } public final Task continueWhile(Callable predicate, Continuation> continuation, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(predicate, "predicate"); Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWhile(predicate, continuation, IMMEDIATE_EXECUTOR, cancellationToken); } public static /* synthetic */ Task continueWhile$default(Task task, Callable callable, Continuation continuation, Executor executor, CancellationToken cancellationToken, int i, Object obj) { if ((i & 4) != 0) { executor = IMMEDIATE_EXECUTOR; } if ((i & 8) != 0) { cancellationToken = null; } return task.continueWhile(callable, continuation, executor, cancellationToken); } public final Task continueWhile(final Callable predicate, final Continuation> continuation, final Executor executor, final CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(predicate, "predicate"); Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return makeVoid().continueWithTask((Continuation>) new Continuation>() { // from class: com.facebook.bolts.Task$continueWhile$predicateContinuation$1 /* JADX WARN: Can't rename method to resolve collision */ @Override // com.facebook.bolts.Continuation public Task then(Task task) throws Exception { Intrinsics.checkNotNullParameter(task, "task"); CancellationToken cancellationToken2 = CancellationToken.this; if (cancellationToken2 != null && cancellationToken2.isCancellationRequested()) { return Task.Companion.cancelled(); } Boolean call = predicate.call(); Intrinsics.checkNotNullExpressionValue(call, "predicate.call()"); if (call.booleanValue()) { return Task.Companion.forResult(null).onSuccessTask(continuation, executor).onSuccessTask(this, executor); } return Task.Companion.forResult(null); } }, executor); } public final Task continueWith(Continuation continuation, Executor executor) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return continueWith(continuation, executor, null); } public final Task continueWith(final Continuation continuation, final Executor executor, final CancellationToken cancellationToken) { List> list; Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { boolean isCompleted = isCompleted(); if (!isCompleted && (list = this.continuations) != null) { list.add(new Continuation() { // from class: com.facebook.bolts.Task$$ExternalSyntheticLambda1 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Void m526continueWith$lambda10$lambda9; m526continueWith$lambda10$lambda9 = Task.m526continueWith$lambda10$lambda9(TaskCompletionSource.this, continuation, executor, cancellationToken, task); return m526continueWith$lambda10$lambda9; } }); } Unit unit = Unit.INSTANCE; reentrantLock.unlock(); if (isCompleted) { Companion.completeImmediately(taskCompletionSource, continuation, this, executor, cancellationToken); } return taskCompletionSource.getTask(); } catch (Throwable th) { reentrantLock.unlock(); throw th; } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: continueWith$lambda-10$lambda-9, reason: not valid java name */ public static final Void m526continueWith$lambda10$lambda9(com.facebook.bolts.TaskCompletionSource tcs, Continuation continuation, Executor executor, CancellationToken cancellationToken, Task task) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(executor, "$executor"); Intrinsics.checkNotNullParameter(task, "task"); Companion.completeImmediately(tcs, continuation, task, executor, cancellationToken); return null; } public final Task continueWith(Continuation continuation) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWith(continuation, IMMEDIATE_EXECUTOR, null); } public final Task continueWith(Continuation continuation, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWith(continuation, IMMEDIATE_EXECUTOR, cancellationToken); } public final Task continueWithTask(Continuation> continuation, Executor executor) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return continueWithTask(continuation, executor, null); } public final Task continueWithTask(final Continuation> continuation, final Executor executor, final CancellationToken cancellationToken) { List> list; Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { boolean isCompleted = isCompleted(); if (!isCompleted && (list = this.continuations) != null) { list.add(new Continuation() { // from class: com.facebook.bolts.Task$$ExternalSyntheticLambda2 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Void m527continueWithTask$lambda12$lambda11; m527continueWithTask$lambda12$lambda11 = Task.m527continueWithTask$lambda12$lambda11(TaskCompletionSource.this, continuation, executor, cancellationToken, task); return m527continueWithTask$lambda12$lambda11; } }); } Unit unit = Unit.INSTANCE; reentrantLock.unlock(); if (isCompleted) { Companion.completeAfterTask(taskCompletionSource, continuation, this, executor, cancellationToken); } return taskCompletionSource.getTask(); } catch (Throwable th) { reentrantLock.unlock(); throw th; } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: continueWithTask$lambda-12$lambda-11, reason: not valid java name */ public static final Void m527continueWithTask$lambda12$lambda11(com.facebook.bolts.TaskCompletionSource tcs, Continuation continuation, Executor executor, CancellationToken cancellationToken, Task task) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(executor, "$executor"); Intrinsics.checkNotNullParameter(task, "task"); Companion.completeAfterTask(tcs, continuation, task, executor, cancellationToken); return null; } public final Task continueWithTask(Continuation> continuation) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWithTask(continuation, IMMEDIATE_EXECUTOR, null); } public final Task continueWithTask(Continuation> continuation, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return continueWithTask(continuation, IMMEDIATE_EXECUTOR, cancellationToken); } public final Task onSuccess(Continuation continuation, Executor executor) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return onSuccess(continuation, executor, null); } public final Task onSuccess(final Continuation continuation, Executor executor, final CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return continueWithTask(new Continuation() { // from class: com.facebook.bolts.Task$$ExternalSyntheticLambda0 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Task m529onSuccess$lambda13; m529onSuccess$lambda13 = Task.m529onSuccess$lambda13(CancellationToken.this, continuation, task); return m529onSuccess$lambda13; } }, executor); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: onSuccess$lambda-13, reason: not valid java name */ public static final Task m529onSuccess$lambda13(CancellationToken cancellationToken, Continuation continuation, Task task) { Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(task, "task"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { return Companion.cancelled(); } if (task.isFaulted()) { return Companion.forError(task.getError()); } if (task.isCancelled()) { return Companion.cancelled(); } return task.continueWith(continuation); } public final Task onSuccess(Continuation continuation) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return onSuccess(continuation, IMMEDIATE_EXECUTOR, null); } public final Task onSuccess(Continuation continuation, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return onSuccess(continuation, IMMEDIATE_EXECUTOR, cancellationToken); } public final Task onSuccessTask(Continuation> continuation, Executor executor) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return onSuccessTask(continuation, executor, null); } public final Task onSuccessTask(final Continuation> continuation, Executor executor, final CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); Intrinsics.checkNotNullParameter(executor, "executor"); return continueWithTask(new Continuation() { // from class: com.facebook.bolts.Task$$ExternalSyntheticLambda4 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Task m530onSuccessTask$lambda14; m530onSuccessTask$lambda14 = Task.m530onSuccessTask$lambda14(CancellationToken.this, continuation, task); return m530onSuccessTask$lambda14; } }, executor); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: onSuccessTask$lambda-14, reason: not valid java name */ public static final Task m530onSuccessTask$lambda14(CancellationToken cancellationToken, Continuation continuation, Task task) { Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(task, "task"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { return Companion.cancelled(); } if (task.isFaulted()) { return Companion.forError(task.getError()); } if (task.isCancelled()) { return Companion.cancelled(); } return task.continueWithTask(continuation); } public final Task onSuccessTask(Continuation> continuation) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return onSuccessTask(continuation, IMMEDIATE_EXECUTOR); } public final Task onSuccessTask(Continuation> continuation, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(continuation, "continuation"); return onSuccessTask(continuation, IMMEDIATE_EXECUTOR, cancellationToken); } private final void runContinuations() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { List> list = this.continuations; if (list != null) { Iterator it = list.iterator(); while (it.hasNext()) { try { ((Continuation) it.next()).then(this); } catch (RuntimeException e) { throw e; } catch (Throwable th) { throw new RuntimeException(th); } } } this.continuations = null; Unit unit = Unit.INSTANCE; reentrantLock.unlock(); } catch (Throwable th2) { reentrantLock.unlock(); throw th2; } } public final boolean trySetCancelled() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (this.completeField) { reentrantLock.unlock(); return false; } this.completeField = true; this.cancelledField = true; this.condition.signalAll(); runContinuations(); return true; } finally { reentrantLock.unlock(); } } public final boolean trySetResult(TResult tresult) { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (this.completeField) { reentrantLock.unlock(); return false; } this.completeField = true; this.resultField = tresult; this.condition.signalAll(); runContinuations(); return true; } finally { reentrantLock.unlock(); } } public final boolean trySetError(Exception exc) { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (this.completeField) { return false; } this.completeField = true; this.errorField = exc; this.errorHasBeenObserved = false; this.condition.signalAll(); runContinuations(); if (!this.errorHasBeenObserved && unobservedExceptionHandler != null) { this.unobservedErrorNotifier = new UnobservedErrorNotifier(this); } return true; } finally { reentrantLock.unlock(); } } public final class TaskCompletionSource extends com.facebook.bolts.TaskCompletionSource { final /* synthetic */ Task this$0; public TaskCompletionSource(Task this$0) { Intrinsics.checkNotNullParameter(this$0, "this$0"); this.this$0 = this$0; } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } public final UnobservedExceptionHandler getUnobservedExceptionHandler() { return Task.unobservedExceptionHandler; } public final void setUnobservedExceptionHandler(UnobservedExceptionHandler unobservedExceptionHandler) { Task.unobservedExceptionHandler = unobservedExceptionHandler; } /* JADX WARN: Multi-variable type inference failed */ public final Task forResult(TResult tresult) { if (tresult == 0) { return Task.TASK_NULL; } if (tresult instanceof Boolean) { return ((Boolean) tresult).booleanValue() ? Task.TASK_TRUE : Task.TASK_FALSE; } com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); taskCompletionSource.setResult(tresult); return taskCompletionSource.getTask(); } public final Task forError(Exception exc) { com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); taskCompletionSource.setError(exc); return taskCompletionSource.getTask(); } public final Task cancelled() { return Task.TASK_CANCELLED; } public final Task delay(long j) { return delay$facebook_bolts_release(j, BoltsExecutors.Companion.scheduled$facebook_bolts_release(), null); } public final Task delay(long j, CancellationToken cancellationToken) { return delay$facebook_bolts_release(j, BoltsExecutors.Companion.scheduled$facebook_bolts_release(), cancellationToken); } public final Task delay$facebook_bolts_release(long j, ScheduledExecutorService executor, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(executor, "executor"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { return cancelled(); } if (j <= 0) { return forResult(null); } final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); final ScheduledFuture schedule = executor.schedule(new Runnable() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda4 @Override // java.lang.Runnable public final void run() { Task.Companion.m537delay$lambda0(TaskCompletionSource.this); } }, j, TimeUnit.MILLISECONDS); if (cancellationToken != null) { cancellationToken.register(new Runnable() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda5 @Override // java.lang.Runnable public final void run() { Task.Companion.m538delay$lambda1(schedule, taskCompletionSource); } }); } return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: delay$lambda-0, reason: not valid java name */ public static final void m537delay$lambda0(com.facebook.bolts.TaskCompletionSource tcs) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); tcs.trySetResult(null); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: delay$lambda-1, reason: not valid java name */ public static final void m538delay$lambda1(ScheduledFuture scheduledFuture, com.facebook.bolts.TaskCompletionSource tcs) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); scheduledFuture.cancel(true); tcs.trySetCancelled(); } public final Task callInBackground(Callable callable) { Intrinsics.checkNotNullParameter(callable, "callable"); return call(callable, Task.BACKGROUND_EXECUTOR, null); } public final Task callInBackground(Callable callable, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(callable, "callable"); return call(callable, Task.BACKGROUND_EXECUTOR, cancellationToken); } public final Task call(Callable callable, Executor executor) { Intrinsics.checkNotNullParameter(callable, "callable"); Intrinsics.checkNotNullParameter(executor, "executor"); return call(callable, executor, null); } public final Task call(final Callable callable, Executor executor, final CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(callable, "callable"); Intrinsics.checkNotNullParameter(executor, "executor"); final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); try { executor.execute(new Runnable() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { Task.Companion.m533call$lambda2(CancellationToken.this, taskCompletionSource, callable); } }); } catch (Exception e) { taskCompletionSource.setError(new ExecutorException(e)); } return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Multi-variable type inference failed */ /* renamed from: call$lambda-2, reason: not valid java name */ public static final void m533call$lambda2(CancellationToken cancellationToken, com.facebook.bolts.TaskCompletionSource tcs, Callable callable) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(callable, "$callable"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { tcs.setCancelled(); return; } try { tcs.setResult(callable.call()); } catch (CancellationException unused) { tcs.setCancelled(); } catch (Exception e) { tcs.setError(e); } } public final Task call(Callable callable) { Intrinsics.checkNotNullParameter(callable, "callable"); return call(callable, Task.IMMEDIATE_EXECUTOR, null); } public final Task call(Callable callable, CancellationToken cancellationToken) { Intrinsics.checkNotNullParameter(callable, "callable"); return call(callable, Task.IMMEDIATE_EXECUTOR, cancellationToken); } public final Task> whenAnyResult(Collection> tasks) { Intrinsics.checkNotNullParameter(tasks, "tasks"); if (tasks.isEmpty()) { return forResult(null); } final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); final AtomicBoolean atomicBoolean = new AtomicBoolean(false); Iterator> it = tasks.iterator(); while (it.hasNext()) { it.next().continueWith(new Continuation() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda0 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Void m540whenAnyResult$lambda3; m540whenAnyResult$lambda3 = Task.Companion.m540whenAnyResult$lambda3(atomicBoolean, taskCompletionSource, task); return m540whenAnyResult$lambda3; } }); } return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: whenAnyResult$lambda-3, reason: not valid java name */ public static final Void m540whenAnyResult$lambda3(AtomicBoolean isAnyTaskComplete, com.facebook.bolts.TaskCompletionSource firstCompleted, Task it) { Intrinsics.checkNotNullParameter(isAnyTaskComplete, "$isAnyTaskComplete"); Intrinsics.checkNotNullParameter(firstCompleted, "$firstCompleted"); Intrinsics.checkNotNullParameter(it, "it"); if (isAnyTaskComplete.compareAndSet(false, true)) { firstCompleted.setResult(it); return null; } it.getError(); return null; } public final Task> whenAny(Collection> tasks) { Intrinsics.checkNotNullParameter(tasks, "tasks"); if (tasks.isEmpty()) { return forResult(null); } final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); final AtomicBoolean atomicBoolean = new AtomicBoolean(false); Iterator> it = tasks.iterator(); while (it.hasNext()) { it.next().continueWith(new Continuation() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda6 @Override // com.facebook.bolts.Continuation public final Object then(Task task) { Void m539whenAny$lambda4; m539whenAny$lambda4 = Task.Companion.m539whenAny$lambda4(atomicBoolean, taskCompletionSource, task); return m539whenAny$lambda4; } }); } return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: whenAny$lambda-4, reason: not valid java name */ public static final Void m539whenAny$lambda4(AtomicBoolean isAnyTaskComplete, com.facebook.bolts.TaskCompletionSource firstCompleted, Task it) { Intrinsics.checkNotNullParameter(isAnyTaskComplete, "$isAnyTaskComplete"); Intrinsics.checkNotNullParameter(firstCompleted, "$firstCompleted"); Intrinsics.checkNotNullParameter(it, "it"); if (isAnyTaskComplete.compareAndSet(false, true)) { firstCompleted.setResult(it); return null; } it.getError(); return null; } public final Task> whenAllResult(final Collection> tasks) { Intrinsics.checkNotNullParameter(tasks, "tasks"); return (Task>) whenAll(tasks).onSuccess(new Continuation>() { // from class: com.facebook.bolts.Task$Companion$whenAllResult$1 @Override // com.facebook.bolts.Continuation public List then(Task task) { Intrinsics.checkNotNullParameter(task, "task"); if (tasks.isEmpty()) { return CollectionsKt__CollectionsKt.emptyList(); } ArrayList arrayList = new ArrayList(); Iterator> it = tasks.iterator(); while (it.hasNext()) { arrayList.add(it.next().getResult()); } return arrayList; } }); } public final Task whenAll(Collection> tasks) { Intrinsics.checkNotNullParameter(tasks, "tasks"); if (tasks.isEmpty()) { return forResult(null); } final com.facebook.bolts.TaskCompletionSource taskCompletionSource = new com.facebook.bolts.TaskCompletionSource(); final ArrayList arrayList = new ArrayList(); final ReentrantLock reentrantLock = new ReentrantLock(); final AtomicInteger atomicInteger = new AtomicInteger(tasks.size()); final AtomicBoolean atomicBoolean = new AtomicBoolean(false); Iterator> it = tasks.iterator(); while (it.hasNext()) { it.next().continueWith(new Continuation() { // from class: com.facebook.bolts.Task$Companion$whenAll$1 @Override // com.facebook.bolts.Continuation public /* bridge */ /* synthetic */ Object then(Task task) { return then((Task) task); } @Override // com.facebook.bolts.Continuation public final Void then(Task it2) { Intrinsics.checkNotNullParameter(it2, "it"); if (it2.isFaulted()) { ReentrantLock reentrantLock2 = reentrantLock; ArrayList arrayList2 = arrayList; reentrantLock2.lock(); try { arrayList2.add(it2.getError()); } finally { reentrantLock2.unlock(); } } if (it2.isCancelled()) { atomicBoolean.set(true); } if (atomicInteger.decrementAndGet() == 0) { if (arrayList.size() != 0) { if (arrayList.size() == 1) { taskCompletionSource.setError(arrayList.get(0)); } else { StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("There were %d exceptions.", Arrays.copyOf(new Object[]{Integer.valueOf(arrayList.size())}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); taskCompletionSource.setError(new AggregateException(format, arrayList)); } } else if (atomicBoolean.get()) { taskCompletionSource.setCancelled(); } else { taskCompletionSource.setResult(null); } } return null; } }); } return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: private */ public final void completeImmediately(final com.facebook.bolts.TaskCompletionSource taskCompletionSource, final Continuation continuation, final Task task, Executor executor, final CancellationToken cancellationToken) { try { executor.execute(new Runnable() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda2 @Override // java.lang.Runnable public final void run() { Task.Companion.m536completeImmediately$lambda5(CancellationToken.this, taskCompletionSource, continuation, task); } }); } catch (Exception e) { taskCompletionSource.setError(new ExecutorException(e)); } } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Multi-variable type inference failed */ /* renamed from: completeImmediately$lambda-5, reason: not valid java name */ public static final void m536completeImmediately$lambda5(CancellationToken cancellationToken, com.facebook.bolts.TaskCompletionSource tcs, Continuation continuation, Task task) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(task, "$task"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { tcs.setCancelled(); return; } try { tcs.setResult(continuation.then(task)); } catch (CancellationException unused) { tcs.setCancelled(); } catch (Exception e) { tcs.setError(e); } } /* JADX INFO: Access modifiers changed from: private */ public final void completeAfterTask(final com.facebook.bolts.TaskCompletionSource taskCompletionSource, final Continuation> continuation, final Task task, Executor executor, final CancellationToken cancellationToken) { try { executor.execute(new Runnable() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda3 @Override // java.lang.Runnable public final void run() { Task.Companion.m534completeAfterTask$lambda7(CancellationToken.this, taskCompletionSource, continuation, task); } }); } catch (Exception e) { taskCompletionSource.setError(new ExecutorException(e)); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: completeAfterTask$lambda-7, reason: not valid java name */ public static final void m534completeAfterTask$lambda7(final CancellationToken cancellationToken, final com.facebook.bolts.TaskCompletionSource tcs, Continuation continuation, Task task) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(continuation, "$continuation"); Intrinsics.checkNotNullParameter(task, "$task"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { tcs.setCancelled(); return; } try { Task task2 = (Task) continuation.then(task); if (task2 != null) { task2.continueWith(new Continuation() { // from class: com.facebook.bolts.Task$Companion$$ExternalSyntheticLambda7 @Override // com.facebook.bolts.Continuation public final Object then(Task task3) { Void m535completeAfterTask$lambda7$lambda6; m535completeAfterTask$lambda7$lambda6 = Task.Companion.m535completeAfterTask$lambda7$lambda6(CancellationToken.this, tcs, task3); return m535completeAfterTask$lambda7$lambda6; } }); } else { tcs.setResult(null); } } catch (CancellationException unused) { tcs.setCancelled(); } catch (Exception e) { tcs.setError(e); } } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Multi-variable type inference failed */ /* renamed from: completeAfterTask$lambda-7$lambda-6, reason: not valid java name */ public static final Void m535completeAfterTask$lambda7$lambda6(CancellationToken cancellationToken, com.facebook.bolts.TaskCompletionSource tcs, Task task) { Intrinsics.checkNotNullParameter(tcs, "$tcs"); Intrinsics.checkNotNullParameter(task, "task"); if (cancellationToken != null && cancellationToken.isCancellationRequested()) { tcs.setCancelled(); return null; } if (task.isCancelled()) { tcs.setCancelled(); } else if (task.isFaulted()) { tcs.setError(task.getError()); } else { tcs.setResult(task.getResult()); } return null; } } static { BoltsExecutors.Companion companion = BoltsExecutors.Companion; BACKGROUND_EXECUTOR = companion.background(); IMMEDIATE_EXECUTOR = companion.immediate$facebook_bolts_release(); UI_THREAD_EXECUTOR = AndroidExecutors.Companion.uiThread(); TASK_NULL = new Task<>((Object) null); TASK_TRUE = new Task<>(Boolean.TRUE); TASK_FALSE = new Task<>(Boolean.FALSE); TASK_CANCELLED = new Task<>(true); } }