package u;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import com.cvte.va.sdk.mica.api.InitManager;
import com.cvte.va.sdk.mica.enumclass.MicKeyCode;
import com.cvte.va.sdk.mica.enumclass.SupportDevicesEnum;
import com.cvte.vman.instance.InvokeCmd;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheResponse;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;

/* JADX INFO: loaded from: classes2.dex */
public class e {
    public static e a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public t.c f9518b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public j.a f9519c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public String f9520d = "";

    public e() {
        t.c cVar = this.f9518b;
        if (cVar != null) {
            cVar.f9513g.c();
            this.f9518b = null;
        }
        this.f9518b = new t.c();
        j.a aVar = this.f9519c;
        if (aVar != null) {
            aVar.f5833q.c();
            this.f9519c = null;
        }
        this.f9519c = new j.a();
    }

    public static e h() {
        if (a == null) {
            a = new e();
        }
        return a;
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    public int a(String str) {
        String str2;
        int i2;
        boolean z2;
        a aVarA;
        int i3;
        if (!str.contains("=")) {
            return -1;
        }
        String[] strArrSplit = str.split("=");
        if (strArrSplit.length < 2) {
            g.a.a.a.a.h0("Invalid command format: ", str, "HidProtocolUtils");
            return -1;
        }
        String str3 = strArrSplit[0];
        str2 = strArrSplit[1];
        try {
            i2 = Integer.parseInt(str2);
        } catch (Exception unused) {
            i2 = -1;
        }
        g.a("HidProtocolUtils", "[processControlCmd]str:" + str3);
        g.a("HidProtocolUtils", "[processControlCmd]valueStr:" + str2 + ", value:" + i2);
        str3.hashCode();
        str3.hashCode();
        switch (str3) {
            case "MIC_MV":
                c.a();
                x.a.a.put("MIC_MV", Integer.valueOf(i2));
                if (a.a().f9515c) {
                    try {
                        if (a.a().f9515c && a.a().c() != i2 && a.a().f9514b) {
                            try {
                                InitManager.getInstance().getContext().getPackageManager().getPackageInfo("com.seewo.osservice", 0);
                                z2 = true;
                            } catch (PackageManager.NameNotFoundException unused2) {
                                z2 = false;
                            }
                            if (z2) {
                                a.a().b(i2, false);
                                InitManager.getInstance().getContext().sendBroadcast(new Intent("com.ifpdos.osservice.show_volume_bar"));
                            } else {
                                a.a().b(i2, true);
                            }
                        }
                    } catch (Exception e2) {
                        g.b("HidProtocolUtils", "handleMicMvEvent: ", e2);
                    }
                }
                return 0;
            case "AI_RECORD_SHORT_PRESS":
                c.a().f(5);
                return 0;
            case "MIC_RINGCALL":
                g.a("HidProtocolUtils", "processControlCmd: ANDROID_CMD_MIC_RINGCALL");
                return 0;
            case "MIC_HOOK":
                if (i2 == 0) {
                    if (c.a().d() == 1) {
                        c.a().h(2);
                    } else {
                        c.a().h(0);
                    }
                } else if (1 == i2) {
                    g.a("HidProtocolUtils", "processControlCmd: if (HookStatus.ON_HOOK == value)");
                    c.a().h(1);
                }
                return 0;
            case "MIC_MUTE":
                c.a();
                x.a.a.put("MIC_MUTE", Integer.valueOf(i2));
                c.a().c(i2);
                return 0;
            case "MIC_REJECT":
                c.a().h(i2 == 1 ? 0 : -1);
                return 0;
            case "AI_RECORD_LONG_PRESS":
                c.a().f(6);
                return 0;
            case "CLIENT_SN":
                h.a().g(str2);
                return 0;
            case "MIC_KEY":
                if (MicKeyCode.isValidKeyCode(i2)) {
                    c.a().f(i2);
                    if (a.a().f9514b) {
                        int iC = a.a().c();
                        if (i2 == 3) {
                            aVarA = a.a();
                            i3 = iC + 5;
                        } else if (i2 == 4) {
                            aVarA = a.a();
                            i3 = iC - 5;
                        }
                        aVarA.b(i3, false);
                    }
                }
                return 0;
            default:
                return 0;
        }
    }

    public String b(byte[] bArr, int i2) {
        if (bArr == null || i2 < 0 || bArr.length < i2) {
            return "";
        }
        int length = i2;
        while (length < bArr.length && bArr[length] != 0) {
            length++;
        }
        if (length == bArr.length) {
            length = bArr.length - 1;
        }
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, i2, length);
        b.b().c("msgExtractCmdString", bArrCopyOfRange);
        return new String(bArrCopyOfRange, StandardCharsets.UTF_8);
    }

    public boolean c(int i2, int i3, byte[] bArr) {
        if (!InitManager.getInstance().getCameraControlEnableStatus()) {
            g.a("HidProtocolUtils", "Isp device is not enable, please enable first.");
            return false;
        }
        boolean zL = this.f9519c.l(i2, i3, bArr);
        if (!zL) {
            k();
        }
        return zL;
    }

    public int d(byte[] bArr) {
        if (bArr.length < 12) {
            throw new IllegalArgumentException("Data array is too short");
        }
        return ((bArr[11] & 255) << 24) | ((bArr[9] & 255) << 8) | (bArr[8] & 255) | ((bArr[10] & 255) << 16);
    }

    public boolean e(String str) {
        if (str == null || str.isEmpty()) {
            g.a("HidProtocolUtils", "requestControl: cmdStr illegality.");
            return false;
        }
        if (!t.a.f9503g) {
            Arrays.fill(this.f9518b.f9509c, (byte) 0);
            byte[] bArr = this.f9518b.f9509c;
            bArr[0] = 1;
            bArr[1] = 0;
            bArr[2] = 0;
            bArr[3] = 0;
            kotlin.reflect.w.internal.y0.n.c2.c.c(bArr, 4, InvokeCmd.CMD_SYSTEMCTRL_SET_SARPOWERMODE);
            byte[] bArr2 = this.f9518b.f9509c;
            int length = str.length();
            if (bArr2.length < 12) {
                throw new IllegalArgumentException("Data array is too short");
            }
            bArr2[8] = (byte) (length & DnsRecord.CLASS_ANY);
            bArr2[9] = (byte) ((length >> 8) & DnsRecord.CLASS_ANY);
            bArr2[10] = (byte) ((length >> 16) & DnsRecord.CLASS_ANY);
            bArr2[11] = (byte) ((length >> 24) & DnsRecord.CLASS_ANY);
            System.arraycopy(str.getBytes(StandardCharsets.UTF_8), 0, this.f9518b.f9509c, 12, str.length());
            b.b().c("requestControl", this.f9518b.f9509c);
        } else if (t.a.f9504h == SupportDevicesEnum.VA_COMM_DONGLE) {
            Arrays.fill(this.f9518b.f9511e, (byte) 0);
            byte[] bArr3 = this.f9518b.f9511e;
            bArr3[0] = 79;
            bArr3[1] = DefaultBinaryMemcacheResponse.RESPONSE_MAGIC_BYTE;
            System.arraycopy(str.getBytes(StandardCharsets.UTF_8), 0, this.f9518b.f9511e, 2, str.length());
            b.b().c("requestControl", this.f9518b.f9511e);
        }
        boolean zC = this.f9518b.c();
        if (!zC) {
            j();
        }
        return zC;
    }

    public int f(byte[] bArr) {
        if (bArr == null || bArr.length != 64) {
            return -1;
        }
        b.b().c("handleByte64Msg", bArr);
        int iD = d(bArr);
        if (iD <= 0) {
            g.c("HidProtocolUtils", "[handleByte64Msg]invalid requestLen:" + iD);
            return -1;
        }
        g.a("HidProtocolUtils", "[handleByte64Msg]requestLen=" + iD);
        String str = new String(Arrays.copyOfRange(bArr, 12, iD + 12), StandardCharsets.UTF_8);
        g.a("HidProtocolUtils", "[handleByte64Msg]cmdStr=" + str);
        return a(str);
    }

    public int g(byte[] bArr) {
        if (bArr == null || bArr.length != 512) {
            return -1;
        }
        b.b().c("handleControlMsg", bArr);
        int iD = d(bArr);
        if (iD <= 0) {
            return -1;
        }
        String str = new String(Arrays.copyOfRange(bArr, 12, iD + 12), StandardCharsets.UTF_8);
        g.a("HidProtocolUtils", "[handleControlMsg]cmdStr=" + str);
        return a(str);
    }

    public int i(byte[] bArr) {
        if (bArr != null && bArr.length == 64) {
            b.b().c("handleDongleMsg", bArr);
            int i2 = bArr[0] & 255;
            int i3 = bArr[1] & 255;
            if (36 == i2 && 129 == i3) {
                String strB = bArr.length < 2 ? "" : b(bArr, 2);
                g.a("HidProtocolUtils", "[handleDongleMsg]cmdStr=" + strB);
                return a(strB);
            }
        }
        return -1;
    }

    public synchronized int j() {
        String strD = t.a.d();
        String str = this.f9520d;
        if (str != null && str.equals(strD)) {
            g.a("HidProtocolUtils", this.f9520d + " same devicePath, return");
            return 0;
        }
        this.f9520d = strD;
        if (strD != null && !strD.isEmpty()) {
            g.a("HidProtocolUtils", "openDspDevice: devicePath=" + this.f9520d);
            t.c cVar = this.f9518b;
            String str2 = this.f9520d;
            Objects.requireNonNull(cVar);
            try {
                if (cVar.f9513g.d(str2)) {
                    cVar.f9512f = true;
                } else {
                    cVar.f9512f = false;
                }
            } catch (Exception e2) {
                Log.e("MicArrayCommApi", e2.toString());
            }
            if (cVar.f9512f) {
                q.a.b().c();
                return 0;
            }
            g.a("HidProtocolUtils", "openDspDevice: openDevice failed,path: " + this.f9520d);
            this.f9520d = "";
            return -1;
        }
        return -1;
    }

    public int k() {
        String string;
        StringBuilder sbF;
        File file;
        byte[] bArr = t.a.a;
        File[] fileArrListFiles = new File("/sys/class/hidraw").listFiles();
        if (fileArrListFiles.length > 0) {
            for (int i2 = 0; i2 < fileArrListFiles.length; i2++) {
                if (fileArrListFiles[i2].isDirectory() && t.a.a(fileArrListFiles[i2].getPath()) != null) {
                    byte[] bArrE = t.a.e(fileArrListFiles[i2].getPath());
                    if (bArrE == null) {
                        sbF = g.a.a.a.a.F("/dev/");
                        file = fileArrListFiles[i2];
                    } else if (t.a.c(bArrE, t.a.f9499c)) {
                        g.a("DeviceUtils", "getDeviceCommPath: found ISP.");
                        sbF = new StringBuilder();
                        sbF.append("/dev/");
                        file = fileArrListFiles[i2];
                    }
                    sbF.append(file.getName());
                    string = sbF.toString();
                    break;
                }
            }
            string = null;
        } else {
            string = null;
        }
        this.f9520d = string;
        StringBuilder sbF2 = g.a.a.a.a.F("openIspDevice: devicePath=");
        sbF2.append(this.f9520d);
        g.a("HidProtocolUtils", sbF2.toString());
        String str = this.f9520d;
        if (str == null || str.isEmpty()) {
            return -1;
        }
        j.a aVar = this.f9519c;
        String str2 = this.f9520d;
        Objects.requireNonNull(aVar);
        try {
            if (aVar.f5833q.d(str2)) {
                aVar.f5832p = true;
            } else {
                aVar.f5832p = false;
            }
        } catch (Exception e2) {
            Log.e("IspCommApi", e2.toString());
        }
        return !aVar.f5832p ? -1 : 0;
    }
}
