package com.ifpdos.factorytest;

import android.content.Context;
import android.content.Intent;
import android.hardware.camera2.CameraManager;
import android.util.AttributeSet;
import android.util.Log;
import com.ifpdos.factorytest.mtr.CameraUtils;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class HdmiInTest extends TestItem {
    private static final int ASK_HDMI_CAMERA_DETECTION = 100;
    private static final String ERROR_CAMERA_ID = "-1";
    private static final String TAG = "HdmiInTest";
    private AskDialog mAskDialog;
    private Disposable mCheckDisposable;
    private Disposable mDetectionDisposable;
    private boolean mIsHdmiCameraFound;
    private Disposable mPollingDisposable;

    public HdmiInTest(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mIsHdmiCameraFound = false;
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        if (this.mContext == null) {
            return false;
        }
        this.mStarted = true;
        this.mIsHdmiCameraFound = false;
        startHdmiCameraDetection();
        return true;
    }

    private void startHdmiCameraDetection() {
        disposeIfNeeded(this.mDetectionDisposable);
        this.mDetectionDisposable = Observable.fromCallable(new Callable() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda6
            @Override // java.util.concurrent.Callable
            public final Object call() {
                return this.f$0.lambda$startHdmiCameraDetection$0();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda7
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$startHdmiCameraDetection$1((String) obj);
            }
        }, new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda8
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$startHdmiCameraDetection$2((Throwable) obj);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ String lambda$startHdmiCameraDetection$0() throws Exception {
        try {
            CameraManager cameraManager = (CameraManager) this.mContext.getSystemService("camera");
            String[] strArr = (String[]) CameraManager.class.getMethod("getCameraIdList", new Class[0]).invoke(cameraManager, new Object[0]);
            if (strArr != null && strArr.length != 0) {
                for (String str : strArr) {
                    String cameraType = CameraUtils.INSTANCE.getCameraType(cameraManager, str);
                    Log.d(TAG, "cameraId = " + str + ", cameraType = " + cameraType);
                    if (CameraUtils.CAMERA_TYPE_HDMI_INGEST.equals(cameraType)) {
                        Log.d(TAG, "startHdmiCameraDetection: found HDMI ingest camera with id = " + str);
                        return str;
                    }
                }
                return "-1";
            }
            Log.e(TAG, "startHdmiCameraDetection: cameraIdList is null or empty");
            return "-1";
        } catch (Exception e) {
            Log.e(TAG, "can not getSystemService camera", e);
            return "-1";
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startHdmiCameraDetection$1(String str) throws Exception {
        if (!"-1".equals(str)) {
            this.mIsHdmiCameraFound = true;
            Log.d(TAG, "HDMI camera found successfully");
            ((MainTestActivity) this.mContext).startActivityForResult(new Intent(this.mContext, (Class<?>) HdmiInTestActivity.class), this.mId.ordinal());
            this.mContext.stopService(new Intent(this.mContext, (Class<?>) FloatNavbarService.class));
            return;
        }
        Log.e(TAG, "startHdmiCameraDetection: can't find HDMI ingest camera");
        showHdmiCameraNotFoundDialog();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startHdmiCameraDetection$2(Throwable th) throws Exception {
        Log.e(TAG, "Camera detection error", th);
        showHdmiCameraNotFoundDialog();
    }

    private void showHdmiCameraNotFoundDialog() {
        this.mAskDialog = new AskDialog(this.mContext, true, null, false, null, this.mContext.getString(R.string.hdmi_camera_detection), this.mContext.getString(R.string.detecting_hdmi_camera), new OnAskDialogResultListener() { // from class: com.ifpdos.factorytest.HdmiInTest.1
            @Override // com.ifpdos.factorytest.OnAskDialogResultListener
            public void onAskDialogResult(Context context, boolean z) {
                Log.e(HdmiInTest.TAG, "User cancelled HDMI camera test");
                HdmiInTest hdmiInTest = HdmiInTest.this;
                hdmiInTest.testDone(false, hdmiInTest.mContext.getString(R.string.user_cancelled_test));
            }
        }, 100);
        startPollingHdmiCamera();
    }

    private void startPollingHdmiCamera() {
        Log.d(TAG, "Start polling HDMI camera detection");
        this.mPollingDisposable = Observable.interval(500L, TimeUnit.MILLISECONDS).take(4L).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda3
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$startPollingHdmiCamera$3((Long) obj);
            }
        }, new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda4
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$startPollingHdmiCamera$4((Throwable) obj);
            }
        }, new Action() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda5
            @Override // io.reactivex.functions.Action
            public final void run() throws Exception {
                this.f$0.lambda$startPollingHdmiCamera$5();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startPollingHdmiCamera$3(Long l) throws Exception {
        Log.d(TAG, "Polling HDMI camera detection, attempt: " + (l.longValue() + 1));
        checkHdmiCamera();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startPollingHdmiCamera$4(Throwable th) throws Exception {
        Log.e(TAG, "Polling error", th);
        AskDialog askDialog = this.mAskDialog;
        if (askDialog != null) {
            askDialog.dismiss();
        }
        testDone(false, this.mContext.getString(R.string.detection_process_error));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$startPollingHdmiCamera$5() throws Exception {
        if (this.mIsHdmiCameraFound) {
            return;
        }
        Log.e(TAG, "HDMI camera not found after polling");
        AskDialog askDialog = this.mAskDialog;
        if (askDialog != null) {
            askDialog.dismiss();
        }
        testDone(false, this.mContext.getString(R.string.hdmi_camera_not_found));
    }

    private void checkHdmiCamera() {
        disposeIfNeeded(this.mCheckDisposable);
        this.mCheckDisposable = Observable.fromCallable(new Callable() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda0
            @Override // java.util.concurrent.Callable
            public final Object call() {
                return this.f$0.lambda$checkHdmiCamera$6();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda1
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) throws Exception {
                this.f$0.lambda$checkHdmiCamera$7((String) obj);
            }
        }, new Consumer() { // from class: com.ifpdos.factorytest.HdmiInTest$$ExternalSyntheticLambda2
            @Override // io.reactivex.functions.Consumer
            public final void accept(Object obj) {
                Log.e(HdmiInTest.TAG, "Polling check error", (Throwable) obj);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ String lambda$checkHdmiCamera$6() throws Exception {
        try {
            CameraManager cameraManager = (CameraManager) this.mContext.getSystemService("camera");
            String[] strArr = (String[]) CameraManager.class.getMethod("getCameraIdList", new Class[0]).invoke(cameraManager, new Object[0]);
            if (strArr == null) {
                return "-1";
            }
            for (String str : strArr) {
                String cameraType = CameraUtils.INSTANCE.getCameraType(cameraManager, str);
                Log.d(TAG, "Polling - cameraId = " + str + ", cameraType = " + cameraType);
                if (CameraUtils.CAMERA_TYPE_HDMI_INGEST.equals(cameraType)) {
                    Log.d(TAG, "Polling - found HDMI ingest camera with id = " + str);
                    return str;
                }
            }
            return "-1";
        } catch (Exception e) {
            Log.e(TAG, "Polling check error", e);
            return "-1";
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$checkHdmiCamera$7(String str) throws Exception {
        if ("-1".equals(str)) {
            return;
        }
        this.mIsHdmiCameraFound = true;
        Log.d(TAG, "HDMI camera found during polling");
        disposeIfNeeded(this.mPollingDisposable);
        AskDialog askDialog = this.mAskDialog;
        if (askDialog != null) {
            askDialog.dismiss();
        }
        ((MainTestActivity) this.mContext).startActivityForResult(new Intent(this.mContext, (Class<?>) HdmiInTestActivity.class), this.mId.ordinal());
        this.mContext.stopService(new Intent(this.mContext, (Class<?>) FloatNavbarService.class));
    }

    private void disposeIfNeeded(Disposable disposable) {
        if (disposable == null || disposable.isDisposed()) {
            return;
        }
        disposable.dispose();
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onDestroy() {
        super.onDestroy();
        disposeIfNeeded(this.mPollingDisposable);
        disposeIfNeeded(this.mDetectionDisposable);
        disposeIfNeeded(this.mCheckDisposable);
        AskDialog askDialog = this.mAskDialog;
        if (askDialog != null) {
            askDialog.dismiss();
        }
    }
}
