package yc;

import android.os.Build;
import android.os.Trace;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.measurement.internal.c0;
import io.flutter.embedding.engine.FlutterJNI;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import xyz.stream.utils.j0;

/* JADX INFO: loaded from: classes.dex */
public final class m implements ed.i, n {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final FlutterJNI f36962a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final HashMap f36963b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f36964c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Object f36965d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicBoolean f36966e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final HashMap f36967f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final g f36969h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final WeakHashMap f36970i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final j0 f36971j;

    public m(FlutterJNI flutterJNI) {
        j0 j0Var = new j0(25);
        this.f36963b = new HashMap();
        this.f36964c = new HashMap();
        this.f36965d = new Object();
        this.f36966e = new AtomicBoolean(false);
        this.f36967f = new HashMap();
        this.f36968g = 1;
        this.f36969h = new g();
        this.f36970i = new WeakHashMap();
        this.f36962a = flutterJNI;
        this.f36971j = j0Var;
    }

    public static void c(m mVar, String str, int i10, i iVar, ByteBuffer byteBuffer, long j10) {
        FlutterJNI flutterJNI = mVar.f36962a;
        String strE = id.b.e("PlatformChannel ScheduleHandler on " + str);
        if (Build.VERSION.SDK_INT >= 29) {
            q3.a.b(i10, androidx.datastore.preferences.core.c.t(strE));
        } else {
            String strT = androidx.datastore.preferences.core.c.t(strE);
            try {
                if (androidx.datastore.preferences.core.c.f1990d == null) {
                    androidx.datastore.preferences.core.c.f1990d = Trace.class.getMethod("asyncTraceEnd", Long.TYPE, String.class, Integer.TYPE);
                }
                androidx.datastore.preferences.core.c.f1990d.invoke(null, Long.valueOf(androidx.datastore.preferences.core.c.f1987a), strT, Integer.valueOf(i10));
            } catch (Exception e10) {
                androidx.datastore.preferences.core.c.h(e10);
            }
        }
        try {
            id.b.g("DartMessenger#handleMessageFromDart on " + str);
            try {
                mVar.invokeHandler(iVar, byteBuffer, i10);
                if (byteBuffer != null && byteBuffer.isDirect()) {
                    byteBuffer.limit(0);
                }
                Trace.endSection();
            } finally {
            }
        } finally {
            flutterJNI.cleanupMessageData(j10);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v2, types: [yc.e] */
    private void dispatchMessageToQueue(@NonNull final String str, @Nullable final i iVar, @Nullable final ByteBuffer byteBuffer, final int i10, final long j10) {
        h hVar = iVar != null ? iVar.taskQueue : null;
        String strE = id.b.e("PlatformChannel ScheduleHandler on " + str);
        if (Build.VERSION.SDK_INT >= 29) {
            q3.a.a(i10, androidx.datastore.preferences.core.c.t(strE));
        } else {
            String strT = androidx.datastore.preferences.core.c.t(strE);
            try {
                if (androidx.datastore.preferences.core.c.f1989c == null) {
                    androidx.datastore.preferences.core.c.f1989c = Trace.class.getMethod("asyncTraceBegin", Long.TYPE, String.class, Integer.TYPE);
                }
                androidx.datastore.preferences.core.c.f1989c.invoke(null, Long.valueOf(androidx.datastore.preferences.core.c.f1987a), strT, Integer.valueOf(i10));
            } catch (Exception e10) {
                androidx.datastore.preferences.core.c.h(e10);
            }
        }
        ?? r02 = new Runnable() { // from class: yc.e
            @Override // java.lang.Runnable
            public final void run() {
                m.c(this.f36942a, str, i10, iVar, byteBuffer, j10);
            }
        };
        h hVar2 = hVar;
        if (hVar == null) {
            hVar2 = this.f36969h;
        }
        hVar2.a(r02);
    }

    private void invokeHandler(@Nullable i iVar, @Nullable ByteBuffer byteBuffer, int i10) {
        FlutterJNI flutterJNI = this.f36962a;
        if (iVar == null) {
            flutterJNI.invokePlatformMessageEmptyResponseCallback(i10);
            return;
        }
        try {
            iVar.f36953a.onMessage(byteBuffer, new j(flutterJNI, i10));
        } catch (Error e10) {
            Thread threadCurrentThread = Thread.currentThread();
            if (threadCurrentThread.getUncaughtExceptionHandler() == null) {
                throw e10;
            }
            threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, e10);
        } catch (Exception unused) {
            flutterJNI.invokePlatformMessageEmptyResponseCallback(i10);
        }
    }

    @Override // ed.i
    public final c0 a(androidx.browser.trusted.l lVar) {
        j0 j0Var = this.f36971j;
        j0Var.getClass();
        Object lVar2 = lVar.f916b ? new l((ExecutorService) j0Var.f36154b) : new g((ExecutorService) j0Var.f36154b);
        c0 c0Var = new c0();
        this.f36970i.put(c0Var, lVar2);
        return c0Var;
    }

    @Override // yc.n
    public void handleMessageFromDart(@NonNull String str, @Nullable ByteBuffer byteBuffer, int i10, long j10) {
        i iVar;
        boolean z10;
        synchronized (this.f36965d) {
            try {
                iVar = (i) this.f36963b.get(str);
                z10 = this.f36966e.get() && iVar == null;
                if (z10) {
                    if (!this.f36964c.containsKey(str)) {
                        this.f36964c.put(str, new LinkedList());
                    }
                    ((List) this.f36964c.get(str)).add(new f(j10, i10, byteBuffer));
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (z10) {
            return;
        }
        dispatchMessageToQueue(str, iVar, byteBuffer, i10, j10);
    }

    @Override // yc.n
    public void handlePlatformMessageResponse(int i10, @Nullable ByteBuffer byteBuffer) {
        ed.g gVar = (ed.g) this.f36967f.remove(Integer.valueOf(i10));
        if (gVar != null) {
            try {
                gVar.reply(byteBuffer);
                if (byteBuffer == null || !byteBuffer.isDirect()) {
                    return;
                }
                byteBuffer.limit(0);
            } catch (Error e10) {
                Thread threadCurrentThread = Thread.currentThread();
                if (threadCurrentThread.getUncaughtExceptionHandler() == null) {
                    throw e10;
                }
                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, e10);
            } catch (Exception unused) {
            }
        }
    }

    @Override // ed.i
    public final void send(String str, ByteBuffer byteBuffer) {
        send(str, byteBuffer, null);
    }

    @Override // ed.i
    public void setMessageHandler(@NonNull String str, @Nullable ed.f fVar) {
        setMessageHandler(str, fVar, null);
    }

    @Override // ed.i
    public void send(@NonNull String str, @Nullable ByteBuffer byteBuffer, @Nullable ed.g gVar) {
        id.b.g("DartMessenger#send on " + str);
        try {
            int i10 = this.f36968g;
            this.f36968g = i10 + 1;
            if (gVar != null) {
                this.f36967f.put(Integer.valueOf(i10), gVar);
            }
            FlutterJNI flutterJNI = this.f36962a;
            if (byteBuffer == null) {
                flutterJNI.dispatchEmptyPlatformMessage(str, i10);
            } else {
                flutterJNI.dispatchPlatformMessage(str, byteBuffer, byteBuffer.position(), i10);
            }
            Trace.endSection();
        } catch (Throwable th) {
            try {
                Trace.endSection();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // ed.i
    public void setMessageHandler(@NonNull String str, @Nullable ed.f fVar, @Nullable ed.h hVar) {
        h hVar2;
        if (fVar == null) {
            synchronized (this.f36965d) {
                this.f36963b.remove(str);
            }
            return;
        }
        if (hVar != null) {
            hVar2 = (h) this.f36970i.get(hVar);
            if (hVar2 == null) {
                throw new IllegalArgumentException("Unrecognized TaskQueue, use BinaryMessenger to create your TaskQueue (ex makeBackgroundTaskQueue).");
            }
        } else {
            hVar2 = null;
        }
        synchronized (this.f36965d) {
            try {
                this.f36963b.put(str, new i(fVar, hVar2));
                List<f> list = (List) this.f36964c.remove(str);
                if (list == null) {
                    return;
                }
                for (f fVar2 : list) {
                    dispatchMessageToQueue(str, (i) this.f36963b.get(str), fVar2.f36948a, fVar2.f36949b, fVar2.f36950c);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
