package com.airbnb.lottie; import android.os.Handler; import android.os.Looper; import com.airbnb.lottie.utils.Logger; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; /* loaded from: classes.dex */ public class LottieTask { public static Executor EXECUTOR = Executors.newCachedThreadPool(); private final Set> failureListeners; private final Handler handler; private volatile LottieResult result; private final Set> successListeners; public LottieTask(Callable> callable) { this(callable, false); } /* JADX INFO: Access modifiers changed from: package-private */ public LottieTask(Callable> callable, boolean z) { this.successListeners = new LinkedHashSet(1); this.failureListeners = new LinkedHashSet(1); this.handler = new Handler(Looper.getMainLooper()); this.result = null; if (z) { try { setResult(callable.call()); return; } catch (Throwable th) { setResult(new LottieResult<>(th)); return; } } EXECUTOR.execute(new LottieFutureTask(this, callable)); } /* JADX INFO: Access modifiers changed from: private */ public void setResult(LottieResult lottieResult) { if (this.result != null) { throw new IllegalStateException("A task may only be set once."); } this.result = lottieResult; notifyListeners(); } public LottieTask addListener(LottieListener lottieListener) { synchronized (this) { LottieResult lottieResult = this.result; if (lottieResult != null && lottieResult.getValue() != null) { lottieListener.onResult(lottieResult.getValue()); } this.successListeners.add(lottieListener); } return this; } public LottieTask removeListener(LottieListener lottieListener) { synchronized (this) { this.successListeners.remove(lottieListener); } return this; } public LottieTask addFailureListener(LottieListener lottieListener) { synchronized (this) { LottieResult lottieResult = this.result; if (lottieResult != null && lottieResult.getException() != null) { lottieListener.onResult(lottieResult.getException()); } this.failureListeners.add(lottieListener); } return this; } public LottieTask removeFailureListener(LottieListener lottieListener) { synchronized (this) { this.failureListeners.remove(lottieListener); } return this; } private void notifyListeners() { this.handler.post(new Runnable(this) { // from class: com.airbnb.lottie.LottieTask$$ExternalSyntheticLambda0 public final LottieTask f$0; @Override // java.lang.Runnable public final void run() { this.f$0.m36lambda$notifyListeners$0$comairbnblottieLottieTask(); } { this.f$0 = this; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$notifyListeners$0$com-airbnb-lottie-LottieTask, reason: not valid java name */ public /* synthetic */ void m36lambda$notifyListeners$0$comairbnblottieLottieTask() { LottieResult lottieResult = this.result; if (lottieResult == null) { return; } if (lottieResult.getValue() != null) { notifySuccessListeners(lottieResult.getValue()); } else { notifyFailureListeners(lottieResult.getException()); } } private void notifySuccessListeners(T t) { synchronized (this) { Iterator it = new ArrayList(this.successListeners).iterator(); while (it.hasNext()) { ((LottieListener) it.next()).onResult(t); } } } private void notifyFailureListeners(Throwable th) { synchronized (this) { ArrayList arrayList = new ArrayList(this.failureListeners); if (arrayList.isEmpty()) { Logger.warning("Lottie encountered an error but no failure listener was added:", th); return; } Iterator it = arrayList.iterator(); while (it.hasNext()) { ((LottieListener) it.next()).onResult(th); } } } /* loaded from: classes.dex */ class LottieFutureTask extends FutureTask> { final LottieTask this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ LottieFutureTask(LottieTask lottieTask, Callable> callable) { super(callable); this.this$0 = lottieTask; } @Override // java.util.concurrent.FutureTask protected void done() { if (isCancelled()) { return; } try { this.this$0.setResult(get()); } catch (InterruptedException | ExecutionException e) { this.this$0.setResult(new LottieResult(e)); } } } }