package e0.a.a.a; import android.content.Context; import android.graphics.Point; import android.hardware.Camera; import android.os.Handler; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import java.util.List; /* compiled from: CameraPreview */ public class d extends SurfaceView implements SurfaceHolder.Callback { public e i; public Handler j; public boolean k = true; public boolean l = true; public boolean m = false; public boolean n = true; public Camera.PreviewCallback o; public float p = 0.1f; public Runnable q = new a(); public Camera.AutoFocusCallback r = new b(); /* compiled from: CameraPreview */ public class a implements Runnable { public a() { } @Override // java.lang.Runnable public void run() { d dVar = d.this; if (dVar.i != null && dVar.k && dVar.l && dVar.m) { dVar.a(); } } } /* compiled from: CameraPreview */ public class b implements Camera.AutoFocusCallback { public b() { } @Override // android.hardware.Camera.AutoFocusCallback public void onAutoFocus(boolean z2, Camera camera) { d.this.b(); } } public d(Context context, e eVar, Camera.PreviewCallback previewCallback) { super(context); this.i = eVar; this.o = previewCallback; this.j = new Handler(); getHolder().addCallback(this); getHolder().setType(3); } private Camera.Size getOptimalPreviewSize() { e eVar = this.i; Camera.Size size = null; if (eVar == null) { return null; } List supportedPreviewSizes = eVar.a.getParameters().getSupportedPreviewSizes(); int width = getWidth(); int height = getHeight(); if (c.q.a.k.a.w(getContext()) == 1) { height = width; width = height; } double d = ((double) width) / ((double) height); if (supportedPreviewSizes == null) { return null; } double d2 = Double.MAX_VALUE; double d3 = Double.MAX_VALUE; for (Camera.Size size2 : supportedPreviewSizes) { if (Math.abs((((double) size2.width) / ((double) size2.height)) - d) <= ((double) this.p) && ((double) Math.abs(size2.height - height)) < d3) { d3 = (double) Math.abs(size2.height - height); size = size2; } } if (size == null) { for (Camera.Size size3 : supportedPreviewSizes) { if (((double) Math.abs(size3.height - height)) < d2) { size = size3; d2 = (double) Math.abs(size3.height - height); } } } return size; } public void a() { try { this.i.a.autoFocus(this.r); } catch (RuntimeException unused) { b(); } } public final void b() { this.j.postDelayed(this.q, 1000); } public final void c(int i, int i2) { ViewGroup.LayoutParams layoutParams = getLayoutParams(); if (getDisplayOrientation() % 180 != 0) { i2 = i; i = i2; } if (this.n) { float f = (float) i; float width = ((float) ((View) getParent()).getWidth()) / f; float f2 = (float) i2; float height = ((float) ((View) getParent()).getHeight()) / f2; if (width <= height) { width = height; } i = Math.round(f * width); i2 = Math.round(f2 * width); } layoutParams.width = i; layoutParams.height = i2; setLayoutParams(layoutParams); } public void d() { Camera.Size optimalPreviewSize = getOptimalPreviewSize(); Camera.Parameters parameters = this.i.a.getParameters(); parameters.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height); this.i.a.setParameters(parameters); Point point = new Point(getWidth(), getHeight()); if (getDisplayOrientation() % 180 != 0) { point = new Point(point.y, point.x); } float f = ((float) optimalPreviewSize.width) / ((float) optimalPreviewSize.height); int i = point.x; float f2 = (float) i; int i2 = point.y; float f3 = (float) i2; if (f2 / f3 > f) { c((int) (f3 * f), i2); } else { c(i, (int) (f2 / f)); } } public void e() { if (this.i != null) { try { this.k = false; getHolder().removeCallback(this); this.i.a.cancelAutoFocus(); this.i.a.setOneShotPreviewCallback(null); this.i.a.stopPreview(); } catch (Exception e) { Log.e("CameraPreview", e.toString(), e); } } } public int getDisplayOrientation() { int i = 0; if (this.i == null) { return 0; } Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); int i2 = this.i.b; if (i2 == -1) { Camera.getCameraInfo(0, cameraInfo); } else { Camera.getCameraInfo(i2, cameraInfo); } int rotation = ((WindowManager) getContext().getSystemService("window")).getDefaultDisplay().getRotation(); if (rotation != 0) { if (rotation == 1) { i = 90; } else if (rotation == 2) { i = 180; } else if (rotation == 3) { i = 270; } } return cameraInfo.facing == 1 ? (360 - ((cameraInfo.orientation + i) % 360)) % 360 : ((cameraInfo.orientation - i) + 360) % 360; } public void setAspectTolerance(float f) { this.p = f; } public void setAutoFocus(boolean z2) { if (this.i != null && this.k && z2 != this.l) { this.l = z2; if (!z2) { Log.v("CameraPreview", "Cancelling autofocus"); this.i.a.cancelAutoFocus(); } else if (this.m) { Log.v("CameraPreview", "Starting autofocus"); a(); } else { b(); } } } public void setShouldScaleToFill(boolean z2) { this.n = z2; } @Override // android.view.SurfaceHolder.Callback public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { if (surfaceHolder.getSurface() != null) { e(); if (this.i != null) { try { getHolder().addCallback(this); this.k = true; d(); this.i.a.setPreviewDisplay(getHolder()); this.i.a.setDisplayOrientation(getDisplayOrientation()); this.i.a.setOneShotPreviewCallback(this.o); this.i.a.startPreview(); if (!this.l) { return; } if (this.m) { a(); } else { b(); } } catch (Exception e) { Log.e("CameraPreview", e.toString(), e); } } } } @Override // android.view.SurfaceHolder.Callback public void surfaceCreated(SurfaceHolder surfaceHolder) { this.m = true; } @Override // android.view.SurfaceHolder.Callback public void surfaceDestroyed(SurfaceHolder surfaceHolder) { this.m = false; e(); } }