package lh;

import android.util.Log;
import com.bumptech.glide.load.data.d;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import lh.f;
import ph.n;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes4.dex */
public class z implements f, f.a {

    /* renamed from: p, reason: collision with root package name */
    private final g f28274p;

    /* renamed from: q, reason: collision with root package name */
    private final f.a f28275q;

    /* renamed from: r, reason: collision with root package name */
    private volatile int f28276r;

    /* renamed from: s, reason: collision with root package name */
    private volatile c f28277s;

    /* renamed from: t, reason: collision with root package name */
    private volatile Object f28278t;

    /* renamed from: u, reason: collision with root package name */
    private volatile n.a f28279u;

    /* renamed from: v, reason: collision with root package name */
    private volatile d f28280v;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public class a implements d.a {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ n.a f28281p;

        a(n.a aVar) {
            this.f28281p = aVar;
        }

        @Override // com.bumptech.glide.load.data.d.a
        public void c(Exception exc) {
            if (z.this.f(this.f28281p)) {
                z.this.i(this.f28281p, exc);
            }
        }

        @Override // com.bumptech.glide.load.data.d.a
        public void f(Object obj) {
            if (z.this.f(this.f28281p)) {
                z.this.h(this.f28281p, obj);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public z(g gVar, f.a aVar) {
        this.f28274p = gVar;
        this.f28275q = aVar;
    }

    private boolean c(Object obj) {
        long b10 = ei.g.b();
        boolean z10 = false;
        try {
            com.bumptech.glide.load.data.e o10 = this.f28274p.o(obj);
            Object a10 = o10.a();
            jh.d q10 = this.f28274p.q(a10);
            e eVar = new e(q10, a10, this.f28274p.k());
            d dVar = new d(this.f28279u.f32706a, this.f28274p.p());
            nh.a d10 = this.f28274p.d();
            d10.a(dVar, eVar);
            if (Log.isLoggable("SourceGenerator", 2)) {
                Log.v("SourceGenerator", "Finished encoding source to cache, key: " + dVar + ", data: " + obj + ", encoder: " + q10 + ", duration: " + ei.g.a(b10));
            }
            if (d10.b(dVar) != null) {
                this.f28280v = dVar;
                this.f28277s = new c(Collections.singletonList(this.f28279u.f32706a), this.f28274p, this);
                this.f28279u.f32708c.b();
                return true;
            }
            if (Log.isLoggable("SourceGenerator", 3)) {
                Log.d("SourceGenerator", "Attempt to write: " + this.f28280v + ", data: " + obj + " to the disk cache failed, maybe the disk cache is disabled? Trying to decode the data directly...");
            }
            try {
                this.f28275q.g(this.f28279u.f32706a, o10.a(), this.f28279u.f32708c, this.f28279u.f32708c.d(), this.f28279u.f32706a);
                return false;
            } catch (Throwable th2) {
                th = th2;
                z10 = true;
                if (!z10) {
                    this.f28279u.f32708c.b();
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
        }
    }

    private boolean d() {
        return this.f28276r < this.f28274p.g().size();
    }

    private void j(n.a aVar) {
        this.f28279u.f32708c.e(this.f28274p.l(), new a(aVar));
    }

    @Override // lh.f.a
    public void a(jh.f fVar, Exception exc, com.bumptech.glide.load.data.d dVar, jh.a aVar) {
        this.f28275q.a(fVar, exc, dVar, this.f28279u.f32708c.d());
    }

    @Override // lh.f
    public boolean b() {
        if (this.f28278t != null) {
            Object obj = this.f28278t;
            this.f28278t = null;
            try {
                if (!c(obj)) {
                    return true;
                }
            } catch (IOException e10) {
                if (Log.isLoggable("SourceGenerator", 3)) {
                    Log.d("SourceGenerator", "Failed to properly rewind or write data to cache", e10);
                }
            }
        }
        if (this.f28277s != null && this.f28277s.b()) {
            return true;
        }
        this.f28277s = null;
        this.f28279u = null;
        boolean z10 = false;
        while (!z10 && d()) {
            List g10 = this.f28274p.g();
            int i10 = this.f28276r;
            this.f28276r = i10 + 1;
            this.f28279u = (n.a) g10.get(i10);
            if (this.f28279u != null && (this.f28274p.e().c(this.f28279u.f32708c.d()) || this.f28274p.u(this.f28279u.f32708c.a()))) {
                j(this.f28279u);
                z10 = true;
            }
        }
        return z10;
    }

    @Override // lh.f
    public void cancel() {
        n.a aVar = this.f28279u;
        if (aVar != null) {
            aVar.f32708c.cancel();
        }
    }

    @Override // lh.f.a
    public void e() {
        throw new UnsupportedOperationException();
    }

    boolean f(n.a aVar) {
        n.a aVar2 = this.f28279u;
        return aVar2 != null && aVar2 == aVar;
    }

    @Override // lh.f.a
    public void g(jh.f fVar, Object obj, com.bumptech.glide.load.data.d dVar, jh.a aVar, jh.f fVar2) {
        this.f28275q.g(fVar, obj, dVar, this.f28279u.f32708c.d(), fVar);
    }

    void h(n.a aVar, Object obj) {
        j e10 = this.f28274p.e();
        if (obj != null && e10.c(aVar.f32708c.d())) {
            this.f28278t = obj;
            this.f28275q.e();
        } else {
            f.a aVar2 = this.f28275q;
            jh.f fVar = aVar.f32706a;
            com.bumptech.glide.load.data.d dVar = aVar.f32708c;
            aVar2.g(fVar, obj, dVar, dVar.d(), this.f28280v);
        }
    }

    void i(n.a aVar, Exception exc) {
        f.a aVar2 = this.f28275q;
        d dVar = this.f28280v;
        com.bumptech.glide.load.data.d dVar2 = aVar.f32708c;
        aVar2.a(dVar, exc, dVar2, dVar2.d());
    }
}
