package com.ifpdos.factorytest;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.ifpdos.Utils.MicCheck;
import com.ifpdos.Utils.ToastUtils;
import com.ifpdos.factorytest.mic.IUdiMicRecordContract;
import com.ifpdos.factorytest.mic.MicRecordHandler;
import com.ifpdos.factorytest.mtr.HDMIIngestAudioHandler;
import com.ifpdos.factorytest.udi.JsonUtils;
import com.ifpdos.factorytest.udi.ProtocolDefineKt;
import com.ifpdos.factorytest.util.AudioUtils;
import com.ifpdos.factorytest.util.StringUtil;
import com.ifpdos.factorytest.util.UdiTestConfigParser;
import com.ifpdos.factorytest.util.VolumeUtil;
import com.ifpdos.udi.sdk.service.sdk.server.Response;
import com.seewo.sdk.SDKAudioHelper;
import java.io.File;

/* JADX INFO: loaded from: classes.dex */
public class MicrophoneArrayTestActivity extends Activity implements View.OnClickListener, IUdiMicRecordContract {
    private static final int DEFAULT_VOLUME = 100;
    private static int RECORD_SECONDS = 3;
    private static final String TAG = "MicTestActivity";
    private static String soundFilePath = "/sdcard/Music/sound.wav";
    Button fail;
    private int lastVolume;
    private boolean mIsFromUdi;
    private String mOriMicId;
    Button pass;
    Button play;
    Button record;
    File soundFile;
    Button stop;
    String testItem;
    MediaRecorder mRecorder = null;
    MediaPlayer mPlayer = null;
    private MediaPlayer.OnCompletionListener mAudioCompleted = null;
    int Count = 0;
    Handler handler = null;
    Runnable runnable = null;
    boolean isRecorder = false;
    boolean micExist = false;
    int retryCount = 0;
    int maxCount = 3;

    @Override // android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.lastVolume = SDKAudioHelper.I.getVolume();
        boolean booleanExtra = getIntent().getBooleanExtra(ProtocolDefineKt.KEY_FROM_UDI, false);
        this.mIsFromUdi = booleanExtra;
        if (!booleanExtra) {
            Log.d(TAG, "not from udi api,set volume 100");
            VolumeUtil.INSTANCE.setVolume(100, true);
        } else {
            MicRecordHandler.INSTANCE.registerIUdiMicRecordImpl(this);
        }
        setContentView(R.layout.activity_microphone_array_test);
        this.record = (Button) findViewById(R.id.record);
        this.play = (Button) findViewById(R.id.play);
        this.pass = (Button) findViewById(R.id.mic_pass);
        this.fail = (Button) findViewById(R.id.mic_fail);
        this.record.setOnClickListener(this);
        this.play.setOnClickListener(this);
        this.mRecorder = new MediaRecorder();
        this.soundFile = new File(soundFilePath);
        this.handler = new Handler(Looper.getMainLooper());
        String stringExtra = getIntent().getStringExtra(StringUtil.TEST_ITEM_INTENT);
        this.testItem = stringExtra;
        if (stringExtra != null && stringExtra.equals(StringUtil.MIC_IN_TEST_INTENT)) {
            ((TextView) findViewById(R.id.micTestTextView)).setText(getResources().getString(R.string.test_mic_in_tips));
            if (UdiTestConfigParser.isMdepAudioTest()) {
                initOriMicInId();
            }
        }
        Log.d(TAG, "testItem = " + this.testItem);
        this.mAudioCompleted = new MediaPlayer.OnCompletionListener() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity.1
            @Override // android.media.MediaPlayer.OnCompletionListener
            public void onCompletion(MediaPlayer mediaPlayer) {
                Log.d(MicrophoneArrayTestActivity.TAG, "onCompletion===");
                MicrophoneArrayTestActivity.this.record.setEnabled(true);
                MicrophoneArrayTestActivity.this.play.setEnabled(true);
                MicrophoneArrayTestActivity.this.play.setText(R.string.play);
                MicrophoneArrayTestActivity.this.mPlayer.stop();
                MicrophoneArrayTestActivity.this.mPlayer.release();
                MicrophoneArrayTestActivity.this.mPlayer = null;
            }
        };
        this.pass.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda2
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$0(view);
            }
        });
        this.fail.setOnClickListener(new View.OnClickListener() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda3
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                this.f$0.lambda$onCreate$1(view);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$0(View view) {
        setResult(1);
        Log.d(TAG, "通过");
        finish();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCreate$1(View view) {
        setResult(2);
        Log.d(TAG, "失败");
        finish();
    }

    private void initOriMicInId() {
        String currentAudioInDeviceId = AudioUtils.getCurrentAudioInDeviceId();
        this.mOriMicId = currentAudioInDeviceId;
        if (currentAudioInDeviceId == null) {
            this.mOriMicId = AudioUtils.ID_VIRTUAL_AUTO;
        }
        Log.d(TAG, "initOriMicInId: " + this.mOriMicId);
        AudioUtils.setAudioInDevice(AudioUtils.getMdepAudioInId());
    }

    private void releaseMic() {
        try {
            this.mRecorder.setOnErrorListener(null);
            this.mRecorder.setOnInfoListener(null);
            this.mRecorder.setPreviewDisplay(null);
            if (this.isRecorder) {
                this.mRecorder.stop();
            }
            this.mRecorder.release();
            this.mRecorder = null;
            Log.e(TAG, "releaseMic: ");
        } catch (Exception e) {
            Log.i("Exception", Log.getStackTraceString(e));
        }
    }

    @Override // android.app.Activity
    public void onDestroy() {
        VolumeUtil.INSTANCE.setVolume(this.lastVolume, false);
        releaseMic();
        releaseMediaPlayer();
        this.soundFile = null;
        this.handler.removeCallbacksAndMessages(this);
        this.handler.removeCallbacks(this.runnable);
        this.handler = null;
        this.runnable = null;
        if (this.mIsFromUdi) {
            MicRecordHandler.INSTANCE.unregisterIUdiMicRecordImpl(this);
        }
        String str = this.testItem;
        if (str != null && str.equals(StringUtil.MIC_IN_TEST_INTENT) && UdiTestConfigParser.isMdepAudioTest()) {
            AudioUtils.setAudioInDevice(this.mOriMicId);
        }
        super.onDestroy();
    }

    @Override // android.app.Activity
    protected void onResume() {
        super.onResume();
    }

    @Override // android.app.Activity
    protected void onStop() {
        super.onStop();
    }

    private void releaseMediaPlayer() {
        try {
            MediaPlayer mediaPlayer = this.mPlayer;
            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying()) {
                    this.mPlayer.stop();
                }
                this.mPlayer.release();
                this.mPlayer = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setClickEnabled(boolean z) {
        this.record.setEnabled(z);
        this.play.setEnabled(z);
        this.pass.setEnabled(z);
        this.fail.setEnabled(z);
    }

    public void initRecorder() {
        try {
            Log.d(TAG, "initRecorder");
            Handler handler = this.handler;
            if (handler != null) {
                handler.removeCallbacksAndMessages(this);
                this.handler.removeCallbacks(this.runnable);
            }
            if (this.mRecorder == null) {
                this.mRecorder = new MediaRecorder();
            }
            this.isRecorder = true;
            this.Count = 0;
            if (UdiTestConfigParser.isMdepAudioTest()) {
                this.mRecorder.setAudioSource(7);
                this.mRecorder.setAudioSamplingRate(HDMIIngestAudioHandler.recordSampleRteHz);
            } else {
                this.mRecorder.setAudioSource(1);
            }
            this.mRecorder.setOutputFormat(2);
            this.mRecorder.setAudioEncoder(3);
            this.mRecorder.setOutputFile(this.soundFile.getAbsolutePath());
            this.mRecorder.prepare();
            this.mRecorder.start();
            this.handler.post(new Runnable() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda0
                @Override // java.lang.Runnable
                public final void run() {
                    this.f$0.lambda$initRecorder$2();
                }
            });
            startRecord();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.toString());
            this.isRecorder = false;
            releaseMic();
            if (this.retryCount < this.maxCount) {
                Log.d(TAG, "获取麦克风失败，重试次数：" + this.retryCount);
                this.mRecorder = new MediaRecorder();
                setClickEnabled(false);
                if (this.retryCount == 0) {
                    ToastUtils.showToast(this, getString(R.string.get_microphone_device_fail_retry));
                }
                this.handler.postDelayed(new Runnable() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda1
                    @Override // java.lang.Runnable
                    public final void run() {
                        this.f$0.lambda$initRecorder$3();
                    }
                }, 1000L);
                return;
            }
            this.retryCount = 0;
            ToastUtils.showToast(this, getString(R.string.recording_error_device_not_exist));
            setClickEnabled(true);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$initRecorder$2() {
        this.record.setText(getString(R.string.recording));
        setClickEnabled(false);
        ToastUtils.showToast(this, getString(R.string.start_recording));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$initRecorder$3() {
        this.retryCount++;
        initRecorder();
    }

    private boolean checkFile() {
        File file = this.soundFile;
        return (file == null || !file.exists() || this.soundFile.length() == 0) ? false : true;
    }

    private boolean checkMicAndStartRecord() {
        if (this.testItem.equals(StringUtil.MIC_IN_TEST_INTENT)) {
            this.micExist = MicCheck.micInExist();
        } else if (this.testItem.equals(StringUtil.MICROPHONE_ARRAY_TEST_INTENT)) {
            this.micExist = MicCheck.micExist();
        }
        if (this.micExist) {
            initRecorder();
            return true;
        }
        this.handler.post(new Runnable() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda5
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$checkMicAndStartRecord$4();
            }
        });
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$checkMicAndStartRecord$4() {
        if (this.testItem.equals(StringUtil.MIC_IN_TEST_INTENT)) {
            ToastUtils.showToast(this, getResources().getString(R.string.test_mic_in_error_tips));
        } else if (this.testItem.equals(StringUtil.MICROPHONE_ARRAY_TEST_INTENT)) {
            ToastUtils.showToast(this, getString(R.string.microphone_node_get_fail));
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.record) {
            Log.d(TAG, "点击录音测试，检测麦克风资源");
            checkMicAndStartRecord();
            return;
        }
        if (id == R.id.play) {
            Handler handler = this.handler;
            if (handler != null) {
                handler.removeCallbacksAndMessages(this);
                this.handler.removeCallbacks(this.runnable);
            }
            MediaPlayer mediaPlayer = this.mPlayer;
            if (mediaPlayer != null) {
                mediaPlayer.reset();
            }
            this.mPlayer = new MediaPlayer();
            Log.d(TAG, "play 11111===");
            if (checkFile()) {
                try {
                    ToastUtils.showToast(this, getString(R.string.play));
                    MediaPlayer.OnCompletionListener onCompletionListener = this.mAudioCompleted;
                    if (onCompletionListener != null) {
                        this.mPlayer.setOnCompletionListener(onCompletionListener);
                    }
                    this.play.setText(getString(R.string.playback_recording));
                    this.record.setEnabled(false);
                    this.play.setEnabled(false);
                    this.mPlayer.reset();
                    this.mPlayer.setAudioStreamType(3);
                    this.mPlayer.setDataSource(soundFilePath);
                    this.mPlayer.setVolume(1.0f, 1.0f);
                    this.mPlayer.prepare();
                    this.mPlayer.start();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    this.play.setText(getString(R.string.play));
                    this.record.setEnabled(true);
                    this.play.setEnabled(true);
                    Log.e(TAG, "播放录音出错，原因：" + e.toString());
                    ToastUtils.showToast(this, getString(R.string.playback_error_retry));
                    return;
                }
            }
            Log.d(TAG, "点击播放录音，录音文件为空");
            ToastUtils.showToast(this, getString(R.string.recording_file_empty_check));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean stopRecord() {
        final String string;
        Log.d(TAG, "record 2222===");
        boolean z = false;
        this.isRecorder = false;
        try {
            this.mRecorder.setOnErrorListener(null);
            this.mRecorder.setOnInfoListener(null);
            this.mRecorder.setPreviewDisplay(null);
            this.mRecorder.stop();
            string = getString(R.string.finish_recording_playback);
            this.Count = 0;
            Log.d(TAG, "录音完成");
            z = true;
        } catch (Exception e) {
            this.Count = 0;
            String string2 = getString(R.string.stop_recording_fail);
            Log.e(TAG, Log.getStackTraceString(e));
            string = string2;
        }
        this.handler.post(new Runnable() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity$$ExternalSyntheticLambda4
            @Override // java.lang.Runnable
            public final void run() {
                this.f$0.lambda$stopRecord$5(string);
            }
        });
        return z;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$stopRecord$5(String str) {
        this.record.setText(R.string.record);
        ToastUtils.showToast(this, str);
        setClickEnabled(true);
    }

    private void startRecord() {
        Runnable runnable = new Runnable() { // from class: com.ifpdos.factorytest.MicrophoneArrayTestActivity.2
            @Override // java.lang.Runnable
            public void run() {
                if (MicrophoneArrayTestActivity.this.Count >= MicrophoneArrayTestActivity.RECORD_SECONDS) {
                    if (MicrophoneArrayTestActivity.this.isRecorder) {
                        MicrophoneArrayTestActivity.this.stopRecord();
                    }
                } else {
                    MicrophoneArrayTestActivity.this.Count++;
                    MicrophoneArrayTestActivity.this.handler.postDelayed(this, 1000L);
                }
            }
        };
        this.runnable = runnable;
        runnable.run();
    }

    @Override // com.ifpdos.factorytest.mic.IUdiMicRecordContract
    public Response exitMicRecordActivity() {
        finish();
        return Response.INSTANCE.makeOkResponse();
    }

    @Override // com.ifpdos.factorytest.mic.IUdiMicRecordContract
    public Response startRecord(String str) {
        if (str == null || str.isEmpty()) {
            Log.e(TAG, "file path error");
            return new Response(400, JsonUtils.INSTANCE.toErrorBody("file path error"));
        }
        File parentFile = new File(str).getParentFile();
        if (parentFile == null || !parentFile.exists()) {
            return new Response(400, JsonUtils.INSTANCE.toErrorBody("file dir not exists"));
        }
        if (this.isRecorder) {
            return new Response(403, JsonUtils.INSTANCE.toErrorBody("recording,don't start again"));
        }
        soundFilePath = str;
        this.soundFile = new File(soundFilePath);
        RECORD_SECONDS = 1800;
        if (!checkMicAndStartRecord()) {
            return new Response(500, JsonUtils.INSTANCE.toErrorBody("start record fail"));
        }
        return Response.INSTANCE.makeOkResponse();
    }

    @Override // com.ifpdos.factorytest.mic.IUdiMicRecordContract
    public Response finishRecord() {
        if (stopRecord()) {
            return Response.INSTANCE.makeOkResponse();
        }
        return Response.INSTANCE.getINTERNAL_SERVER_ERROR();
    }
}
