package d5;

import android.content.Context;
import android.media.AudioManager;
import d4.C0545d;
import e4.AbstractC0577u;
import e5.m;
import e5.n;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class e implements FlutterPlugin {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public f f6154k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public Context f6155l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public BinaryMessenger f6156m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public io.sentry.internal.debugmeta.c f6157n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final ConcurrentHashMap f6158o = new ConcurrentHashMap();
    public a p = new a(false, false, 2, 1, 1, 0);

    public static void c(n nVar, boolean z5) {
        AbstractC0983h.e("player", nVar);
        nVar.f6296b.a("audio.onPrepared", AbstractC0577u.n(new C0545d("value", Boolean.valueOf(z5))));
    }

    public final AudioManager a() {
        Context context = this.f6155l;
        if (context == null) {
            AbstractC0983h.i("context");
            throw null;
        }
        Object systemService = context.getApplicationContext().getSystemService("audio");
        AbstractC0983h.c("null cannot be cast to non-null type android.media.AudioManager", systemService);
        return (AudioManager) systemService;
    }

    public final void b(String str) {
        AbstractC0983h.e("message", str);
        f fVar = this.f6154k;
        if (fVar != null) {
            fVar.a("audio.onLog", AbstractC0577u.n(new C0545d("value", str)));
        } else {
            AbstractC0983h.i("globalEvents");
            throw null;
        }
    }

    @Override // io.flutter.embedding.engine.plugins.FlutterPlugin
    public final void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding flutterPluginBinding) {
        AbstractC0983h.e("binding", flutterPluginBinding);
        this.f6155l = flutterPluginBinding.getApplicationContext();
        this.f6156m = flutterPluginBinding.getBinaryMessenger();
        this.f6157n = new io.sentry.internal.debugmeta.c(this);
        final int i = 0;
        new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "xyz.luan/audioplayers").setMethodCallHandler(new MethodChannel.MethodCallHandler(this) { // from class: d5.b

            /* JADX INFO: renamed from: l, reason: collision with root package name */
            public final /* synthetic */ e f6153l;

            {
                this.f6153l = this;
            }

            @Override // io.flutter.plugin.common.MethodChannel.MethodCallHandler
            public final void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                switch (i) {
                    case 0:
                        AbstractC0983h.e("call", methodCall);
                        AbstractC0983h.e("response", result);
                        try {
                            new c(2, this.f6153l, e.class, "methodHandler", "methodHandler(Lio/flutter/plugin/common/MethodCall;Lio/flutter/plugin/common/MethodChannel$Result;)V", 0).invoke(methodCall, result);
                        } catch (Throwable th) {
                            result.error("Unexpected AndroidAudioError", th.getMessage(), th);
                            return;
                        }
                        break;
                    default:
                        AbstractC0983h.e("call", methodCall);
                        AbstractC0983h.e("response", result);
                        try {
                            new d(2, this.f6153l, e.class, "globalMethodHandler", "globalMethodHandler(Lio/flutter/plugin/common/MethodCall;Lio/flutter/plugin/common/MethodChannel$Result;)V", 0).invoke(methodCall, result);
                        } catch (Throwable th2) {
                            result.error("Unexpected AndroidAudioError", th2.getMessage(), th2);
                        }
                        break;
                }
            }
        });
        final int i5 = 1;
        new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "xyz.luan/audioplayers.global").setMethodCallHandler(new MethodChannel.MethodCallHandler(this) { // from class: d5.b

            /* JADX INFO: renamed from: l, reason: collision with root package name */
            public final /* synthetic */ e f6153l;

            {
                this.f6153l = this;
            }

            @Override // io.flutter.plugin.common.MethodChannel.MethodCallHandler
            public final void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                switch (i5) {
                    case 0:
                        AbstractC0983h.e("call", methodCall);
                        AbstractC0983h.e("response", result);
                        try {
                            new c(2, this.f6153l, e.class, "methodHandler", "methodHandler(Lio/flutter/plugin/common/MethodCall;Lio/flutter/plugin/common/MethodChannel$Result;)V", 0).invoke(methodCall, result);
                        } catch (Throwable th) {
                            result.error("Unexpected AndroidAudioError", th.getMessage(), th);
                            return;
                        }
                        break;
                    default:
                        AbstractC0983h.e("call", methodCall);
                        AbstractC0983h.e("response", result);
                        try {
                            new d(2, this.f6153l, e.class, "globalMethodHandler", "globalMethodHandler(Lio/flutter/plugin/common/MethodCall;Lio/flutter/plugin/common/MethodChannel$Result;)V", 0).invoke(methodCall, result);
                        } catch (Throwable th2) {
                            result.error("Unexpected AndroidAudioError", th2.getMessage(), th2);
                        }
                        break;
                }
            }
        });
        this.f6154k = new f(new EventChannel(flutterPluginBinding.getBinaryMessenger(), "xyz.luan/audioplayers.global/events"));
    }

    @Override // io.flutter.embedding.engine.plugins.FlutterPlugin
    public final void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding flutterPluginBinding) {
        AbstractC0983h.e("binding", flutterPluginBinding);
        ConcurrentHashMap concurrentHashMap = this.f6158o;
        Collection<n> collectionValues = concurrentHashMap.values();
        AbstractC0983h.d("<get-values>(...)", collectionValues);
        for (n nVar : collectionValues) {
            nVar.e();
            f fVar = nVar.f6296b;
            EventChannel.EventSink eventSink = fVar.f6160l;
            if (eventSink != null) {
                eventSink.endOfStream();
                fVar.f6160l = null;
            }
            fVar.f6159k.setStreamHandler(null);
        }
        concurrentHashMap.clear();
        io.sentry.internal.debugmeta.c cVar = this.f6157n;
        if (cVar == null) {
            AbstractC0983h.i("soundPoolManager");
            throw null;
        }
        HashMap map = (HashMap) cVar.f7827m;
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            m mVar = (m) ((Map.Entry) it.next()).getValue();
            mVar.f6292a.release();
            mVar.f6293b.clear();
            mVar.f6294c.clear();
        }
        map.clear();
        f fVar2 = this.f6154k;
        if (fVar2 == null) {
            AbstractC0983h.i("globalEvents");
            throw null;
        }
        EventChannel.EventSink eventSink2 = fVar2.f6160l;
        if (eventSink2 != null) {
            eventSink2.endOfStream();
            fVar2.f6160l = null;
        }
        fVar2.f6159k.setStreamHandler(null);
    }
}
