package com.seewo.factoryaging.mic.i;

import android.media.AudioRecord;
import android.util.Log;
import com.seewo.factoryaging.mic.g;
import com.seewo.factoryaging.p0.c;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/* JADX INFO: compiled from: RecorderThread.java */
/* JADX INFO: loaded from: classes.dex */
public class a extends h.c.a.a.a {
    private short[] d;
    private AudioRecord f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f323g;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private b f325i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private Timer f326j;
    private DataOutputStream e = null;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private boolean f324h = false;

    /* JADX INFO: renamed from: com.seewo.factoryaging.mic.i.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: RecorderThread.java */
    class C0011a extends TimerTask {
        C0011a(a aVar) {
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            boolean zA = g.a.a();
            if (zA) {
                com.seewo.factoryaging.p0.a.a.e("麦克风", "PASS");
                return;
            }
            com.seewo.factoryaging.p0.a.a.e("麦克风", "FAIL:[" + zA + "," + g.a.c() + "]");
        }
    }

    /* JADX INFO: compiled from: RecorderThread.java */
    public interface b {
        void a(double d);
    }

    public a(b bVar, AudioRecord audioRecord, File file, int i2) {
        this.f323g = i2;
        this.d = new short[i2];
        this.f = audioRecord;
        this.f325i = bVar;
    }

    private void d() {
        this.f324h = false;
        if (this.f != null) {
            Log.i("RecorderThread", "AudioRecord release");
            this.f.release();
            this.f = null;
        }
        try {
            if (this.e != null) {
                this.e.flush();
                this.e.close();
                this.e = null;
            }
        } catch (IOException e) {
            Log.e("RecorderThread", "release audio  error (code = 1): " + e.toString());
            Log.getStackTraceString(e);
        }
    }

    public void e() {
        this.f324h = true;
        start();
        if (c.a.a()) {
            Timer timer = new Timer();
            this.f326j = timer;
            timer.schedule(new C0011a(this), 0L, 60000L);
        }
    }

    public void f() {
        this.f325i = null;
        c();
        d();
        Timer timer = this.f326j;
        if (timer != null) {
            timer.cancel();
            this.f326j = null;
        }
    }

    @Override // java.lang.Thread
    public void interrupt() {
        super.interrupt();
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public void run() {
        try {
            try {
                this.f.startRecording();
                while (this.f324h) {
                    int i2 = this.f.read(this.d, 0, this.f323g);
                    if (i2 < 0) {
                        Log.d("RecorderThread", "len=" + i2);
                    } else {
                        synchronized (this) {
                            if (i2 > 0) {
                                long j2 = 0;
                                for (short s : this.d) {
                                    j2 += (long) (s * s);
                                }
                                double dLog10 = Math.log10(j2 / ((double) i2)) * 10.0d;
                                Thread.sleep(80L);
                                if (this.f325i != null) {
                                    this.f325i.a(dLog10);
                                }
                            }
                        }
                    }
                }
                d();
            } catch (Exception e) {
                Log.e("RecorderThread", "record occur error : " + e);
                com.seewo.factoryaging.p0.a.a.e("麦克风", "FAIL:AudioRecord occur error");
            }
        } finally {
            d();
        }
    }
}
