package P0;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.os.Trace;
import java.io.IOException;
import x0.AbstractC1709a;

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final j f5292r;

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public h f5294t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public IOException f5295u;

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

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public i(m mVar, Looper looper, j jVar, h hVar, int i, long j8) {
        super(looper);
        this.f5300z = mVar;
        this.f5292r = jVar;
        this.f5294t = hVar;
        this.f5291q = i;
        this.f5293s = j8;
    }

    public final void a(boolean z8) {
        this.f5299y = z8;
        this.f5295u = null;
        if (hasMessages(1)) {
            this.f5298x = true;
            removeMessages(1);
            if (!z8) {
                sendEmptyMessage(2);
            }
        } else {
            synchronized (this) {
                try {
                    this.f5298x = true;
                    this.f5292r.b();
                    Thread thread = this.f5297w;
                    if (thread != null) {
                        thread.interrupt();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        if (z8) {
            this.f5300z.f5305b = null;
            long jElapsedRealtime = SystemClock.elapsedRealtime();
            h hVar = this.f5294t;
            hVar.getClass();
            hVar.s(this.f5292r, jElapsedRealtime, jElapsedRealtime - this.f5293s, true);
            this.f5294t = null;
        }
    }

    public final void b() {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long j8 = jElapsedRealtime - this.f5293s;
        h hVar = this.f5294t;
        hVar.getClass();
        hVar.e(this.f5292r, jElapsedRealtime, j8, this.f5296v);
        this.f5295u = null;
        m mVar = this.f5300z;
        Q0.a aVar = mVar.f5304a;
        i iVar = mVar.f5305b;
        iVar.getClass();
        aVar.execute(iVar);
    }

    @Override // android.os.Handler
    public final void handleMessage(Message message) {
        if (this.f5299y) {
            return;
        }
        int i = message.what;
        if (i == 1) {
            b();
            return;
        }
        if (i == 4) {
            throw ((Error) message.obj);
        }
        this.f5300z.f5305b = null;
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long j8 = jElapsedRealtime - this.f5293s;
        h hVar = this.f5294t;
        hVar.getClass();
        if (this.f5298x) {
            hVar.s(this.f5292r, jElapsedRealtime, j8, false);
            return;
        }
        int i5 = message.what;
        if (i5 == 2) {
            try {
                hVar.g(this.f5292r, jElapsedRealtime, j8);
                return;
            } catch (RuntimeException e8) {
                AbstractC1709a.p("LoadTask", "Unexpected exception handling load completed", e8);
                this.f5300z.f5306c = new l(e8);
                return;
            }
        }
        if (i5 != 3) {
            return;
        }
        IOException iOException = (IOException) message.obj;
        this.f5295u = iOException;
        int i8 = this.f5296v + 1;
        this.f5296v = i8;
        B1.f fVarN = hVar.n(this.f5292r, jElapsedRealtime, j8, iOException, i8);
        int i9 = fVarN.f961a;
        if (i9 == 3) {
            this.f5300z.f5306c = this.f5295u;
            return;
        }
        if (i9 != 2) {
            if (i9 == 1) {
                this.f5296v = 1;
            }
            long jMin = fVarN.f962b;
            if (jMin == -9223372036854775807L) {
                jMin = Math.min((this.f5296v - 1) * 1000, 5000);
            }
            m mVar = this.f5300z;
            AbstractC1709a.j(mVar.f5305b == null);
            mVar.f5305b = this;
            if (jMin > 0) {
                sendEmptyMessageDelayed(1, jMin);
            } else {
                b();
            }
        }
    }

    @Override // java.lang.Runnable
    public final void run() {
        boolean z8;
        try {
            synchronized (this) {
                z8 = this.f5298x;
                this.f5297w = Thread.currentThread();
            }
            if (!z8) {
                Trace.beginSection("load:".concat(this.f5292r.getClass().getSimpleName()));
                try {
                    this.f5292r.a();
                    Trace.endSection();
                } catch (Throwable th) {
                    Trace.endSection();
                    throw th;
                }
            }
            synchronized (this) {
                this.f5297w = null;
                Thread.interrupted();
            }
            if (this.f5299y) {
                return;
            }
            sendEmptyMessage(2);
        } catch (IOException e8) {
            if (this.f5299y) {
                return;
            }
            obtainMessage(3, e8).sendToTarget();
        } catch (Exception e9) {
            if (this.f5299y) {
                return;
            }
            AbstractC1709a.p("LoadTask", "Unexpected exception loading stream", e9);
            obtainMessage(3, new l(e9)).sendToTarget();
        } catch (OutOfMemoryError e10) {
            if (this.f5299y) {
                return;
            }
            AbstractC1709a.p("LoadTask", "OutOfMemory error loading stream", e10);
            obtainMessage(3, new l(e10)).sendToTarget();
        } catch (Error e11) {
            if (!this.f5299y) {
                AbstractC1709a.p("LoadTask", "Unexpected error loading stream", e11);
                obtainMessage(4, e11).sendToTarget();
            }
            throw e11;
        }
    }
}
