package com.seewo.factoryaging.mic;

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.Build;
import android.os.SystemProperties;
import android.util.Log;
import com.cvte.vman.R;
import com.cvte.vman.VmanMiddleWare;
import com.google.gson.Gson;
import com.seewo.factoryaging.FactoryAgingApplication;
import com.seewo.factoryaging.mic.h.a;
import com.seewo.factoryaging.z;
import j.k0.j0;
import j.p0.d.r;
import j.u0.q;
import j.v;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: MicUtils.kt */
/* JADX INFO: loaded from: classes.dex */
public final class g {
    public static final g a = new g();
    private static String b = "1";

    private g() {
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x008e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private final boolean d() {
        /*
            r12 = this;
            java.io.File r0 = new java.io.File
            java.lang.String r1 = "/proc/asound/"
            r0.<init>(r1)
            boolean r1 = r0.exists()
            java.lang.String r2 = "MicUtils"
            r3 = 0
            if (r1 == 0) goto Lbc
            boolean r1 = r0.isDirectory()
            if (r1 != 0) goto L18
            goto Lbc
        L18:
            java.io.File[] r0 = r0.listFiles()
            if (r0 != 0) goto L1f
            return r3
        L1f:
            java.util.ArrayList r1 = new java.util.ArrayList
            r1.<init>()
            int r4 = r0.length
            r5 = r3
        L26:
            if (r5 >= r4) goto L40
            r6 = r0[r5]
            java.lang.String r7 = r6.getName()
            java.lang.String r8 = "file1.name"
            j.p0.d.r.e(r7, r8)
            boolean r7 = r12.h(r7)
            if (r7 == 0) goto L3a
            goto L3d
        L3a:
            r1.add(r6)
        L3d:
            int r5 = r5 + 1
            goto L26
        L40:
            java.util.Iterator r0 = r1.iterator()
        L44:
            boolean r1 = r0.hasNext()
            if (r1 == 0) goto Lbb
            java.lang.Object r1 = r0.next()
            java.io.File r1 = (java.io.File) r1
            boolean r4 = r1.exists()
            if (r4 == 0) goto L44
            boolean r4 = r1.isDirectory()
            if (r4 != 0) goto L5d
            goto L44
        L5d:
            java.io.File[] r1 = r1.listFiles()
            java.lang.String r4 = "cardList"
            j.p0.d.r.e(r1, r4)
            int r4 = r1.length
            r5 = r3
        L68:
            if (r5 >= r4) goto L44
            r6 = r1[r5]
            java.lang.String r7 = r6.getName()
            java.lang.String r8 = "pcmFile.name"
            j.p0.d.r.e(r7, r8)
            java.lang.String r9 = "pcm"
            r10 = 2
            r11 = 0
            boolean r7 = j.u0.h.D(r7, r9, r3, r10, r11)
            if (r7 == 0) goto L8e
            java.lang.String r7 = r6.getName()
            j.p0.d.r.e(r7, r8)
            java.lang.String r9 = "c"
            boolean r7 = j.u0.h.p(r7, r9, r3, r10, r11)
            if (r7 != 0) goto L9d
        L8e:
            java.lang.String r7 = r6.getName()
            j.p0.d.r.e(r7, r8)
            java.lang.String r8 = "usbid"
            boolean r7 = j.u0.h.D(r7, r8, r3, r10, r11)
            if (r7 == 0) goto Lb8
        L9d:
            java.lang.StringBuilder r0 = new java.lang.StringBuilder
            r0.<init>()
            java.lang.String r1 = "pcmFile = "
            r0.append(r1)
            r0.append(r6)
            java.lang.String r1 = ", has mic = true"
            r0.append(r1)
            java.lang.String r0 = r0.toString()
            android.util.Log.i(r2, r0)
            r0 = 1
            return r0
        Lb8:
            int r5 = r5 + 1
            goto L68
        Lbb:
            return r3
        Lbc:
            java.lang.String r0 = "fail to get /proc/asound listFiles"
            android.util.Log.d(r2, r0)
            return r3
        */
        throw new UnsupportedOperationException("Method not decompiled: com.seewo.factoryaging.mic.g.d():boolean");
    }

    private final boolean e() throws IOException {
        OkHttpClient okHttpClientBuild = h.b.b.a.f.d().build();
        Request.Builder builder = new Request.Builder();
        String strA = h.b.b.a.f.a("/v1/voice/audio/in/device/list");
        r.e(strA, "getFullUrl(\"/v1/voice/audio/in/device/list\")");
        Response responseExecute = okHttpClientBuild.newCall(builder.url(strA).build()).execute();
        int iCode = responseExecute.code();
        Gson gson = new Gson();
        Log.i("MicUtils", "hasAudioInDeviceByUdi response code = " + iCode);
        if (iCode != 200) {
            return false;
        }
        ResponseBody responseBodyBody = responseExecute.body();
        String strString = responseBodyBody != null ? responseBodyBody.string() : null;
        Log.i("MicUtils", "hasAudioInDeviceByUdi body: " + strString);
        List<a.C0010a> listA = ((com.seewo.factoryaging.mic.h.a) gson.fromJson(strString, com.seewo.factoryaging.mic.h.a.class)).a();
        r.e(listA, "audioInDevice.audioDeviceList");
        ArrayList arrayList = new ArrayList();
        Iterator<T> it = listA.iterator();
        while (true) {
            if (!it.hasNext()) {
                Log.i("MicUtils", "hasAudioInDeviceByUdi filter devices: " + arrayList);
                return !arrayList.isEmpty();
            }
            Object next = it.next();
            a.C0010a c0010a = (a.C0010a) next;
            if ((r.a(c0010a.b(), "TYPE_CVTE_VIRTUAL") || r.a(c0010a.b(), "TYPE_BUILTIN_MIC")) ? false : true) {
                arrayList.add(next);
            }
        }
    }

    private final boolean f() {
        boolean zIsMicrophoneAvaliable = VmanMiddleWare.getInstance().getAudioCtrl().isMicrophoneAvaliable();
        Log.d("MicUtils", "use vman,micExist: " + zIsMicrophoneAvaliable);
        return zIsMicrophoneAvaliable;
    }

    private final boolean h(String str) {
        return !q.D(str, "card", false, 2, null) || k(str) || q.D(str, "cards", false, 2, null);
    }

    private final boolean j() {
        return SystemProperties.get("ro.customer.name").equals("seewo_tc01") || SystemProperties.get("ro.cvte.flavor.name").equals("sahara_impact") || SystemProperties.get("ro.ocfg.def_audio_card").equals("card0");
    }

    private final boolean k(String str) {
        return q.D(str, "card0", false, 2, null) && !j();
    }

    private final boolean m() {
        try {
            com.seewo.vcommons.data.c.r("hw.cvte.mic.aecEn", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.agcEn", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.cfgGain", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.esEn", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.hpfEn", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.lpfEn", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.nrEn", "0");
            com.seewo.vcommons.data.c.r("mic.hw.test.req", "C");
            return true;
        } catch (Exception e) {
            Log.e("MicUtils", "setNoiseReductionDisable: error", e);
            return false;
        }
    }

    private final boolean n() {
        try {
            com.seewo.vcommons.data.c.r("hw.cvte.mic.aecEn", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.agcEn", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.cfgGain", "0");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.esEn", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.hpfEn", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.lpfEn", "1");
            com.seewo.vcommons.data.c.r("hw.cvte.mic.nrEn", "1");
            com.seewo.vcommons.data.c.r("mic.hw.test.req", "C");
            return true;
        } catch (Exception e) {
            Log.e("MicUtils", "setNoiseReductionEnable: error", e);
            return false;
        }
    }

    public final boolean a() {
        boolean z = true;
        if (!r.a(com.seewo.vcommons.data.c.i("persist.sys.mic.type", "usb"), "usb")) {
            Log.d("MicUtils", "checkUsbMicExistIfNeed is not usb mic, return true");
            return true;
        }
        Object systemService = FactoryAgingApplication.e.d().getSystemService("audio");
        if (systemService == null) {
            throw new NullPointerException("null cannot be cast to non-null type android.media.AudioManager");
        }
        AudioDeviceInfo[] devices = ((AudioManager) systemService).getDevices(1);
        r.e(devices, "devices");
        for (AudioDeviceInfo audioDeviceInfo : devices) {
            Log.d("MicUtils", "checkUsbMicExistIfNeed device type: " + audioDeviceInfo.getType());
        }
        int length = devices.length;
        int i2 = 0;
        while (true) {
            if (i2 >= length) {
                z = false;
                break;
            }
            AudioDeviceInfo audioDeviceInfo2 = devices[i2];
            if (audioDeviceInfo2.getType() == 11 || audioDeviceInfo2.getType() == 22) {
                break;
            }
            i2++;
        }
        Log.d("MicUtils", "checkUsbMicExist isUsbMicExist: " + z);
        return z;
    }

    public final int b() {
        z zVar = (z) com.seewo.factoryaging.d1.b.a.f("/v1/system/pc/mic/temperature", z.class);
        if (zVar != null) {
            return zVar.a();
        }
        return 0;
    }

    public final String c() {
        return b;
    }

    public final boolean g(Context context) {
        r.f(context, "context");
        try {
            return context.getResources().getBoolean(R.bool.enableCheckMicDevicesByUdi) ? e() : (Build.VERSION.SDK_INT < 33 || !context.getResources().getBoolean(R.bool.enableCheckProcASound)) ? d() : f();
        } catch (Throwable th) {
            Log.e("MicUtils", "invoke hasMic error", th);
            return false;
        }
    }

    public final boolean i() {
        try {
            a.C0010a c0010a = (a.C0010a) com.seewo.factoryaging.d1.b.a.f("/v1/voice/audio/in/device", a.C0010a.class);
            if (c0010a == null) {
                return false;
            }
            return r.a(c0010a.a(), "ID_VIRTUAL_OFF");
        } catch (Exception e) {
            Log.e("MicUtils", "isMicOff: error", e);
            return false;
        }
    }

    public final void l(boolean z) {
        Log.d("MicUtils", "setMicEnable: " + z);
        com.seewo.factoryaging.d1.b.k(com.seewo.factoryaging.d1.b.a, "/v1/voice/audio/in/device", j0.c(v.a("id", z ? "ID_VIRTUAL_AUTO" : "ID_VIRTUAL_OFF")), null, 4, null);
    }

    public final boolean o(boolean z) {
        if (Build.VERSION.SDK_INT < 33) {
            return z ? n() : m();
        }
        Log.d("MicUtils", "not support setNoiseReductionEnable by prop,ignore");
        return false;
    }
}
