package ai.advance.liveness.lib;

import ai.advance.common.camera.GuardianCameraView;
import ai.advance.liveness.lib.Detector;
import ai.advance.liveness.lib.http.entity.ResultEntity;
import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import ඓ.K.K.K.C1293;
import K.K.K.C2373;
import K.K.K.ʬ.C2367;
import K.K.K.ʬ.C2368;
import K.K.K.ʬ.C2369;
import K.K.㸾.ῲ.ῲ.C2377;
import K.K.㸾.K.C2383;
import K.K.㸾.K.C2385;
import K.K.㸾.K.C2386;
import K.K.㸾.K.C2387;
import K.K.㸾.K.C2388;
import K.K.㸾.K.C2393;
import K.K.㸾.K.㳳.AbstractC2391;
import K.K.㸾.K.㳳.AbstractC2392;

public class LivenessView extends GuardianCameraView implements GuardianCameraView.AbstractC0010, Detector.AbstractC0012, Detector.AbstractC0023 {

    /* renamed from: В  reason: contains not printable characters */
    public ArrayList<Detector.DetectionType> f91;

    /* renamed from: ಯ  reason: contains not printable characters */
    public long f92;

    /* renamed from: ᆯ  reason: contains not printable characters */
    public int f93;

    /* renamed from: ሽ  reason: contains not printable characters */
    public AbstractC2392 f94;

    /* renamed from: ጒ  reason: contains not printable characters */
    public Detector.WarnCode f95;

    /* renamed from: ᑾ  reason: contains not printable characters */
    public Handler f96;

    /* renamed from: ᝁ  reason: contains not printable characters */
    public Context f97;

    /* renamed from: ⶍ  reason: contains not printable characters */
    public Detector.DetectionType f98;

    /* renamed from: ゎ  reason: contains not printable characters */
    public C2373 f99;

    /* renamed from: ト  reason: contains not printable characters */
    public C2368 f100;

    /* renamed from: 㮔  reason: contains not printable characters */
    public boolean f101;

    /* renamed from: 㮝  reason: contains not printable characters */
    public JSONArray f102;

    /* renamed from: 㹎  reason: contains not printable characters */
    public long f103;

    /* renamed from: 䃖  reason: contains not printable characters */
    public Detector f104;

    /* renamed from: ai.advance.liveness.lib.LivenessView$ʬ  reason: contains not printable characters */
    public class RunnableC0024 implements Runnable {
        public RunnableC0024() {
        }

        public void run() {
            LivenessView.this.f94.m75();
        }
    }

    /* renamed from: ai.advance.liveness.lib.LivenessView$ඓ  reason: contains not printable characters */
    public static /* synthetic */ class C0025 {

        /* renamed from: K  reason: contains not printable characters */
        public static final /* synthetic */ int[] f106 = new int[Detector.DetectionType.values().length];

        /* JADX WARNING: Can't wrap try/catch for region: R(8:0|1|2|3|4|5|6|8) */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0014 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x001f */
        static {
            f106[Detector.DetectionType.DONE.ordinal()] = 1;
            f106[Detector.DetectionType.AIMLESS.ordinal()] = 2;
            try {
                f106[Detector.DetectionType.NONE.ordinal()] = 3;
            } catch (NoSuchFieldError unused) {
            }
        }
    }

    /* renamed from: ai.advance.liveness.lib.LivenessView$ῲ  reason: contains not printable characters */
    public class RunnableC0026 implements Runnable {
        public RunnableC0026() {
        }

        public void run() {
            LivenessView.this.f94.m6433();
        }
    }

    /* renamed from: ai.advance.liveness.lib.LivenessView$K  reason: contains not printable characters */
    public class RunnableC0027 implements Runnable {

        /* renamed from: ῲ  reason: contains not printable characters */
        public final /* synthetic */ AbstractC2391 f109;

        /* renamed from: ai.advance.liveness.lib.LivenessView$K$K  reason: contains not printable characters */
        public class RunnableC0028 implements Runnable {

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ ResultEntity f111;

            public RunnableC0028(ResultEntity resultEntity) {
                this.f111 = resultEntity;
            }

            public void run() {
                Detector detector = LivenessView.this.f104;
                detector.m63().m6400(this.f111.f8209);
                ResultEntity resultEntity = this.f111;
                if (resultEntity.f8209) {
                    AbstractC2391 r0 = RunnableC0027.this.f109;
                    String str = C2383.f8244;
                    C2377.m6361(C2377.this);
                    return;
                }
                if ("NO_RESPONSE".equals(resultEntity.f8212)) {
                    C2383.m6380(e.CHECKING_BAD_NETWORK);
                    C2383.m6382("Please check network");
                }
                ((C2377.C2381) RunnableC0027.this.f109).m6376(this.f111);
            }
        }

        public RunnableC0027(AbstractC2391 r2) {
            this.f109 = r2;
        }

        /* JADX WARNING: Code restructure failed: missing block: B:14:0x006a, code lost:
            if (r2.f8209 != false) goto L_0x0077;
         */
        /* JADX WARNING: Removed duplicated region for block: B:30:0x00ae  */
        /* JADX WARNING: Removed duplicated region for block: B:36:0x00d0  */
        /* JADX WARNING: Removed duplicated region for block: B:40:0x00f7  */
        /* JADX WARNING: Removed duplicated region for block: B:48:? A[ADDED_TO_REGION, RETURN, SYNTHETIC] */
        public void run() {
            C2385 r1;
            ResultEntity resultEntity;
            String str;
            Detector detector = LivenessView.this.f104;
            ResultEntity resultEntity2 = detector.f51;
            if (resultEntity2 == null) {
                r1 = detector.f54.get("bestImage");
                C2386 r2 = detector.m63();
                r2.f8267 = true;
                r2.f8272 = System.currentTimeMillis();
                resultEntity = new ResultEntity();
                if (r1 == null) {
                    resultEntity.f8212 = "NO_BEST_IMAGE";
                    resultEntity.f8214 = "not get best image(sdk message)";
                } else {
                    String r4 = r1.m6387();
                    str = "";
                    if (C2387.m6421()) {
                        resultEntity.f8209 = true;
                        detector.f51 = resultEntity;
                        C2383.m6383(r1.m6387(), r1.m6385(), str, resultEntity);
                    } else {
                        try {
                            resultEntity = (ResultEntity) C2367.m6264(C2387.m6414(r4, r1.f8257, r1.f8250, Locale.getDefault().toString()), ResultEntity.class);
                        } catch (Exception unused) {
                            resultEntity = null;
                        }
                    }
                }
                resultEntity2 = resultEntity;
                if (!resultEntity2.f8209) {
                    StringBuilder r22 = C1293.m3416("CHECKING_");
                    r22.append(resultEntity2.f8212);
                    C2383.f8246 = r22.toString();
                }
                detector.m63().f8267 = false;
                detector.m63().m6398(resultEntity2);
            }
            if (!(LivenessView.this.m112() || this.f109 == null)) {
                LivenessView.this.f96.post(new RunnableC0028(resultEntity2));
                return;
            }
            return;
            if (resultEntity == null) {
                resultEntity = new ResultEntity();
            }
            if (resultEntity.f8209) {
                detector.f51 = resultEntity;
                try {
                    str = new JSONObject(resultEntity.f8215).optString("livenessId");
                } catch (JSONException e) {
                    C2369.m6274(e.getMessage());
                }
                C2383.m6383(r1.m6387(), r1.m6385(), str, resultEntity);
                for (String str2 : detector.f54.keySet()) {
                    C2385 r3 = detector.f54.get(str2);
                    if (r3 != null) {
                        C2383.f8243.add(r3.m6387());
                    }
                }
            } else {
                C2383.m6383(null, null, null, resultEntity);
            }
            resultEntity2 = resultEntity;
            if (!resultEntity2.f8209) {
            }
            detector.m63().f8267 = false;
            detector.m63().m6398(resultEntity2);
            if (LivenessView.this.m112()) {
                return;
            }
            return;
            C2383.m6383(r1.m6387(), r1.m6385(), str, resultEntity);
            while (r1.hasNext()) {
            }
            resultEntity2 = resultEntity;
            if (!resultEntity2.f8209) {
            }
            detector.m63().f8267 = false;
            detector.m63().m6398(resultEntity2);
            if (LivenessView.this.m112()) {
            }
        }
    }

    /* renamed from: ai.advance.liveness.lib.LivenessView$㸾  reason: contains not printable characters */
    public class RunnableC0029 implements Runnable {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final /* synthetic */ String f112;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final /* synthetic */ boolean f114;

        /* renamed from: 㸾  reason: contains not printable characters */
        public final /* synthetic */ String f115;

        public RunnableC0029(boolean z, String str, String str2) {
            this.f114 = z;
            this.f112 = str;
            this.f115 = str2;
        }

        public void run() {
            if (C2387.m6407()) {
                Toast.makeText(LivenessView.this.getContext(), "This is a test account for development and debugging only!", 1).show();
            }
            LivenessView.this.f94.m76(this.f114, this.f112, this.f115);
        }
    }

    public LivenessView(Context context) {
        this(context, null);
    }

    public LivenessView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public Detector.DetectionType getCurrentDetectionType() {
        return this.f98;
    }

    @Override // ai.advance.common.camera.GuardianCameraView.AbstractC0010
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        if (m113() && !this.f101) {
            this.f101 = true;
            this.f93 = 0;
            this.f98 = this.f91.get(this.f93);
            this.f104.m66(this.f98, this);
        }
    }

    public void setLivenssCallback(AbstractC2392 k) {
        this.f94 = k;
    }

    public void setPrepareMillSeconds(long j) {
        this.f92 = j;
    }

    public void setSoundPlayEnable(boolean z) {
        C2373 k = this.f99;
        if (k != null) {
            k.m6360(z);
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView.AbstractC0010
    /* renamed from: ʬ  reason: contains not printable characters */
    public void m89() {
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: ʬ  reason: contains not printable characters */
    public void m90(int i) {
        try {
            super.m3(i);
        } catch (Exception e) {
            C2393.m6434("[" + i + "] restartCamera exception:" + e.getMessage());
        }
    }

    /* renamed from: Ԛ  reason: contains not printable characters */
    public synchronized void m91() {
        C2373 k = this.f99;
        if (k != null) {
            Handler handler = k.f8218;
            if (handler != null) {
                handler.removeMessages(1500);
            }
            k.f8219 = null;
            MediaPlayer mediaPlayer = k.f8220;
            if (mediaPlayer != null) {
                mediaPlayer.reset();
                k.f8220.release();
                k.f8220 = null;
            }
        }
        m21();
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: ؿ  reason: contains not printable characters */
    public void m92() {
        C2388.m6426();
        super.m5();
    }

    /* renamed from: ݜ  reason: contains not printable characters */
    public synchronized void m93() {
        SensorManager sensorManager;
        this.f94 = null;
        if (this.f104 != null) {
            this.f104.m63().m6183("camera_preview_size_list", this.f102);
            C2386 r1 = this.f104.m63();
            r1.m6183("camera_view_size", getMeasuredWidth() + "*" + getMeasuredHeight());
        }
        m91();
        if (this.f104 != null) {
            this.f104.f49 = null;
            this.f104.m60();
        }
        if (this.f96 != null) {
            this.f96.removeCallbacksAndMessages(null);
            this.f96 = null;
        }
        if (this.f100 != null) {
            C2368 r0 = this.f100;
            if (!(r0.f8176 == null || (sensorManager = r0.f8177) == null)) {
                sensorManager.unregisterListener(r0);
            }
        }
        if (this.f91 != null) {
            this.f91.clear();
        }
    }

    /* renamed from: ᓣ  reason: contains not printable characters */
    public final void m94() {
        this.f97 = getContext();
        this.f99 = new C2373(this.f97);
        this.f100 = new C2368(this.f97);
        this.f104 = new Detector((Activity) this.f97);
        Detector detector = this.f104;
        detector.f50 = this.f92;
        detector.f49 = this;
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: ῲ  reason: contains not printable characters */
    public int m95(Camera.Size size) {
        return m6() ? size.height : size.width;
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0023
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m96() {
        if (m112()) {
            this.f96.post(new RunnableC0024());
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: ῲ  reason: contains not printable characters */
    public synchronized void m97(int i) {
        try {
            C2388.m6426();
            super.m9(i);
        } catch (Exception e) {
            C2383.m6380(e.DEVICE_NOT_SUPPORT);
            C2393.m6434("[" + i + "] open camera exception:" + e.getMessage());
        }
        return;
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0012
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m98(long j) {
        if (m112()) {
            this.f94.m6430(j);
        }
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0012
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m99(C2385 r6) {
        Detector.WarnCode warnCode;
        if (m112() && (warnCode = r6.f8258) != this.f95 && System.currentTimeMillis() - this.f103 > 300) {
            this.f95 = warnCode;
            this.f94.m6432(this.f95);
            this.f103 = System.currentTimeMillis();
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: K  reason: contains not printable characters */
    public int m100(Camera.Size size) {
        return m6() ? size.width : size.height;
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0012
    /* renamed from: K  reason: contains not printable characters */
    public Detector.DetectionType m101(C2385 r4) {
        Exception e;
        Detector.DetectionType detectionType;
        Detector.DetectionType detectionType2 = Detector.DetectionType.DONE;
        try {
            if (this.f97 != null) {
                this.f93++;
                if (this.f93 >= this.f91.size()) {
                    if (m112()) {
                        this.f94.m6429();
                    }
                } else if (m112()) {
                    detectionType = this.f91.get(this.f93);
                    try {
                        this.f98 = detectionType;
                        this.f94.m6433();
                        return detectionType;
                    } catch (Exception e2) {
                        e = e2;
                    }
                }
            }
            return detectionType2;
        } catch (Exception e3) {
            detectionType = detectionType2;
            e = e3;
            StringBuilder r1 = C1293.m3416("an error occur :");
            r1.append(e.getMessage());
            C2369.m6274(r1.toString());
            return detectionType;
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: K  reason: contains not printable characters */
    public Camera.Size m102(Camera.Parameters parameters) {
        this.f102 = new JSONArray();
        List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
        float viewWidth = ((float) getViewWidth()) / ((float) getViewHeight());
        Camera.Size size = null;
        float f = 3.0f;
        for (Camera.Size size2 : supportedPreviewSizes) {
            JSONArray jSONArray = this.f102;
            jSONArray.put(size2.width + "*" + size2.height);
            if (size2.width >= getViewWidth() && size2.height >= getViewHeight()) {
                float f2 = ((float) size2.width) / ((float) size2.height);
                if (f2 >= viewWidth || ((double) Math.abs(f2 - viewWidth)) <= 0.01d) {
                    float f3 = f2 - viewWidth;
                    if (f3 < f) {
                        size = size2;
                        f = f3;
                    }
                }
            }
        }
        return size == null ? supportedPreviewSizes.get(0) : size;
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0012
    /* renamed from: K  reason: contains not printable characters */
    public void m103() {
        if (m112()) {
            this.f96.post(new RunnableC0026());
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m104(int i, boolean z, long j) {
        C2373 k = this.f99;
        if (k != null) {
            k.m6359(i, z, j);
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView
    /* renamed from: K  reason: contains not printable characters */
    public synchronized void m105(GuardianCameraView guardianCameraView) {
        try {
            super.m15(guardianCameraView);
        } catch (Exception e) {
            C2393.m6434("[" + this.f21 + "] startPreview exception：" + e.getMessage());
        }
        return;
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0012
    /* renamed from: K  reason: contains not printable characters */
    public void m106(Detector.DetectionFailedType detectionFailedType) {
        if (m112()) {
            this.f94.m6431(detectionFailedType, this.f98);
        }
        Detector detector = this.f104;
        if (detector != null) {
            detector.f49 = null;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public synchronized void m107(AbstractC2391 r3) {
        m91();
        if (m112()) {
            if (r3 != null) {
                ((C2377.C2381) r3).m6375();
            }
            new Thread(new RunnableC0027(r3)).start();
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public synchronized void m108(AbstractC2392 k) {
        if (C2388.f8275 == null || C2388.f8275.size() <= 0) {
            m109(k, false, Detector.DetectionType.BLINK, Detector.DetectionType.POS_YAW);
        } else {
            Detector.DetectionType[] detectionTypeArr = new Detector.DetectionType[C2388.f8275.size()];
            for (int i = 0; i < C2388.f8275.size(); i++) {
                detectionTypeArr[i] = C2388.f8275.get(i);
            }
            m109(k, C2388.f8273, detectionTypeArr);
        }
    }

    @Override // ai.advance.liveness.lib.Detector.AbstractC0023
    /* renamed from: K  reason: contains not printable characters */
    public void m110(boolean z, String str, String str2) {
        if (m112()) {
            this.f96.post(new RunnableC0029(z, str, str2));
        }
    }

    @Override // ai.advance.common.camera.GuardianCameraView.AbstractC0010
    /* renamed from: K  reason: contains not printable characters */
    public void m111(byte[] bArr, Camera.Size size) {
        this.f104.m68(bArr, size);
    }

    /* renamed from: 㗽  reason: contains not printable characters */
    public final boolean m112() {
        return (this.f96 == null || this.f94 == null) ? false : true;
    }

    /* renamed from: 䀟  reason: contains not printable characters */
    public boolean m113() {
        return true;
    }

    /* renamed from: K  reason: contains not printable characters */
    public synchronized void m109(AbstractC2392 k, boolean z, Detector.DetectionType... detectionTypeArr) {
        String str;
        String str2;
        boolean z2;
        if (TextUtils.isEmpty(C2369.f8187) && !C2387.m6421() && C2369.m6345("You haven't called the LivenessDetectionSDK.bindUser() method, we strongly recommend that you call this method to bind the user id so that we can debug the log together when something goes wrong.")) {
            Log.w("Guardian" + "-sdk", "You haven't called the LivenessDetectionSDK.bindUser() method, we strongly recommend that you call this method to bind the user id so that we can debug the log together when something goes wrong.");
        }
        if (C2388.f8274 == C2369.m6291()) {
            setAutoFocusEnable(1000);
        }
        C2383.f8246 = null;
        C2383.f8245 = null;
        C2383.f8244 = null;
        C2383.f8242 = null;
        C2383.f8243.clear();
        this.f94 = k;
        if (detectionTypeArr.length == 0) {
            str2 = "EMPTY_DETECTION_TYPE_LIST";
            str = "Detection Types need at least one term";
        } else {
            int i = 0;
            while (true) {
                z2 = true;
                if (i >= detectionTypeArr.length) {
                    break;
                }
                int i2 = C0025.f106[detectionTypeArr[i].ordinal()];
                if (i2 == 1 || i2 == 2 || i2 == 3) {
                    z2 = false;
                } else {
                    i++;
                }
            }
            z2 = false;
            if (z2) {
                this.f96 = new Handler(Looper.getMainLooper());
                this.f91 = new ArrayList<>(Arrays.asList(detectionTypeArr));
                if (z) {
                    Collections.shuffle(this.f91);
                }
                m94();
                if (this.f104.f58 == -1) {
                    C2383.m6380(e.DEVICE_NOT_SUPPORT);
                    k.m76(false, e.DEVICE_NOT_SUPPORT.toString(), "camera error");
                } else {
                    m14(C2388.f8274, this);
                }
            } else {
                str2 = "NOT_SUPPORTED_DETECTION_TYPE";
                str = "Type of detection not supported\n detectionType must in (POS_PAW,BLINK,MOUTH)";
            }
        }
        AbstractC2392 k2 = this.f94;
        if (k2 != null) {
            k2.m76(false, str2, str);
        } else {
            C2369.m6274(str);
        }
    }
}
