package com.journeyapps.barcodescanner;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.OrientationEventListener;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.browser.trusted.k;
import java.util.ArrayList;
import l5.i;
import mc.c;
import mc.d;
import mc.e;
import mc.u;
import mc.v;
import nb.g;
import nc.f;
import nc.h;
import nc.j;
import nc.l;
import nc.n;
import nc.p;
import rb.m;
import xyz.stream.utils.e0;
import xyz.stream.utils.f0;

/* JADX INFO: loaded from: classes.dex */
public class CameraPreview extends ViewGroup {

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    public static final /* synthetic */ int f24569g0 = 0;
    public v Q;
    public Rect R;
    public v S;
    public Rect T;
    public Rect U;
    public v V;
    public double W;

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

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    public p f24571a0;

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

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

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

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    public final d f24575c0;

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

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    public final i f24577d0;

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

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    public final f0 f24579e0;

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

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public final e f24581f0;

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final ArrayList f24585j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public nc.i f24587l;

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

    public CameraPreview(Context context) {
        super(context);
        this.f24576d = false;
        this.f24582g = false;
        this.f24584i = -1;
        this.f24585j = new ArrayList();
        this.f24587l = new nc.i();
        this.T = null;
        this.U = null;
        this.V = null;
        this.W = 0.1d;
        this.f24571a0 = null;
        this.f24573b0 = false;
        this.f24575c0 = new d(this);
        this.f24577d0 = new i(this, 4);
        this.f24579e0 = new f0(this, 21);
        this.f24581f0 = new e(this, 0);
        c(context, null);
    }

    public static void a(CameraPreview cameraPreview) {
        if (cameraPreview.f24570a == null || cameraPreview.getDisplayRotation() == cameraPreview.f24584i) {
            return;
        }
        cameraPreview.e();
        cameraPreview.g();
    }

    private int getDisplayRotation() {
        return this.f24572b.getDefaultDisplay().getRotation();
    }

    public Rect b(Rect rect, Rect rect2) {
        Rect rect3 = new Rect(rect);
        rect3.intersect(rect2);
        if (this.V != null) {
            rect3.inset(Math.max(0, (rect3.width() - this.V.f30645a) / 2), Math.max(0, (rect3.height() - this.V.f30646b) / 2));
            return rect3;
        }
        int iMin = (int) Math.min(((double) rect3.width()) * this.W, ((double) rect3.height()) * this.W);
        rect3.inset(iMin, iMin);
        if (rect3.height() > rect3.width()) {
            rect3.inset(0, (rect3.height() - rect3.width()) / 2);
        }
        return rect3;
    }

    public final void c(Context context, AttributeSet attributeSet) {
        if (getBackground() == null) {
            setBackgroundColor(-16777216);
        }
        d(attributeSet);
        this.f24572b = (WindowManager) context.getSystemService("window");
        this.f24574c = new Handler(this.f24577d0);
        this.f24583h = new k(7);
    }

    public final void d(AttributeSet attributeSet) {
        TypedArray typedArrayObtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, m.zxing_camera_preview);
        int dimension = (int) typedArrayObtainStyledAttributes.getDimension(m.zxing_camera_preview_zxing_framing_rect_width, -1.0f);
        int dimension2 = (int) typedArrayObtainStyledAttributes.getDimension(m.zxing_camera_preview_zxing_framing_rect_height, -1.0f);
        if (dimension > 0 && dimension2 > 0) {
            this.V = new v(dimension, dimension2);
        }
        this.f24576d = typedArrayObtainStyledAttributes.getBoolean(m.zxing_camera_preview_zxing_use_texture_view, true);
        int integer = typedArrayObtainStyledAttributes.getInteger(m.zxing_camera_preview_zxing_preview_scaling_strategy, -1);
        if (integer == 1) {
            this.f24571a0 = new nc.k();
        } else if (integer == 2) {
            this.f24571a0 = new nc.m();
        } else if (integer == 3) {
            this.f24571a0 = new n();
        }
        typedArrayObtainStyledAttributes.recycle();
    }

    public void e() {
        TextureView textureView;
        SurfaceView surfaceView;
        g.V();
        this.f24584i = -1;
        f fVar = this.f24570a;
        if (fVar != null) {
            g.V();
            if (fVar.f31208f) {
                fVar.f31203a.b(fVar.f31214l);
            } else {
                fVar.f31209g = true;
            }
            fVar.f31208f = false;
            this.f24570a = null;
            this.f24582g = false;
        } else {
            this.f24574c.sendEmptyMessage(rb.i.zxing_camera_closed);
        }
        if (this.S == null && (surfaceView = this.f24578e) != null) {
            surfaceView.getHolder().removeCallback(this.f24575c0);
        }
        if (this.S == null && (textureView = this.f24580f) != null) {
            textureView.setSurfaceTextureListener(null);
        }
        this.f24588m = null;
        this.Q = null;
        this.U = null;
        k kVar = this.f24583h;
        OrientationEventListener orientationEventListener = (OrientationEventListener) kVar.f913d;
        if (orientationEventListener != null) {
            orientationEventListener.disable();
        }
        kVar.f913d = null;
        kVar.f912c = null;
        kVar.f914e = null;
        this.f24581f0.e();
    }

    public void f() {
    }

    public final void g() {
        g.V();
        int i10 = 0;
        if (this.f24570a == null) {
            Context context = getContext();
            f fVar = new f();
            fVar.f31208f = false;
            fVar.f31209g = true;
            fVar.f31211i = new nc.i();
            nc.e eVar = new nc.e(fVar, i10);
            fVar.f31212j = new nc.e(fVar, 1);
            fVar.f31213k = new nc.e(fVar, 2);
            fVar.f31214l = new nc.e(fVar, 3);
            g.V();
            if (j.f31234e == null) {
                j.f31234e = new j();
            }
            j jVar = j.f31234e;
            fVar.f31203a = jVar;
            h hVar = new h(context);
            fVar.f31205c = hVar;
            hVar.f31225g = fVar.f31211i;
            fVar.f31210h = new Handler();
            nc.i iVar = this.f24587l;
            if (!fVar.f31208f) {
                fVar.f31211i = iVar;
                hVar.f31225g = iVar;
            }
            this.f24570a = fVar;
            fVar.f31206d = this.f24574c;
            g.V();
            fVar.f31208f = true;
            fVar.f31209g = false;
            synchronized (jVar.f31238d) {
                jVar.f31237c++;
                jVar.b(eVar);
            }
            this.f24584i = getDisplayRotation();
        }
        if (this.S != null) {
            i();
        } else {
            SurfaceView surfaceView = this.f24578e;
            if (surfaceView != null) {
                surfaceView.getHolder().addCallback(this.f24575c0);
            } else {
                TextureView textureView = this.f24580f;
                if (textureView != null) {
                    if (textureView.isAvailable()) {
                        new c(this, i10).onSurfaceTextureAvailable(this.f24580f.getSurfaceTexture(), this.f24580f.getWidth(), this.f24580f.getHeight());
                    } else {
                        this.f24580f.setSurfaceTextureListener(new c(this, i10));
                    }
                }
            }
        }
        requestLayout();
        k kVar = this.f24583h;
        Context context2 = getContext();
        f0 f0Var = this.f24579e0;
        OrientationEventListener orientationEventListener = (OrientationEventListener) kVar.f913d;
        if (orientationEventListener != null) {
            orientationEventListener.disable();
        }
        kVar.f913d = null;
        kVar.f912c = null;
        kVar.f914e = null;
        Context applicationContext = context2.getApplicationContext();
        kVar.f914e = f0Var;
        kVar.f912c = (WindowManager) applicationContext.getSystemService("window");
        u uVar = new u(kVar, applicationContext);
        kVar.f913d = uVar;
        uVar.enable();
        kVar.f911b = ((WindowManager) kVar.f912c).getDefaultDisplay().getRotation();
    }

    public f getCameraInstance() {
        return this.f24570a;
    }

    public nc.i getCameraSettings() {
        return this.f24587l;
    }

    public Rect getFramingRect() {
        return this.T;
    }

    public v getFramingRectSize() {
        return this.V;
    }

    public double getMarginFraction() {
        return this.W;
    }

    public Rect getPreviewFramingRect() {
        return this.U;
    }

    public p getPreviewScalingStrategy() {
        p pVar = this.f24571a0;
        return pVar != null ? pVar : this.f24580f != null ? new nc.k() : new nc.m();
    }

    public v getPreviewSize() {
        return this.Q;
    }

    public final void h(e0 e0Var) {
        f fVar;
        if (this.f24582g || (fVar = this.f24570a) == null) {
            return;
        }
        fVar.f31204b = e0Var;
        g.V();
        if (!fVar.f31208f) {
            throw new IllegalStateException("CameraInstance is not open");
        }
        fVar.f31203a.b(fVar.f31213k);
        this.f24582g = true;
        f();
        this.f24581f0.d();
    }

    public final void i() {
        Rect rect;
        float f10;
        v vVar = this.S;
        if (vVar == null || this.Q == null || (rect = this.R) == null) {
            return;
        }
        if (this.f24578e != null && vVar.equals(new v(rect.width(), this.R.height()))) {
            h(new e0(this.f24578e.getHolder()));
            return;
        }
        TextureView textureView = this.f24580f;
        if (textureView == null || textureView.getSurfaceTexture() == null) {
            return;
        }
        if (this.Q != null) {
            int width = this.f24580f.getWidth();
            int height = this.f24580f.getHeight();
            v vVar2 = this.Q;
            float f11 = height;
            float f12 = width / f11;
            float f13 = vVar2.f30645a / vVar2.f30646b;
            float f14 = 1.0f;
            if (f12 < f13) {
                float f15 = f13 / f12;
                f10 = 1.0f;
                f14 = f15;
            } else {
                f10 = f12 / f13;
            }
            Matrix matrix = new Matrix();
            matrix.setScale(f14, f10);
            float f16 = width;
            matrix.postTranslate((f16 - (f14 * f16)) / 2.0f, (f11 - (f10 * f11)) / 2.0f);
            this.f24580f.setTransform(matrix);
        }
        h(new e0(this.f24580f.getSurfaceTexture()));
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.f24576d) {
            TextureView textureView = new TextureView(getContext());
            this.f24580f = textureView;
            textureView.setSurfaceTextureListener(new c(this, 0));
            addView(this.f24580f);
            return;
        }
        SurfaceView surfaceView = new SurfaceView(getContext());
        this.f24578e = surfaceView;
        surfaceView.getHolder().addCallback(this.f24575c0);
        addView(this.f24578e);
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        v vVar = new v(i12 - i10, i13 - i11);
        this.f24588m = vVar;
        f fVar = this.f24570a;
        if (fVar != null && fVar.f31207e == null) {
            int displayRotation = getDisplayRotation();
            l lVar = new l();
            lVar.f31241c = new nc.m();
            lVar.f31240b = displayRotation;
            lVar.f31239a = vVar;
            this.f24586k = lVar;
            lVar.f31241c = getPreviewScalingStrategy();
            f fVar2 = this.f24570a;
            l lVar2 = this.f24586k;
            fVar2.f31207e = lVar2;
            fVar2.f31205c.f31226h = lVar2;
            g.V();
            if (!fVar2.f31208f) {
                throw new IllegalStateException("CameraInstance is not open");
            }
            fVar2.f31203a.b(fVar2.f31212j);
            boolean z11 = this.f24573b0;
            if (z11) {
                f fVar3 = this.f24570a;
                fVar3.getClass();
                g.V();
                if (fVar3.f31208f) {
                    fVar3.f31203a.b(new androidx.media3.exoplayer.audio.e(6, fVar3, z11));
                }
            }
        }
        SurfaceView surfaceView = this.f24578e;
        if (surfaceView == null) {
            TextureView textureView = this.f24580f;
            if (textureView != null) {
                textureView.layout(0, 0, getWidth(), getHeight());
                return;
            }
            return;
        }
        Rect rect = this.R;
        if (rect == null) {
            surfaceView.layout(0, 0, getWidth(), getHeight());
        } else {
            surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom);
        }
    }

    @Override // android.view.View
    public final void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof Bundle)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        Bundle bundle = (Bundle) parcelable;
        super.onRestoreInstanceState(bundle.getParcelable("super"));
        setTorch(bundle.getBoolean("torch"));
    }

    @Override // android.view.View
    public final Parcelable onSaveInstanceState() {
        Parcelable parcelableOnSaveInstanceState = super.onSaveInstanceState();
        Bundle bundle = new Bundle();
        bundle.putParcelable("super", parcelableOnSaveInstanceState);
        bundle.putBoolean("torch", this.f24573b0);
        return bundle;
    }

    public void setCameraSettings(nc.i iVar) {
        this.f24587l = iVar;
    }

    public void setFramingRectSize(v vVar) {
        this.V = vVar;
    }

    public void setMarginFraction(double d10) {
        if (d10 >= 0.5d) {
            throw new IllegalArgumentException("The margin fraction must be less than 0.5");
        }
        this.W = d10;
    }

    public void setPreviewScalingStrategy(p pVar) {
        this.f24571a0 = pVar;
    }

    public void setTorch(boolean z10) {
        this.f24573b0 = z10;
        f fVar = this.f24570a;
        if (fVar != null) {
            g.V();
            if (fVar.f31208f) {
                fVar.f31203a.b(new androidx.media3.exoplayer.audio.e(6, fVar, z10));
            }
        }
    }

    public void setUseTextureView(boolean z10) {
        this.f24576d = z10;
    }

    public CameraPreview(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f24576d = false;
        this.f24582g = false;
        this.f24584i = -1;
        this.f24585j = new ArrayList();
        this.f24587l = new nc.i();
        this.T = null;
        this.U = null;
        this.V = null;
        this.W = 0.1d;
        this.f24571a0 = null;
        this.f24573b0 = false;
        this.f24575c0 = new d(this);
        this.f24577d0 = new i(this, 4);
        this.f24579e0 = new f0(this, 21);
        this.f24581f0 = new e(this, 0);
        c(context, attributeSet);
    }

    public CameraPreview(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f24576d = false;
        this.f24582g = false;
        this.f24584i = -1;
        this.f24585j = new ArrayList();
        this.f24587l = new nc.i();
        this.T = null;
        this.U = null;
        this.V = null;
        this.W = 0.1d;
        this.f24571a0 = null;
        this.f24573b0 = false;
        this.f24575c0 = new d(this);
        this.f24577d0 = new i(this, 4);
        this.f24579e0 = new f0(this, 21);
        this.f24581f0 = new e(this, 0);
        c(context, attributeSet);
    }
}
