package a.c.d.d;

import a.c.d.d.c;
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.os.Handler;
import android.util.Range;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceHolder;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucar.vehiclesdk.camera.AbstractCamera;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;

public class a implements AbstractCamera {
    private static final String j = "BuiltInCamera";

    /* renamed from: a  reason: collision with root package name */
    private final CameraManager f99a;
    private final String b;
    private final String c;
    private CameraDevice d = null;
    private CaptureRequest.Builder e = null;
    private CameraCaptureSession f = null;
    private Surface g;
    private final AtomicBoolean h = new AtomicBoolean(false);
    private final CameraCaptureSession.StateCallback i = new b();

    /* renamed from: a.c.d.d.a$a  reason: collision with other inner class name */
    public class C0010a extends CameraDevice.StateCallback {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ c.e f100a;

        public C0010a(c.e eVar) {
            this.f100a = eVar;
        }

        public void onClosed(CameraDevice cameraDevice) {
            a.this.h.set(false);
            a.this.d = null;
            this.f100a.c();
        }

        public void onDisconnected(CameraDevice cameraDevice) {
            a.this.h.set(false);
            a.this.d = null;
            this.f100a.a();
        }

        public void onError(CameraDevice cameraDevice, int i) {
            a.this.h.set(false);
            a.this.d = null;
            this.f100a.a(i, "");
        }

        public void onOpened(CameraDevice cameraDevice) {
            a.this.h.set(true);
            a.this.d = cameraDevice;
            this.f100a.b();
        }
    }

    public class b extends CameraCaptureSession.StateCallback {
        public b() {
        }

        public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
            a.a.a.b.b(a.j, "onConfigureFailed");
            a.this.h.set(false);
        }

        public void onConfigured(CameraCaptureSession cameraCaptureSession) {
            if (!a.this.h.get()) {
                a.a.a.b.a(a.j, "onConfigured, camera " + a.this.b + " has closed");
                return;
            }
            a.this.f = cameraCaptureSession;
            try {
                a.this.f.setRepeatingRequest(a.this.e.build(), null, null);
            } catch (CameraAccessException | IllegalStateException e) {
                a.this.h.set(false);
                a.a.a.b.b(a.j, "onConfigured, setRepeatingRequest: " + e.getMessage());
            }
        }
    }

    public a(CameraManager cameraManager, String str, String str2) {
        this.f99a = cameraManager;
        this.b = str;
        this.c = str2;
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public void changeConfiguration(Surface surface, Size size, Range<Integer> range) {
        CameraCaptureSession cameraCaptureSession = this.f;
        if (cameraCaptureSession != null) {
            try {
                cameraCaptureSession.stopRepeating();
            } catch (CameraAccessException e2) {
                a.a.a.b.b(j, "changeConfiguration, stopRepeating: " + e2.getMessage());
            }
        }
        CaptureRequest.Builder builder = this.e;
        if (builder != null && this.d != null) {
            builder.removeTarget(this.g);
            this.g = surface;
            this.e.addTarget(surface);
            try {
                this.d.createCaptureSession(Collections.singletonList(surface), this.i, null);
            } catch (CameraAccessException e3) {
                this.h.set(false);
                a.a.a.b.b(j, "changeConfiguration, createCaptureSession: " + e3.getMessage());
            }
        }
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public void close() {
        a.a.a.b.a(j, "close");
        this.h.set(false);
        CameraCaptureSession cameraCaptureSession = this.f;
        if (cameraCaptureSession != null) {
            try {
                cameraCaptureSession.stopRepeating();
                this.f = null;
            } catch (CameraAccessException | IllegalStateException e2) {
                a.a.a.b.b(j, "close, stopRepeating: " + e2.getMessage());
            }
        }
        CameraDevice cameraDevice = this.d;
        if (cameraDevice != null) {
            cameraDevice.close();
            this.d = null;
        }
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public UCarCommon.CameraInfo getInfo() throws Exception {
        CameraCharacteristics cameraCharacteristics = this.f99a.getCameraCharacteristics(this.b);
        int intValue = ((Integer) cameraCharacteristics.get(CameraCharacteristics.LENS_FACING)).intValue();
        if (intValue == 0) {
            intValue = 1;
        } else if (intValue == 1) {
            intValue = 0;
        }
        return new UCarCommon.CameraInfo(this.b, this.c, UCarCommon.LensFacing.fromInt(intValue), UCarCommon.Orientation.fromInt(((Integer) cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)).intValue()), ((StreamConfigurationMap) cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)).getOutputSizes(SurfaceHolder.class), (Range[]) cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES));
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public void open(c.e eVar) {
        a.a.a.b.a(j, "open");
        try {
            this.f99a.openCamera(this.b, new C0010a(eVar), (Handler) null);
        } catch (CameraAccessException | IllegalArgumentException e2) {
            this.h.set(false);
            a.a.a.b.b(j, "open, openCamera: " + e2.getMessage());
            eVar.a(-1, e2.getMessage());
        }
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public void startPreview(Surface surface, Range<Integer> range) {
        try {
            CaptureRequest.Builder createCaptureRequest = this.d.createCaptureRequest(1);
            this.e = createCaptureRequest;
            createCaptureRequest.set(CaptureRequest.CONTROL_AF_MODE, 3);
            this.e.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, range);
            this.e.addTarget(surface);
            this.g = surface;
            this.d.createCaptureSession(Collections.singletonList(surface), this.i, null);
        } catch (CameraAccessException e2) {
            this.h.set(false);
            a.a.a.b.b(j, "startPreview, createCaptureSession: " + e2.getMessage());
        }
    }

    @Override // com.ucar.vehiclesdk.camera.AbstractCamera
    public void takePicture() {
        a.a.a.b.e(j, "This feature is not implemented yet");
    }
}
