384 lines
12 KiB
Java
384 lines
12 KiB
Java
package org.greenrobot.eventbus;
|
|
|
|
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 org.greenrobot.eventbus.meta.SubscriberInfoIndex;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class EventBus {
|
|
public static String q = "EventBus";
|
|
static volatile EventBus r;
|
|
private static final EventBusBuilder s = new EventBusBuilder();
|
|
private static final Map<Class<?>, List<Class<?>>> t = new HashMap();
|
|
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> a;
|
|
private final Map<Object, List<Class<?>>> b;
|
|
private final Map<Class<?>, Object> c;
|
|
private final ThreadLocal<PostingThreadState> d;
|
|
private final HandlerPoster e;
|
|
private final BackgroundPoster f;
|
|
private final AsyncPoster g;
|
|
private final SubscriberMethodFinder h;
|
|
private final ExecutorService i;
|
|
private final boolean j;
|
|
private final boolean k;
|
|
private final boolean l;
|
|
private final boolean m;
|
|
private final boolean n;
|
|
private final boolean o;
|
|
private final int p;
|
|
|
|
/* renamed from: org.greenrobot.eventbus.EventBus$2, reason: invalid class name */
|
|
static /* synthetic */ class AnonymousClass2 {
|
|
static final /* synthetic */ int[] a = new int[ThreadMode.values().length];
|
|
|
|
static {
|
|
try {
|
|
a[ThreadMode.POSTING.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
a[ThreadMode.MAIN.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
try {
|
|
a[ThreadMode.BACKGROUND.ordinal()] = 3;
|
|
} catch (NoSuchFieldError unused3) {
|
|
}
|
|
try {
|
|
a[ThreadMode.ASYNC.ordinal()] = 4;
|
|
} catch (NoSuchFieldError unused4) {
|
|
}
|
|
}
|
|
}
|
|
|
|
static final class PostingThreadState {
|
|
final List<Object> a = new ArrayList();
|
|
boolean b;
|
|
boolean c;
|
|
Subscription d;
|
|
Object e;
|
|
boolean f;
|
|
|
|
PostingThreadState() {
|
|
}
|
|
}
|
|
|
|
public EventBus() {
|
|
this(s);
|
|
}
|
|
|
|
private void a(Object obj, SubscriberMethod subscriberMethod) {
|
|
Class<?> cls = subscriberMethod.c;
|
|
Subscription subscription = new Subscription(obj, subscriberMethod);
|
|
CopyOnWriteArrayList<Subscription> copyOnWriteArrayList = this.a.get(cls);
|
|
if (copyOnWriteArrayList == null) {
|
|
copyOnWriteArrayList = new CopyOnWriteArrayList<>();
|
|
this.a.put(cls, copyOnWriteArrayList);
|
|
} else if (copyOnWriteArrayList.contains(subscription)) {
|
|
throw new EventBusException("Subscriber " + obj.getClass() + " already registered to event " + cls);
|
|
}
|
|
int size = copyOnWriteArrayList.size();
|
|
for (int i = 0; i <= size; i++) {
|
|
if (i == size || subscriberMethod.d > copyOnWriteArrayList.get(i).b.d) {
|
|
copyOnWriteArrayList.add(i, subscription);
|
|
break;
|
|
}
|
|
}
|
|
List<Class<?>> list = this.b.get(obj);
|
|
if (list == null) {
|
|
list = new ArrayList<>();
|
|
this.b.put(obj, list);
|
|
}
|
|
list.add(cls);
|
|
if (subscriberMethod.e) {
|
|
if (!this.o) {
|
|
b(subscription, this.c.get(cls));
|
|
return;
|
|
}
|
|
for (Map.Entry<Class<?>, Object> entry : this.c.entrySet()) {
|
|
if (cls.isAssignableFrom(entry.getKey())) {
|
|
b(subscription, entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static EventBus b() {
|
|
if (r == null) {
|
|
synchronized (EventBus.class) {
|
|
if (r == null) {
|
|
r = new EventBus();
|
|
}
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
public void c(Object obj) {
|
|
List<SubscriberMethod> a = this.h.a(obj.getClass());
|
|
synchronized (this) {
|
|
Iterator<SubscriberMethod> it = a.iterator();
|
|
while (it.hasNext()) {
|
|
a(obj, it.next());
|
|
}
|
|
}
|
|
}
|
|
|
|
public synchronized void d(Object obj) {
|
|
List<Class<?>> list = this.b.get(obj);
|
|
if (list != null) {
|
|
Iterator<Class<?>> it = list.iterator();
|
|
while (it.hasNext()) {
|
|
a(obj, it.next());
|
|
}
|
|
this.b.remove(obj);
|
|
} else {
|
|
Log.w(q, "Subscriber to unregister was not registered before: " + obj.getClass());
|
|
}
|
|
}
|
|
|
|
public String toString() {
|
|
return "EventBus[indexCount=" + this.p + ", eventInheritance=" + this.o + "]";
|
|
}
|
|
|
|
EventBus(EventBusBuilder eventBusBuilder) {
|
|
this.d = new ThreadLocal<PostingThreadState>(this) { // from class: org.greenrobot.eventbus.EventBus.1
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // java.lang.ThreadLocal
|
|
public PostingThreadState initialValue() {
|
|
return new PostingThreadState();
|
|
}
|
|
};
|
|
this.a = new HashMap();
|
|
this.b = new HashMap();
|
|
this.c = new ConcurrentHashMap();
|
|
this.e = new HandlerPoster(this, Looper.getMainLooper(), 10);
|
|
this.f = new BackgroundPoster(this);
|
|
this.g = new AsyncPoster(this);
|
|
List<SubscriberInfoIndex> list = eventBusBuilder.j;
|
|
this.p = list != null ? list.size() : 0;
|
|
this.h = new SubscriberMethodFinder(eventBusBuilder.j, eventBusBuilder.h, eventBusBuilder.g);
|
|
this.k = eventBusBuilder.a;
|
|
this.l = eventBusBuilder.b;
|
|
this.m = eventBusBuilder.c;
|
|
this.n = eventBusBuilder.d;
|
|
this.j = eventBusBuilder.e;
|
|
this.o = eventBusBuilder.f;
|
|
this.i = eventBusBuilder.i;
|
|
}
|
|
|
|
private void b(Subscription subscription, Object obj) {
|
|
if (obj != null) {
|
|
a(subscription, obj, Looper.getMainLooper() == Looper.myLooper());
|
|
}
|
|
}
|
|
|
|
public void b(Object obj) {
|
|
PostingThreadState postingThreadState = this.d.get();
|
|
List<Object> list = postingThreadState.a;
|
|
list.add(obj);
|
|
if (postingThreadState.b) {
|
|
return;
|
|
}
|
|
postingThreadState.c = Looper.getMainLooper() == Looper.myLooper();
|
|
postingThreadState.b = true;
|
|
if (!postingThreadState.f) {
|
|
while (!list.isEmpty()) {
|
|
try {
|
|
a(list.remove(0), postingThreadState);
|
|
} finally {
|
|
postingThreadState.b = false;
|
|
postingThreadState.c = false;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
throw new EventBusException("Internal error. Abort state was not reset");
|
|
}
|
|
|
|
public synchronized boolean a(Object obj) {
|
|
return this.b.containsKey(obj);
|
|
}
|
|
|
|
private void a(Object obj, Class<?> cls) {
|
|
CopyOnWriteArrayList<Subscription> copyOnWriteArrayList = this.a.get(cls);
|
|
if (copyOnWriteArrayList != null) {
|
|
int size = copyOnWriteArrayList.size();
|
|
int i = 0;
|
|
while (i < size) {
|
|
Subscription subscription = copyOnWriteArrayList.get(i);
|
|
if (subscription.a == obj) {
|
|
subscription.c = false;
|
|
copyOnWriteArrayList.remove(i);
|
|
i--;
|
|
size--;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void a(Object obj, PostingThreadState postingThreadState) throws Error {
|
|
boolean a;
|
|
Class<?> cls = obj.getClass();
|
|
if (this.o) {
|
|
List<Class<?>> a2 = a(cls);
|
|
int size = a2.size();
|
|
a = false;
|
|
for (int i = 0; i < size; i++) {
|
|
a |= a(obj, postingThreadState, a2.get(i));
|
|
}
|
|
} else {
|
|
a = a(obj, postingThreadState, cls);
|
|
}
|
|
if (a) {
|
|
return;
|
|
}
|
|
if (this.l) {
|
|
Log.d(q, "No subscribers registered for event " + cls);
|
|
}
|
|
if (!this.n || cls == NoSubscriberEvent.class || cls == SubscriberExceptionEvent.class) {
|
|
return;
|
|
}
|
|
b(new NoSubscriberEvent(this, obj));
|
|
}
|
|
|
|
private boolean a(Object obj, PostingThreadState postingThreadState, Class<?> cls) {
|
|
CopyOnWriteArrayList<Subscription> copyOnWriteArrayList;
|
|
synchronized (this) {
|
|
copyOnWriteArrayList = this.a.get(cls);
|
|
}
|
|
if (copyOnWriteArrayList == null || copyOnWriteArrayList.isEmpty()) {
|
|
return false;
|
|
}
|
|
Iterator<Subscription> it = copyOnWriteArrayList.iterator();
|
|
while (it.hasNext()) {
|
|
Subscription next = it.next();
|
|
postingThreadState.e = obj;
|
|
postingThreadState.d = next;
|
|
try {
|
|
a(next, obj, postingThreadState.c);
|
|
if (postingThreadState.f) {
|
|
return true;
|
|
}
|
|
} finally {
|
|
postingThreadState.e = null;
|
|
postingThreadState.d = null;
|
|
postingThreadState.f = false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void a(Subscription subscription, Object obj, boolean z) {
|
|
int i = AnonymousClass2.a[subscription.b.b.ordinal()];
|
|
if (i == 1) {
|
|
a(subscription, obj);
|
|
return;
|
|
}
|
|
if (i == 2) {
|
|
if (z) {
|
|
a(subscription, obj);
|
|
return;
|
|
} else {
|
|
this.e.a(subscription, obj);
|
|
return;
|
|
}
|
|
}
|
|
if (i == 3) {
|
|
if (z) {
|
|
this.f.a(subscription, obj);
|
|
return;
|
|
} else {
|
|
a(subscription, obj);
|
|
return;
|
|
}
|
|
}
|
|
if (i == 4) {
|
|
this.g.a(subscription, obj);
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Unknown thread mode: " + subscription.b.b);
|
|
}
|
|
|
|
private static List<Class<?>> a(Class<?> cls) {
|
|
List<Class<?>> list;
|
|
synchronized (t) {
|
|
list = t.get(cls);
|
|
if (list == null) {
|
|
list = new ArrayList<>();
|
|
for (Class<?> cls2 = cls; cls2 != null; cls2 = cls2.getSuperclass()) {
|
|
list.add(cls2);
|
|
a(list, cls2.getInterfaces());
|
|
}
|
|
t.put(cls, list);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static void a(List<Class<?>> list, Class<?>[] clsArr) {
|
|
for (Class<?> cls : clsArr) {
|
|
if (!list.contains(cls)) {
|
|
list.add(cls);
|
|
a(list, cls.getInterfaces());
|
|
}
|
|
}
|
|
}
|
|
|
|
void a(PendingPost pendingPost) {
|
|
Object obj = pendingPost.a;
|
|
Subscription subscription = pendingPost.b;
|
|
PendingPost.a(pendingPost);
|
|
if (subscription.c) {
|
|
a(subscription, obj);
|
|
}
|
|
}
|
|
|
|
void a(Subscription subscription, Object obj) {
|
|
try {
|
|
subscription.b.a.invoke(subscription.a, obj);
|
|
} catch (IllegalAccessException e) {
|
|
throw new IllegalStateException("Unexpected exception", e);
|
|
} catch (InvocationTargetException e2) {
|
|
a(subscription, obj, e2.getCause());
|
|
}
|
|
}
|
|
|
|
private void a(Subscription subscription, Object obj, Throwable th) {
|
|
if (obj instanceof SubscriberExceptionEvent) {
|
|
if (this.k) {
|
|
Log.e(q, "SubscriberExceptionEvent subscriber " + subscription.a.getClass() + " threw an exception", th);
|
|
SubscriberExceptionEvent subscriberExceptionEvent = (SubscriberExceptionEvent) obj;
|
|
Log.e(q, "Initial event " + subscriberExceptionEvent.b + " caused exception in " + subscriberExceptionEvent.c, subscriberExceptionEvent.a);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
if (!this.j) {
|
|
if (this.k) {
|
|
Log.e(q, "Could not dispatch event: " + obj.getClass() + " to subscribing class " + subscription.a.getClass(), th);
|
|
}
|
|
if (this.m) {
|
|
b(new SubscriberExceptionEvent(this, th, obj, subscription.a));
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
throw new EventBusException("Invoking subscriber failed", th);
|
|
}
|
|
|
|
ExecutorService a() {
|
|
return this.i;
|
|
}
|
|
}
|