package com.facetec.sdk; import android.app.Activity; import android.content.Context; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.RectF; import android.graphics.SurfaceTexture; 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.Build; import android.os.Handler; import android.os.HandlerThread; import android.renderscript.Allocation; import android.util.Size; import android.view.Surface; import android.view.TextureView; import android.view.View; import android.view.ViewGroup; import androidx.core.app.ActivityCompat; import com.facetec.sdk.Cdo; import com.facetec.sdk.ae; import com.facetec.sdk.ah; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public final class af extends ah { static final boolean s = true; private Handler D; private final TextureView.SurfaceTextureListener I; final aj k; final g m; final WeakReference n; /* renamed from: o, reason: collision with root package name */ CameraDevice f6403o; Cdo p; CaptureRequest.Builder q; CameraCaptureSession r; private String u; private CameraCharacteristics v; private int w; private HandlerThread y; static final String[] f = {"Pixel 6", "Surface Duo 2", "2203129G"}; static Surface t = null; private static Boolean A = null; private static Boolean B = null; private final Cdo.e x = new Cdo.e(this) { // from class: com.facetec.sdk.af$$ExternalSyntheticLambda0 public final af f$0; @Override // com.facetec.sdk.Cdo.e public final void onImageAvailable(byte[] bArr, Size size) { this.f$0.d(bArr, size); } { this.f$0 = this; } }; final Semaphore l = new Semaphore(1); private boolean z = false; private boolean C = false; private final CameraDevice.StateCallback E = new CameraDevice.StateCallback(this) { // from class: com.facetec.sdk.af.2 private af d; { this.d = this; } @Override // android.hardware.camera2.CameraDevice.StateCallback public final void onOpened(CameraDevice cameraDevice) { aw.e("CTOT"); af afVar = this.d; aw.c("CTCPT"); afVar.f6403o = cameraDevice; try { try { if (afVar.p != null) { SurfaceTexture surfaceTexture = afVar.m.getSurfaceTexture(); if (!af.s && surfaceTexture == null) { throw new AssertionError(); } surfaceTexture.setDefaultBufferSize(afVar.k.e, afVar.k.c); Surface surface = new Surface(surfaceTexture); Surface surface2 = afVar.p.a.getSurface(); afVar.q = afVar.f6403o.createCaptureRequest(1); afVar.q.addTarget(surface); afVar.q.addTarget(surface2); if (af.t != null) { afVar.q.addTarget(af.t); } afVar.q.set(CaptureRequest.CONTROL_MODE, 1); if (afVar.e(CameraCharacteristics.CONTROL_AVAILABLE_SCENE_MODES, 11)) { afVar.q.set(CaptureRequest.CONTROL_SCENE_MODE, 11); } afVar.q.set(CaptureRequest.CONTROL_AWB_LOCK, Boolean.FALSE); afVar.q.set(CaptureRequest.CONTROL_AE_LOCK, Boolean.FALSE); afVar.q.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, 0); boolean z = false; for (String str : af.f) { if (Build.MODEL.contains(str)) { z = true; } } if (z) { if (afVar.q.get(CaptureRequest.EDGE_MODE) != null) { afVar.q.set(CaptureRequest.EDGE_MODE, 0); } if (afVar.q.get(CaptureRequest.NOISE_REDUCTION_MODE) != null) { afVar.q.set(CaptureRequest.NOISE_REDUCTION_MODE, 0); } } if (ah.d.e(ah.d.c)) { if (afVar.q.get(CaptureRequest.CONTROL_CAPTURE_INTENT) != null) { afVar.q.set(CaptureRequest.CONTROL_CAPTURE_INTENT, 4); } if (afVar.q.get(CaptureRequest.CONTROL_AWB_MODE) != null) { afVar.q.set(CaptureRequest.CONTROL_AWB_MODE, 0); } } aw.c("CTCCST"); afVar.f6403o.createCaptureSession(af.t == null ? Arrays.asList(surface, surface2) : Arrays.asList(surface, surface2, af.t), new CameraCaptureSession.StateCallback(afVar) { // from class: com.facetec.sdk.af.3 private af b; @Override // android.hardware.camera2.CameraCaptureSession.StateCallback public final void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { } { this.b = afVar; } @Override // android.hardware.camera2.CameraCaptureSession.StateCallback public final void onConfigured(CameraCaptureSession cameraCaptureSession) { aw.e("CTCCST"); af afVar2 = this.b; boolean z2 = false; try { try { z2 = afVar2.l.tryAcquire(2L, TimeUnit.SECONDS); } finally { if (0 != 0) { afVar2.l.release(); } } } catch (CameraAccessException | InterruptedException e2) { k.a(e2); if (!z2) { return; } } if (afVar2.f6403o == null) { if (z2) { return; } else { return; } } afVar2.r = cameraCaptureSession; if (!ba.c) { if (afVar2.e(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES, 4)) { afVar2.q.set(CaptureRequest.CONTROL_AF_MODE, 4); } } else if (afVar2.e(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES, 3)) { afVar2.q.set(CaptureRequest.CONTROL_AF_MODE, 3); } if (afVar2.e(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES, 1)) { afVar2.q.set(CaptureRequest.CONTROL_AE_MODE, 1); } aw.c("CTPRT"); afVar2.r.setRepeatingRequest(afVar2.q.build(), null, null); aw.e("CTPRT"); if (!z2) { return; } afVar2.l.release(); } }, null); } } catch (CameraAccessException e2) { k.a(e2); } catch (IllegalStateException unused) { if (afVar.n.get() != null) { afVar.n.get().e(d.CAMERA_ALREADY_CLOSED.toString()); t.a(afVar.n.get(), d.CAMERA_ALREADY_CLOSED, t.e((Activity) afVar.n.get())); } } afVar.l.release(); aw.e("CTCPT"); } catch (Throwable th) { afVar.l.release(); throw th; } } @Override // android.hardware.camera2.CameraDevice.StateCallback public final void onDisconnected(CameraDevice cameraDevice) { this.d.b(cameraDevice); } @Override // android.hardware.camera2.CameraDevice.StateCallback public final void onError(CameraDevice cameraDevice, int i) { af afVar = this.d; afVar.b(cameraDevice); be beVar = afVar.n.get(); if (beVar != null) { beVar.e("Camera2 device error: ".concat(String.valueOf(i))); d dVar = d.CAMERA2_ERROR; StringBuilder sb = new StringBuilder("Camera2 device error: "); sb.append(i); sb.append(t.e((Activity) beVar)); t.a(beVar, dVar, sb.toString()); } } }; @Override // com.facetec.sdk.ah final void a(ViewGroup viewGroup) { } @Override // com.facetec.sdk.ah final void b(boolean z, ViewGroup viewGroup) { } @Override // com.facetec.sdk.ah final void d(boolean z) { } @Override // com.facetec.sdk.ah final void e(boolean z) { } /* JADX INFO: Access modifiers changed from: package-private */ public af(Activity activity) throws ae { TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener(this) { // from class: com.facetec.sdk.af.4 private af d; @Override // android.view.TextureView.SurfaceTextureListener public final void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } { this.d = this; } @Override // android.view.TextureView.SurfaceTextureListener public final void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) { this.d.c(i, i2); } @Override // android.view.TextureView.SurfaceTextureListener public final void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) { this.d.d(i, i2); } @Override // android.view.TextureView.SurfaceTextureListener public final boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { try { this.d.i(); return true; } catch (Exception unused) { return true; } } }; this.I = surfaceTextureListener; this.n = new WeakReference<>((be) activity); if (!ba.c) { this.k = c(); } else { this.k = h(); } g gVar = new g(activity); this.m = gVar; HandlerThread handlerThread = new HandlerThread("CameraBackground"); this.y = handlerThread; handlerThread.start(); this.D = new Handler(this.y.getLooper()); if (gVar.isAvailable()) { b(activity, gVar.getWidth(), gVar.getHeight()); } else { gVar.setSurfaceTextureListener(surfaceTextureListener); } } /* JADX INFO: Access modifiers changed from: private */ public void d(byte[] bArr, Size size) { cd cdVar; if (this.g) { aw.e("CTFFT"); } this.e = true; bg bgVar = (bg) this.n.get(); if (bgVar == null || (cdVar = bgVar.b) == null) { return; } cdVar.b(bArr, size.getWidth(), size.getHeight(), this.w, Boolean.TRUE); this.g = false; } @Override // com.facetec.sdk.ah final void b() { if (this.z) { return; } this.z = true; Cdo cdo = this.p; if (cdo != null) { cdo.e(this.x); } } private void d(Context context, StreamConfigurationMap streamConfigurationMap) { Size[] outputSizes = streamConfigurationMap.getOutputSizes(Allocation.class); Arrays.sort(outputSizes, new Comparator() { // from class: com.facetec.sdk.af$$ExternalSyntheticLambda1 @Override // java.util.Comparator public final int compare(Object obj, Object obj2) { int a; a = af.a((Size) obj, (Size) obj2); return a; } }); Size size = new Size(640, 360); float f2 = this.k.e / this.k.c; int width = size.getWidth() * size.getHeight(); int length = outputSizes.length; Size size2 = null; int i = 0; while (true) { if (i >= length) { break; } Size size3 = outputSizes[i]; int height = size3.getHeight() * size3.getWidth(); if (height >= width && height <= (width << 2)) { float width2 = size3.getWidth() / size3.getHeight(); if (width2 <= 3.0f) { if (width2 == f2) { size2 = size3; break; } size2 = size3; } else { continue; } } i++; } if (size2 != null) { size = size2; } ah.a = new aj(size.getWidth(), size.getHeight()); try { Cdo cdo = new Cdo(context, size); this.p = cdo; if (this.z) { cdo.e(this.x); } } catch (Throwable th) { k.a(th); av.e(th.getMessage()); throw new RuntimeException(th); } } /* JADX INFO: Access modifiers changed from: private */ public static /* synthetic */ int a(Size size, Size size2) { return Integer.compare(size.getHeight() * size.getWidth(), size2.getHeight() * size2.getWidth()); } private void d(Activity activity) throws CameraAccessException, ae { c(activity, h(activity)); } private void b(Activity activity) throws CameraAccessException, ae { c(activity, i(activity)); } private void c(Activity activity, e eVar) throws CameraAccessException, ae { if (eVar != null) { String str = eVar.a; CameraCharacteristics cameraCharacteristics = eVar.b; StreamConfigurationMap streamConfigurationMap = eVar.c; int intValue = ((Integer) cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)).intValue(); this.w = intValue; j = intValue; activity.getWindowManager().getDefaultDisplay().getSize(new Point()); activity.getWindowManager().getDefaultDisplay().getRealSize(new Point()); if (activity.getResources().getConfiguration().orientation == 2) { this.m.setAspectRatio(this.k.e, this.k.c); } else { this.m.setAspectRatio(this.k.c, this.k.e); } d(activity, streamConfigurationMap); this.u = str; this.v = cameraCharacteristics; return; } throw new ae(ae.e.FRONT_FACING_NOT_FOUND); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static final class e { String a; CameraCharacteristics b; StreamConfigurationMap c; boolean d = false; e() { } } private static e i(Context context) throws ae { return a(context, 1); } private static e h(Context context) throws ae { return a(context, 0); } private static CameraManager j(Context context) { return (CameraManager) context.getApplicationContext().getSystemService("camera"); } private static e a(Context context, int i) throws ae { StreamConfigurationMap streamConfigurationMap; CameraManager j = j(context); try { String[] cameraIdList = j.getCameraIdList(); CameraAccessException e2 = null; for (String str : cameraIdList) { try { CameraCharacteristics cameraCharacteristics = j.getCameraCharacteristics(str); Integer num = (Integer) cameraCharacteristics.get(CameraCharacteristics.LENS_FACING); if ((num == null || num.intValue() == i || ai.e(cameraIdList)) && (streamConfigurationMap = (StreamConfigurationMap) cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)) != null) { Integer num2 = (Integer) cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); e eVar = new e(); eVar.a = str; eVar.b = cameraCharacteristics; eVar.c = streamConfigurationMap; if (num2.intValue() != 2) { eVar.d = true; } return eVar; } } catch (CameraAccessException e3) { e2 = e3; e2.getMessage(); } } if (e2 == null) { return null; } throw new ae(ae.e.ACCESS_ERROR, e2); } catch (CameraAccessException e4) { throw new ae(e4); } } /* JADX INFO: Access modifiers changed from: package-private */ public static boolean e(Context context) throws ae { synchronized (af.class) { if (A == null) { e h = h(context); if (h == null) { return false; } A = Boolean.valueOf(h.d); } return A.booleanValue(); } } /* JADX INFO: Access modifiers changed from: package-private */ public static boolean d(Context context) throws ae { synchronized (af.class) { if (B == null) { e i = i(context); if (i == null) { return false; } B = Boolean.valueOf(i.d); } return B.booleanValue(); } } private void b(Activity activity, int i, int i2) throws ae { if (this.C) { return; } if (ActivityCompat.checkSelfPermission(activity, "android.permission.CAMERA") != 0) { throw new ae(ae.e.PERMISSION_DENIED); } try { if (!ba.c) { d(activity); } else { b(activity); } d(i, i2); CameraManager j = j(activity); if (j == null) { throw new ae(ae.e.PERMISSION_DENIED); } try { if (!this.l.tryAcquire(2500L, TimeUnit.MILLISECONDS)) { throw new ae(ae.e.OPEN_TIMEOUT); } aw.c("CTOT"); aw.c("CTFFT"); try { j.openCamera(this.u, this.E, (Handler) null); } catch (Exception e2) { this.l.release(); throw new ae(ae.e.UNKNOWN, e2.getMessage()); } } catch (InterruptedException e3) { k.a(e3); throw new ae(ae.e.LOCK_OPEN_TIMEOUT, e3); } } catch (CameraAccessException e4) { throw new ae(ae.e.UNKNOWN, e4.getMessage()); } } protected final void i() throws ae { boolean z; try { z = this.l.tryAcquire(10L, TimeUnit.SECONDS); } catch (InterruptedException e2) { k.a(e2); z = false; } try { try { CameraCaptureSession cameraCaptureSession = this.r; if (cameraCaptureSession != null) { cameraCaptureSession.close(); this.r = null; } CameraDevice cameraDevice = this.f6403o; if (cameraDevice != null) { cameraDevice.close(); this.f6403o = null; } } catch (Exception e3) { throw new ae(ae.e.CLOSE_ERROR, e3); } } finally { if (z) { this.l.release(); } } } final void b(CameraDevice cameraDevice) { this.l.release(); cameraDevice.close(); this.f6403o = null; } final boolean e(CameraCharacteristics.Key key, int i) { int[] iArr = (int[]) this.v.get(key); if (iArr != null) { for (int i2 : iArr) { if (i2 == i) { return true; } } } return false; } protected final void d(int i, int i2) { be beVar = this.n.get(); if (this.m == null || beVar == null) { return; } int rotation = beVar.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); float f2 = i; float f3 = i2; RectF rectF = new RectF(BitmapDescriptorFactory.HUE_RED, BitmapDescriptorFactory.HUE_RED, f2, f3); RectF rectF2 = new RectF(BitmapDescriptorFactory.HUE_RED, BitmapDescriptorFactory.HUE_RED, this.k.c, this.k.e); float centerX = rectF.centerX(); float centerY = rectF.centerY(); if (1 == rotation || 3 == rotation) { rectF2.offset(centerX - rectF2.centerX(), centerY - rectF2.centerY()); matrix.setRectToRect(rectF, rectF2, Matrix.ScaleToFit.FILL); float max = Math.max(f3 / this.k.c, f2 / this.k.e); matrix.postScale(max, max, centerX, centerY); matrix.postRotate((rotation - 2) * 90, centerX, centerY); } else if (2 == rotation) { matrix.postRotate(180.0f, centerX, centerY); } this.m.setTransform(matrix); } protected final void c(int i, int i2) { be beVar = this.n.get(); if (beVar == null) { return; } try { b(beVar, i, i2); } catch (Throwable th) { d dVar = d.CAMERA2_ERROR; StringBuilder sb = new StringBuilder("Camera2 SurfaceTexture error: "); sb.append(th.getMessage()); sb.append(t.e((Activity) beVar)); t.d(beVar, th, dVar, sb.toString(), true); StringBuilder sb2 = new StringBuilder("Camera 2 SurfaceTexture error: "); sb2.append(th.getMessage()); beVar.e(sb2.toString()); } } /* JADX INFO: Access modifiers changed from: package-private */ public static aj f(Context context) throws ae { return b(context, h(context)); } /* JADX INFO: Access modifiers changed from: package-private */ public static aj g(Context context) throws ae { return b(context, i(context)); } private static aj b(Context context, e eVar) throws ae { float[] fArr = {1.7f, 1.6f, 1.5f, 1.4f, 1.3f}; ArrayList arrayList = new ArrayList(); if (eVar == null) { throw new ae(ae.e.FRONT_FACING_NOT_FOUND); } Size[] outputSizes = eVar.c.getOutputSizes(SurfaceTexture.class); if (outputSizes.length == 0) { throw new ae(ae.e.NO_OUTPUT_SIZES); } Arrays.sort(outputSizes, new Comparator() { // from class: com.facetec.sdk.af$$ExternalSyntheticLambda2 @Override // java.util.Comparator public final int compare(Object obj, Object obj2) { int d; d = af.d((Size) obj, (Size) obj2); return d; } }); StringBuilder sb = new StringBuilder(); sb.append(outputSizes[0].getWidth()); sb.append("x"); sb.append(outputSizes[0].getHeight()); ah.c = sb.toString(); m.c(outputSizes); av.e(context).getDefaultDisplay().getRealSize(new Point()); for (int i = 0; i < 5; i++) { float f2 = fArr[i]; for (Size size : outputSizes) { float width = size.getWidth(); float height = size.getHeight(); if (width <= 1920.0f && height <= 1080.0f) { float f3 = width / height; if (f3 >= f2 && f3 <= 1.9f && width >= 640.0f && width <= r3.y && height <= r3.x) { arrayList.add(size); } } } if (arrayList.size() > 0) { break; } } if (arrayList.size() > 0) { return new aj(((Size) arrayList.get(0)).getWidth(), ((Size) arrayList.get(0)).getHeight()); } return new aj(outputSizes[0].getWidth(), outputSizes[0].getHeight()); } /* JADX INFO: Access modifiers changed from: private */ public static /* synthetic */ int d(Size size, Size size2) { return (size2.getWidth() * size2.getHeight()) - (size.getWidth() * size.getHeight()); } @Override // com.facetec.sdk.ah public final void a() { HandlerThread handlerThread = this.y; if (handlerThread != null) { handlerThread.quitSafely(); try { this.y.join(); this.y = null; this.D.removeCallbacksAndMessages(null); this.D = null; } catch (InterruptedException e2) { k.a(e2); } } try { i(); } catch (Exception unused) { } Cdo cdo = this.p; if (cdo != null) { cdo.b = null; this.p = null; } this.C = true; } @Override // com.facetec.sdk.ah public final View d() { return this.m; } }