package com.bumptech.glide.load.engine;

import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.google.android.gms.measurement.internal.u1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class l implements g, Runnable, Comparable, u5.b {
    public o Q;
    public a5.l R;
    public j S;
    public int T;
    public DecodeJob$Stage U;
    public DecodeJob$RunReason V;
    public boolean W;
    public Object X;
    public Thread Y;
    public a5.h Z;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public a5.h f5725a0;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    public Object f5727b0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public DataSource f5729c0;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final e8.h f5730d;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public com.bumptech.glide.load.data.e f5731d0;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final p0.e f5732e;

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public volatile h f5733e0;

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public volatile boolean f5735f0;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public volatile boolean f5737g0;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public com.bumptech.glide.k f5738h;

    /* JADX INFO: renamed from: h0, reason: collision with root package name */
    public boolean f5739h0;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public a5.h f5740i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public Priority f5741j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public w f5742k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f5743l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f5744m;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final i f5724a = new i();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ArrayList f5726b = new ArrayList();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final u5.e f5728c = new u5.e();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final k f5734f = new k();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final l2.f f5736g = new l2.f(1);

    public l(e8.h hVar, p0.e eVar) {
        this.f5730d = hVar;
        this.f5732e = eVar;
    }

    public final e0 a(com.bumptech.glide.load.data.e eVar, Object obj, DataSource dataSource) {
        if (obj == null) {
            eVar.b();
            return null;
        }
        try {
            int i10 = t5.i.f32966a;
            SystemClock.elapsedRealtimeNanos();
            e0 e0VarE = e(obj, dataSource);
            if (Log.isLoggable("DecodeJob", 2)) {
                e0VarE.toString();
                SystemClock.elapsedRealtimeNanos();
                Objects.toString(this.f5742k);
                Thread.currentThread().getName();
            }
            return e0VarE;
        } finally {
            eVar.b();
        }
    }

    @Override // com.bumptech.glide.load.engine.g
    public final void b(a5.h hVar, Exception exc, com.bumptech.glide.load.data.e eVar, DataSource dataSource) {
        eVar.b();
        GlideException glideException = new GlideException("Fetching data failed", Collections.singletonList(exc));
        glideException.f(hVar, dataSource, eVar.a());
        this.f5726b.add(glideException);
        if (Thread.currentThread() == this.Y) {
            m();
            return;
        }
        this.V = DecodeJob$RunReason.f5633b;
        u uVar = (u) this.S;
        (uVar.Q ? uVar.f5788i : uVar.R ? uVar.f5789j : uVar.f5787h).execute(this);
    }

    @Override // com.bumptech.glide.load.engine.g
    public final void c() {
        this.V = DecodeJob$RunReason.f5633b;
        u uVar = (u) this.S;
        (uVar.Q ? uVar.f5788i : uVar.R ? uVar.f5789j : uVar.f5787h).execute(this);
    }

    @Override // java.lang.Comparable
    public final int compareTo(Object obj) {
        l lVar = (l) obj;
        int iOrdinal = this.f5741j.ordinal() - lVar.f5741j.ordinal();
        return iOrdinal == 0 ? this.T - lVar.T : iOrdinal;
    }

    @Override // u5.b
    public final u5.e d() {
        return this.f5728c;
    }

    public final e0 e(Object obj, DataSource dataSource) {
        Class<?> cls = obj.getClass();
        i iVar = this.f5724a;
        c0 loadPath = iVar.f5696c.f5581b.getLoadPath(cls, iVar.f5700g, iVar.f5704k);
        a5.l lVar = this.R;
        if (Build.VERSION.SDK_INT >= 26) {
            boolean z10 = dataSource == DataSource.f5592d || iVar.f5711r;
            a5.k kVar = j5.n.f28669i;
            Boolean bool = (Boolean) lVar.get(kVar);
            if (bool == null || (bool.booleanValue() && !z10)) {
                lVar = new a5.l();
                t5.d dVar = this.R.f382b;
                t5.d dVar2 = lVar.f382b;
                dVar2.j(dVar);
                dVar2.put(kVar, Boolean.valueOf(z10));
            }
        }
        a5.l lVar2 = lVar;
        com.bumptech.glide.load.data.g gVarG = this.f5738h.f5581b.g(obj);
        try {
            return loadPath.a(this.f5743l, this.f5744m, lVar2, gVarG, new xyz.stream.utils.e0(this, dataSource, 6));
        } finally {
            gVarG.b();
        }
    }

    public final void f() {
        e0 e0VarA;
        boolean zA;
        if (Log.isLoggable("DecodeJob", 2)) {
            String str = "data: " + this.f5727b0 + ", cache key: " + this.Z + ", fetcher: " + this.f5731d0;
            int i10 = t5.i.f32966a;
            SystemClock.elapsedRealtimeNanos();
            Objects.toString(this.f5742k);
            if (str != null) {
                ", ".concat(str);
            }
            Thread.currentThread().getName();
        }
        d0 d0Var = null;
        try {
            e0VarA = a(this.f5731d0, this.f5727b0, this.f5729c0);
        } catch (GlideException e10) {
            e10.f(this.f5725a0, this.f5729c0, null);
            this.f5726b.add(e10);
            e0VarA = null;
        }
        if (e0VarA == null) {
            m();
            return;
        }
        DataSource dataSource = this.f5729c0;
        boolean z10 = this.f5739h0;
        if (e0VarA instanceof a0) {
            ((a0) e0VarA).initialize();
        }
        if (((d0) this.f5734f.f5723c) != null) {
            d0Var = (d0) t5.n.checkNotNull((d0) d0.f5658e.acquire());
            d0Var.f5662d = false;
            d0Var.f5661c = true;
            d0Var.f5660b = e0VarA;
            e0VarA = d0Var;
        }
        j(e0VarA, dataSource, z10);
        this.U = DecodeJob$Stage.f5640e;
        try {
            k kVar = this.f5734f;
            if (((d0) kVar.f5723c) != null) {
                kVar.b(this.f5730d, this.R);
            }
            l2.f fVar = this.f5736g;
            synchronized (fVar) {
                fVar.f29990c = true;
                zA = fVar.a();
            }
            if (zA) {
                l();
            }
        } finally {
            if (d0Var != null) {
                d0Var.c();
            }
        }
    }

    public final h g() {
        int iOrdinal = this.U.ordinal();
        i iVar = this.f5724a;
        if (iOrdinal == 1) {
            return new f0(iVar, this);
        }
        if (iOrdinal == 2) {
            return new e(iVar.a(), iVar, this);
        }
        if (iOrdinal == 3) {
            return new j0(iVar, this);
        }
        if (iOrdinal == 5) {
            return null;
        }
        throw new IllegalStateException("Unrecognized stage: " + this.U);
    }

    public final DecodeJob$Stage h(DecodeJob$Stage decodeJob$Stage) {
        int iOrdinal = decodeJob$Stage.ordinal();
        if (iOrdinal == 0) {
            int i10 = ((n) this.Q).f5750f;
            DecodeJob$Stage decodeJob$Stage2 = DecodeJob$Stage.f5637b;
            switch (i10) {
                case 1:
                case 2:
                    return h(decodeJob$Stage2);
                default:
                    return decodeJob$Stage2;
            }
        }
        if (iOrdinal == 1) {
            int i11 = ((n) this.Q).f5750f;
            DecodeJob$Stage decodeJob$Stage3 = DecodeJob$Stage.f5638c;
            switch (i11) {
                case 1:
                case 3:
                    return h(decodeJob$Stage3);
                case 2:
                default:
                    return decodeJob$Stage3;
            }
        }
        DecodeJob$Stage decodeJob$Stage4 = DecodeJob$Stage.f5641f;
        if (iOrdinal == 2) {
            return this.W ? decodeJob$Stage4 : DecodeJob$Stage.f5639d;
        }
        if (iOrdinal == 3 || iOrdinal == 5) {
            return decodeJob$Stage4;
        }
        throw new IllegalArgumentException("Unrecognized stage: " + decodeJob$Stage);
    }

    public final void i(com.bumptech.glide.k kVar, Object obj, w wVar, a5.h hVar, int i10, int i11, Class cls, Class cls2, Priority priority, o oVar, t5.d dVar, boolean z10, boolean z11, boolean z12, a5.l lVar, u uVar, int i12) {
        i iVar = this.f5724a;
        iVar.f5696c = kVar;
        iVar.f5697d = obj;
        iVar.f5707n = hVar;
        iVar.f5698e = i10;
        iVar.f5699f = i11;
        iVar.f5709p = oVar;
        iVar.f5700g = cls;
        iVar.f5701h = this.f5730d;
        iVar.f5704k = cls2;
        iVar.f5708o = priority;
        iVar.f5702i = lVar;
        iVar.f5703j = dVar;
        iVar.f5710q = z10;
        iVar.f5711r = z11;
        this.f5738h = kVar;
        this.f5740i = hVar;
        this.f5741j = priority;
        this.f5742k = wVar;
        this.f5743l = i10;
        this.f5744m = i11;
        this.Q = oVar;
        this.W = z12;
        this.R = lVar;
        this.S = uVar;
        this.T = i12;
        this.V = DecodeJob$RunReason.f5632a;
        this.X = obj;
    }

    public final void j(e0 e0Var, DataSource dataSource, boolean z10) {
        o();
        u uVar = (u) this.S;
        synchronized (uVar) {
            uVar.T = e0Var;
            uVar.U = dataSource;
            uVar.f5781b0 = z10;
        }
        synchronized (uVar) {
            try {
                uVar.f5780b.a();
                if (uVar.f5779a0) {
                    uVar.T.a();
                    uVar.g();
                    return;
                }
                if (uVar.f5778a.f5776a.isEmpty()) {
                    throw new IllegalStateException("Received a resource without any callbacks to notify");
                }
                if (uVar.V) {
                    throw new IllegalStateException("Already have resource");
                }
                com.google.android.gms.measurement.internal.a0 a0Var = uVar.f5784e;
                e0 e0Var2 = uVar.T;
                boolean z11 = uVar.f5792m;
                a5.h hVar = uVar.f5791l;
                x xVar = uVar.f5782c;
                a0Var.getClass();
                uVar.Y = new y(e0Var2, z11, true, hVar, xVar);
                uVar.V = true;
                t tVar = uVar.f5778a;
                tVar.getClass();
                ArrayList<s> arrayList = new ArrayList(tVar.f5776a);
                uVar.e(arrayList.size() + 1);
                ((q) uVar.f5785f).b(uVar, uVar.f5791l, uVar.Y);
                for (s sVar : arrayList) {
                    sVar.f5775b.execute(new r(uVar, sVar.f5774a, 1));
                }
                uVar.c();
            } finally {
            }
        }
    }

    public final void k() {
        boolean zA;
        o();
        GlideException glideException = new GlideException("Failed to load resource", new ArrayList(this.f5726b));
        u uVar = (u) this.S;
        synchronized (uVar) {
            uVar.W = glideException;
        }
        synchronized (uVar) {
            try {
                uVar.f5780b.a();
                if (uVar.f5779a0) {
                    uVar.g();
                } else {
                    if (uVar.f5778a.f5776a.isEmpty()) {
                        throw new IllegalStateException("Received an exception without any callbacks to notify");
                    }
                    if (uVar.X) {
                        throw new IllegalStateException("Already failed once");
                    }
                    uVar.X = true;
                    a5.h hVar = uVar.f5791l;
                    t tVar = uVar.f5778a;
                    tVar.getClass();
                    ArrayList<s> arrayList = new ArrayList(tVar.f5776a);
                    uVar.e(arrayList.size() + 1);
                    ((q) uVar.f5785f).b(uVar, hVar, null);
                    for (s sVar : arrayList) {
                        sVar.f5775b.execute(new r(uVar, sVar.f5774a, 0));
                    }
                    uVar.c();
                }
            } finally {
            }
        }
        l2.f fVar = this.f5736g;
        synchronized (fVar) {
            fVar.f29991d = true;
            zA = fVar.a();
        }
        if (zA) {
            l();
        }
    }

    public final void l() {
        l2.f fVar = this.f5736g;
        synchronized (fVar) {
            fVar.f29990c = false;
            fVar.f29989b = false;
            fVar.f29991d = false;
        }
        k kVar = this.f5734f;
        kVar.f5721a = null;
        kVar.f5722b = null;
        kVar.f5723c = null;
        i iVar = this.f5724a;
        iVar.f5696c = null;
        iVar.f5697d = null;
        iVar.f5707n = null;
        iVar.f5700g = null;
        iVar.f5704k = null;
        iVar.f5702i = null;
        iVar.f5708o = null;
        iVar.f5703j = null;
        iVar.f5709p = null;
        iVar.f5694a.clear();
        iVar.f5705l = false;
        iVar.f5695b.clear();
        iVar.f5706m = false;
        this.f5735f0 = false;
        this.f5738h = null;
        this.f5740i = null;
        this.R = null;
        this.f5741j = null;
        this.f5742k = null;
        this.S = null;
        this.U = null;
        this.f5733e0 = null;
        this.Y = null;
        this.Z = null;
        this.f5727b0 = null;
        this.f5729c0 = null;
        this.f5731d0 = null;
        this.f5737g0 = false;
        this.f5726b.clear();
        this.f5732e.a(this);
    }

    public final void m() {
        this.Y = Thread.currentThread();
        int i10 = t5.i.f32966a;
        SystemClock.elapsedRealtimeNanos();
        boolean zA = false;
        while (!this.f5737g0 && this.f5733e0 != null && !(zA = this.f5733e0.a())) {
            this.U = h(this.U);
            this.f5733e0 = g();
            if (this.U == DecodeJob$Stage.f5639d) {
                c();
                return;
            }
        }
        if ((this.U == DecodeJob$Stage.f5641f || this.f5737g0) && !zA) {
            k();
        }
    }

    public final void n() {
        int iOrdinal = this.V.ordinal();
        if (iOrdinal == 0) {
            this.U = h(DecodeJob$Stage.f5636a);
            this.f5733e0 = g();
            m();
        } else if (iOrdinal == 1) {
            m();
        } else if (iOrdinal == 2) {
            f();
        } else {
            throw new IllegalStateException("Unrecognized run reason: " + this.V);
        }
    }

    public final void o() {
        this.f5728c.a();
        if (this.f5735f0) {
            throw new IllegalStateException("Already notified", this.f5726b.isEmpty() ? null : (Throwable) u1.e(this.f5726b, 1));
        }
        this.f5735f0 = true;
    }

    @Override // com.bumptech.glide.load.engine.g
    public final void onDataFetcherReady(a5.h hVar, Object obj, com.bumptech.glide.load.data.e eVar, DataSource dataSource, a5.h hVar2) {
        this.Z = hVar;
        this.f5727b0 = obj;
        this.f5731d0 = eVar;
        this.f5729c0 = dataSource;
        this.f5725a0 = hVar2;
        this.f5739h0 = hVar != this.f5724a.a().get(0);
        if (Thread.currentThread() == this.Y) {
            f();
            return;
        }
        this.V = DecodeJob$RunReason.f5634c;
        u uVar = (u) this.S;
        (uVar.Q ? uVar.f5788i : uVar.R ? uVar.f5789j : uVar.f5787h).execute(this);
    }

    @Override // java.lang.Runnable
    public final void run() {
        com.bumptech.glide.load.data.e eVar = this.f5731d0;
        try {
            try {
                if (this.f5737g0) {
                    k();
                    if (eVar != null) {
                        eVar.b();
                        return;
                    }
                    return;
                }
                n();
                if (eVar != null) {
                    eVar.b();
                }
            } catch (Throwable th) {
                if (eVar != null) {
                    eVar.b();
                }
                throw th;
            }
        } catch (CallbackException e10) {
            throw e10;
        } catch (Throwable th2) {
            if (Log.isLoggable("DecodeJob", 3)) {
                Objects.toString(this.U);
            }
            if (this.U != DecodeJob$Stage.f5640e) {
                this.f5726b.add(th2);
                k();
            }
            if (!this.f5737g0) {
                throw th2;
            }
            throw th2;
        }
    }
}
