package a.c.a.b;

import android.content.Context;
import android.util.SparseArray;
import java.util.Timer;
import java.util.TimerTask;

public class b {
    private static final String k = "AOAConnectManager";
    private static final String l = "AOAConnectThread";
    private static final String m = "AOAReadThread";
    private static final int n = 500;
    private static final int o = 67108864;
    private static final long p = 2000;
    private static final int q = 4;
    private static final int r = 0;
    private static final int s = 4;
    private static final int t = 4;
    private static final int u = 1;
    private static final int v = 8;
    private static final int w = 1;
    private static final int x = 9;
    private static final b y = new b();

    /* renamed from: a  reason: collision with root package name */
    private final SparseArray<h> f55a = new SparseArray<>();
    private boolean b = false;
    private boolean c = false;
    private Context d = null;
    private c e = null;
    private d f = null;
    private AbstractC0005b g = null;
    private Timer h;
    private TimerTask i;
    private a j;

    public class a extends TimerTask {
        public a() {
        }

        public void run() {
            a.a.a.b.a(b.k, "connect time out, no device attached");
            b.this.g();
        }
    }

    /* renamed from: a.c.a.b.b$b  reason: collision with other inner class name */
    public interface AbstractC0005b {
        void a();

        void a(String str);

        void b();

        void c();
    }

    public static class c extends Thread {

        /* renamed from: a  reason: collision with root package name */
        private boolean f57a = false;

        public c() {
            a.a.a.b.a(b.k, "AOAConnectThread Created");
            setName(b.l);
        }

        public void a() {
            this.f57a = false;
        }

        public void run() {
            this.f57a = true;
            a.a.a.b.a(b.k, "Begin to connect UCar by AOA");
            c.e().h();
            while (this.f57a) {
                try {
                    if (c.e().l()) {
                        a.a.a.b.b(b.k, "UCar connect exit");
                        return;
                    }
                    Thread.sleep(500);
                } catch (Exception e) {
                    a.a.a.b.b(b.k, "Exception when connect UCar by AOA", e);
                    return;
                }
            }
            a.a.a.b.b(b.k, "UCar Connect Cancelled");
        }
    }

    public class d extends Thread {

        /* renamed from: a  reason: collision with root package name */
        private boolean f58a = false;
        private byte[] b = new byte[8];
        private final byte[] c = new byte[8];

        public d() {
            a.a.a.b.a(b.k, "AOAReadThread Created");
            setName(b.m);
        }

        public void a() {
            this.f58a = false;
        }

        public void run() {
            String str;
            this.f58a = true;
            a.a.a.b.a(b.k, "Begin to read data by AOA");
            while (this.f58a) {
                try {
                    int a2 = c.e().a(this.c, 8);
                    if (a2 < 0) {
                        str = "head bulkTransferIn fail";
                    } else if (a2 != 0) {
                        int a3 = a.c.c.a.a(this.c, 0);
                        int a4 = a.c.c.a.a(this.c, 4);
                        if (a3 >= 1 && a3 <= 240 && a4 >= 0) {
                            if (a4 <= b.o) {
                                int max = Math.max(a4, 8);
                                if (this.b.length < max) {
                                    this.b = new byte[max];
                                }
                                if (c.e().a(this.b, max) < 0) {
                                    str = "body bulkTransferIn fail";
                                } else if (a3 == 1) {
                                    b.this.a((b) this.b, (byte[]) a4);
                                } else {
                                    h b2 = b.this.b(a3);
                                    if (b2 != null) {
                                        b2.c(this.b, 0, a4);
                                    } else {
                                        a.a.a.b.e(b.k, "AOAReadThread channelId " + a3 + " not found read thread");
                                    }
                                }
                            }
                        }
                        str = "channelId or lenMsg is error";
                    }
                    a.a.a.b.b(b.k, str);
                    return;
                } catch (Exception e) {
                    a.a.a.b.b(b.k, "Exception when read data by AOA", e);
                    return;
                }
            }
        }
    }

    private b() {
    }

    private String a(int i2) {
        return a.c.b.z.b.a(i2).name();
    }

    private void a(int i2, String str, int i3) {
        try {
            g gVar = new g(i2, str, i3);
            gVar.start();
            gVar.n();
            this.f55a.append(i2, gVar);
        } catch (Exception e2) {
            a.a.a.b.b(k, "Start ServerSocketRead Thread Fail", e2);
        }
    }

    private void a(int i2, String str, int i3, int i4) {
        try {
            f fVar = new f(i2, str, i3, i4);
            fVar.start();
            fVar.n();
            this.f55a.append(i2, fVar);
        } catch (Exception e2) {
            a.a.a.b.b(k, "Start ClientSocketRead Thread Fail", e2);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(byte[] bArr, int i2) {
        if (i2 == 10) {
            int a2 = a.c.c.a.a(bArr, 0);
            int a3 = a.c.c.a.a(bArr, 4);
            int d2 = a.c.c.a.d(bArr, 8);
            int d3 = a.c.c.a.d(bArr, 9);
            a.a.a.b.a(k, "received create socket msg, port " + a2 + " channelId" + a3 + " socketType" + d2 + " channelMsgType" + d3);
            if (d2 == 1) {
                if (a3 != -1) {
                    a.a.a.b.e(k, "Expect received sever socket channel id is -1, but received " + a3);
                }
                if (this.f55a.get(a2) == null) {
                    a(a2, a(a2), d3);
                }
                b();
            } else if (d2 == 2) {
                a(a2, a(a2), a3, d3);
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private h b(int i2) {
        for (int i3 = 0; i3 < this.f55a.size(); i3++) {
            h valueAt = this.f55a.valueAt(i3);
            if (valueAt != null && valueAt.c() == i2) {
                return valueAt;
            }
        }
        return null;
    }

    private void b() {
        if (!this.b) {
            for (int i2 = 0; i2 < this.f55a.size(); i2++) {
                if (this.f55a.valueAt(i2) == null) {
                    a.a.a.b.a(k, "null read thread port at " + this.f55a.keyAt(i2));
                    return;
                }
            }
            a.a.a.b.a(k, "all needed default socket ready ");
            n();
            d().h();
            this.b = true;
        }
    }

    public static b d() {
        return y;
    }

    private void e() {
        this.i = new a();
        this.h = new Timer();
    }

    private void j() {
        r();
        this.f55a.put(a.c.b.z.b.RTSP.a(), null);
        this.f55a.put(a.c.b.z.b.UIBC.a(), null);
    }

    public void a() {
        if (this.h != null) {
            a.a.a.b.a(k, "cancel connect timer");
            this.h.cancel();
            this.i = null;
            this.h = null;
        }
    }

    public void a(int i2, int i3, int i4, int i5) {
        a.a.a.b.a(k, "send create socket msg, port " + i2 + " channelId " + i3 + " socketType " + i4 + " channelMsgType " + i5);
        byte[] bArr = new byte[8];
        byte[] bArr2 = new byte[10];
        System.arraycopy(a.c.c.a.a(1), 0, bArr, 0, 4);
        System.arraycopy(a.c.c.a.a(10), 0, bArr, 4, 4);
        System.arraycopy(a.c.c.a.a(i2), 0, bArr2, 0, 4);
        System.arraycopy(a.c.c.a.a(i3), 0, bArr2, 4, 4);
        System.arraycopy(a.c.c.a.b(i4), 0, bArr2, 8, 1);
        System.arraycopy(a.c.c.a.b(i5), 0, bArr2, 9, 1);
        if (c.e().a(bArr, 8, bArr2, 10) < 0) {
            a.a.a.b.b(k, "bulkTransferOut create socket msg failed");
        }
    }

    public void a(int i2, boolean z) {
        a(i2, -1, 1, z ? 1 : 2);
    }

    public void a(AbstractC0005b bVar) {
        this.g = bVar;
    }

    public void a(Context context, String str, String str2, String str3, String str4, String str5, String str6) {
        a.a.a.b.a(k, "init");
        if (this.d == null) {
            this.d = context;
            c.e().a(this.d, str, str2, str3, str4, str5, str6);
            this.j = new a(context);
        }
    }

    public void a(String str) {
        AbstractC0005b bVar = this.g;
        if (bVar != null) {
            bVar.a(str);
        }
    }

    public void c() {
        a.a.a.b.a(k, "deInit");
        p();
        q();
        r();
        c.e().c();
    }

    public void f() {
        a.a.a.b.a(k, "notify connect error");
        this.c = false;
    }

    public void g() {
        a.a.a.b.a(k, "notify disconnected, is connect right: " + this.c);
        if (!this.c) {
            c();
            AbstractC0005b bVar = this.g;
            if (bVar != null) {
                bVar.a();
            }
        }
    }

    public void h() {
        this.c = true;
        AbstractC0005b bVar = this.g;
        if (bVar != null) {
            bVar.c();
        }
    }

    public void i() {
        m();
        AbstractC0005b bVar = this.g;
        if (bVar != null) {
            bVar.b();
        }
        p();
    }

    public void k() {
        e();
        if (this.h != null) {
            a.a.a.b.a(k, "schedule cancel connect timer");
            this.h.schedule(this.i, p);
        }
    }

    public void l() {
        a.a.a.b.a(k, "startAOAConnectThread, mAOAConnectThread:" + this.e);
        try {
            c cVar = this.e;
            if (cVar != null) {
                if (cVar.isAlive()) {
                    a.a.a.b.a(k, "mAOAConnectThread is alive, not need recreate. ");
                    return;
                }
            }
            c cVar2 = new c();
            this.e = cVar2;
            cVar2.start();
        } catch (Exception e2) {
            a.a.a.b.b(k, "Start AOAConnectThread Fail", e2);
        }
    }

    public void m() {
        try {
            j();
            d dVar = new d();
            this.f = dVar;
            dVar.start();
        } catch (Exception e2) {
            a.a.a.b.b(k, "Start AOAReadThread Fail", e2);
        }
    }

    public void n() {
        a(a.c.b.z.b.RTP.a(), -1, 1, 2);
    }

    public void o() {
        a aVar = this.j;
        if (aVar != null) {
            aVar.a();
        }
        l();
    }

    public void p() {
        a.a.a.b.a(k, "stopAOAConnectThread");
        try {
            c cVar = this.e;
            if (cVar != null) {
                cVar.a();
                this.e = null;
            }
        } catch (Exception e2) {
            a.a.a.b.b(k, "Stop AOAConnectThread Fail", e2);
        }
    }

    public void q() {
        try {
            d dVar = this.f;
            if (dVar != null) {
                dVar.a();
                this.f = null;
            }
        } catch (Exception e2) {
            a.a.a.b.b(k, "Stop AOAReadThread Fail", e2);
        }
    }

    public void r() {
        try {
            this.b = false;
            for (int i2 = 0; i2 < this.f55a.size(); i2++) {
                h valueAt = this.f55a.valueAt(i2);
                if (valueAt != null) {
                    valueAt.a();
                }
            }
            this.f55a.clear();
        } catch (Exception e2) {
            a.a.a.b.b(k, "Stop SocketRead Thread Fail", e2);
        }
    }

    public void s() {
        a aVar = this.j;
        if (aVar != null) {
            aVar.b();
        }
        p();
    }
}
