package com.epson.cameracopy.device;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import epson.common.ImageUtil;
import java.util.ArrayList;
import java.util.List;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class PortraitCameraView extends CameraBridgeViewBase implements Camera.PreviewCallback {
    @SuppressLint({"InlinedApi"})
    private static final String[] FAVORITE_FOCUS_MODES = {"continuous-video", "continuous-picture", "auto"};
    private static final int MAGIC_TEXTURE_ID = 10;
    private static final String TAG = "portCameraView";
    private byte[] mBuffer;
    protected Camera mCamera;
    protected JavaCameraFrame[] mCameraFrame;
    /* access modifiers changed from: private */
    public boolean mCameraFrameReady = false;
    /* access modifiers changed from: private */
    public int mChainIdx = 0;
    /* access modifiers changed from: private */
    public Mat[] mFrameChain;
    /* access modifiers changed from: private */
    public boolean mStopThread;
    private SurfaceTexture mSurfaceTexture;
    private Thread mThread;

    private boolean isInRange(double d, double d2, double d3) {
        return d - d2 <= d3 && d + d2 >= d3;
    }

    public static class JavaCameraSizeAccessor implements CameraBridgeViewBase.ListItemAccessor {
        public int getWidth(Object obj) {
            return ((Camera.Size) obj).width;
        }

        public int getHeight(Object obj) {
            return ((Camera.Size) obj).height;
        }
    }

    public PortraitCameraView(Context context, int i) {
        super(context, i);
    }

    public PortraitCameraView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Removed duplicated region for block: B:12:0x0024  */
    /* JADX WARNING: Removed duplicated region for block: B:17:0x002e  */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x007a  */
    /* JADX WARNING: Removed duplicated region for block: B:9:0x001a  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public org.opencv.core.Size calculateCameraFrameSize(java.util.List<?> r19, org.opencv.android.CameraBridgeViewBase.ListItemAccessor r20, int r21, int r22) {
        /*
            r18 = this;
            r7 = r18
            r8 = r20
            int r0 = r7.mMaxWidth
            r1 = -1
            if (r0 == r1) goto L_0x0013
            int r0 = r7.mMaxWidth
            r2 = r21
            if (r0 >= r2) goto L_0x0015
            int r0 = r7.mMaxWidth
            r9 = r0
            goto L_0x0016
        L_0x0013:
            r2 = r21
        L_0x0015:
            r9 = r2
        L_0x0016:
            int r0 = r7.mMaxHeight
            if (r0 == r1) goto L_0x0024
            int r0 = r7.mMaxHeight
            r1 = r22
            if (r0 >= r1) goto L_0x0026
            int r0 = r7.mMaxHeight
            r10 = r0
            goto L_0x0027
        L_0x0024:
            r1 = r22
        L_0x0026:
            r10 = r1
        L_0x0027:
            r12 = 0
        L_0x0028:
            int r0 = r19.size()
            if (r12 >= r0) goto L_0x006c
            r13 = r19
            java.lang.Object r0 = r13.get(r12)
            int r1 = r8.getWidth(r0)
            int r0 = r8.getHeight(r0)
            r2 = 650(0x28a, float:9.11E-43)
            if (r1 >= r2) goto L_0x0067
            r2 = 300(0x12c, float:4.2E-43)
            if (r1 < r2) goto L_0x0067
            r2 = 4608683618675807573(0x3ff5555555555555, double:1.3333333333333333)
            r4 = 4585925428558828667(0x3fa47ae147ae147b, double:0.04)
            double r14 = (double) r1
            double r0 = (double) r0
            double r16 = r14 / r0
            r22 = r12
            r11 = r0
            r0 = r18
            r1 = r2
            r3 = r4
            r5 = r16
            boolean r0 = r0.isInRange(r1, r3, r5)
            if (r0 == 0) goto L_0x0069
            org.opencv.core.Size r0 = new org.opencv.core.Size
            r0.<init>(r14, r11)
            return r0
        L_0x0067:
            r22 = r12
        L_0x0069:
            int r12 = r22 + 1
            goto L_0x0028
        L_0x006c:
            r13 = r19
            java.util.Iterator r0 = r19.iterator()
            r1 = 0
            r2 = 0
        L_0x0074:
            boolean r3 = r0.hasNext()
            if (r3 == 0) goto L_0x0091
            java.lang.Object r3 = r0.next()
            int r4 = r8.getWidth(r3)
            int r3 = r8.getHeight(r3)
            if (r4 > r9) goto L_0x0074
            if (r3 > r10) goto L_0x0074
            if (r4 < r1) goto L_0x0074
            if (r3 < r2) goto L_0x0074
            r2 = r3
            r1 = r4
            goto L_0x0074
        L_0x0091:
            org.opencv.core.Size r0 = new org.opencv.core.Size
            double r3 = (double) r1
            double r1 = (double) r2
            r0.<init>(r3, r1)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.cameracopy.device.PortraitCameraView.calculateCameraFrameSize(java.util.List, org.opencv.android.CameraBridgeViewBase$ListItemAccessor, int, int):org.opencv.core.Size");
    }

    private Camera.Size selectPreviewSize(List<Camera.Size> list, double d) {
        Camera.Size size = null;
        double d2 = 0.0d;
        for (Camera.Size next : list) {
            if (Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d) {
                double d3 = ((double) (next.width * next.height)) / 307200.0d;
                if (d3 > 1.0d) {
                    d3 = 1.0d / d3;
                }
                if (d3 > d2) {
                    size = next;
                    d2 = d3;
                }
            }
        }
        return size;
    }

    protected boolean setPreviewSize(List<Camera.Size> list, Camera.Parameters parameters) {
        Point screenSize = getScreenSize();
        Camera.Size selectPreviewSize = selectPreviewSize(list, ((double) screenSize.y) / ((double) screenSize.x));
        if (selectPreviewSize == null) {
            return false;
        }
        parameters.setPreviewSize(selectPreviewSize.width, selectPreviewSize.height);
        return true;
    }

    private Point getScreenSize() {
        return ImageUtil.getDisplaySize(((Activity) getContext()).getWindowManager().getDefaultDisplay());
    }

    /* access modifiers changed from: protected */
    @SuppressLint({"NewApi"})
    public boolean initializeCamera(int i, int i2) {
        boolean z;
        Log.m52d(TAG, "Initialize java camera");
        synchronized (this) {
            this.mCamera = openBackCamera();
            z = false;
            if (this.mCamera == null) {
                return false;
            }
            try {
                Camera.Parameters parameters = this.mCamera.getParameters();
                Log.m52d(TAG, "getSupportedPreviewSizes()");
                List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
                if (supportedPreviewSizes != null) {
                    parameters.setPreviewFormat(17);
                    if (!setPictureAndPreviewResolutionManualMode(parameters)) {
                        setPreviewSize(supportedPreviewSizes, parameters);
                        setPictureSize(parameters.getPreviewSize(), parameters);
                    }
                    parameters.setJpegQuality(95);
                    int i3 = Build.VERSION.SDK_INT;
                    setForcusMode(parameters);
                    parameters.setRotation(90);
                    this.mCamera.setParameters(parameters);
                    Camera.Parameters parameters2 = this.mCamera.getParameters();
                    this.mFrameWidth = parameters2.getPreviewSize().height;
                    this.mFrameHeight = parameters2.getPreviewSize().width;
                    int i4 = this.mFrameHeight;
                    int i5 = this.mFrameWidth;
                    if (getLayoutParams().width == -1 && getLayoutParams().height == -1) {
                        this.mScale = Math.min(((float) i2) / ((float) this.mFrameHeight), ((float) i) / ((float) this.mFrameWidth));
                    } else {
                        this.mScale = 0.0f;
                    }
                    if (this.mFpsMeter != null) {
                        this.mFpsMeter.setResolution(this.mFrameWidth, this.mFrameHeight);
                    }
                    this.mBuffer = new byte[(((this.mFrameWidth * this.mFrameHeight) * ImageFormat.getBitsPerPixel(parameters2.getPreviewFormat())) / 8)];
                    this.mCamera.addCallbackBuffer(this.mBuffer);
                    this.mCamera.setPreviewCallbackWithBuffer(this);
                    this.mFrameChain = new Mat[2];
                    this.mFrameChain[0] = new Mat((i5 / 2) + i5, i4, CvType.CV_8UC1);
                    this.mFrameChain[1] = new Mat(i5 + (i5 / 2), i4, CvType.CV_8UC1);
                    AllocateCache();
                    this.mCameraFrame = new JavaCameraFrame[2];
                    this.mCameraFrame[0] = new JavaCameraFrame(this.mFrameChain[0], this.mFrameWidth, this.mFrameHeight);
                    this.mCameraFrame[1] = new JavaCameraFrame(this.mFrameChain[1], this.mFrameWidth, this.mFrameHeight);
                    if (Build.VERSION.SDK_INT >= 11) {
                        this.mSurfaceTexture = new SurfaceTexture(10);
                        this.mCamera.setPreviewTexture(this.mSurfaceTexture);
                    } else {
                        this.mCamera.setPreviewDisplay((SurfaceHolder) null);
                    }
                    Log.m52d(TAG, "startPreview");
                    this.mCamera.startPreview();
                    z = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return z;
    }

    private static Camera openBackCamera() {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera camera = null;
        boolean z = false;
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == 0) {
                try {
                    camera = Camera.open(i);
                    z = true;
                } catch (RuntimeException e) {
                    Log.m53e(TAG, "Camera #" + i + "failed to open: " + e.getMessage());
                }
                if (z) {
                    return camera;
                }
            }
        }
        return null;
    }

    public boolean checkBackCamera() {
        disconnectCamera();
        Camera openBackCamera = openBackCamera();
        if (openBackCamera == null) {
            return false;
        }
        openBackCamera.release();
        return true;
    }

    private void setForcusMode(Camera.Parameters parameters) {
        boolean autoPictureMode = CameraPreviewControl.getInstance(getContext()).getAutoPictureMode();
        List<String> supportedFocusModes = parameters.getSupportedFocusModes();
        if (supportedFocusModes != null) {
            if (autoPictureMode || !supportedFocusModes.contains("auto")) {
                for (String str : FAVORITE_FOCUS_MODES) {
                    if (supportedFocusModes.contains(str)) {
                        parameters.setFocusMode(str);
                        return;
                    }
                }
                return;
            }
            parameters.setFocusMode("auto");
        }
    }

    private Point OLDselectPreviewSizeFromPictureSize(double d, List<Camera.Size> list) {
        int i;
        if (list == null || list.size() <= 0 || d <= 0.0d) {
            return null;
        }
        Point point = new Point();
        for (Camera.Size next : list) {
            if (next.height * next.width >= 76800 && Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d && next.width > point.x) {
                point.x = next.width;
                point.y = next.height;
            }
        }
        if (point.x > 0) {
            return point;
        }
        int i2 = 0;
        Camera.Size size = list.get(0);
        point.x = size.width;
        point.y = size.height;
        double d2 = Double.MAX_VALUE;
        for (Camera.Size next2 : list) {
            if (next2.height > 0) {
                double abs = Math.abs((((double) next2.width) / ((double) next2.height)) - d);
                if (d2 > abs + 0.05d) {
                    i2 = next2.width * next2.height;
                    point.x = next2.width;
                    point.y = next2.height;
                    d2 = abs;
                } else if (d2 > abs - 0.05d && i2 < (i = next2.width * next2.height)) {
                    point.x = next2.width;
                    point.y = next2.height;
                    d2 = abs;
                    i2 = i;
                }
            }
        }
        return point;
    }

    private SizeWrapper selectPreviewSizeFromPictureSize(double d, List<Camera.Size> list) {
        if (list == null || d <= 0.0d) {
            return null;
        }
        CameraSizeList cameraSizeList = new CameraSizeList(list);
        int height = getHeight();
        int width = getWidth();
        SizeWrapper selectSize01 = PreviewResolutionSelector.selectSize01(d, cameraSizeList, height, width);
        if (selectSize01 != null) {
            return selectSize01;
        }
        return PreviewResolutionSelector.selectSize02(d, cameraSizeList, height, width);
    }

    private boolean setPictureAndPreviewResolutionManualMode(Camera.Parameters parameters) {
        CameraPreviewControl instance = CameraPreviewControl.getInstance(getContext());
        if (instance.getPictureResolutionMode() != 1) {
            return false;
        }
        Point pictureSize = instance.getPictureSize();
        if (pictureSize.x <= 0 || pictureSize.y <= 0) {
            return false;
        }
        parameters.setPictureSize(pictureSize.x, pictureSize.y);
        SizeWrapper selectPreviewSizeFromPictureSize = selectPreviewSizeFromPictureSize(((double) pictureSize.x) / ((double) pictureSize.y), parameters.getSupportedPreviewSizes());
        if (selectPreviewSizeFromPictureSize != null) {
            parameters.setPreviewSize(selectPreviewSizeFromPictureSize.getWidth(), selectPreviewSizeFromPictureSize.getHeight());
        }
        return true;
    }

    static void setPictureSize(Camera.Size size, Camera.Parameters parameters) {
        Camera.Size pictureSize01 = setPictureSize01(parameters.getSupportedPictureSizes(), ((double) size.width) / ((double) size.height));
        if (pictureSize01 != null) {
            parameters.setPictureSize(pictureSize01.width, pictureSize01.height);
        } else {
            setPictureSize02(size, parameters);
        }
    }

    static Camera.Size setPictureSize01(List<Camera.Size> list, double d) {
        int i = 0;
        Camera.Size size = null;
        for (Camera.Size next : list) {
            if (Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d && i < next.width) {
                i = next.width;
                size = next;
            }
        }
        return size;
    }

    static void setPictureSize02(Camera.Size size, Camera.Parameters parameters) {
        List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
        ArrayList arrayList = new ArrayList(supportedPictureSizes.size());
        for (Camera.Size next : supportedPictureSizes) {
            arrayList.add(new Point(next.width, next.height));
        }
        Point selectSize = selectSize(new Point(size.width, size.height), arrayList);
        if (selectSize != null) {
            parameters.setPictureSize(selectSize.x, selectSize.y);
        }
    }

    static Point selectSize(Point point, List<Point> list) {
        double d = ((double) point.x) / ((double) point.y);
        if (list == null) {
            return null;
        }
        double[] dArr = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Point point2 = list.get(i);
            dArr[i] = ((double) point2.x) * ((double) point2.y) * getRate(d, ((double) point2.x) / ((double) point2.y));
        }
        int maxIndex = getMaxIndex(dArr);
        if (maxIndex < 0 || maxIndex >= dArr.length) {
            return null;
        }
        return list.get(maxIndex);
    }

    static int getMaxIndex(double[] dArr) {
        if (dArr == null || dArr.length < 1) {
            return -1;
        }
        double d = dArr[0];
        int i = 0;
        for (int i2 = 0; i2 < dArr.length; i2++) {
            double d2 = dArr[i2];
            if (d < d2) {
                i = i2;
                d = d2;
            }
        }
        return i;
    }

    static double getRate(double d, double d2) {
        if (d <= 0.0d || d2 <= 0.0d) {
            return 1.0d;
        }
        double d3 = d2 / d;
        if (d3 > 1.0d) {
            d3 = 1.0d / d3;
        }
        return 1.0d / (Math.pow((d3 - 1.0d) * 10.0d, 2.0d) + 1.0d);
    }

    public Camera.Size getCameraPictureSize() {
        Camera camera = this.mCamera;
        if (camera == null) {
            return null;
        }
        try {
            Camera.Parameters parameters = camera.getParameters();
            if (parameters != null) {
                return parameters.getPictureSize();
            }
            return null;
        } catch (Exception unused) {
            return null;
        }
    }

    public List<Camera.Size> getPictureSizeList() {
        Camera camera = this.mCamera;
        if (camera == null) {
            return null;
        }
        try {
            Camera.Parameters parameters = camera.getParameters();
            if (parameters == null) {
                return null;
            }
            return parameters.getSupportedPictureSizes();
        } catch (Exception unused) {
            return null;
        }
    }

    protected void releaseCamera() {
        synchronized (this) {
            if (this.mCamera != null) {
                this.mCamera.stopPreview();
                this.mCamera.setPreviewCallback((Camera.PreviewCallback) null);
                this.mCamera.release();
            }
            this.mCamera = null;
            if (this.mFrameChain != null) {
                this.mFrameChain[0].release();
                this.mFrameChain[1].release();
            }
            if (this.mCameraFrame != null) {
                this.mCameraFrame[0].release();
                this.mCameraFrame[1].release();
            }
        }
    }

    protected boolean connectCamera(int i, int i2) {
        Log.m52d(TAG, "Connecting to camera");
        if (!initializeCamera(i, i2)) {
            return false;
        }
        this.mCameraFrameReady = false;
        Log.m52d(TAG, "Starting processing thread");
        this.mStopThread = false;
        this.mThread = new Thread(new CameraWorker(), "CameraWorker");
        this.mThread.start();
        return true;
    }

    protected void disconnectCamera() {
        Log.m52d(TAG, "Disconnecting from camera");
        try {
            this.mStopThread = true;
            Log.m52d(TAG, "Notify thread");
            synchronized (this) {
                notify();
            }
            Log.m52d(TAG, "Wating for thread");
            if (this.mThread != null) {
                this.mThread.join();
            }
        } catch (InterruptedException e) {
            try {
                e.printStackTrace();
            } catch (Throwable th) {
                this.mThread = null;
                throw th;
            }
        }
        this.mThread = null;
        releaseCamera();
        this.mCameraFrameReady = false;
    }

    public void onPreviewFrame(byte[] bArr, Camera camera) {
        synchronized (this) {
            this.mFrameChain[this.mChainIdx].put(0, 0, bArr);
            this.mCameraFrameReady = true;
            notify();
        }
        Camera camera2 = this.mCamera;
        if (camera2 != null) {
            camera2.addCallbackBuffer(this.mBuffer);
        }
    }

    private class JavaCameraFrame implements CameraBridgeViewBase.CvCameraViewFrame {
        private int mHeight;
        private Mat mRgba = new Mat();
        private Mat mRotated;
        private int mWidth;
        private Mat mYuvFrameData;

        public Mat gray() {
            Mat mat = this.mRotated;
            if (mat != null) {
                mat.release();
            }
            this.mRotated = this.mYuvFrameData.submat(0, this.mWidth, 0, this.mHeight);
            this.mRotated = this.mRotated.mo42753t();
            Mat mat2 = this.mRotated;
            Core.flip(mat2, mat2, 1);
            return this.mRotated;
        }

        public Mat rgba() {
            Imgproc.cvtColor(this.mYuvFrameData, this.mRgba, 96, 4);
            Mat mat = this.mRotated;
            if (mat != null) {
                mat.release();
            }
            this.mRotated = this.mRgba.mo42753t();
            Mat mat2 = this.mRotated;
            Core.flip(mat2, mat2, 1);
            return this.mRotated;
        }

        public JavaCameraFrame(Mat mat, int i, int i2) {
            this.mWidth = i;
            this.mHeight = i2;
            this.mYuvFrameData = mat;
        }

        public void release() {
            this.mRgba.release();
            Mat mat = this.mRotated;
            if (mat != null) {
                mat.release();
            }
        }
    }

    private class CameraWorker implements Runnable {
        private CameraWorker() {
        }

        public void run() {
            boolean z;
            do {
                synchronized (PortraitCameraView.this) {
                    while (!PortraitCameraView.this.mCameraFrameReady && !PortraitCameraView.this.mStopThread) {
                        try {
                            PortraitCameraView.this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    z = false;
                    if (PortraitCameraView.this.mCameraFrameReady) {
                        int unused = PortraitCameraView.this.mChainIdx = 1 - PortraitCameraView.this.mChainIdx;
                        boolean unused2 = PortraitCameraView.this.mCameraFrameReady = false;
                        z = true;
                    }
                }
                if (!PortraitCameraView.this.mStopThread && z && !PortraitCameraView.this.mFrameChain[1 - PortraitCameraView.this.mChainIdx].empty()) {
                    PortraitCameraView portraitCameraView = PortraitCameraView.this;
                    portraitCameraView.deliverAndDrawFrame(portraitCameraView.mCameraFrame[1 - PortraitCameraView.this.mChainIdx]);
                }
            } while (!PortraitCameraView.this.mStopThread);
            Log.m52d(PortraitCameraView.TAG, "Finish processing thread");
        }
    }
}
