package g.i.a.c.s;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import com.cvte.vman.instance.InvokeCmd;
import g.h.a.i;
import io.netty.handler.codec.compression.Bzip2Constants;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import no.nordicsemi.android.dfu.DfuServiceInitiator;

/* JADX INFO: loaded from: classes.dex */
public class e extends b {
    public e(Context context, g.i.a.c.q.f fVar, g.i.a.c.m.a.b bVar) {
        super(context, fVar, bVar);
    }

    @Override // g.i.a.c.m.a.a
    public boolean f(boolean z2) {
        if (!super.f(z2)) {
            return false;
        }
        if (this.f5605w != 515) {
            if (this.f5594f) {
                g.a.a.a.a.m0(g.a.a.a.a.F("start to re-connect the RCU which going to active image, current state is: "), this.f5605w);
            }
            int iY0 = y0(this.O, r().M);
            if (iY0 != 0) {
                StringBuilder sbG = g.a.a.a.a.G("Something error in OTA process, errorCode: ", iY0, "mProcessState");
                sbG.append(this.E);
                i.D0(sbG.toString());
                h0(iY0, true);
                return false;
            }
        }
        if (z2) {
            try {
                q0();
                z(Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE, true);
            } catch (g.i.a.c.a e2) {
                e2.printStackTrace();
                h0(e2.f5446m, false);
            }
        } else {
            u0();
            h0(274, false);
        }
        return true;
    }

    @Override // g.i.a.c.m.a.a
    public void o() {
        int iV;
        try {
            setName("ProcessorX0013SEx");
            i.w0("ProcessorX0013SEx running.");
            iV = v();
        } catch (Exception e2) {
            e2.printStackTrace();
            i.D0(e2.toString());
            h0(0, false);
        }
        if (iV != 0) {
            h0(iV, false);
            return;
        }
        w0();
        k(this.G);
        if (this.f5594f) {
            i.w0("GattDfuTaskX0000 stopped");
        }
        if (this.E == 525) {
            z(259, true);
        }
    }

    public final boolean w0() {
        g.i.a.c.l.e.a aVar;
        int i2;
        z(InvokeCmd.CMD_XBUG_GET_VMAN_SERVICE_LOG_LEVEL, true);
        this.O = this.P;
        this.f5602t = true;
        boolean z2 = false;
        while (!this.f5600r) {
            try {
            } catch (g.i.a.c.a e2) {
                g.a.a.a.a.R(this.E, new StringBuilder(), ", ", e2);
                int i3 = e2.f5446m;
                if (i3 == 4128) {
                    h0(i3, true);
                } else {
                    if (i3 != 4097 && i3 != 265) {
                        u0();
                    }
                    h0(i3, false);
                }
            }
            if (!z0() || !x0()) {
                return false;
            }
            this.J += s().f5711m;
            if (s().p()) {
                i.w0("no pendding image file to upload.");
                s().f5721w = this.J;
                if (this.T) {
                    q0();
                    i2 = Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
                } else {
                    i2 = 523;
                }
                z(i2, true);
                z2 = true;
            } else {
                i.U3("has pendding image file to upload");
                if (t().M == 1) {
                    this.O = this.P;
                    this.f5602t = true;
                    this.J = 0;
                    q0();
                } else if (t().M == 3 && (aVar = this.H) != null && k0(aVar, this.J, t().C * 4096)) {
                    i.w0("make device to enter the ota advertiser mode, and let the app continue update image");
                    this.f5602t = true;
                    this.J = 0;
                    g0((byte) 1);
                }
                c(DfuServiceInitiator.DEFAULT_SCAN_TIMEOUT);
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e3) {
                e3.printStackTrace();
            }
            if (z2) {
                return z2;
            }
        }
        h0(InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO, true);
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:50:0x0141  */
    /* JADX WARN: Removed duplicated region for block: B:51:0x014c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean x0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        /*
            Method dump skipped, instruction units count: 603
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i.a.c.s.e.x0():boolean");
    }

    public final int y0(String str, int i2) {
        int i3 = 0;
        while (!this.f5600r) {
            int iM0 = m0(str);
            if (iM0 == 0) {
                return 0;
            }
            if ((iM0 & (-2049)) != 133) {
                U(this.q0);
            } else {
                i.W3("connect fail with GATT_ERROR, do not need disconnect");
            }
            L(this.q0);
            try {
                Thread.sleep(1600L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            i3++;
            g.a.a.a.a.a0("tryConnectTime=", i3);
            if (i3 > i2) {
                return iM0;
            }
        }
        return InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO;
    }

    public final boolean z0() throws g.i.a.c.g.b, g.i.a.c.g.c {
        boolean z2;
        String str = this.O;
        if (this.f5600r) {
            throw new g.i.a.c.g.c("user aborted", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_DISPLAY_EDID_INFO);
        }
        z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_SET, true);
        int iY0 = y0(str, r().M);
        if (iY0 != 0) {
            if (iY0 == 4128) {
                throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iY0);
            }
            ArrayList arrayList = new ArrayList();
            if (str != null && !BluetoothAdapter.checkBluetoothAddress(str)) {
                throw new IllegalArgumentException(g.a.a.a.a.t("invalid device address ", str));
            }
            arrayList.add(new g.i.a.b.e.h.g.a(null, str, null, null, null, null, null, null, null, -1, null, null, 0, null));
            g.i.a.b.e.h.e eVarA0 = a0();
            eVarA0.A = arrayList;
            eVarA0.f5392q = str;
            F(eVarA0);
            int iY02 = y0(str, r().M);
            if (iY02 != 0) {
                if (iY02 == 4128) {
                    throw new g.i.a.c.g.c("aborted, connectRemoteDevice failed", iY02);
                }
                throw new g.i.a.c.g.c("connectRemoteDevice failed", iY02);
            }
        }
        f0();
        if (this.f5601s) {
            z2 = true;
            J();
        } else {
            this.f5601s = false;
            z2 = true;
            z(InvokeCmd.CMD_XBUG_VMAN_FUNTION_CHECK_TIMEOUT_BASE_GET, true);
            try {
                Thread.sleep(1500L);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
            this.W = new g.i.a.c.q.i(this.f5596n, 2);
            if (this.A0 != null) {
                if (this.f5594f) {
                    i.U3("start to read remote dev info");
                }
                byte[] bArrS = S(this.A0);
                if (bArrS == null) {
                    throw g.a.a.a.a.x0("Get dev info failed", "get remote dev info failed", 270);
                }
                t().s(bArrS, 0);
                b(t().B);
            }
            t0();
            s0();
            if (this.f5594f) {
                i.w0(t().toString());
            }
            K();
            List<g.i.a.c.l.e.a> list = this.F;
            if (list != null && list.size() > 0) {
                Iterator<g.i.a.c.l.e.a> it = this.F.iterator();
                while (it.hasNext()) {
                    int i2 = it.next().f5573x;
                }
            }
            this.f5601s = true;
            i.w0("Ota Environment prepared.");
        }
        if (this.G != null) {
            return z2;
        }
        h0(4097, false);
        return false;
    }
}
