package nc;

import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
import android.os.Handler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import mc.v;

/* JADX INFO: loaded from: classes.dex */
public final class h {

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public static final /* synthetic */ int f31218n = 0;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Camera f31219a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Camera.CameraInfo f31220b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public b f31221c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public rb.a f31222d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f31223e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f31224f;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public l f31226h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public v f31227i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public v f31228j;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final Context f31230l;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public i f31225g = new i();

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f31229k = -1;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final g f31231m = new g(this);

    public h(Context context) {
        this.f31230l = context;
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0014  */
    /* JADX WARN: Removed duplicated region for block: B:18:0x0024 A[Catch: Exception -> 0x003a, TryCatch #1 {Exception -> 0x003a, blocks: (B:5:0x0006, B:16:0x001e, B:18:0x0024, B:20:0x0035, B:19:0x002e), top: B:36:0x0006 }] */
    /* JADX WARN: Removed duplicated region for block: B:19:0x002e A[Catch: Exception -> 0x003a, TryCatch #1 {Exception -> 0x003a, blocks: (B:5:0x0006, B:16:0x001e, B:18:0x0024, B:20:0x0035, B:19:0x002e), top: B:36:0x0006 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        int i10;
        Camera camera = this.f31219a;
        if (camera == null) {
            throw new RuntimeException("Camera not open");
        }
        try {
            int i11 = this.f31226h.f31240b;
            if (i11 == 0) {
                i10 = 0;
                Camera.CameraInfo cameraInfo = this.f31220b;
                int i12 = cameraInfo.facing != 1 ? (360 - ((cameraInfo.orientation + i10) % 360)) % 360 : ((cameraInfo.orientation - i10) + 360) % 360;
                this.f31229k = i12;
                camera.setDisplayOrientation(i12);
            } else {
                if (i11 == 1) {
                    i10 = 90;
                } else if (i11 == 2) {
                    i10 = 180;
                } else if (i11 == 3) {
                    i10 = 270;
                }
                Camera.CameraInfo cameraInfo2 = this.f31220b;
                if (cameraInfo2.facing != 1) {
                }
                this.f31229k = i12;
                camera.setDisplayOrientation(i12);
            }
        } catch (Exception unused) {
        }
        try {
            try {
                c(false);
            } catch (Exception unused2) {
                c(true);
            }
        } catch (Exception unused3) {
        }
        Camera.Size previewSize = this.f31219a.getParameters().getPreviewSize();
        if (previewSize == null) {
            this.f31228j = this.f31227i;
        } else {
            this.f31228j = new v(previewSize.width, previewSize.height);
        }
        this.f31231m.f31216b = this.f31228j;
    }

    public final void b() {
        int iZ = nb.g.z(this.f31225g.f31232a);
        Camera cameraOpen = iZ == -1 ? null : Camera.open(iZ);
        this.f31219a = cameraOpen;
        if (cameraOpen == null) {
            throw new RuntimeException("Failed to open camera");
        }
        int iZ2 = nb.g.z(this.f31225g.f31232a);
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        this.f31220b = cameraInfo;
        Camera.getCameraInfo(iZ2, cameraInfo);
    }

    public final void c(boolean z10) {
        Camera.Parameters parameters = this.f31219a.getParameters();
        String str = this.f31224f;
        if (str == null) {
            this.f31224f = parameters.flatten();
        } else {
            parameters.unflatten(str);
        }
        if (parameters == null) {
            return;
        }
        parameters.flatten();
        this.f31225g.getClass();
        int i10 = c.f31196a;
        List<String> supportedFocusModes = parameters.getSupportedFocusModes();
        String strA = c.a(supportedFocusModes, "auto");
        if (!z10 && strA == null) {
            strA = c.a(supportedFocusModes, "macro", "edof");
        }
        if (strA != null && !strA.equals(parameters.getFocusMode())) {
            parameters.setFocusMode(strA);
        }
        if (!z10) {
            c.b(parameters, false);
            if (this.f31225g.f31233b && !"negative".equals(parameters.getColorEffect())) {
                String strA2 = c.a(parameters.getSupportedColorEffects(), "negative");
                if (strA2 != null) {
                    parameters.setColorEffect(strA2);
                }
            }
            this.f31225g.getClass();
            this.f31225g.getClass();
        }
        List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
        ArrayList arrayList = new ArrayList();
        if (supportedPreviewSizes == null) {
            Camera.Size previewSize = parameters.getPreviewSize();
            if (previewSize != null) {
                arrayList.add(new v(previewSize.width, previewSize.height));
            }
        } else {
            for (Camera.Size size : supportedPreviewSizes) {
                arrayList.add(new v(size.width, size.height));
            }
        }
        int[] iArr = null;
        if (arrayList.size() == 0) {
            this.f31227i = null;
        } else {
            l lVar = this.f31226h;
            int i11 = this.f31229k;
            if (i11 == -1) {
                throw new IllegalStateException("Rotation not calculated yet. Call configure() first.");
            }
            boolean z11 = i11 % 180 != 0;
            v vVar = lVar.f31239a;
            if (vVar == null) {
                vVar = null;
            } else if (z11) {
                vVar = new v(vVar.f30646b, vVar.f30645a);
            }
            p pVar = lVar.f31241c;
            pVar.getClass();
            if (vVar != null) {
                Collections.sort(arrayList, new o(pVar, vVar));
            }
            Objects.toString(vVar);
            Objects.toString(arrayList);
            v vVar2 = (v) arrayList.get(0);
            this.f31227i = vVar2;
            parameters.setPreviewSize(vVar2.f30645a, vVar2.f30646b);
        }
        if (Build.DEVICE.equals("glass-1")) {
            List<int[]> supportedPreviewFpsRange = parameters.getSupportedPreviewFpsRange();
            if (supportedPreviewFpsRange != null && !supportedPreviewFpsRange.isEmpty()) {
                Iterator<int[]> it = supportedPreviewFpsRange.iterator();
                while (it.hasNext()) {
                    Arrays.toString(it.next());
                    it.hasNext();
                }
            }
            if (supportedPreviewFpsRange != null && !supportedPreviewFpsRange.isEmpty()) {
                Iterator<int[]> it2 = supportedPreviewFpsRange.iterator();
                while (true) {
                    if (!it2.hasNext()) {
                        break;
                    }
                    int[] next = it2.next();
                    int i12 = next[0];
                    int i13 = next[1];
                    if (i12 >= 10000 && i13 <= 20000) {
                        iArr = next;
                        break;
                    }
                }
                if (iArr != null) {
                    int[] iArr2 = new int[2];
                    parameters.getPreviewFpsRange(iArr2);
                    if (Arrays.equals(iArr2, iArr)) {
                        Arrays.toString(iArr);
                    } else {
                        Arrays.toString(iArr);
                        parameters.setPreviewFpsRange(iArr[0], iArr[1]);
                    }
                }
            }
        }
        parameters.flatten();
        this.f31219a.setParameters(parameters);
    }

    public final void d(boolean z10) {
        String flashMode;
        Camera camera = this.f31219a;
        if (camera != null) {
            try {
                Camera.Parameters parameters = camera.getParameters();
                if (z10 != ((parameters == null || (flashMode = parameters.getFlashMode()) == null || (!"on".equals(flashMode) && !"torch".equals(flashMode))) ? false : true)) {
                    b bVar = this.f31221c;
                    if (bVar != null) {
                        bVar.f31190a = true;
                        bVar.f31191b = false;
                        bVar.f31194e.removeMessages(1);
                        if (bVar.f31192c) {
                            try {
                                bVar.f31193d.cancelAutoFocus();
                            } catch (RuntimeException unused) {
                            }
                        }
                    }
                    Camera.Parameters parameters2 = this.f31219a.getParameters();
                    c.b(parameters2, z10);
                    this.f31225g.getClass();
                    this.f31219a.setParameters(parameters2);
                    b bVar2 = this.f31221c;
                    if (bVar2 != null) {
                        bVar2.f31190a = false;
                        bVar2.b();
                    }
                }
            } catch (RuntimeException unused2) {
            }
        }
    }

    public final void e() {
        Camera camera = this.f31219a;
        if (camera == null || this.f31223e) {
            return;
        }
        camera.startPreview();
        this.f31223e = true;
        this.f31221c = new b(this.f31219a, this.f31225g);
        i iVar = this.f31225g;
        rb.a aVar = new rb.a();
        aVar.f32519b = this.f31230l;
        aVar.f32518a = this;
        aVar.f32520c = new Handler();
        this.f31222d = aVar;
        iVar.getClass();
    }
}
