package com.google.android.gms.internal.ads;

import android.content.Context;
import android.net.Uri;
import android.os.SystemClock;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

/* JADX INFO: loaded from: classes2.dex */
public final class h50 extends qr1 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Context f12014d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final sv1 f12015e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ru f12016f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final String f12017g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f12018h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final boolean f12019i;

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

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

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public volatile yk f12023m;

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

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

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public long f12028r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public com.google.common.util.concurrent.w f12029s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final AtomicLong f12030t;

    public h50(Context context, sv1 sv1Var, String str, int i10, n50 n50Var, ru ruVar) {
        super(false);
        this.f12014d = context;
        this.f12015e = sv1Var;
        this.f12016f = ruVar;
        this.f12017g = str;
        this.f12018h = i10;
        this.f12024n = false;
        this.f12025o = false;
        this.f12026p = false;
        this.f12027q = false;
        this.f12028r = 0L;
        this.f12030t = new AtomicLong(-1L);
        this.f12029s = null;
        this.f12019i = ((Boolean) n7.y.f31093e.f31096c.b(ao.f8979n2)).booleanValue();
        g(n50Var);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r4v1 */
    /* JADX WARN: Type inference failed for: r4v2 */
    /* JADX WARN: Type inference failed for: r4v3 */
    /* JADX WARN: Type inference failed for: r4v4, types: [boolean] */
    /* JADX WARN: Type inference failed for: r5v18, types: [m7.n] */
    /* JADX WARN: Type inference failed for: r5v21 */
    /* JADX WARN: Type inference failed for: r5v28, types: [java.lang.StringBuilder] */
    /* JADX WARN: Type inference failed for: r5v41 */
    /* JADX WARN: Type inference failed for: r5v42 */
    /* JADX WARN: Type inference failed for: r7v3, types: [com.google.android.gms.internal.ads.ru] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws Throwable {
        vk vkVarB;
        boolean z10;
        boolean z11;
        boolean z12;
        long j10;
        boolean z13;
        boolean z14;
        long jElapsedRealtime;
        gl glVar;
        dy1 dy1VarB = dy1Var;
        String string = "ms";
        if (this.f12021k) {
            throw new IOException("Attempt to open an already open GcacheDataSource.");
        }
        ?? r42 = 1;
        this.f12021k = true;
        Uri uri = dy1VarB.f10564a;
        this.f12022l = uri;
        boolean z15 = this.f12019i;
        if (!z15) {
            m(dy1Var);
        }
        this.f12023m = yk.zza(uri);
        tn tnVar = ao.f8767a5;
        n7.y yVar = n7.y.f31093e;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            if (this.f12023m != null) {
                this.f12023m.f18864g = dy1VarB.f10566c;
                yk ykVar = this.f12023m;
                String str = this.f12017g;
                ykVar.f18865h = str != null ? str : "";
                this.f12023m.f18866i = this.f12018h;
                vkVarB = m7.n.C.f30471j.b(this.f12023m);
            } else {
                vkVarB = null;
            }
            if (vkVarB != null && vkVarB.w()) {
                synchronized (vkVarB) {
                    z10 = vkVarB.f17666a;
                }
                this.f12024n = z10;
                synchronized (vkVarB) {
                    z11 = vkVarB.f17669d;
                }
                this.f12026p = z11;
                synchronized (vkVarB) {
                    z12 = vkVarB.f17667b;
                }
                this.f12027q = z12;
                synchronized (vkVarB) {
                    j10 = vkVarB.f17668c;
                }
                this.f12028r = j10;
                this.f12025o = true;
                if (!p()) {
                    this.f12020j = vkVarB.zzb();
                    if (this.f12019i) {
                        m(dy1Var);
                    }
                    return -1L;
                }
            }
        } else if (this.f12023m != null) {
            this.f12023m.f18864g = dy1VarB.f10566c;
            yk ykVar2 = this.f12023m;
            String str2 = this.f12017g;
            ykVar2.f18865h = str2 != null ? str2 : "";
            this.f12023m.f18866i = this.f12018h;
            long jLongValue = (this.f12023m.f18863f ? (Long) yVar.f31096c.b(ao.f8801c5) : (Long) yVar.f31096c.b(ao.f8784b5)).longValue();
            ?? sb = m7.n.C;
            sb.f30472k.getClass();
            long jElapsedRealtime2 = SystemClock.elapsedRealtime();
            al alVarA = new el(this.f12014d).a(this.f12023m);
            try {
                try {
                    glVar = (gl) alVarA.f10610a.get(jLongValue, TimeUnit.MILLISECONDS);
                } catch (Throwable th) {
                    th = th;
                    r42 = sb;
                }
            } catch (InterruptedException unused) {
                z14 = false;
            } catch (ExecutionException | TimeoutException unused2) {
                z13 = false;
            } catch (Throwable th2) {
                th = th2;
                r42 = 0;
            }
            try {
                this.f12024n = glVar.f11781b;
                this.f12026p = glVar.f11782c;
                this.f12027q = glVar.f11784e;
                this.f12028r = glVar.f11783d;
                if (!p()) {
                    this.f12020j = glVar.f11780a;
                    if (z15) {
                        m(dy1Var);
                    }
                    sb.f30472k.getClass();
                    long jElapsedRealtime3 = SystemClock.elapsedRealtime() - jElapsedRealtime2;
                    this.f12016f.f(jElapsedRealtime3, true);
                    this.f12025o = true;
                    StringBuilder sb2 = new StringBuilder(String.valueOf(jElapsedRealtime3).length() + 24);
                    sb2.append("Cache connection took ");
                    sb2.append(jElapsedRealtime3);
                    sb2.append("ms");
                    p7.l0.g(sb2.toString());
                    return -1L;
                }
                sb.f30472k.getClass();
                long jElapsedRealtime4 = SystemClock.elapsedRealtime() - jElapsedRealtime2;
                this.f12016f.f(jElapsedRealtime4, true);
                this.f12025o = true;
                StringBuilder sb3 = new StringBuilder(String.valueOf(jElapsedRealtime4).length() + 24);
                sb3.append("Cache connection took ");
                sb3.append(jElapsedRealtime4);
                sb3.append("ms");
                string = sb3.toString();
            } catch (InterruptedException unused3) {
                z14 = true;
                alVarA.cancel(true);
                Thread.currentThread().interrupt();
                m7.n.C.f30472k.getClass();
                jElapsedRealtime = SystemClock.elapsedRealtime() - jElapsedRealtime2;
                this.f12016f.f(jElapsedRealtime, z14);
                this.f12025o = z14;
                sb = new StringBuilder(String.valueOf(jElapsedRealtime).length() + 24);
                sb.append("Cache connection took ");
                sb.append(jElapsedRealtime);
                sb.append("ms");
                string = sb.toString();
            } catch (ExecutionException | TimeoutException unused4) {
                z13 = true;
                alVarA.cancel(true);
                m7.n.C.f30472k.getClass();
                jElapsedRealtime = SystemClock.elapsedRealtime() - jElapsedRealtime2;
                this.f12016f.f(jElapsedRealtime, z13);
                this.f12025o = z13;
                sb = new StringBuilder(String.valueOf(jElapsedRealtime).length() + 24);
                sb.append("Cache connection took ");
                sb.append(jElapsedRealtime);
                sb.append("ms");
                string = sb.toString();
            } catch (Throwable th3) {
                th = th3;
                m7.n.C.f30472k.getClass();
                long jElapsedRealtime5 = SystemClock.elapsedRealtime() - jElapsedRealtime2;
                this.f12016f.f(jElapsedRealtime5, r42);
                this.f12025o = r42;
                StringBuilder sb4 = new StringBuilder(String.valueOf(jElapsedRealtime5).length() + 24);
                sb4.append("Cache connection took ");
                sb4.append(jElapsedRealtime5);
                sb4.append(string);
                p7.l0.g(sb4.toString());
                throw th;
            }
            p7.l0.g(string);
        }
        this.f12025o = false;
        if (this.f12023m != null) {
            ox1 ox1Var = new ox1(dy1VarB);
            ox1Var.a(Uri.parse(this.f12023m.zza));
            dy1VarB = ox1Var.b();
        }
        return this.f12015e.b(dy1VarB);
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws IOException {
        if (!this.f12021k) {
            throw new IOException("Attempt to read closed GcacheDataSource.");
        }
        InputStream inputStream = this.f12020j;
        int iD = inputStream != null ? inputStream.read(bArr, i10, i11) : this.f12015e.d(bArr, i10, i11);
        if (!this.f12019i || this.f12020j != null) {
            n(iD);
        }
        return iD;
    }

    public final boolean p() {
        if (!this.f12019i) {
            return false;
        }
        tn tnVar = ao.f8817d5;
        n7.y yVar = n7.y.f31093e;
        if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue() || this.f12026p) {
            return ((Boolean) yVar.f31096c.b(ao.f8834e5)).booleanValue() && !this.f12027q;
        }
        return true;
    }

    @Override // com.google.android.gms.internal.ads.qr1, com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    public final Uri zzc() {
        return this.f12022l;
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() throws IOException {
        if (!this.f12021k) {
            throw new IOException("Attempt to close an already closed GcacheDataSource.");
        }
        this.f12021k = false;
        this.f12022l = null;
        boolean z10 = (this.f12019i && this.f12020j == null) ? false : true;
        InputStream inputStream = this.f12020j;
        if (inputStream != null) {
            hb.a.d(inputStream);
            this.f12020j = null;
        } else {
            this.f12015e.zzd();
        }
        if (z10) {
            o();
        }
    }
}
