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

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.os.Trace;
import androidx.annotation.Nullable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

/* JADX INFO: loaded from: classes2.dex */
public final class x extends Handler implements Runnable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final sp2 f18274a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long f18275b;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ z f18279f;

    @Nullable
    private v zzd;

    @Nullable
    private IOException zze;

    @Nullable
    private Thread zzg;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public x(z zVar, Looper looper, sp2 sp2Var, v vVar, long j10) {
        super(looper);
        Objects.requireNonNull(zVar);
        this.f18279f = zVar;
        this.f18274a = sp2Var;
        this.zzd = vVar;
        this.f18275b = j10;
    }

    public final void a(int i10) throws IOException {
        IOException iOException = this.zze;
        if (iOException != null && this.f18276c > i10) {
            throw iOException;
        }
    }

    public final void b(long j10) {
        z zVar = this.f18279f;
        ih1.b1(zVar.g() == null);
        zVar.h(this);
        if (j10 > 0) {
            sendEmptyMessageDelayed(1, j10);
        } else {
            d();
        }
    }

    public final void c(boolean z10) {
        this.f18278e = z10;
        this.zze = null;
        if (hasMessages(1)) {
            this.f18277d = true;
            removeMessages(1);
            if (!z10) {
                sendEmptyMessage(2);
            }
        } else {
            synchronized (this) {
                try {
                    this.f18277d = true;
                    this.f18274a.f16542g = true;
                    Thread thread = this.zzg;
                    if (thread != null) {
                        thread.interrupt();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        if (z10) {
            this.f18279f.h(null);
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            v vVar = this.zzd;
            vVar.getClass();
            ((yp2) vVar).l(this.f18274a, jElapsedRealtime, jElapsedRealtime - this.f18275b, true);
            this.zzd = null;
        }
    }

    public final void d() {
        to2 to2Var;
        SystemClock.elapsedRealtime();
        v vVar = this.zzd;
        vVar.getClass();
        int i10 = this.f18276c;
        yp2 yp2Var = (yp2) vVar;
        sp2 sp2Var = this.f18274a;
        f92 f92Var = sp2Var.f16537b;
        if (i10 == 0) {
            Uri uri = sp2Var.f16545j.f10564a;
            Collections.emptyMap();
            to2Var = new to2();
        } else {
            Uri uri2 = f92Var.f11125c;
            to2Var = new to2();
        }
        yo2 yo2Var = new yo2(1, -1, null, 0, null, bb1.o(sp2Var.f16544i), bb1.o(yp2Var.f18934b0));
        hp2 hp2Var = yp2Var.f18937d;
        hp2Var.getClass();
        hp2Var.a(new androidx.browser.trusted.k(hp2Var, to2Var, yo2Var, i10));
        this.zze = null;
        z zVar = this.f18279f;
        x xVarG = zVar.g();
        xVarG.getClass();
        zVar.f19025a.execute(xVarG);
    }

    @Override // android.os.Handler
    public final void handleMessage(Message message) {
        long jMin;
        h2.f fVar;
        k3 k3Var;
        if (this.f18278e) {
            return;
        }
        int i10 = message.what;
        if (i10 == 1) {
            d();
            return;
        }
        if (i10 == 4) {
            throw ((Error) message.obj);
        }
        z zVar = this.f18279f;
        zVar.h(null);
        long j10 = this.f18275b;
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long j11 = jElapsedRealtime - j10;
        v vVar = this.zzd;
        vVar.getClass();
        if (this.f18277d) {
            ((yp2) vVar).l(this.f18274a, jElapsedRealtime, j11, false);
            return;
        }
        int i11 = message.what;
        if (i11 == 2) {
            try {
                ((yp2) vVar).m(this.f18274a, jElapsedRealtime, j11);
                return;
            } catch (RuntimeException e10) {
                vt0.zzf("LoadTask", "Unexpected exception handling load completed", e10);
                this.f18279f.i(new zzabe(e10));
                return;
            }
        }
        if (i11 != 3) {
            return;
        }
        IOException iOException = (IOException) message.obj;
        this.zze = iOException;
        int i12 = this.f18276c;
        this.f18276c = i12 + 1;
        sp2 sp2Var = this.f18274a;
        yp2 yp2Var = (yp2) vVar;
        Uri uri = sp2Var.f16537b.f11125c;
        to2 to2Var = new to2();
        String str = bb1.f9474a;
        if ((iOException instanceof zzat) || (iOException instanceof FileNotFoundException) || (iOException instanceof zzhp) || (iOException instanceof zzabe)) {
            jMin = -9223372036854775807L;
            break;
        }
        for (Throwable cause = iOException; cause != null; cause = cause.getCause()) {
            if ((cause instanceof zzhc) && ((zzhc) cause).zza == 2008) {
                jMin = -9223372036854775807L;
                break;
            }
        }
        jMin = Math.min(i12 * 1000, 5000);
        if (jMin == -9223372036854775807L) {
            fVar = z.f19024c;
        } else {
            int iX = yp2Var.x();
            int i13 = iX > yp2Var.f18956m0 ? 1 : 0;
            if (yp2Var.f18948i0 || !((k3Var = yp2Var.f18932a0) == null || k3Var.zza() == -9223372036854775807L)) {
                yp2Var.f18956m0 = iX;
            } else {
                boolean z10 = yp2Var.W;
                if (!z10 || yp2Var.t()) {
                    yp2Var.f18942f0 = z10;
                    yp2Var.f18950j0 = 0L;
                    yp2Var.f18956m0 = 0;
                    for (jq2 jq2Var : yp2Var.T) {
                        jq2Var.k(false);
                    }
                    sp2Var.f16541f.f3366a = 0L;
                    sp2Var.f16544i = 0L;
                    sp2Var.f16543h = true;
                    sp2Var.f16546k = false;
                } else {
                    yp2Var.f18954l0 = true;
                    fVar = z.f19023b;
                }
            }
            fVar = new h2.f(jMin, i13);
        }
        int i14 = fVar.f26404a;
        boolean z11 = !(i14 == 0 || i14 == 1);
        yo2 yo2Var = new yo2(1, -1, null, 0, null, bb1.o(sp2Var.f16544i), bb1.o(yp2Var.f18934b0));
        hp2 hp2Var = yp2Var.f18937d;
        hp2Var.getClass();
        hp2Var.a(new n5.r(hp2Var, to2Var, yo2Var, iOException, z11, 5));
        int i15 = fVar.f26404a;
        if (i15 == 3) {
            zVar.i(this.zze);
            return;
        }
        if (i15 != 2) {
            if (i15 == 1) {
                this.f18276c = 1;
            }
            long jMin2 = fVar.f26405b;
            if (jMin2 == -9223372036854775807L) {
                jMin2 = Math.min((this.f18276c - 1) * 1000, 5000);
            }
            b(jMin2);
        }
    }

    @Override // java.lang.Runnable
    public final void run() {
        boolean z10;
        try {
            synchronized (this) {
                z10 = !this.f18277d;
                this.zzg = Thread.currentThread();
            }
            if (z10) {
                sp2 sp2Var = this.f18274a;
                String simpleName = sp2Var.getClass().getSimpleName();
                StringBuilder sb = new StringBuilder(simpleName.length() + 5);
                sb.append("load:");
                sb.append(simpleName);
                Trace.beginSection(sb.toString());
                try {
                    sp2Var.b();
                    Trace.endSection();
                } catch (Throwable th) {
                    Trace.endSection();
                    throw th;
                }
            }
            synchronized (this) {
                this.zzg = null;
                Thread.interrupted();
            }
            if (this.f18278e) {
                return;
            }
            sendEmptyMessage(2);
        } catch (IOException e10) {
            if (this.f18278e) {
                return;
            }
            obtainMessage(3, e10).sendToTarget();
        } catch (OutOfMemoryError e11) {
            if (this.f18278e) {
                return;
            }
            vt0.zzf("LoadTask", "OutOfMemory error loading stream", e11);
            obtainMessage(3, new zzabe(e11)).sendToTarget();
        } catch (Error e12) {
            if (!this.f18278e) {
                vt0.zzf("LoadTask", "Unexpected error loading stream", e12);
                obtainMessage(4, e12).sendToTarget();
            }
            throw e12;
        } catch (Exception e13) {
            if (this.f18278e) {
                return;
            }
            vt0.zzf("LoadTask", "Unexpected exception loading stream", e13);
            obtainMessage(3, new zzabe(e13)).sendToTarget();
        }
    }
}
