package a0;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import b0.AbstractC0399d;
import b0.B;
import b0.D;
import java.io.IOException;
import java.util.concurrent.ExecutorService;

/* JADX INFO: loaded from: classes.dex */
public final class r {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final ExecutorService f2007a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private b f2008b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private boolean f2009c;

    public interface a {
        void l(c cVar);

        void n(c cVar, IOException iOException);

        void v(c cVar);
    }

    private final class b extends Handler implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final c f2010b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private final a f2011g;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        private volatile Thread f2012r;

        public b(Looper looper, c cVar, a aVar) {
            super(looper);
            this.f2010b = cVar;
            this.f2011g = aVar;
        }

        private void a() {
            r.this.f2009c = false;
            r.this.f2008b = null;
        }

        public void b() {
            this.f2010b.u();
            if (this.f2012r != null) {
                this.f2012r.interrupt();
            }
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (message.what == 2) {
                throw ((Error) message.obj);
            }
            a();
            if (this.f2010b.m()) {
                this.f2011g.v(this.f2010b);
                return;
            }
            int i2 = message.what;
            if (i2 == 0) {
                this.f2011g.l(this.f2010b);
            } else {
                if (i2 != 1) {
                    return;
                }
                this.f2011g.n(this.f2010b, (IOException) message.obj);
            }
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                this.f2012r = Thread.currentThread();
                Process.setThreadPriority(-16);
                if (!this.f2010b.m()) {
                    B.a(this.f2010b.getClass().getSimpleName() + ".load()");
                    this.f2010b.q();
                    B.c();
                }
                sendEmptyMessage(0);
            } catch (IOException e2) {
                obtainMessage(1, e2).sendToTarget();
            } catch (Error e3) {
                Log.e("LoadTask", "Unexpected error loading stream", e3);
                obtainMessage(2, e3).sendToTarget();
                throw e3;
            } catch (InterruptedException unused) {
                AbstractC0399d.e(this.f2010b.m());
                sendEmptyMessage(0);
            } catch (Exception e4) {
                Log.e("LoadTask", "Unexpected exception loading stream", e4);
                obtainMessage(1, new d(e4)).sendToTarget();
            }
        }
    }

    public interface c {
        boolean m();

        void q();

        void u();
    }

    public static final class d extends IOException {
        public d(Exception exc) {
            super("Unexpected " + exc.getClass().getSimpleName() + ": " + exc.getMessage(), exc);
        }
    }

    public r(String str) {
        this.f2007a = D.F(str);
    }

    public void c() {
        AbstractC0399d.e(this.f2009c);
        this.f2008b.b();
    }

    public boolean d() {
        return this.f2009c;
    }

    public void e() {
        f(null);
    }

    public void f(Runnable runnable) {
        if (this.f2009c) {
            c();
        }
        if (runnable != null) {
            this.f2007a.submit(runnable);
        }
        this.f2007a.shutdown();
    }

    public void g(c cVar, a aVar) {
        Looper looperMyLooper = Looper.myLooper();
        AbstractC0399d.e(looperMyLooper != null);
        h(looperMyLooper, cVar, aVar);
    }

    public void h(Looper looper, c cVar, a aVar) {
        AbstractC0399d.e(!this.f2009c);
        this.f2009c = true;
        b bVar = new b(looper, cVar, aVar);
        this.f2008b = bVar;
        this.f2007a.submit(bVar);
    }
}
