package a.c.d.d;

import a.c.b.c;
import a.c.b.f;
import a.c.b.i;
import a.c.b.k;
import a.c.b.s;
import a.c.b.z.e;
import android.content.Context;
import android.util.Range;
import android.util.Size;
import com.ucar.databus.proto.UCarProto;
import com.ucar.vehiclesdk.ICameraInfoListener;
import com.ucar.vehiclesdk.UCarCommon;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

public class b {
    private static final String f = "Vehicle_CameraManager";
    private static c g;

    /* renamed from: a  reason: collision with root package name */
    private ICameraInfoListener f102a = null;
    private a.b.a.g.a b = null;
    private Future<Boolean> c = null;
    private AtomicBoolean d = new AtomicBoolean(false);
    private AtomicBoolean e = new AtomicBoolean(false);

    public class a extends a.b.a.g.a {
        public a(a.c.b.z.b bVar, boolean z, boolean z2) {
            super(bVar, z, z2);
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void a(boolean z) {
            a.a.a.b.a(b.f, "camera channel closed.");
            b.this.e.set(false);
            b.this.b();
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void h() {
            a.a.a.b.a(b.f, "camera channel ready.");
            b.this.e.set(true);
        }
    }

    /* renamed from: a.c.d.d.b$b  reason: collision with other inner class name */
    public class C0011b implements e {
        public C0011b() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            if (!bool.booleanValue()) {
                a.a.a.b.a(b.f, "send camera data failed");
            }
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(b.f, "send camera data error", exc);
            b.this.e.set(false);
            b.this.b();
        }
    }

    public b(Context context) {
        g = c.a(context);
    }

    public static UCarProto.NotifyAddCamera a(UCarCommon.CameraInfo cameraInfo) {
        try {
            Size[] supportedSizes = cameraInfo.getSupportedSizes();
            Range<Integer>[] fpsRanges = cameraInfo.getFpsRanges();
            UCarProto.NotifyAddCamera.Builder maxSize = UCarProto.NotifyAddCamera.newBuilder().setCameraId(cameraInfo.getId()).setName(cameraInfo.getName()).setLensFacingValue(cameraInfo.getLensFacing().getValue()).setOrientationValue(UCarCommon.Orientation.ORIENTATION_0.getValue()).setMaxSize((UCarProto.PictureSize) UCarProto.PictureSize.newBuilder().setWidth(supportedSizes[0].getWidth()).setHeight(supportedSizes[0].getHeight()).build());
            for (Size size : supportedSizes) {
                maxSize.addSupportedSizes((UCarProto.PictureSize) UCarProto.PictureSize.newBuilder().setWidth(size.getWidth()).setHeight(size.getHeight()).build());
            }
            for (Range<Integer> range : fpsRanges) {
                maxSize.addFpsRanges((UCarProto.FpsRange) UCarProto.FpsRange.newBuilder().setMax(range.getUpper().intValue()).setMin(range.getLower().intValue()).build());
            }
            return (UCarProto.NotifyAddCamera) maxSize.build();
        } catch (Exception e2) {
            a.a.a.b.b(f, "createNotifyAddMessage" + e2.getMessage());
            return null;
        }
    }

    public static UCarProto.NotifyRemoveCamera a(String[] strArr) {
        try {
            return (UCarProto.NotifyRemoveCamera) UCarProto.NotifyRemoveCamera.newBuilder().addAllIds(Arrays.asList(strArr)).build();
        } catch (Exception e2) {
            a.a.a.b.b(f, "createNotifyRemoveMessage" + e2.getMessage());
            return null;
        }
    }

    private void a(ByteBuffer byteBuffer, UCarProto.VideoType videoType, short s) {
        s s2 = s.s();
        s2.g().a(byteBuffer.remaining() + 20, s, k.c(), f.RAW, i.SEND, c.VIDEO, videoType.getNumber());
        s2.a(byteBuffer, byteBuffer.remaining());
        a.b.a.g.a aVar = this.b;
        if (aVar != null) {
            aVar.a(s2, (e) new C0011b());
        }
    }

    private boolean c() {
        if (this.c != null && !a()) {
            try {
                this.c.get();
                this.c = null;
            } catch (Exception e2) {
                a.a.a.b.b(f, "Wait start camera channel error.", e2);
            }
        }
        return a();
    }

    public void a(int i) {
        ICameraInfoListener iCameraInfoListener;
        a.a.a.b.a(f, "onConnectStateChanged state= " + i);
        if (i == 2) {
            c cVar = g;
            if (!(cVar == null || (iCameraInfoListener = this.f102a) == null)) {
                cVar.a(iCameraInfoListener.getAndroidCameraInfo());
                g.b(this.f102a.getNativeCamera());
            }
            this.d.set(true);
        } else if (i == 1) {
            this.d.set(false);
            b();
            c cVar2 = g;
            if (cVar2 != null) {
                cVar2.c();
            }
        }
    }

    public void a(ICameraInfoListener iCameraInfoListener) {
        a.a.a.b.a(f, "registerInfoListener");
        this.f102a = iCameraInfoListener;
        if (this.d.get() && this.f102a != null && g != null) {
            a.a.a.b.a(f, "registerInfoListener: register cameras");
            g.a(this.f102a.getAndroidCameraInfo());
            g.b(this.f102a.getNativeCamera());
        }
    }

    public synchronized void a(String str) {
        if (this.b == null) {
            this.b = new a(a.c.b.z.b.VIDEO, false, false);
        }
        if (this.c == null) {
            a.a.a.b.a(f, "startCameraChannel start address:" + str);
            try {
                this.b.b(5);
                this.c = this.b.a(0, str);
            } catch (IOException e2) {
                a.a.a.b.b(f, "start camera channel error.", e2);
            }
        }
    }

    public void a(String str, UCarCommon.CameraAction cameraAction, UCarCommon.CameraActionArgs cameraActionArgs) {
        c cVar = g;
        if (cVar != null) {
            cVar.a(cameraActionArgs.getCameraId(), cameraAction, cameraActionArgs.getPictureSize(), cameraActionArgs.getFpsRange());
        }
    }

    public boolean a() {
        a.b.a.g.a aVar;
        return this.e.get() && (aVar = this.b) != null && aVar.b();
    }

    public boolean a(UCarCommon.VideoType videoType, ByteBuffer byteBuffer, short s) {
        if (!c()) {
            return false;
        }
        a.a.a.b.a(f, "sendCameraData len = " + byteBuffer.remaining() + ", frameNumber=" + ((int) s));
        a(byteBuffer, UCarProto.VideoType.forNumber(videoType.getValue()), s);
        return true;
    }

    public void b() {
        a.a.a.b.a(f, "stopCameraChannel");
        a.b.a.g.a aVar = this.b;
        if (aVar != null) {
            aVar.i();
            this.b = null;
        }
        this.c = null;
    }

    public void b(ICameraInfoListener iCameraInfoListener) {
        c cVar;
        a.a.a.b.a(f, "unRegisterInfoListener");
        if (this.d.get() && (cVar = g) != null) {
            cVar.c();
        }
        this.f102a = null;
    }
}
