package g.i.a.c.z;

import android.content.Context;
import android.util.SparseIntArray;
import com.cvte.vman.instance.InvokeCmd;
import g.i.a.c.l.c;
import io.netty.handler.codec.compression.Bzip2Constants;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import no.nordicsemi.android.dfu.DfuBaseService;

/* JADX INFO: loaded from: classes.dex */
public class l extends k {
    public g.i.a.c.u.a v0;
    public int w0;
    public boolean x0;
    public SparseIntArray y0;

    public l(Context context, g.i.a.c.q.f fVar, g.i.a.c.u.a aVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
        this.v0 = aVar;
    }

    @Override // g.i.a.c.x.a, g.i.a.c.o.b
    public void J() {
        int i2 = s().f5713o;
        int i3 = s().f5716r;
        if (i3 < 0 || i3 >= i2) {
            g.h.a.i.U3("invalid FileIndex: " + i3 + ", reset to 0");
            i3 = 0;
        }
        s().f5714p = i3;
        g.i.a.c.l.e.a aVar = this.F.get(i3);
        this.G = aVar;
        if (aVar != null) {
            if (this.f5594f) {
                g.h.a.i.U3(String.format("mCurBinInputStream's binId=0x%04X", Integer.valueOf(aVar.f5575z)));
            }
            g.i.a.c.q.g gVarS = s();
            g.i.a.c.l.e.a aVar2 = this.G;
            gVarS.n(aVar2.f5575z, aVar2.f5570u, aVar2.f5572w, aVar2.h() + 12, r().f5707x);
        } else {
            g.h.a.i.V3(false, "mCurBinInputStream == null");
        }
        int i4 = i3 + 1;
        if (i4 < i2) {
            this.H = this.F.get(i4);
            this.I = i4;
        } else {
            this.H = null;
            this.I = -1;
        }
    }

    @Override // g.i.a.c.x.a, g.i.a.c.o.b
    public void K() throws g.i.a.c.g.b {
        k(this.G);
        c.b bVar = new c.b();
        bVar.f5536e = r().f5698o;
        bVar.f5533b = r().f5700q;
        bVar.f5534c = this.Q;
        bVar.f5535d = r().c();
        bVar.a = this.f5595m;
        bVar.f5547p = this.S;
        bVar.f5541j = this.R;
        bVar.f5542k = t();
        bVar.f5545n = r().n();
        bVar.f5546o = r().o();
        boolean zP = r().p();
        int i2 = r().f5705v;
        bVar.f5543l = zP;
        bVar.f5544m = i2;
        bVar.f5548q = true;
        g.i.a.c.q.a aVarN = g.i.a.c.l.b.n(bVar.a());
        if (aVarN == null) {
            this.y0 = null;
            this.F = null;
        } else if (aVarN.f5650h == 4096) {
            this.F = aVarN.f5658p;
            this.y0 = aVarN.f5659q;
        } else {
            this.F = null;
            this.y0 = null;
        }
        List<g.i.a.c.l.e.a> list = this.F;
        if (list == null || list.size() <= 0) {
            g.h.a.i.w0("pendingImageInputStreams == null || pendingImageInputStreams.size() <= 0");
            throw new g.i.a.c.g.b("load image file error", 4097);
        }
        if (s().f5716r == 0) {
            this.l0 = new int[this.F.size()];
        }
        s().f5713o = this.F.size();
        if (this.f5594f) {
            g.h.a.i.U3(s().toString());
        }
        J();
    }

    @Override // g.i.a.c.m.a.a
    public void g(int i2, boolean z2) {
        if (i2 != 0) {
            try {
                int iMax = Math.max(i2 - 12, 0);
                byte[] bArr = new byte[s().f5710f];
                if (z2) {
                    this.G.g(bArr, iMax);
                } else {
                    this.G.read(bArr, 0, iMax);
                }
            } catch (IOException e2) {
                g.h.a.i.W3(e2.toString());
                return;
            }
        }
        s().r(i2);
    }

    @Override // g.i.a.c.m.a.a
    public void o() {
        int iV;
        try {
            setName("ProcessorXS0011ForceCopy");
            g.h.a.i.w0("ProcessorXS0011ForceCopy running.");
            iV = v();
        } catch (g.i.a.c.a e2) {
            g.h.a.i.W3(e2.toString());
            X(e2.f5446m, false);
        } catch (Exception e3) {
            g.h.a.i.W3(e3.toString());
            X(0, false);
        }
        if (iV != 0) {
            X(iV, false);
            return;
        }
        z(InvokeCmd.CMD_XBUG_GET_VMAN_SERVICE_LOG_LEVEL, true);
        this.O = this.P;
        this.f5602t = true;
        this.J = 0;
        this.x0 = false;
        if (t0()) {
            if (t().f5686v) {
                g.h.a.i.U3("RWS, no need to disconnect manully");
                if (t().f5675k <= 5) {
                    Y(false);
                } else {
                    this.x0 = true;
                    j0(0);
                    if (t().f5687w == 1) {
                        this.w0 = 2;
                    } else {
                        int i2 = t().f5687w;
                        this.w0 = 1;
                    }
                }
                z(522, true);
                g.h.a.i.x0(this.f5594f, "wait master to handover ...");
                try {
                    Thread.sleep(((long) r().f5692b0) * 1000);
                } catch (InterruptedException unused) {
                }
                g.h.a.i.w0("start to upload secondary bud ...");
                this.f5602t = true;
                this.f5601s = false;
                this.J = 0;
                if (t0()) {
                    if (this.T) {
                        Y(true);
                        if (r().d(1)) {
                            g.i.a.b.e.b.a.c(this.i0.getRemoteDevice(this.O));
                            g.i.a.b.e.b.a.d(this.O);
                        }
                        z(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE, true);
                    }
                    z(523, true);
                }
            } else if (this.T) {
                Y(true);
                if (r().d(1)) {
                    g.i.a.b.e.b.a.c(this.i0.getRemoteDevice(this.O));
                    g.i.a.b.e.b.a.d(this.O);
                }
                z(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE, true);
            } else {
                z(523, true);
            }
        }
        k(this.G);
        g.h.a.i.x0(this.f5594f, "DfuThread stopped");
        if (this.E == 525) {
            z(259, true);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:42:0x00cb, code lost:
    
        if (r6 == 1) goto L43;
     */
    /* JADX WARN: Removed duplicated region for block: B:38:0x00c2  */
    /* JADX WARN: Removed duplicated region for block: B:41:0x00c7 A[Catch: a -> 0x0107, TRY_ENTER, TryCatch #0 {a -> 0x0107, blocks: (B:7:0x000e, B:10:0x0015, B:12:0x001f, B:14:0x0028, B:16:0x002c, B:41:0x00c7, B:43:0x00cd, B:54:0x00f0, B:45:0x00d3, B:49:0x00de, B:51:0x00e4, B:36:0x00a9), top: B:78:0x000e }] */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00d1  */
    /* JADX WARN: Removed duplicated region for block: B:57:0x00f7  */
    /* JADX WARN: Removed duplicated region for block: B:90:0x00f6 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public boolean t0() {
        /*
            Method dump skipped, instruction units count: 308
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.z.l.t0():boolean");
    }

    public final int u0() {
        g.i.a.c.l.e.a aVar;
        g.i.a.c.q.g gVarS;
        z(521, true);
        s().s();
        if (this.f5594f) {
            g.h.a.i.U3("processCopyProcedure ...");
            g.h.a.i.U3(s().toString());
        }
        try {
            f0(s().f5718t);
            s().q();
            x();
            this.J += s().f5711m;
            if (s().p()) {
                g.h.a.i.V3(false, "no pendding image file to upload");
                s().f5721w = this.J;
                return 3;
            }
            g.h.a.i.w0("has pendding image file to upload");
            if (t().M == 1) {
                this.O = this.P;
                this.f5602t = true;
                this.J = 0;
                Y(true);
                gVarS = s();
            } else {
                if (t().M != 3 || (aVar = this.H) == null || !Z(aVar, this.J, t().C * 4096)) {
                    return 2;
                }
                g.h.a.i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                this.f5602t = true;
                this.J = 0;
                V((byte) 1, true);
                gVarS = s();
            }
            int i2 = gVarS.f5721w / 30;
            c(30000L);
            return 2;
        } catch (g.i.a.c.a unused) {
            return 1;
        }
    }

    public final void v0() {
        SparseIntArray sparseIntArray = this.y0;
        if (sparseIntArray == null || sparseIntArray.size() <= 0) {
            g.h.a.i.U3("no force copy images exist");
            return;
        }
        for (int i2 = 0; i2 < this.y0.size(); i2++) {
            int iKeyAt = this.y0.keyAt(i2);
            g.h.a.i.U3(String.format(Locale.US, "force copy data image:%d, imageId=0x%02X, value=0x%02X", Integer.valueOf(i2), Integer.valueOf(iKeyAt), Integer.valueOf(this.y0.valueAt(i2))));
            f0(iKeyAt);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:153:0x041e A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:154:0x041f  */
    /* JADX WARN: Removed duplicated region for block: B:57:0x015e  */
    /* JADX WARN: Type inference failed for: r2v25 */
    /* JADX WARN: Type inference failed for: r2v38 */
    /* JADX WARN: Type inference failed for: r2v8, types: [boolean] */
    /* JADX WARN: Type inference failed for: r4v21 */
    /* JADX WARN: Type inference failed for: r4v22, types: [boolean, byte] */
    /* JADX WARN: Type inference failed for: r4v23 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final int w0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 1291
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.z.l.w0():int");
    }

    public final int x0() throws g.i.a.c.g.b {
        g.i.a.c.q.g gVarS;
        if (!(!this.f5600r)) {
            X(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, false);
            return 0;
        }
        z(521, true);
        g.h.a.i.w0(String.format("mOtaWorkMode=0x%04X, ICType=%2X", Integer.valueOf(this.S), Integer.valueOf(t().f5674j)));
        g.h.a.i.U3(s().toString());
        s().s();
        i0(s().f5718t);
        if (!r().f5708y) {
            this.Z = 0;
        }
        g.h.a.i.V3(this.f5594f, String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
        if (this.Z == 0) {
            U((byte) 0);
        }
        if (this.Z >= s().f5710f) {
            g.h.a.i.x0(this.f5594f, "Last send reach the bottom");
        } else {
            y0(s().f5718t);
        }
        s().q();
        x();
        this.J += s().f5711m;
        if (s().p()) {
            g.h.a.i.w0("no pendding image file to upload,");
            try {
                W(s().f5718t, (byte) 1);
                s().f5721w = this.J;
                return 3;
            } catch (g.i.a.c.a unused) {
                s().f5716r = s().f5715q;
                return 1;
            }
        }
        g.h.a.i.w0("has pendding image file to upload");
        if (t().M != 1) {
            if (t().M == 3) {
                g.i.a.c.l.e.a aVar = this.H;
                if (aVar == null) {
                    try {
                        W(s().f5718t, (byte) 1);
                    } catch (g.i.a.c.a unused2) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                } else if (Z(aVar, this.J, t().C * 4096)) {
                    try {
                        W(s().f5718t, (byte) 1);
                        g.h.a.i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                        this.f5602t = true;
                        this.J = 0;
                        V((byte) 1, true);
                        gVarS = s();
                    } catch (g.i.a.c.a unused3) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                } else {
                    try {
                        W(s().f5718t, (byte) 0);
                    } catch (g.i.a.c.a unused4) {
                        s().f5716r = s().f5715q;
                        return 1;
                    }
                }
            } else {
                try {
                    W(s().f5718t, (byte) 0);
                } catch (g.i.a.c.a unused5) {
                    s().f5716r = s().f5715q;
                    return 1;
                }
            }
            return 2;
        }
        try {
            W(s().f5718t, (byte) 1);
            this.O = this.P;
            this.f5602t = true;
            this.J = 0;
            Y(true);
            gVarS = s();
        } catch (g.i.a.c.a unused6) {
            s().f5716r = s().f5715q;
            return 1;
        }
        int i2 = gVarS.f5721w / 30;
        c(30000L);
        return 2;
    }

    public final void y0(int i2) throws g.i.a.c.g.b {
        if (this.Z == 0 && t().f5675k < 3) {
            this.Z = 12;
        }
        g.h.a.i.x0(this.f5594f, String.format(Locale.US, "mImageUpdateOffset=0x%08X(%d)", Integer.valueOf(this.Z), Integer.valueOf(this.Z)));
        int i3 = s().f5711m;
        int i4 = this.Z;
        if (i3 == i4 || i4 == -1) {
            return;
        }
        g.h.a.i.W3("mBytesSent != mImageUpdateOffset, reload image bin file");
        K();
        g(this.Z, false);
    }

    public final boolean z0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        g0(this.O);
        if (this.f5601s) {
            J();
        } else {
            this.f5601s = false;
            if (!this.f5600r) {
                z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
                try {
                    Thread.sleep(1500L);
                } catch (InterruptedException e2) {
                    e2.printStackTrace();
                }
                g.i.a.c.u.a aVar = this.v0;
                if (aVar != null) {
                    try {
                        L((short) 1554, new byte[]{(byte) (aVar.f5751f & DnsRecord.CLASS_ANY)});
                    } catch (Exception unused) {
                        g.h.a.i.U3("CMD_OTA_TEST error, ignore");
                    }
                }
                this.W = new g.i.a.c.q.i(17, 2);
                r0();
                if (t().f5686v) {
                    if (t().f5675k < 6) {
                        g.h.a.i.x0(this.f5594f, String.format("<< CMD_OTA_GET_OTHER_INFO (0x%04X)", (short) 1547));
                        byte[] bArrE0 = e0((short) 1547, null);
                        if (bArrE0 == null || bArrE0.length <= 0) {
                            t().f5688x = 0;
                        } else {
                            t().f5688x = bArrE0[0] & 1;
                        }
                        if (t().f5688x != 0) {
                            throw new g.i.a.c.g.c("rws state not ready", 282);
                        }
                    } else if (this.x0 && t().f5687w != this.w0) {
                        throw new g.i.a.c.g.c(String.format("role swap failed, bud role(%d) din't change, target is %d", Integer.valueOf(t().f5687w), Integer.valueOf(this.w0)), DfuBaseService.NOTIFICATION_ID);
                    }
                }
                o0();
                if (t().N && t().f5675k >= 5) {
                    q0();
                }
                if (t().M == 3) {
                    p0();
                }
                if (this.f5594f) {
                    g.h.a.i.w0(t().toString());
                }
                K();
                this.f5601s = true;
                g.h.a.i.w0("Ota Environment prepared.");
            } else {
                X(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, false);
            }
        }
        if (this.G != null) {
            return true;
        }
        X(4097, false);
        return false;
    }
}
