@@ -443,9 +443,27 @@ public void removeAllStickyEvents() {
443
443
}
444
444
}
445
445
446
+ public boolean hasSubscriberForEvent (Class <?> eventClass ) {
447
+ List <Class <?>> eventTypes = lookupAllEventTypes (eventClass );
448
+ if (eventTypes != null ) {
449
+ int countTypes = eventTypes .size ();
450
+ for (int h = 0 ; h < countTypes ; h ++) {
451
+ Class <?> clazz = eventTypes .get (h );
452
+ CopyOnWriteArrayList <Subscription > subscriptions ;
453
+ synchronized (this ) {
454
+ subscriptions = subscriptionsByEventType .get (clazz );
455
+ }
456
+ if (subscriptions != null && !subscriptions .isEmpty ()) {
457
+ return true ;
458
+ }
459
+ }
460
+ }
461
+ return false ;
462
+ }
463
+
446
464
private void postSingleEvent (Object event , PostingThreadState postingState ) throws Error {
447
465
Class <?> eventClass = event .getClass ();
448
- List <Class <?>> eventTypes = findEventTypes (eventClass );
466
+ List <Class <?>> eventTypes = lookupAllEventTypes (eventClass );
449
467
boolean subscriptionFound = false ;
450
468
int countTypes = eventTypes .size ();
451
469
for (int h = 0 ; h < countTypes ; h ++) {
@@ -512,8 +530,8 @@ private void postToSubscription(Subscription subscription, Object event, boolean
512
530
}
513
531
}
514
532
515
- /** Finds all Class objects including super classes and interfaces. */
516
- private List <Class <?>> findEventTypes (Class <?> eventClass ) {
533
+ /** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
534
+ private List <Class <?>> lookupAllEventTypes (Class <?> eventClass ) {
517
535
synchronized (eventTypesCache ) {
518
536
List <Class <?>> eventTypes = eventTypesCache .get (eventClass );
519
537
if (eventTypes == null ) {
@@ -567,7 +585,7 @@ void invokeSubscriber(Subscription subscription, Object event) {
567
585
568
586
private void handleSubscriberException (Subscription subscription , Object event , Throwable cause ) {
569
587
if (event instanceof SubscriberExceptionEvent ) {
570
- if (logSubscriberExceptions ) {
588
+ if (logSubscriberExceptions ) {
571
589
// Don't send another SubscriberExceptionEvent to avoid infinite event recursion, just log
572
590
Log .e (TAG , "SubscriberExceptionEvent subscriber " + subscription .subscriber .getClass ()
573
591
+ " threw an exception" , cause );
@@ -583,7 +601,7 @@ private void handleSubscriberException(Subscription subscription, Object event,
583
601
Log .e (TAG , "Could not dispatch event: " + event .getClass () + " to subscribing class "
584
602
+ subscription .subscriber .getClass (), cause );
585
603
}
586
- if (sendSubscriberExceptionEvent ) {
604
+ if (sendSubscriberExceptionEvent ) {
587
605
SubscriberExceptionEvent exEvent = new SubscriberExceptionEvent (this , cause , event ,
588
606
subscription .subscriber );
589
607
post (exEvent );
0 commit comments