package g0;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.droidlogic.app.RecordAudioManager;
import com.droidlogic.app.RecordCallback;
import java.util.concurrent.atomic.AtomicBoolean;
import o0.AbstractC0651c;

/* JADX INFO: renamed from: g0.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0478b implements InterfaceC0480d {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final a f4022e = new a(null);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private C0479c f4023a = new C0479c(409600);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final AtomicBoolean f4024b = new AtomicBoolean(false);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private Handler f4025c = new HandlerC0073b(AbstractC0651c.b("AmlogicAudioRecorder"));

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final byte[] f4026d = new byte[4096];

    /* JADX INFO: renamed from: g0.b$a */
    public static final class a {
        public /* synthetic */ a(kotlin.jvm.internal.g gVar) {
            this();
        }

        private a() {
        }
    }

    /* JADX INFO: renamed from: g0.b$b, reason: collision with other inner class name */
    public static final class HandlerC0073b extends Handler {
        HandlerC0073b(Looper looper) {
            super(looper);
        }

        @Override // android.os.Handler
        public void handleMessage(Message msg) {
            kotlin.jvm.internal.l.e(msg, "msg");
            super.handleMessage(msg);
            int length = msg.arg1;
            Object obj = msg.obj;
            kotlin.jvm.internal.l.c(obj, "null cannot be cast to non-null type kotlin.ByteArray");
            byte[] bArr = (byte[]) obj;
            if (length > 0) {
                o0.g.I.saveData(bArr, 1);
                try {
                    if (bArr.length < length) {
                        length = bArr.length;
                    }
                    if (T0.a.h(10)) {
                        length /= 2;
                        for (int i2 = 0; i2 <= length - 4; i2 += 4) {
                            System.arraycopy(bArr, (i2 * 2) + 4, bArr, i2, 4);
                        }
                    }
                    int i3 = length / 4096;
                    for (int i4 = 0; i4 < i3; i4++) {
                        System.arraycopy(bArr, C0478b.this.f4026d.length * i4, C0478b.this.f4026d, 0, C0478b.this.f4026d.length);
                        C0479c c0479c = C0478b.this.f4023a;
                        if (c0479c != null) {
                            c0479c.c(C0478b.this.f4026d);
                        }
                    }
                } catch (Exception e2) {
                    Y0.c.d("AmlogicAudioRecorder", e2);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void d(C0478b this$0, byte[] bArr, int i2) {
        Message messageObtainMessage;
        kotlin.jvm.internal.l.e(this$0, "this$0");
        Handler handler = this$0.f4025c;
        if (handler == null || (messageObtainMessage = handler.obtainMessage()) == null) {
            return;
        }
        messageObtainMessage.obj = bArr;
        messageObtainMessage.arg1 = i2;
        Handler handler2 = this$0.f4025c;
        if (handler2 != null) {
            handler2.sendMessage(messageObtainMessage);
        }
    }

    @Override // g0.InterfaceC0480d
    public int configure(int i2, int i3, int i4) {
        return -11;
    }

    @Override // g0.InterfaceC0480d
    public int read(byte[] audioData, int i2, int i3) throws InterruptedException {
        kotlin.jvm.internal.l.e(audioData, "audioData");
        while (this.f4024b.get()) {
            C0479c c0479c = this.f4023a;
            if (i3 <= (c0479c != null ? c0479c.e() : 0)) {
                C0479c c0479c2 = this.f4023a;
                if (c0479c2 != null) {
                    return c0479c2.b(audioData, i2, i3);
                }
                return 0;
            }
            if (this.f4024b.get()) {
                Thread.sleep(10L);
            }
        }
        Y0.c.a("AmlogicAudioRecorder", "read: end");
        return -1;
    }

    @Override // g0.InterfaceC0480d
    public int release() {
        this.f4024b.set(false);
        C0479c c0479c = this.f4023a;
        if (c0479c != null) {
            c0479c.a();
        }
        this.f4023a = null;
        return 0;
    }

    @Override // g0.InterfaceC0480d
    public int startRecording() {
        Y0.c.a("AmlogicAudioRecorder", "startRecording: ");
        o0.g.I.start(1);
        try {
            if (this.f4023a == null) {
                this.f4023a = new C0479c(409600);
            }
            boolean zStartRecordAudio = RecordAudioManager.getInstance().startRecordAudio(new RecordCallback() { // from class: g0.a
                @Override // com.droidlogic.app.RecordCallback
                public final void onDataCallback(byte[] bArr, int i2) {
                    C0478b.d(this.f4021a, bArr, i2);
                }
            });
            this.f4024b.set(true);
            Y0.c.a("AmlogicAudioRecorder", "startRecording: " + zStartRecordAudio);
            return zStartRecordAudio ? 0 : -1;
        } catch (Throwable th) {
            Y0.c.c("AmlogicAudioRecorder", "startRecording: ", th);
            return -1;
        }
    }

    @Override // g0.InterfaceC0480d
    public int stop() {
        try {
            this.f4024b.set(false);
            o0.g.I.stop(1);
            Handler handler = this.f4025c;
            if (handler != null) {
                handler.removeCallbacksAndMessages(null);
            }
            RecordAudioManager.getInstance().stopRecordAudio();
            return 0;
        } catch (Throwable th) {
            Y0.c.c("AmlogicAudioRecorder", "stop: ", th);
            return -1;
        }
    }
}
