package com.seewo.factoryaging.camera2record;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.widget.Toast;
import com.cvte.vman.VmanMiddleWare;
import com.seewo.factoryaging.s;
import j.k0.n;
import j.k0.o;
import j.k0.w;
import j.p0.d.j;
import j.p0.d.r;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import org.greenrobot.eventbus.ThreadMode;
import org.greenrobot.eventbus.m;

/* JADX INFO: compiled from: Camera2RecordPresenter.kt */
/* JADX INFO: loaded from: classes.dex */
public final class g extends e {
    public static final a v = new a(null);
    private static String w;
    private Context c;
    private CameraDevice d;
    private CameraCaptureSession e;
    private Size f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private Size f314g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private MediaRecorder f315h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private Handler f316i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final Semaphore f317j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private SurfaceTexture f318k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private int f319l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private int f320m;
    private CameraManager n;
    private CaptureRequest.Builder o;
    private boolean p;
    private boolean q;
    private final long r;
    private final Handler s;
    private boolean t;
    private final c u;

    /* JADX INFO: compiled from: Camera2RecordPresenter.kt */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(j jVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: Camera2RecordPresenter.kt */
    public static final class b implements Comparator<Size> {
        @Override // java.util.Comparator
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(Size size, Size size2) {
            r.f(size, "lhs");
            r.f(size2, "rhs");
            return Long.signum((((long) size.getWidth()) * ((long) size.getHeight())) - (((long) size2.getWidth()) * ((long) size2.getHeight())));
        }
    }

    /* JADX INFO: compiled from: Camera2RecordPresenter.kt */
    public static final class c extends CameraDevice.StateCallback {
        c() {
        }

        @Override // android.hardware.camera2.CameraDevice.StateCallback
        public void onDisconnected(CameraDevice cameraDevice) {
            r.f(cameraDevice, "cameraDevice");
            try {
                g.this.f317j.release();
                cameraDevice.close();
                Log.d("Camera2RecordPresenter", "onDisconnected: cameraDevice.close()");
                g.this.K(null);
            } catch (Throwable th) {
                Log.e("Camera2RecordPresenter", "onDisconnected exception: " + th.getMessage(), th);
            }
        }

        @Override // android.hardware.camera2.CameraDevice.StateCallback
        public void onError(CameraDevice cameraDevice, int i2) {
            r.f(cameraDevice, "cameraDevice");
            try {
                g.this.f317j.release();
                com.seewo.factoryaging.p0.a.a.e("摄像头", "FAIL:Camera Callback onError");
                Log.d("Camera2RecordPresenter", "onError: " + i2);
                cameraDevice.close();
                g.this.K(null);
            } catch (Throwable th) {
                Log.e("Camera2RecordPresenter", "onError exception: " + th.getMessage(), th);
            }
        }

        @Override // android.hardware.camera2.CameraDevice.StateCallback
        public void onOpened(CameraDevice cameraDevice) {
            r.f(cameraDevice, "cameraDevice");
            try {
                g.this.K(cameraDevice);
                g.this.M();
                Log.d("Camera2RecordPresenter", "onOpened: startPreview");
                g.this.f317j.release();
            } catch (Throwable th) {
                Log.e("Camera2RecordPresenter", "onOpened exception: " + th.getMessage(), th);
                com.seewo.factoryaging.p0.a.a.e("摄像头", "FAIL:Camera onOpened exception");
            }
        }
    }

    /* JADX INFO: compiled from: Camera2RecordPresenter.kt */
    public static final class d extends CameraCaptureSession.StateCallback {
        d() {
        }

        @Override // android.hardware.camera2.CameraCaptureSession.StateCallback
        public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
            r.f(cameraCaptureSession, "session");
            Toast.makeText(g.this.A(), "Failed", 0).show();
        }

        @Override // android.hardware.camera2.CameraCaptureSession.StateCallback
        public void onConfigured(CameraCaptureSession cameraCaptureSession) {
            r.f(cameraCaptureSession, "session");
            g.this.e = cameraCaptureSession;
            g.this.Q();
        }
    }

    public g(Context context) {
        r.f(context, "context");
        this.c = context;
        this.f317j = new Semaphore(1);
        this.q = true;
        this.r = 60000L;
        this.s = new Handler();
        this.u = new c();
        org.greenrobot.eventbus.c.c().q(this);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void B(g gVar) throws CameraAccessException {
        r.f(gVar, "this$0");
        gVar.N();
    }

    private final boolean C() {
        try {
            return VmanMiddleWare.getInstance().getAvManager().getCamSupportBlackScreen();
        } catch (Exception e) {
            Log.e("Camera2RecordPresenter", "isCamSupportBlackScreen error" + e.getMessage());
            return false;
        }
    }

    private final void H() {
        Log.d("Camera2RecordPresenter", "rollbackCameraConfig");
        J(false);
        if (this.p) {
            I(this.q);
        }
        Log.d("Camera2RecordPresenter", "rollbackCameraConfig finish");
    }

    private final void I(boolean z) {
        try {
            VmanMiddleWare.getInstance().getAvManager().setCamAutoBlackScreen(z);
            Log.d("Camera2RecordPresenter", "setCamAutoBlackScreen: " + z);
        } catch (Exception e) {
            Log.e("Camera2RecordPresenter", "setCamAutoBlackScreen error" + e.getMessage());
        }
    }

    private final void J(boolean z) {
        try {
            VmanMiddleWare.getInstance().getAvManager().setCameraAutoTest(z);
            Log.d("Camera2RecordPresenter", "setCameraAutoTest: " + z);
        } catch (Exception e) {
            Log.e("Camera2RecordPresenter", "setCameraAutoTest error" + e.getMessage());
        }
    }

    private final void L(CaptureRequest.Builder builder) {
        builder.set(CaptureRequest.CONTROL_MODE, 1);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void M() {
        if (this.d == null || this.f318k == null || this.f == null) {
            return;
        }
        try {
            v();
            s sVarB = com.seewo.factoryaging.e1.f.a.b();
            SurfaceTexture surfaceTexture = this.f318k;
            if (surfaceTexture != null) {
                surfaceTexture.setDefaultBufferSize(sVarB.b(), sVarB.a());
            }
            CameraDevice cameraDevice = this.d;
            this.o = cameraDevice != null ? cameraDevice.createCaptureRequest(1) : null;
            Surface surface = new Surface(this.f318k);
            CaptureRequest.Builder builder = this.o;
            if (builder != null) {
                builder.addTarget(surface);
            }
            org.greenrobot.eventbus.c.c().o(new com.seewo.factoryaging.t0.c());
            org.greenrobot.eventbus.c.c().o(new com.seewo.factoryaging.t0.f());
            CameraDevice cameraDevice2 = this.d;
            if (cameraDevice2 != null) {
                cameraDevice2.createCaptureSession(n.b(surface), new d(), this.f316i);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private final void N() throws CameraAccessException {
        List<String> listZ = z();
        Log.d("Camera2RecordPresenter", "switchCamera: mCameraManager = " + this.n + " cameraNum = " + listZ.size());
        if (listZ.size() == 0) {
            com.seewo.factoryaging.p0.a.a.e("摄像头", "FAIL:camera num is 0");
        }
        if (!r.a(com.seewo.factoryaging.r0.k.a.a(), "none")) {
            Log.d("Camera2RecordPresenter", "switchCamera: getDpInCameraId = " + com.seewo.factoryaging.r0.k.a.a() + " return");
            return;
        }
        if (listZ.size() <= 1) {
            this.s.postDelayed(new Runnable() { // from class: com.seewo.factoryaging.camera2record.b
                @Override // java.lang.Runnable
                public final void run() throws CameraAccessException {
                    g.O(this.b);
                }
            }, this.r);
            return;
        }
        Log.d("Camera2RecordPresenter", "switchCamera: begin");
        if (!com.seewo.vcommons.data.c.i("persist.sys.forcebackcamera", "-1").equals("-1")) {
            Log.d("Camera2RecordPresenter", "switchCamera: set persist.sys.forcebackcamera: -1 ");
            com.seewo.vcommons.data.c.r("persist.sys.forcebackcamera", "-1");
        }
        if (!com.seewo.vcommons.data.c.i("persist.sys.forcefrontcamera", "-1").equals("-1")) {
            Log.d("Camera2RecordPresenter", "switchCamera: set persist.sys.forcefrontcamera: -1 ");
            com.seewo.vcommons.data.c.r("persist.sys.forcefrontcamera", "-1");
        }
        t();
        int iY = (w.y(listZ, w) + 1) % listZ.size();
        x(listZ.get(iY));
        org.greenrobot.eventbus.c.c().o(new com.seewo.factoryaging.t0.d(String.valueOf(iY)));
        this.s.postDelayed(new Runnable() { // from class: com.seewo.factoryaging.camera2record.a
            @Override // java.lang.Runnable
            public final void run() throws CameraAccessException {
                g.P(this.b);
            }
        }, this.r);
        Log.d("Camera2RecordPresenter", "switchCamera: end");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void O(g gVar) throws CameraAccessException {
        r.f(gVar, "this$0");
        gVar.N();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void P(g gVar) throws CameraAccessException {
        r.f(gVar, "this$0");
        gVar.N();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void Q() {
        if (this.d == null) {
            return;
        }
        try {
            CaptureRequest.Builder builder = this.o;
            r.c(builder);
            L(builder);
            CameraCaptureSession cameraCaptureSession = this.e;
            if (cameraCaptureSession != null) {
                CaptureRequest.Builder builder2 = this.o;
                r.c(builder2);
                cameraCaptureSession.setRepeatingRequest(builder2.build(), null, this.f316i);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private final Size r(Size[] sizeArr, int i2, int i3, Size size) {
        ArrayList arrayList = new ArrayList();
        int width = size.getWidth();
        int height = size.getHeight();
        for (Size size2 : sizeArr) {
            if (size2.getHeight() == (size2.getWidth() * height) / width && size2.getWidth() >= i2 && size2.getHeight() >= i3) {
                arrayList.add(size2);
            }
        }
        if (arrayList.size() <= 0) {
            return size;
        }
        Object objMin = Collections.min(arrayList, new b());
        r.e(objMin, "{\n            Collection…eSizesByArea())\n        }");
        return (Size) objMin;
    }

    private final Size s(Size[] sizeArr) {
        for (Size size : sizeArr) {
            if (size.getWidth() == (size.getHeight() * 4) / 3 && size.getWidth() <= 1080) {
                return size;
            }
        }
        return sizeArr[sizeArr.length - 1];
    }

    private final void t() {
        Log.d("Camera2RecordPresenter", "closeCamera");
        H();
        u();
        Log.d("Camera2RecordPresenter", "closeCamera finish");
    }

    private final void u() {
        try {
            try {
                this.f317j.acquire();
                v();
                if (this.d != null) {
                    CameraDevice cameraDevice = this.d;
                    if (cameraDevice != null) {
                        cameraDevice.close();
                    }
                    this.d = null;
                }
                if (this.f315h != null) {
                    MediaRecorder mediaRecorder = this.f315h;
                    if (mediaRecorder != null) {
                        mediaRecorder.release();
                    }
                    this.f315h = null;
                }
            } catch (Exception e) {
                com.seewo.factoryaging.p0.a.a.e("摄像头", "FAIL:closeCamera fail");
                Log.e("Camera2RecordPresenter", "closeCamera: error " + e);
            }
        } finally {
            this.f317j.release();
        }
    }

    private final void v() {
        CameraCaptureSession cameraCaptureSession = this.e;
        if (cameraCaptureSession != null) {
            if (cameraCaptureSession != null) {
                cameraCaptureSession.close();
            }
            this.e = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void w(g gVar) {
        r.f(gVar, "this$0");
        gVar.u();
    }

    @SuppressLint({"MissingPermission"})
    private final void x(String str) {
        Class<?> cls;
        try {
            if (!this.f317j.tryAcquire(2500L, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }
            w = str;
            Log.d("Camera2RecordPresenter", "doStartCamera: currentCameraId = " + w);
            CameraManager cameraManager = this.n;
            CameraCharacteristics cameraCharacteristics = cameraManager != null ? cameraManager.getCameraCharacteristics(str) : null;
            StreamConfigurationMap streamConfigurationMap = cameraCharacteristics != null ? (StreamConfigurationMap) cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) : null;
            if (cameraCharacteristics != null) {
            }
            if (streamConfigurationMap == null) {
                throw new RuntimeException("Cannot get available preview/video sizes");
            }
            Size[] outputSizes = streamConfigurationMap.getOutputSizes(MediaRecorder.class);
            r.e(outputSizes, "map.getOutputSizes(MediaRecorder::class.java)");
            this.f314g = s(outputSizes);
            Size[] outputSizes2 = streamConfigurationMap.getOutputSizes(SurfaceTexture.class);
            r.e(outputSizes2, "map.getOutputSizes(SurfaceTexture::class.java)");
            int i2 = this.f319l;
            int i3 = this.f320m;
            Size size = this.f314g;
            r.c(size);
            this.f = r(outputSizes2, i2, i3, size);
            CameraManager cameraManager2 = this.n;
            Method declaredMethod = (cameraManager2 == null || (cls = cameraManager2.getClass()) == null) ? null : cls.getDeclaredMethod("openCamera", String.class, CameraDevice.StateCallback.class, Handler.class);
            if (declaredMethod != null) {
                declaredMethod.setAccessible(true);
            }
            if (declaredMethod != null) {
                declaredMethod.invoke(this.n, str, this.u, null);
            }
            com.seewo.factoryaging.p0.a.a.e("摄像头", "PASS");
        } catch (Throwable th) {
            com.seewo.factoryaging.p0.a.a.e("摄像头", "FAIL:openCamera fail");
            Log.e("Camera2RecordPresenter", "doStartCamera: error " + th);
        }
    }

    private final boolean y() {
        try {
            return VmanMiddleWare.getInstance().getAvManager().getCamAutoBlackScreen();
        } catch (Exception e) {
            Log.e("Camera2RecordPresenter", "getCamAutoBlackScreen error" + e.getMessage());
            return false;
        }
    }

    public final Context A() {
        return this.c;
    }

    public final void K(CameraDevice cameraDevice) {
        this.d = cameraDevice;
    }

    @m(sticky = true, threadMode = ThreadMode.MAIN)
    public final void handTouchSignalEvent(com.seewo.factoryaging.t0.g gVar) throws CameraAccessException {
        r.f(gVar, "event");
        Log.d("Camera2RecordPresenter", "handTouchSignalEvent: " + gVar.a());
        if (gVar.a()) {
            f fVarI = i();
            if (fVarI != null) {
                fVarI.k(false);
            }
            t();
            return;
        }
        f fVarI2 = i();
        if (fVarI2 != null) {
            fVarI2.k(true);
        }
        SurfaceTexture surfaceTexture = this.f318k;
        r.c(surfaceTexture);
        m(surfaceTexture, this.f319l, this.f320m);
    }

    @m(sticky = true, threadMode = ThreadMode.MAIN)
    public final void handleBackPress(com.seewo.factoryaging.t0.b bVar) {
        r.f(bVar, "event");
        Log.d("Camera2RecordPresenter", "AgingCloseEvent: stopRendering");
        l();
    }

    @Override // com.seewo.factoryaging.camera2record.e
    public void l() {
        if (this.t) {
            return;
        }
        this.t = true;
        H();
        new Thread(new Runnable() { // from class: com.seewo.factoryaging.camera2record.c
            @Override // java.lang.Runnable
            public final void run() {
                g.w(this.b);
            }
        }).start();
        org.greenrobot.eventbus.c.c().s(this);
    }

    @Override // com.seewo.factoryaging.camera2record.e
    public void m(SurfaceTexture surfaceTexture, int i2, int i3) throws CameraAccessException {
        r.f(surfaceTexture, "textureView");
        this.f318k = surfaceTexture;
        this.f319l = i2;
        this.f320m = i3;
        Object systemService = this.c.getSystemService("camera");
        this.n = systemService instanceof CameraManager ? (CameraManager) systemService : null;
        List<String> listZ = z();
        w = listZ.isEmpty() ^ true ? listZ.get(0) : null;
        J(true);
        boolean zC = C();
        this.p = zC;
        if (zC) {
            this.q = y();
            I(false);
            Log.d("Camera2RecordPresenter", "getCamSupportBlackScreen: " + this.p + " originalBlackScreen: " + this.q);
        }
        String str = w;
        if (str != null) {
            x(str);
        }
        if (listZ.size() >= 2 && r.a(com.seewo.factoryaging.r0.k.a.a(), "none")) {
            org.greenrobot.eventbus.c.c().o(new com.seewo.factoryaging.t0.d("0"));
        }
        this.s.postDelayed(new Runnable() { // from class: com.seewo.factoryaging.camera2record.d
            @Override // java.lang.Runnable
            public final void run() throws CameraAccessException {
                g.B(this.b);
            }
        }, this.r);
    }

    public final List<String> z() throws CameraAccessException {
        String str;
        if (this.n == null) {
            Log.d("Camera2RecordPresenter", "mCameraManager is null");
            return o.g();
        }
        CameraCharacteristics.Key key = new CameraCharacteristics.Key("android.info.deviceType", String.class);
        CameraManager cameraManager = this.n;
        r.c(cameraManager);
        String[] cameraIdList = cameraManager.getCameraIdList();
        r.e(cameraIdList, "mCameraManager!!.cameraIdList");
        ArrayList arrayList = new ArrayList();
        for (String str2 : cameraIdList) {
            try {
                CameraManager cameraManager2 = this.n;
                r.c(cameraManager2);
                str = (String) cameraManager2.getCameraCharacteristics(str2).get(key);
            } catch (Throwable unused) {
            }
            if (str == null) {
                str = "BUILD_IN";
            }
            r.e(str, "try {\n                mC… \"BUILD_IN\"\n            }");
            Log.d("Camera2RecordPresenter", "camera " + str2 + " type is " + str);
            if (!r.a(str, "HDMI")) {
                arrayList.add(str2);
            }
        }
        return arrayList;
    }
}
