package com.epson.cameracopy.device;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.util.AttributeSet;

public class OpenCvCameraView extends PortraitCameraView implements Camera.PictureCallback, Camera.ShutterCallback, Camera.AutoFocusCallback {
    private static final String TAG = "OpenCvCameraView";
    private AutoFocusCallBack mAutoFocusCallBack;
    private boolean mHasAutoFocus;
    private volatile boolean mInAutofocus;
    private String mPictureFileName;
    private MyPictureTakeCallback mPictureTakeCallback;
    private boolean mPictureTaking;
    private boolean mTakePictuerAutofocus;

    public interface AutoFocusCallBack {
        void onAutoFocusCompleted(boolean z);
    }

    public interface MyPictureTakeCallback {
        void onMyPictureTaken(boolean z, String str);
    }

    public void onShutter() {
    }

    public void setDisplayOriantation(int i) {
    }

    public boolean isInAutofocus() {
        return mInAutofocus;
    }

    public OpenCvCameraView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            mHasAutoFocus = packageManager.hasSystemFeature("android.hardware.camera.autofocus");
        }
    }

    public Camera getCamera() {
        return mCamera;
    }


    public void onPictureTaken(byte[] r4, android.hardware.Camera r5) {

        throw new UnsupportedOperationException("Method not decompiled: com.epson.cameracopy.device.OpenCvCameraView.onPictureTaken(byte[], android.hardware.Camera):void");
    }

    public synchronized void takePicture(String str, MyPictureTakeCallback myPictureTakeCallback) {
        mPictureTaking = true;
        if (!mInAutofocus) {
            mInAutofocus = true;
            mPictureFileName = str;
            mPictureTakeCallback = myPictureTakeCallback;
            if (!mHasAutoFocus) {
                mCamera.setPreviewCallback((Camera.PreviewCallback) null);
                mCamera.takePicture(this, (Camera.PictureCallback) null, this);
                mPictureTaking = false;
                mInAutofocus = false;
                return;
            }
            mTakePictuerAutofocus = true;
            localAutofocus();
        }
    }

    private void localAutofocus() {
        if (!mHasAutoFocus) {
            mInAutofocus = false;
            return;
        }
        try {
            mCamera.autoFocus(this);
        } catch (Exception unused) {
            onAutoFocus(false, mCamera);
        }
    }

    private void localCancelAutofocus() {
        if (!mHasAutoFocus) {
            mInAutofocus = false;
        } else if (mCamera != null) {
            mCamera.cancelAutoFocus();
        }
    }

    public void onAutoFocus(boolean z, Camera camera) {
        if (mTakePictuerAutofocus) {
            mTakePictuerAutofocus = false;
            mCamera.setPreviewCallback((Camera.PreviewCallback) null);
            mCamera.takePicture(this, (Camera.PictureCallback) null, this);
        } else {
            AutoFocusCallBack autoFocusCallBack = mAutoFocusCallBack;
            if (autoFocusCallBack != null) {
                autoFocusCallBack.onAutoFocusCompleted(z);
                mAutoFocusCallBack = null;
            }
        }
        mInAutofocus = false;
    }

    public synchronized void doAutofocus(AutoFocusCallBack autoFocusCallBack) {
        if (!mInAutofocus && !mPictureTaking) {
            mAutoFocusCallBack = autoFocusCallBack;
            mInAutofocus = true;
            localAutofocus();
        }
    }

    public void cancelAutofocus() {
        localCancelAutofocus();
        mInAutofocus = false;
        mAutoFocusCallBack = null;
        mTakePictuerAutofocus = false;
        mPictureTaking = false;
        mPictureTakeCallback = null;
    }

    public void enableView() {
        super.enableView();
        mInAutofocus = false;
        mPictureTaking = false;
    }

    public float getScale() {
        if (mScale <= 0.0f) {
            return 1.0f;
        }
        return mScale;
    }
}
