package defpackage;

import android.os.Handler;
import android.os.Looper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/* JADX INFO: loaded from: classes.dex */
public class cl5<T> {
    public static final ExecutorService e = Executors.newCachedThreadPool(new el5());
    public final LinkedHashSet a = new LinkedHashSet(1);
    public final LinkedHashSet b = new LinkedHashSet(1);
    public final Handler c = new Handler(Looper.getMainLooper());
    public volatile bl5 d = null;

    public static class a<T> extends FutureTask<bl5<T>> {
        public cl5 a;

        @Override // java.util.concurrent.FutureTask
        public final void done() {
            try {
                if (isCancelled()) {
                    return;
                }
                try {
                    cl5 cl5Var = this.a;
                    bl5<T> bl5Var = get();
                    ExecutorService executorService = cl5.e;
                    cl5Var.d(bl5Var);
                } catch (InterruptedException | ExecutionException e) {
                    cl5 cl5Var2 = this.a;
                    bl5 bl5Var2 = new bl5(e);
                    ExecutorService executorService2 = cl5.e;
                    cl5Var2.d(bl5Var2);
                }
            } finally {
                this.a = null;
            }
        }
    }

    public cl5(zj5 zj5Var) {
        d(new bl5(zj5Var));
    }

    public final synchronized void a(sk5 sk5Var) {
        Throwable th;
        try {
            bl5 bl5Var = this.d;
            if (bl5Var != null && (th = bl5Var.b) != null) {
                sk5Var.onResult(th);
            }
            this.b.add(sk5Var);
        } catch (Throwable th2) {
            throw th2;
        }
    }

    public final synchronized void b(sk5 sk5Var) {
        zj5 zj5Var;
        try {
            bl5 bl5Var = this.d;
            if (bl5Var != null && (zj5Var = bl5Var.a) != null) {
                sk5Var.onResult(zj5Var);
            }
            this.a.add(sk5Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void c() {
        bl5 bl5Var = this.d;
        if (bl5Var == null) {
            return;
        }
        zj5 zj5Var = bl5Var.a;
        if (zj5Var != null) {
            synchronized (this) {
                Iterator it = new ArrayList(this.a).iterator();
                while (it.hasNext()) {
                    ((sk5) it.next()).onResult(zj5Var);
                }
            }
            return;
        }
        Throwable th = bl5Var.b;
        synchronized (this) {
            ArrayList arrayList = new ArrayList(this.b);
            if (arrayList.isEmpty()) {
                gh5.c("Lottie encountered an error but no failure listener was added:", th);
                return;
            }
            Iterator it2 = arrayList.iterator();
            while (it2.hasNext()) {
                ((sk5) it2.next()).onResult(th);
            }
        }
    }

    public final void d(bl5 bl5Var) {
        if (this.d != null) {
            throw new IllegalStateException("A task may only be set once.");
        }
        this.d = bl5Var;
        if (Looper.myLooper() == Looper.getMainLooper()) {
            c();
        } else {
            this.c.post(new tb5(this, 3));
        }
    }

    public cl5(Callable callable, boolean z) {
        if (z) {
            try {
                d((bl5) callable.call());
                return;
            } catch (Throwable th) {
                d(new bl5(th));
                return;
            }
        }
        ExecutorService executorService = e;
        a aVar = new a(callable);
        aVar.a = this;
        executorService.execute(aVar);
    }
}
