package a.c.d.d;

import a.c.d.d.e;
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.Range;
import android.util.Size;
import android.view.Surface;
import androidx.core.app.ActivityCompat;
import com.ucar.vehiclesdk.UCarAdapter;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucar.vehiclesdk.camera.AbstractCamera;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class c {
    private static final String g = "CameraProvider";
    private static final String h = "1";
    private static final String i = "Car_Camera_1";
    private static volatile c j;
    private static short k;
    private static CameraManager l;

    /* renamed from: a  reason: collision with root package name */
    private Context f104a;
    private final Map<String, AbstractCamera> b = new ConcurrentHashMap();
    private e c = null;
    private String d = null;
    private String e = null;
    private final e.a f = new b();

    public class a extends CameraManager.AvailabilityCallback {
        public a() {
        }

        public void onCameraAvailable(String str) {
            a.a.a.b.a(c.g, "onCameraAvailable " + str);
            if (str.equals(c.this.e)) {
                c.this.e = null;
            } else if (str.equals(c.this.d)) {
                UCarAdapter.getInstance().notifyCameraStateChanged(str, UCarCommon.CameraState.CAMERA_STATE_PREEMPTED);
            }
        }

        public void onCameraUnavailable(String str) {
            a.a.a.b.a(c.g, "onCameraUnavailable " + str);
        }
    }

    public class b implements e.a {
        public b() {
        }

        @Override // a.c.d.d.e.a
        public void a(String str) {
            UCarAdapter.getInstance().notifyCameraStateChanged(c.this.d, UCarCommon.CameraState.CAMERA_STATE_ERROR);
        }

        @Override // a.c.d.d.e.a
        public void a(ByteBuffer byteBuffer, int i) {
            UCarAdapter.getInstance().sendCameraData(UCarCommon.VideoType.STREAM_CAMERA_PREVIEW, byteBuffer, c.a());
        }
    }

    /* renamed from: a.c.d.d.c$c  reason: collision with other inner class name */
    public class C0012c implements e {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ String f107a;
        public final /* synthetic */ Size b;
        public final /* synthetic */ Range c;
        public final /* synthetic */ AbstractCamera d;

        public C0012c(String str, Size size, Range range, AbstractCamera abstractCamera) {
            this.f107a = str;
            this.b = size;
            this.c = range;
            this.d = abstractCamera;
        }

        @Override // a.c.d.d.c.e
        public void a() {
            a.a.a.b.a(c.g, "onDisconnected");
            c.this.a(this.f107a);
        }

        @Override // a.c.d.d.c.e
        public void a(int i, String str) {
            a.a.a.b.a(c.g, "onError");
            UCarAdapter.getInstance().notifyCameraStateChanged(this.f107a, UCarCommon.CameraState.CAMERA_STATE_ERROR);
            c.this.a(this.f107a);
        }

        @Override // a.c.d.d.c.e
        public void b() {
            a.a.a.b.a(c.g, "onOpened");
            UCarAdapter.getInstance().notifyCameraStateChanged(this.f107a, UCarCommon.CameraState.CAMERA_STATE_OPENED);
            c.this.c = new e(this.b, ((Integer) this.c.getUpper()).intValue(), c.this.f);
            Surface a2 = c.this.c.a();
            if (a2 != null) {
                this.d.startPreview(a2, this.c);
                c.this.c.c();
            }
        }

        @Override // a.c.d.d.c.e
        public void c() {
            a.a.a.b.a(c.g, "onClosed");
            UCarAdapter.getInstance().notifyCameraStateChanged(this.f107a, UCarCommon.CameraState.CAMERA_STATE_CLOSED);
        }
    }

    public static /* synthetic */ class d {

        /* renamed from: a  reason: collision with root package name */
        public static final /* synthetic */ int[] f108a;

        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x000e */
        static {
            UCarCommon.CameraAction.values();
            int[] iArr = new int[4];
            f108a = iArr;
            UCarCommon.CameraAction cameraAction = UCarCommon.CameraAction.CAMERA_OPEN;
            iArr[0] = 1;
            try {
                int[] iArr2 = f108a;
                UCarCommon.CameraAction cameraAction2 = UCarCommon.CameraAction.CAMERA_CLOSE;
                iArr2[2] = 2;
            } catch (NoSuchFieldError unused) {
            }
        }
    }

    public interface e {
        void a();

        void a(int i, String str);

        void b();

        void c();
    }

    public c(Context context) {
        this.f104a = context;
        l = (CameraManager) context.getSystemService("camera");
        a.a.a.b.a(g, "CarCameraProvider getSystemService: context=" + context);
        l.registerAvailabilityCallback(new a(), (Handler) null);
    }

    public static synchronized c a(Context context) {
        c cVar;
        synchronized (c.class) {
            if (j == null) {
                j = new c(context);
            }
            cVar = j;
        }
        return cVar;
    }

    public static /* synthetic */ short a() {
        short s = k;
        k = (short) (s + 1);
        return s;
    }

    public void a(AbstractCamera abstractCamera) {
        try {
            UCarAdapter.getInstance().addCamera(abstractCamera.getInfo());
            this.b.put(abstractCamera.getInfo().getId(), abstractCamera);
        } catch (Exception e2) {
            a.a.a.b.b(g, "addCamera" + e2.getMessage());
        }
    }

    public synchronized void a(String str) {
        AbstractCamera abstractCamera;
        if (str != null) {
            this.e = str;
            this.d = null;
            k = 0;
            if (this.b.containsKey(str) && (abstractCamera = this.b.get(str)) != null) {
                abstractCamera.close();
            }
            e eVar = this.c;
            if (eVar != null) {
                eVar.d();
                this.c = null;
            }
        }
    }

    public synchronized void a(String str, Size size, Range<Integer> range) {
        AbstractCamera abstractCamera;
        a.a.a.b.a(g, "openCamera cameraId=" + str + ", width=" + size.getWidth() + ", height=" + size.getHeight() + ", fpsRange=(" + range.getLower() + ", " + range.getUpper() + ")");
        if (TextUtils.isEmpty(str.trim())) {
            a.a.a.b.e(g, "openCamera: cameraId should never be empty");
            return;
        }
        AbstractCamera abstractCamera2 = this.b.get(str);
        if (abstractCamera2 == null) {
            a.a.a.b.e(g, "openCamera: camera:" + str + " is not available");
        } else if (str.equals(this.d)) {
            a.a.a.b.a(g, "openCamera: already exist, configure camera:" + str);
            e eVar = this.c;
            if (eVar != null) {
                eVar.d();
                this.c = null;
            }
            e eVar2 = new e(size, range.getUpper().intValue(), this.f);
            this.c = eVar2;
            Surface a2 = eVar2.a();
            if (a2 != null) {
                abstractCamera2.changeConfiguration(a2, size, range);
                this.c.c();
            }
        } else {
            if (!TextUtils.isEmpty(this.d) && (abstractCamera = this.b.get(this.d)) != null) {
                abstractCamera.close();
            }
            k = 0;
            abstractCamera2.open(new C0012c(str, size, range, abstractCamera2));
            this.d = str;
        }
    }

    public void a(String str, UCarCommon.CameraAction cameraAction, Size size, Range<Integer> range) {
        Log.d(g, "onCarCameraRequest action = " + cameraAction);
        int ordinal = cameraAction.ordinal();
        if (ordinal != 0) {
            if (ordinal == 2) {
                a(str);
            }
        } else if (ActivityCompat.checkSelfPermission(this.f104a, "android.permission.CAMERA") != 0) {
            UCarAdapter.getInstance().notifyCameraStateChanged(str, UCarCommon.CameraState.CAMERA_STATE_NO_PERMISSION);
        } else {
            a(str, size, range);
        }
    }

    public void a(ArrayList<UCarCommon.CameraInfo> arrayList) {
        a.a.a.b.a(g, "registerCarCameras: register android builtin Cameras");
        Iterator<UCarCommon.CameraInfo> it = arrayList.iterator();
        while (it.hasNext()) {
            UCarCommon.CameraInfo next = it.next();
            String id = next.getId();
            if (!this.b.containsKey(id)) {
                a(new a(l, id, next.getName()));
            }
        }
    }

    public void b() {
        a(this.d);
        this.b.clear();
    }

    public void b(AbstractCamera abstractCamera) {
        try {
            UCarAdapter.getInstance().removeCamera(new String[]{abstractCamera.getInfo().getId()});
            this.b.remove(abstractCamera.getInfo().getId());
        } catch (Exception e2) {
            a.a.a.b.b(g, "removeCamera" + e2.getMessage());
        }
    }

    public void b(ArrayList<AbstractCamera> arrayList) {
        a.a.a.b.e(g, "registerExternalCarCameras: This feature is not implemented yet");
    }

    public void c() {
        try {
            a.a.a.b.a(g, "unregister all Cameras");
            UCarAdapter.getInstance().removeCamera((String[]) this.b.keySet().toArray(new String[this.b.size()]));
            b();
        } catch (Exception e2) {
            a.a.a.b.b(g, "unregisterCameras" + e2.getMessage());
        }
    }

    public void finalize() throws Throwable {
        try {
            b();
        } finally {
            super.finalize();
        }
    }
}
