package u;

import android.media.DeniedByServerException;
import android.media.NotProvisionedException;
import android.media.UnsupportedSchemeException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import b0.D;
import java.util.HashMap;
import java.util.UUID;
import u.InterfaceC0722a;
import u.d;

/* JADX INFO: loaded from: classes.dex */
public class i implements u.b {

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public static final UUID f6528r = new UUID(-1301668207276963122L, -6645017420763422227L);

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final UUID f6529s = new UUID(-7348484286925749626L, -6083546864340672619L);

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public static final UUID f6530t = new UUID(6801168765294362723L, -8541191043631480108L);

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final UUID f6531u = new UUID(81985529216486895L, 81985529216486895L);

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final u.d f6534c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final HashMap f6535d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    final h f6537f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    final UUID f6539h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private HandlerThread f6540i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private Handler f6541j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f6542k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private int f6544m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private u.c f6545n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private Exception f6546o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private InterfaceC0722a.b f6547p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private byte[] f6548q;

    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            i.this.f6533b.i();
        }
    }

    class b implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Exception f6550b;

        b(Exception exc) {
            this.f6550b = exc;
        }

        @Override // java.lang.Runnable
        public void run() {
            i.this.f6533b.f(this.f6550b);
        }
    }

    public interface c {
        void f(Exception exc);

        void i();
    }

    private class d implements d.b {
        @Override // u.d.b
        public void a(u.d dVar, byte[] bArr, int i2, int i3, byte[] bArr2) {
            i.this.f6536e.sendEmptyMessage(i2);
        }

        private d() {
        }
    }

    private class e extends Handler {
        public e(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            if (i.this.f6542k != 0) {
                if (i.this.f6544m == 3 || i.this.f6544m == 4) {
                    int i2 = message.what;
                    if (i2 == 1) {
                        i.this.f6544m = 3;
                        i.this.D();
                    } else if (i2 == 2) {
                        i.this.C();
                    } else {
                        if (i2 != 3) {
                            return;
                        }
                        i.this.f6544m = 3;
                        i.this.x(new u.g());
                    }
                }
            }
        }
    }

    private class f extends Handler {
        public f(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            try {
                int i2 = message.what;
                if (i2 == 0) {
                    i iVar = i.this;
                    e = iVar.f6537f.a(iVar.f6539h, (d.c) message.obj);
                } else {
                    if (i2 != 1) {
                        throw new RuntimeException();
                    }
                    i iVar2 = i.this;
                    e = iVar2.f6537f.b(iVar2.f6539h, (d.a) message.obj);
                }
            } catch (Exception e2) {
                e = e2;
            }
            i.this.f6538g.obtainMessage(message.what, e).sendToTarget();
        }
    }

    private class g extends Handler {
        public g(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            int i2 = message.what;
            if (i2 == 0) {
                i.this.A(message.obj);
            } else {
                if (i2 != 1) {
                    return;
                }
                i.this.y(message.obj);
            }
        }
    }

    private i(UUID uuid, Looper looper, h hVar, HashMap map, Handler handler, c cVar, u.d dVar) {
        this.f6539h = uuid;
        this.f6537f = hVar;
        this.f6535d = map;
        this.f6532a = handler;
        this.f6533b = cVar;
        this.f6534c = dVar;
        dVar.c(new d());
        this.f6536e = new e(looper);
        this.f6538g = new g(looper);
        this.f6544m = 1;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void A(Object obj) {
        this.f6543l = false;
        int i2 = this.f6544m;
        if (i2 == 2 || i2 == 3 || i2 == 4) {
            if (obj instanceof Exception) {
                x((Exception) obj);
                return;
            }
            try {
                this.f6534c.j((byte[]) obj);
                if (this.f6544m == 2) {
                    B(false);
                } else {
                    C();
                }
            } catch (DeniedByServerException e2) {
                x(e2);
            }
        }
    }

    private void B(boolean z2) {
        try {
            byte[] bArrE = this.f6534c.e();
            this.f6548q = bArrE;
            this.f6545n = this.f6534c.h(this.f6539h, bArrE);
            this.f6544m = 3;
            C();
        } catch (NotProvisionedException e2) {
            if (z2) {
                D();
            } else {
                x(e2);
            }
        } catch (Exception e3) {
            x(e3);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void C() {
        try {
            u.d dVar = this.f6534c;
            byte[] bArr = this.f6548q;
            InterfaceC0722a.b bVar = this.f6547p;
            d.a aVarD = dVar.d(bArr, bVar.f6518b, bVar.f6517a, 1, this.f6535d);
            if (this.f6539h == f6529s && r(aVarD)) {
                y(aVarD.a().getBytes());
            } else {
                this.f6541j.obtainMessage(1, aVarD).sendToTarget();
            }
        } catch (NotProvisionedException e2) {
            z(e2);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void D() {
        if (this.f6543l) {
            return;
        }
        this.f6543l = true;
        this.f6541j.obtainMessage(0, this.f6534c.b()).sendToTarget();
    }

    private static u.f o(UUID uuid) throws k {
        try {
            return new u.f(uuid);
        } catch (UnsupportedSchemeException e2) {
            throw new k(1, e2);
        } catch (Exception e3) {
            throw new k(2, e3);
        }
    }

    private boolean q(byte[] bArr) {
        return new String(bArr).startsWith("kid");
    }

    private boolean r(d.a aVar) {
        return aVar.a().startsWith("kid");
    }

    public static i s(UUID uuid, Looper looper, h hVar, HashMap map, Handler handler, c cVar) {
        return t(uuid, looper, hVar, map, handler, cVar, o(uuid));
    }

    public static i t(UUID uuid, Looper looper, h hVar, HashMap map, Handler handler, c cVar, u.d dVar) {
        return new i(uuid, looper, hVar, map, handler, cVar, dVar);
    }

    public static i u(Looper looper, h hVar, HashMap map, Handler handler, c cVar) {
        return s(f6530t, looper, hVar, map, handler, cVar);
    }

    public static i v(Looper looper, h hVar, String str, Handler handler, c cVar) {
        HashMap map;
        if (TextUtils.isEmpty(str)) {
            map = null;
        } else {
            map = new HashMap();
            map.put("PRCustomData", str);
        }
        return s(f6529s, looper, hVar, map, handler, cVar);
    }

    public static i w(Looper looper, h hVar, HashMap map, Handler handler, c cVar) {
        return s(f6528r, looper, hVar, map, handler, cVar);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void x(Exception exc) {
        this.f6546o = exc;
        Handler handler = this.f6532a;
        if (handler != null && this.f6533b != null) {
            handler.post(new b(exc));
        }
        if (this.f6544m != 4) {
            this.f6544m = 0;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void y(Object obj) {
        int i2 = this.f6544m;
        if (i2 == 3 || i2 == 4) {
            if (obj instanceof Exception) {
                z((Exception) obj);
                return;
            }
            try {
                byte[] bArrI = this.f6534c.i(this.f6548q, (byte[]) obj);
                if (this.f6539h == f6529s && bArrI != null && q((byte[]) obj)) {
                    this.f6534c.f(this.f6548q, bArrI);
                }
                this.f6544m = 4;
                Handler handler = this.f6532a;
                if (handler == null || this.f6533b == null) {
                    return;
                }
                handler.post(new a());
            } catch (Exception e2) {
                z(e2);
            }
        }
    }

    private void z(Exception exc) {
        if (exc instanceof NotProvisionedException) {
            D();
        } else {
            x(exc);
        }
    }

    @Override // u.b
    public boolean a(String str) {
        int i2 = this.f6544m;
        if (i2 == 3 || i2 == 4) {
            return this.f6545n.a(str);
        }
        throw new IllegalStateException();
    }

    @Override // u.b
    public final int b() {
        return this.f6544m;
    }

    @Override // u.b
    public void c(InterfaceC0722a interfaceC0722a) {
        byte[] bArrC;
        int i2 = this.f6542k + 1;
        this.f6542k = i2;
        if (i2 != 1) {
            return;
        }
        if (this.f6541j == null) {
            HandlerThread handlerThread = new HandlerThread("DrmRequestHandler");
            this.f6540i = handlerThread;
            handlerThread.start();
            this.f6541j = new f(this.f6540i.getLooper());
        }
        if (this.f6547p == null) {
            InterfaceC0722a.b bVarA = interfaceC0722a.a(this.f6539h);
            this.f6547p = bVarA;
            if (bVarA == null) {
                x(new IllegalStateException("Media does not support uuid: " + this.f6539h));
                return;
            }
            if (D.f2965a < 21 && (bArrC = y.g.c(bVarA.f6518b, f6528r)) != null) {
                this.f6547p = new InterfaceC0722a.b(this.f6547p.f6517a, bArrC);
            }
        }
        this.f6544m = 2;
        B(true);
    }

    @Override // u.b
    public void close() {
        int i2 = this.f6542k - 1;
        this.f6542k = i2;
        if (i2 != 0) {
            return;
        }
        this.f6544m = 1;
        this.f6543l = false;
        this.f6536e.removeCallbacksAndMessages(null);
        this.f6538g.removeCallbacksAndMessages(null);
        this.f6541j.removeCallbacksAndMessages(null);
        this.f6541j = null;
        this.f6540i.quit();
        this.f6540i = null;
        this.f6547p = null;
        this.f6545n = null;
        this.f6546o = null;
        byte[] bArr = this.f6548q;
        if (bArr != null) {
            this.f6534c.g(bArr);
            this.f6548q = null;
        }
    }

    @Override // u.b
    public final u.c d() {
        int i2 = this.f6544m;
        if (i2 == 3 || i2 == 4) {
            return this.f6545n;
        }
        throw new IllegalStateException();
    }

    @Override // u.b
    public final Exception e() {
        if (this.f6544m == 0) {
            return this.f6546o;
        }
        return null;
    }

    public final void finalize() {
    }

    public final String p(String str) {
        return this.f6534c.a(str);
    }
}
