package fe;

import android.os.Looper;
import com.google.android.gms.common.api.internal.v0;
import com.google.android.gms.internal.ads.z82;
import com.google.android.gms.internal.atv_ads_framework.d1;
import com.google.android.gms.measurement.internal.c0;
import com.google.android.gms.measurement.internal.d0;
import com.google.android.gms.measurement.internal.u1;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import kotlinx.coroutines.y;
import org.greenrobot.eventbus.EventBusException;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.android.AndroidComponentsImpl;

/* JADX INFO: loaded from: classes3.dex */
public final class d {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public static volatile d f26054q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public static final e f26055r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final HashMap f26056s;

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final z82 f26060d = new z82(this, 3);

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final boolean f26067k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final boolean f26068l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final boolean f26069m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final boolean f26070n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final boolean f26071o;

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

    static {
        e eVar = new e();
        eVar.f26074a = e.f26073b;
        f26055r = eVar;
        f26056s = new HashMap();
    }

    public d() {
        e eVar = f26055r;
        eVar.getClass();
        AndroidComponentsImpl androidComponentsImpl = AndroidComponentsImpl.f31565c;
        this.f26072p = androidComponentsImpl != null ? androidComponentsImpl.f31566a : new c0(28);
        this.f26057a = new HashMap();
        this.f26058b = new HashMap();
        this.f26059c = new ConcurrentHashMap();
        d0 d0Var = androidComponentsImpl != null ? androidComponentsImpl.f31567b : null;
        this.f26061e = d0Var;
        this.f26062f = d0Var != null ? new f(this, Looper.getMainLooper()) : null;
        this.f26063g = new a(this);
        this.f26064h = new v0(this);
        this.f26065i = new m();
        this.f26067k = true;
        this.f26068l = true;
        this.f26069m = true;
        this.f26070n = true;
        this.f26071o = true;
        this.f26066j = eVar.f26074a;
    }

    public static void a(ArrayList arrayList, Class[] clsArr) {
        for (Class cls : clsArr) {
            if (!arrayList.contains(cls)) {
                arrayList.add(cls);
                a(arrayList, cls.getInterfaces());
            }
        }
    }

    public static d b() {
        d dVar = f26054q;
        if (dVar == null) {
            synchronized (d.class) {
                try {
                    dVar = f26054q;
                    if (dVar == null) {
                        dVar = new d();
                        f26054q = dVar;
                    }
                } finally {
                }
            }
        }
        return dVar;
    }

    public final void c(i iVar) {
        Object obj = iVar.f26083a;
        n nVar = iVar.f26084b;
        iVar.f26083a = null;
        iVar.f26084b = null;
        iVar.f26085c = null;
        ArrayList arrayList = i.f26082d;
        synchronized (arrayList) {
            if (arrayList.size() < 10000) {
                arrayList.add(iVar);
            }
        }
        if (nVar.f26105c) {
            d(obj, nVar);
        }
    }

    public final void d(Object obj, n nVar) {
        try {
            nVar.f26104b.f26089a.invoke(nVar.f26103a, obj);
        } catch (IllegalAccessException e10) {
            throw new IllegalStateException("Unexpected exception", e10);
        } catch (InvocationTargetException e11) {
            Throwable cause = e11.getCause();
            boolean z10 = obj instanceof j;
            boolean z11 = this.f26067k;
            g gVar = this.f26072p;
            if (!z10) {
                if (z11) {
                    gVar.d(Level.SEVERE, "Could not dispatch event: " + obj.getClass() + " to subscribing class " + nVar.f26103a.getClass(), cause);
                }
                if (this.f26069m) {
                    e(new j(cause, obj, nVar.f26103a));
                    return;
                }
                return;
            }
            if (z11) {
                Level level = Level.SEVERE;
                gVar.d(level, "SubscriberExceptionEvent subscriber " + nVar.f26103a.getClass() + " threw an exception", cause);
                j jVar = (j) obj;
                gVar.d(level, "Initial event " + jVar.f26087b + " caused exception in " + jVar.f26088c, jVar.f26086a);
            }
        }
    }

    public final void e(Object obj) {
        c cVar = (c) this.f26060d.get();
        ArrayList arrayList = cVar.f26050a;
        arrayList.add(obj);
        if (cVar.f26051b) {
            return;
        }
        cVar.f26052c = this.f26061e == null || Looper.getMainLooper() == Looper.myLooper();
        cVar.f26051b = true;
        while (!arrayList.isEmpty()) {
            try {
                f(arrayList.remove(0), cVar);
            } finally {
                cVar.f26051b = false;
                cVar.f26052c = false;
            }
        }
    }

    public final void f(Object obj, c cVar) {
        boolean zG;
        List list;
        Class<?> cls = obj.getClass();
        int i10 = 0;
        if (this.f26071o) {
            HashMap map = f26056s;
            synchronized (map) {
                try {
                    List list2 = (List) map.get(cls);
                    list = list2;
                    if (list2 == null) {
                        ArrayList arrayList = new ArrayList();
                        for (Class<?> superclass = cls; superclass != null; superclass = superclass.getSuperclass()) {
                            arrayList.add(superclass);
                            a(arrayList, superclass.getInterfaces());
                        }
                        f26056s.put(cls, arrayList);
                        list = arrayList;
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            int size = list.size();
            zG = false;
            for (int i11 = 0; i11 < size; i11++) {
                zG |= g(obj, cVar, (Class) list.get(i11));
            }
        } else {
            zG = g(obj, cVar, cls);
        }
        if (zG) {
            return;
        }
        if (this.f26068l) {
            this.f26072p.a(Level.FINE, "No subscribers registered for event " + cls);
        }
        if (!this.f26070n || cls == h.class || cls == j.class) {
            return;
        }
        e(new h(this, obj, i10));
    }

    public final boolean g(Object obj, c cVar, Class cls) {
        CopyOnWriteArrayList<n> copyOnWriteArrayList;
        synchronized (this) {
            copyOnWriteArrayList = (CopyOnWriteArrayList) this.f26057a.get(cls);
        }
        if (copyOnWriteArrayList == null || copyOnWriteArrayList.isEmpty()) {
            return false;
        }
        for (n nVar : copyOnWriteArrayList) {
            cVar.f26053d = obj;
            i(nVar, obj, cVar.f26052c);
        }
        return true;
    }

    public final void h(Object obj) {
        synchronized (this.f26059c) {
            this.f26059c.put(obj.getClass(), obj);
        }
        e(obj);
    }

    public final void i(n nVar, Object obj, boolean z10) {
        int i10 = b.f26049a[nVar.f26104b.f26090b.ordinal()];
        if (i10 == 1) {
            d(obj, nVar);
            return;
        }
        f fVar = this.f26062f;
        if (i10 == 2) {
            if (z10) {
                d(obj, nVar);
                return;
            } else {
                fVar.a(obj, nVar);
                return;
            }
        }
        if (i10 == 3) {
            if (fVar != null) {
                fVar.a(obj, nVar);
                return;
            } else {
                d(obj, nVar);
                return;
            }
        }
        if (i10 != 4) {
            if (i10 != 5) {
                throw new IllegalStateException("Unknown thread mode: " + nVar.f26104b.f26090b);
            }
            v0 v0Var = this.f26064h;
            v0Var.getClass();
            ((d1) v0Var.f8295b).g(i.a(obj, nVar));
            ((d) v0Var.f8296c).f26066j.execute(v0Var);
            return;
        }
        if (!z10) {
            d(obj, nVar);
            return;
        }
        a aVar = this.f26063g;
        aVar.getClass();
        i iVarA = i.a(obj, nVar);
        synchronized (aVar) {
            try {
                aVar.f26046a.g(iVarA);
                if (!aVar.f26048c) {
                    aVar.f26048c = true;
                    aVar.f26047b.f26066j.execute(aVar);
                }
            } finally {
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:56:0x00a7  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void j(Object obj) {
        int i10;
        l lVar;
        Method[] methods;
        Subscribe subscribe;
        if (y.G()) {
            try {
                AndroidComponentsImpl androidComponentsImpl = AndroidComponentsImpl.f31565c;
            } catch (ClassNotFoundException unused) {
                throw new RuntimeException("It looks like you are using EventBus on Android, make sure to add the \"eventbus\" Android library to your dependencies.");
            }
        }
        Class<?> cls = obj.getClass();
        this.f26065i.getClass();
        ConcurrentHashMap concurrentHashMap = m.f26101a;
        List list = (List) concurrentHashMap.get(cls);
        List list2 = list;
        if (list == null) {
            synchronized (m.f26102b) {
                i10 = 0;
                int i11 = 0;
                while (true) {
                    if (i11 >= 4) {
                        lVar = new l();
                        break;
                    }
                    try {
                        l[] lVarArr = m.f26102b;
                        lVar = lVarArr[i11];
                        if (lVar != null) {
                            lVarArr[i11] = null;
                        } else {
                            i11++;
                        }
                    } finally {
                    }
                }
            }
            lVar.f26099e = cls;
            lVar.f26100f = false;
            while (true) {
                Class cls2 = lVar.f26099e;
                if (cls2 != null) {
                    int i12 = 1;
                    try {
                        try {
                            methods = cls2.getDeclaredMethods();
                        } catch (LinkageError e10) {
                            throw new EventBusException(u1.j("Could not inspect methods of ".concat(lVar.f26099e.getName()), ". Please make this class visible to EventBus annotation processor to avoid reflection."), e10);
                        }
                    } catch (Throwable unused2) {
                        methods = lVar.f26099e.getMethods();
                        lVar.f26100f = true;
                    }
                    int length = methods.length;
                    int i13 = 0;
                    while (i13 < length) {
                        Method method = methods[i13];
                        int modifiers = method.getModifiers();
                        if ((modifiers & 1) != 0 && (modifiers & 5192) == 0) {
                            Class<?>[] parameterTypes = method.getParameterTypes();
                            if (parameterTypes.length == i12 && (subscribe = (Subscribe) method.getAnnotation(Subscribe.class)) != null) {
                                Class<?> cls3 = parameterTypes[0];
                                HashMap map = lVar.f26096b;
                                Object objPut = map.put(cls3, method);
                                if (objPut != null) {
                                    if (objPut instanceof Method) {
                                        if (!lVar.a((Method) objPut, cls3)) {
                                            throw new IllegalStateException();
                                        }
                                        map.put(cls3, lVar);
                                    }
                                    if (lVar.a(method, cls3)) {
                                        lVar.f26095a.add(new k(method, cls3, subscribe.threadMode(), subscribe.priority(), subscribe.sticky()));
                                    }
                                }
                            }
                        }
                        i13++;
                        i12 = 1;
                    }
                    if (lVar.f26100f) {
                        lVar.f26099e = null;
                    } else {
                        Class superclass = lVar.f26099e.getSuperclass();
                        lVar.f26099e = superclass;
                        String name = superclass.getName();
                        if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.") || name.startsWith("androidx.")) {
                            lVar.f26099e = null;
                        }
                    }
                } else {
                    ArrayList arrayList = new ArrayList(lVar.f26095a);
                    lVar.f26095a.clear();
                    lVar.f26096b.clear();
                    lVar.f26097c.clear();
                    lVar.f26098d.setLength(0);
                    lVar.f26099e = null;
                    lVar.f26100f = false;
                    synchronized (m.f26102b) {
                        while (true) {
                            if (i10 >= 4) {
                                break;
                            }
                            try {
                                l[] lVarArr2 = m.f26102b;
                                if (lVarArr2[i10] == null) {
                                    lVarArr2[i10] = lVar;
                                    break;
                                }
                                i10++;
                            } finally {
                            }
                        }
                    }
                    if (arrayList.isEmpty()) {
                        throw new EventBusException("Subscriber " + cls + " and its super classes have no public methods with the @Subscribe annotation");
                    }
                    concurrentHashMap.put(cls, arrayList);
                    list2 = arrayList;
                }
            }
        }
        synchronized (this) {
            try {
                Iterator it = list2.iterator();
                while (it.hasNext()) {
                    l(obj, (k) it.next());
                }
            } finally {
            }
        }
    }

    public final void k(Object obj) {
        synchronized (this.f26059c) {
            try {
                Class<?> cls = obj.getClass();
                if (obj.equals(this.f26059c.get(cls))) {
                    this.f26059c.remove(cls);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void l(Object obj, k kVar) {
        Object value;
        Class cls = kVar.f26091c;
        n nVar = new n(obj, kVar);
        HashMap map = this.f26057a;
        CopyOnWriteArrayList copyOnWriteArrayList = (CopyOnWriteArrayList) map.get(cls);
        if (copyOnWriteArrayList == null) {
            copyOnWriteArrayList = new CopyOnWriteArrayList();
            map.put(cls, copyOnWriteArrayList);
        } else if (copyOnWriteArrayList.contains(nVar)) {
            throw new EventBusException("Subscriber " + obj.getClass() + " already registered to event " + cls);
        }
        int size = copyOnWriteArrayList.size();
        for (int i10 = 0; i10 <= size; i10++) {
            if (i10 != size) {
                if (kVar.f26092d <= ((n) copyOnWriteArrayList.get(i10)).f26104b.f26092d) {
                }
            }
            copyOnWriteArrayList.add(i10, nVar);
            break;
        }
        HashMap map2 = this.f26058b;
        List arrayList = (List) map2.get(obj);
        if (arrayList == null) {
            arrayList = new ArrayList();
            map2.put(obj, arrayList);
        }
        arrayList.add(cls);
        if (kVar.f26093e) {
            ConcurrentHashMap concurrentHashMap = this.f26059c;
            d0 d0Var = this.f26061e;
            if (!this.f26071o) {
                Object obj2 = concurrentHashMap.get(cls);
                if (obj2 != null) {
                    i(nVar, obj2, d0Var == null || Looper.getMainLooper() == Looper.myLooper());
                    return;
                }
                return;
            }
            for (Map.Entry entry : concurrentHashMap.entrySet()) {
                if (cls.isAssignableFrom((Class) entry.getKey()) && (value = entry.getValue()) != null) {
                    i(nVar, value, d0Var == null || Looper.getMainLooper() == Looper.myLooper());
                }
            }
        }
    }

    public final synchronized void m(Object obj) {
        try {
            List list = (List) this.f26058b.get(obj);
            if (list != null) {
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    List list2 = (List) this.f26057a.get((Class) it.next());
                    if (list2 != null) {
                        int size = list2.size();
                        int i10 = 0;
                        while (i10 < size) {
                            n nVar = (n) list2.get(i10);
                            if (nVar.f26103a == obj) {
                                nVar.f26105c = false;
                                list2.remove(i10);
                                i10--;
                                size--;
                            }
                            i10++;
                        }
                    }
                }
                this.f26058b.remove(obj);
            } else {
                this.f26072p.a(Level.WARNING, "Subscriber to unregister was not registered before: " + obj.getClass());
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final String toString() {
        return "EventBus[indexCount=0, eventInheritance=" + this.f26071o + "]";
    }
}
