package g0;

import H0.q;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import g0.C0491o;
import l0.C0617a;
import n0.AbstractC0644a;
import o0.AbstractC0651c;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final d f4068m = new d(null);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final y1.a f4069a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final y1.a f4070b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final boolean f4071c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final Context f4072d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private AbstractC0487k f4073e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private AbstractC0487k f4074f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final C0617a f4075g;

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final e f4078j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private byte[] f4079k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private byte[] f4080l;

    /* JADX INFO: renamed from: g0.o$a */
    static final class a extends kotlin.jvm.internal.m implements y1.a {

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

        a() {
            super(0);
        }

        @Override // y1.a
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public final AbstractC0487k invoke() {
            return new C0490n();
        }
    }

    /* JADX INFO: renamed from: g0.o$b */
    static final class b extends kotlin.jvm.internal.m implements y1.a {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final b f4082b = new b();

        b() {
            super(0);
        }

        @Override // y1.a
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public final AbstractC0487k invoke() {
            return new C0488l();
        }
    }

    /* JADX INFO: renamed from: g0.o$c */
    public static final class c implements C0617a.InterfaceC0082a {
        c() {
        }

        @Override // l0.C0617a.InterfaceC0082a
        public void a(q last, q current) {
            kotlin.jvm.internal.l.e(last, "last");
            kotlin.jvm.internal.l.e(current, "current");
            Y0.c.a("SystemRecorder", "onSourceChange: lastSource=" + last + ", newSource=" + current);
            if (o0.h.f5991a.o()) {
                if (current != q.ANDROID) {
                    AbstractC0487k abstractC0487k = C0491o.this.f4074f;
                    if (abstractC0487k != null) {
                        abstractC0487k.c(0);
                        return;
                    }
                    return;
                }
                AbstractC0487k abstractC0487k2 = C0491o.this.f4074f;
                if (abstractC0487k2 != null) {
                    abstractC0487k2.d();
                }
            }
        }
    }

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

        private d() {
        }
    }

    /* JADX INFO: renamed from: g0.o$e */
    public static final class e extends BroadcastReceiver {
        e() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final void b(C0491o this$0) {
            kotlin.jvm.internal.l.e(this$0, "this$0");
            AbstractC0487k abstractC0487k = this$0.f4073e;
            if (abstractC0487k != null) {
                abstractC0487k.d();
            }
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            AbstractC0487k abstractC0487k;
            Y0.c.a("SystemRecorder", "onReceive: " + intent);
            if (kotlin.jvm.internal.l.a(intent != null ? intent.getAction() : null, "android.intent.action.HEADSET_PLUG")) {
                int intExtra = intent.getIntExtra("state", -1);
                Y0.c.a("SystemRecorder", "onReceive(ACTION_HEADSET_PLUG): state = " + intExtra);
                if ((intExtra == 1 || intExtra == 0) && !C0491o.this.f4077i && o0.h.f5991a.k() && C0491o.this.f4073e != null && (abstractC0487k = C0491o.this.f4073e) != null && abstractC0487k.b()) {
                    AbstractC0487k abstractC0487k2 = C0491o.this.f4073e;
                    if (abstractC0487k2 != null) {
                        abstractC0487k2.c(1);
                    }
                    Handler handlerA = AbstractC0651c.a();
                    final C0491o c0491o = C0491o.this;
                    handlerA.postDelayed(new Runnable() { // from class: g0.p
                        @Override // java.lang.Runnable
                        public final void run() {
                            C0491o.e.b(c0491o);
                        }
                    }, 3000L);
                }
                if (C0491o.this.f4077i) {
                    C0491o.this.f4077i = false;
                }
            }
        }
    }

    private C0491o(Context context, y1.a aVar, y1.a aVar2, boolean z2) {
        this.f4069a = aVar;
        this.f4070b = aVar2;
        this.f4071c = z2;
        this.f4072d = context != null ? context.getApplicationContext() : null;
        C0617a c0617a = new C0617a();
        this.f4075g = c0617a;
        this.f4078j = new e();
        if (z2) {
            c0617a.a(new c());
            e();
        }
    }

    private final void e() {
        if (this.f4071c) {
            this.f4075g.b();
            if (this.f4076h) {
                return;
            }
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.HEADSET_PLUG");
            Context context = this.f4072d;
            if (context != null) {
                context.registerReceiver(this.f4078j, intentFilter);
            }
            this.f4076h = this.f4072d != null;
            this.f4077i = true;
        }
    }

    private final int f(String str, AbstractC0487k abstractC0487k) {
        int iRelease;
        int iStop = 0;
        if (abstractC0487k == null) {
            return 0;
        }
        if (abstractC0487k.b()) {
            try {
                iStop = abstractC0487k.stop();
            } catch (Throwable th) {
                Y0.c.b("SystemRecorder", "releaseRecorder: " + str + " stop failed, " + th);
                iStop = -1;
            }
            if (iStop != 0) {
                Y0.c.b("SystemRecorder", "releaseRecorder: " + str + " stop result=" + iStop);
            }
        }
        try {
            iRelease = abstractC0487k.release();
        } catch (Throwable th2) {
            Y0.c.b("SystemRecorder", "releaseRecorder: " + str + " release failed, " + th2);
            iRelease = -1;
        }
        if (iRelease == 0) {
            return iStop;
        }
        Y0.c.b("SystemRecorder", "releaseRecorder: " + str + " release result=" + iRelease);
        return -1;
    }

    private final int g() {
        AbstractC0487k abstractC0487k = this.f4074f;
        AbstractC0487k abstractC0487k2 = this.f4073e;
        this.f4074f = null;
        this.f4073e = null;
        this.f4079k = null;
        this.f4080l = null;
        return (f("mic", abstractC0487k) == 0 && f("system", abstractC0487k2) == 0) ? 0 : -1;
    }

    private final int h() {
        int i2;
        if (!this.f4071c) {
            return 0;
        }
        try {
            this.f4075g.c();
            i2 = 0;
        } catch (Throwable th) {
            Y0.c.b("SystemRecorder", "unregisterPlatformCallbacks: source observer failed, " + th);
            i2 = -1;
        }
        if (this.f4076h) {
            try {
                Context context = this.f4072d;
                if (context != null) {
                    context.unregisterReceiver(this.f4078j);
                }
            } catch (Throwable th2) {
                try {
                    Y0.c.b("SystemRecorder", "unregisterPlatformCallbacks: headset receiver failed, " + th2);
                    return -1;
                } finally {
                    this.f4076h = false;
                }
            }
        }
        return i2;
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x0051  */
    @Override // g0.InterfaceC0480d
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public int configure(int r6, int r7, int r8) {
        /*
            r5 = this;
            java.lang.StringBuilder r0 = new java.lang.StringBuilder
            r0.<init>()
            java.lang.String r1 = "configure: sampleRateInHz="
            r0.append(r1)
            r0.append(r6)
            java.lang.String r1 = ", channelCount="
            r0.append(r1)
            r0.append(r7)
            java.lang.String r1 = ", audioSource="
            r0.append(r1)
            r0.append(r8)
            java.lang.String r0 = r0.toString()
            java.lang.String r1 = "SystemRecorder"
            Y0.c.a(r1, r0)
            int r0 = r5.g()
            r2 = -1
            if (r0 == 0) goto L33
            java.lang.String r6 = "configure: failed to release previous recorders"
            Y0.c.b(r1, r6)
            return r2
        L33:
            r5.e()
            r0 = r8 & 2
            r3 = 2
            if (r0 != r3) goto L51
            java.lang.String r0 = "configure: use SystemMicRecorder"
            Y0.c.a(r1, r0)
            y1.a r0 = r5.f4069a
            java.lang.Object r0 = r0.invoke()
            g0.k r0 = (g0.AbstractC0487k) r0
            r5.f4074f = r0
            if (r0 == 0) goto L51
            int r0 = r0.configure(r6, r7, r8)
            goto L52
        L51:
            r0 = r2
        L52:
            java.lang.StringBuilder r3 = new java.lang.StringBuilder
            r3.<init>()
            java.lang.String r4 = "configure: micRecorder configure result="
            r3.append(r4)
            r3.append(r0)
            java.lang.String r3 = r3.toString()
            Y0.c.a(r1, r3)
            r3 = r8 & 1
            r4 = 1
            if (r3 != r4) goto L81
            java.lang.String r0 = "configure: use SystemAudioRecorder"
            Y0.c.a(r1, r0)
            y1.a r0 = r5.f4070b
            java.lang.Object r0 = r0.invoke()
            g0.k r0 = (g0.AbstractC0487k) r0
            r5.f4073e = r0
            if (r0 == 0) goto L80
            int r2 = r0.configure(r6, r7, r8)
        L80:
            return r2
        L81:
            g0.k r6 = r5.f4074f
            if (r6 != 0) goto L91
            g0.k r6 = r5.f4073e
            if (r6 != 0) goto L91
            java.lang.String r6 = "configure: audioSource is not support"
            Y0.c.b(r1, r6)
            r6 = -10
            return r6
        L91:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: g0.C0491o.configure(int, int, int):int");
    }

    @Override // g0.InterfaceC0480d
    public int read(byte[] audioData, int i2, int i3) {
        int i4;
        int i5;
        kotlin.jvm.internal.l.e(audioData, "audioData");
        AbstractC0487k abstractC0487k = this.f4074f;
        if (abstractC0487k != null) {
            if (this.f4073e == null) {
                if (abstractC0487k != null) {
                    return abstractC0487k.read(audioData, i2, i3);
                }
                return -1;
            }
            if (this.f4079k == null) {
                this.f4079k = new byte[i3];
            }
            if (abstractC0487k != null) {
                byte[] bArr = this.f4079k;
                kotlin.jvm.internal.l.b(bArr);
                i4 = abstractC0487k.read(bArr, i2, i3);
            } else {
                i4 = -1;
            }
            if (i4 > 0) {
                if (this.f4080l == null) {
                    this.f4080l = new byte[i3];
                }
                AbstractC0487k abstractC0487k2 = this.f4073e;
                if (abstractC0487k2 != null) {
                    byte[] bArr2 = this.f4080l;
                    kotlin.jvm.internal.l.b(bArr2);
                    i5 = abstractC0487k2.read(bArr2, i2, i3);
                } else {
                    i5 = -1;
                }
                if (i5 <= 0) {
                    Y0.c.b("SystemRecorder", "read: systemRecorder read failed");
                    return i5;
                }
                try {
                    byte[] bArr3 = this.f4079k;
                    kotlin.jvm.internal.l.b(bArr3);
                    byte[] bArr4 = this.f4080l;
                    kotlin.jvm.internal.l.b(bArr4);
                    AbstractC0644a.a(audioData, bArr3, bArr4, 1.0f, 1.0f);
                    return i5;
                } catch (Throwable th) {
                    Y0.c.b("SystemRecorder", "mixRawAudioBytes error: " + th);
                    return -1;
                }
            }
            Y0.c.i("SystemRecorder", "read: micRecorder read failed");
        }
        AbstractC0487k abstractC0487k3 = this.f4073e;
        if (abstractC0487k3 != null) {
            return abstractC0487k3.read(audioData, i2, i3);
        }
        return -1;
    }

    @Override // g0.InterfaceC0480d
    public int release() {
        return ((this.f4074f != null || this.f4073e != null) && g() == 0 && h() == 0) ? 0 : -1;
    }

    @Override // g0.InterfaceC0480d
    public int startRecording() {
        AbstractC0487k abstractC0487k = this.f4074f;
        if (abstractC0487k == null) {
            AbstractC0487k abstractC0487k2 = this.f4073e;
            if (abstractC0487k2 != null) {
                return abstractC0487k2.startRecording();
            }
            return -1;
        }
        if (abstractC0487k != null && this.f4073e == null) {
            if (abstractC0487k != null) {
                return abstractC0487k.startRecording();
            }
            return -1;
        }
        if (abstractC0487k != null) {
            abstractC0487k.startRecording();
        }
        AbstractC0487k abstractC0487k3 = this.f4073e;
        if (abstractC0487k3 != null) {
            return abstractC0487k3.startRecording();
        }
        return -1;
    }

    @Override // g0.InterfaceC0480d
    public int stop() {
        AbstractC0487k abstractC0487k = this.f4074f;
        if (abstractC0487k == null) {
            AbstractC0487k abstractC0487k2 = this.f4073e;
            if (abstractC0487k2 != null) {
                return abstractC0487k2.stop();
            }
            return -1;
        }
        if (abstractC0487k != null && this.f4073e == null) {
            if (abstractC0487k != null) {
                return abstractC0487k.stop();
            }
            return -1;
        }
        if (abstractC0487k != null) {
            abstractC0487k.stop();
        }
        AbstractC0487k abstractC0487k3 = this.f4073e;
        if (abstractC0487k3 != null) {
            return abstractC0487k3.stop();
        }
        return -1;
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public C0491o(Context context) {
        this(context, a.f4081b, b.f4082b, true);
        kotlin.jvm.internal.l.e(context, "context");
    }
}
