package com.mbridge.msdk.dycreator.bus; import android.os.Looper; import android.util.Log; import java.lang.reflect.InvocationTargetException; 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.concurrent.Executors; /* loaded from: classes4.dex */ public final class EventBus { private static volatile EventBus b; private boolean n; static ExecutorService a = Executors.newCachedThreadPool(); public static String TAG = "Event"; private static final Map, List>> c = new HashMap(); private final Map, CopyOnWriteArrayList> d = new HashMap(); private final Map>> e = new HashMap(); private final Map, Object> f = new ConcurrentHashMap(); private final ThreadLocal> g = new ThreadLocal>() { // from class: com.mbridge.msdk.dycreator.bus.EventBus.1 @Override // java.lang.ThreadLocal public /* synthetic */ List initialValue() { return new ArrayList(); } }; private final ThreadLocal h = new ThreadLocal() { // from class: com.mbridge.msdk.dycreator.bus.EventBus.2 @Override // java.lang.ThreadLocal public /* synthetic */ BooleanWrapper initialValue() { return new BooleanWrapper(); } }; private String i = "onEvent"; private final HandlerPoster j = new HandlerPoster(this, Looper.getMainLooper(), 10); private final BackgroundPoster k = new BackgroundPoster(this); private final AsyncPoster l = new AsyncPoster(this); private final SubscriberMethodFinder m = new SubscriberMethodFinder(); private boolean o = true; private Map p = new ConcurrentHashMap(); public static final class BooleanWrapper { boolean a; } public interface PostCallback { void onPostCompleted(List list); } public static EventBus getDefault() { if (b == null) { synchronized (EventBus.class) { try { if (b == null) { b = new EventBus(); } } finally { } } } return b; } public static void clearCaches() { SubscriberMethodFinder.a(); c.clear(); } public static void skipMethodNameVerificationFor(Class cls) { SubscriberMethodFinder.a(cls); } public static void clearSkipMethodNameVerifications() { SubscriberMethodFinder.clearSkipMethodNameVerifications(); } public final void configureLogSubscriberExceptions(boolean z) { if (this.n) { throw new EventBusException("This method must be called before any registration"); } this.o = z; } public final void register(Object obj) { a(obj, this.i, false); } public final void register(String str, Object obj) { Map map = this.p; if (map != null && map.containsKey(str)) { unregister(this.p.get(str)); } this.p.put(str, obj); a(obj, this.i, false); } public final void register(Object obj, String str) { a(obj, str, false); } public final void registerSticky(Object obj) { a(obj, this.i, true); } public final void registerSticky(Object obj, String str) { a(obj, str, true); } private void a(Object obj, String str, boolean z) { Iterator it = this.m.a(obj.getClass(), str).iterator(); while (it.hasNext()) { a(obj, it.next(), z); } } public final void register(Object obj, Class cls, Class... clsArr) { a(obj, this.i, false, cls, clsArr); } public final synchronized void register(Object obj, String str, Class cls, Class... clsArr) { a(obj, str, false, cls, clsArr); } public final void registerSticky(Object obj, Class cls, Class... clsArr) { a(obj, this.i, true, cls, clsArr); } public final synchronized void registerSticky(Object obj, String str, Class cls, Class... clsArr) { a(obj, str, true, cls, clsArr); } private synchronized void a(Object obj, String str, boolean z, Class cls, Class... clsArr) { try { for (SubscriberMethod subscriberMethod : this.m.a(obj.getClass(), str)) { if (cls == subscriberMethod.c) { a(obj, subscriberMethod, z); } else if (clsArr != null) { int length = clsArr.length; int i = 0; while (true) { if (i >= length) { break; } if (clsArr[i] == subscriberMethod.c) { a(obj, subscriberMethod, z); break; } i++; } } } } catch (Throwable th) { throw th; } } private void a(Object obj, SubscriberMethod subscriberMethod, boolean z) { Object obj2; this.n = true; Class cls = subscriberMethod.c; CopyOnWriteArrayList copyOnWriteArrayList = this.d.get(cls); Subscription subscription = new Subscription(obj, subscriberMethod); if (copyOnWriteArrayList == null) { copyOnWriteArrayList = new CopyOnWriteArrayList<>(); this.d.put(cls, copyOnWriteArrayList); } else { Iterator it = copyOnWriteArrayList.iterator(); while (it.hasNext()) { if (it.next().equals(subscription)) { throw new EventBusException("Subscriber " + obj.getClass() + " already registered to event " + cls); } } } subscriberMethod.a.setAccessible(true); copyOnWriteArrayList.add(subscription); List> list = this.e.get(obj); if (list == null) { list = new ArrayList<>(); this.e.put(obj, list); } list.add(cls); if (z) { synchronized (this.f) { obj2 = this.f.get(cls); } if (obj2 != null) { a(subscription, obj2, Looper.getMainLooper() == Looper.myLooper()); } } } public final synchronized void unregister(Object obj, Class... clsArr) { try { if (clsArr.length == 0) { throw new IllegalArgumentException("Provide at least one event class"); } List> list = this.e.get(obj); if (list != null) { for (Class cls : clsArr) { a(obj, cls); list.remove(cls); } if (list.isEmpty()) { this.e.remove(obj); } } else { Log.w(TAG, "Subscriber to unregister was not registered before: " + obj.getClass()); } } catch (Throwable th) { throw th; } } private void a(Object obj, Class cls) { CopyOnWriteArrayList copyOnWriteArrayList = this.d.get(cls); if (copyOnWriteArrayList != null) { int size = copyOnWriteArrayList.size(); int i = 0; while (i < size) { if (copyOnWriteArrayList.get(i).a == obj) { copyOnWriteArrayList.remove(i); i--; size--; } i++; } } } public final synchronized void unregister(Object obj) { try { List> list = this.e.get(obj); if (list != null) { Iterator> it = list.iterator(); while (it.hasNext()) { a(obj, it.next()); } this.e.remove(obj); } else { Log.w(TAG, "Subscriber to unregister was not registered before: " + obj.getClass()); } } catch (Throwable th) { throw th; } } public final synchronized void unregister(String str) { try { Map map = this.p; if (map != null && map.containsKey(str)) { Object remove = this.p.remove(str); List> list = this.e.get(remove); if (list != null) { Iterator> it = list.iterator(); while (it.hasNext()) { a(remove, it.next()); } this.e.remove(remove); } else { Log.w(TAG, "Subscriber to unregister was not registered before: " + remove.getClass()); } } } catch (Throwable th) { throw th; } } public final void post(Object obj) { CopyOnWriteArrayList copyOnWriteArrayList; List list = this.g.get(); list.add(obj); BooleanWrapper booleanWrapper = this.h.get(); if (booleanWrapper.a) { return; } boolean z = Looper.getMainLooper() == Looper.myLooper(); booleanWrapper.a = true; while (!list.isEmpty()) { try { Object remove = list.remove(0); if (remove != null) { try { Class cls = remove.getClass(); List> a2 = a(cls); int size = a2.size(); boolean z2 = false; for (int i = 0; i < size; i++) { Class cls2 = a2.get(i); synchronized (this) { copyOnWriteArrayList = this.d.get(cls2); } if (copyOnWriteArrayList != null) { Iterator it = copyOnWriteArrayList.iterator(); while (it.hasNext()) { a(it.next(), remove, z); } z2 = true; } } if (!z2) { StringBuilder sb = new StringBuilder(); sb.append("No subscripers registered for event "); sb.append(cls); if (cls != NoSubscriberEvent.class && cls != SubscriberExceptionEvent.class) { post(new NoSubscriberEvent(this, remove)); } } } catch (Exception e) { e.printStackTrace(); } } } finally { booleanWrapper.a = false; } } } public final void postSticky(Object obj) { post(obj); synchronized (this.f) { this.f.put(obj.getClass(), obj); } } public final Object getStickyEvent(Class cls) { Object obj; synchronized (this.f) { obj = this.f.get(cls); } return obj; } public final Object removeStickyEvent(Class cls) { Object remove; synchronized (this.f) { remove = this.f.remove(cls); } return remove; } public final boolean removeStickyEvent(Object obj) { synchronized (this.f) { try { Class cls = obj.getClass(); if (!obj.equals(this.f.get(cls))) { return false; } this.f.remove(cls); return true; } catch (Throwable th) { throw th; } } } public final void release() { if (b != null) { b = null; a = null; Map, List>> map = c; if (map != null && map.size() > 0) { map.clear(); } Map, CopyOnWriteArrayList> map2 = this.d; if (map2 != null && map2.size() > 0) { this.d.clear(); } Map>> map3 = this.e; if (map3 != null && map3.size() > 0) { this.e.clear(); } Map, Object> map4 = this.f; if (map4 != null && map4.size() > 0) { this.f.clear(); } Map map5 = this.p; if (map5 == null || map5.size() <= 0) { return; } this.p.clear(); } } /* renamed from: com.mbridge.msdk.dycreator.bus.EventBus$3, reason: invalid class name */ public static /* synthetic */ class AnonymousClass3 { static final /* synthetic */ int[] a; static { int[] iArr = new int[ThreadMode.values().length]; a = iArr; try { iArr[ThreadMode.PostThread.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { a[ThreadMode.MainThread.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { a[ThreadMode.BackgroundThread.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { a[ThreadMode.Async.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } } } private void a(Subscription subscription, Object obj, boolean z) { int i = AnonymousClass3.a[subscription.b.b.ordinal()]; if (i == 1) { a(subscription, obj); return; } if (i == 2) { if (z) { a(subscription, obj); return; } else { this.j.a(subscription, obj); return; } } if (i == 3) { if (z) { this.k.enqueue(subscription, obj); return; } else { a(subscription, obj); return; } } if (i == 4) { this.l.enqueue(subscription, obj); return; } throw new IllegalStateException("Unknown thread mode: " + subscription.b.b); } private List> a(Class cls) { List> list; Map, List>> map = c; synchronized (map) { try { list = map.get(cls); if (list == null) { list = new ArrayList<>(); for (Class cls2 = cls; cls2 != null; cls2 = cls2.getSuperclass()) { list.add(cls2); a(list, cls2.getInterfaces()); } c.put(cls, list); } } catch (Throwable th) { throw th; } } return list; } private static void a(List> list, Class[] clsArr) { for (Class cls : clsArr) { if (!list.contains(cls)) { list.add(cls); a(list, cls.getInterfaces()); } } } public final void a(PendingPost pendingPost) { Object obj = pendingPost.a; Subscription subscription = pendingPost.b; PendingPost.a(pendingPost); a(subscription, obj); } private void a(Subscription subscription, Object obj) throws Error { try { subscription.b.a.invoke(subscription.a, obj); } catch (IllegalAccessException e) { throw new IllegalStateException("Unexpected exception", e); } catch (InvocationTargetException e2) { Throwable cause = e2.getCause(); if (obj instanceof SubscriberExceptionEvent) { Log.e(TAG, "SubscriberExceptionEvent subscriber " + subscription.a.getClass() + " threw an exception", cause); SubscriberExceptionEvent subscriberExceptionEvent = (SubscriberExceptionEvent) obj; Log.e(TAG, "Initial event " + subscriberExceptionEvent.causingEvent + " caused exception in " + subscriberExceptionEvent.causingSubscriber, subscriberExceptionEvent.throwable); return; } if (this.o) { Log.e(TAG, "Could not dispatch event: " + obj.getClass() + " to subscribing class " + subscription.a.getClass(), cause); } post(new SubscriberExceptionEvent(this, cause, obj, subscription.a)); } } }