4343import com .optimizely .ab .config .parser .ConfigParseException ;
4444import com .optimizely .ab .error .ErrorHandler ;
4545import com .optimizely .ab .event .EventHandler ;
46+ import com .optimizely .ab .event .EventProcessor ;
47+ import com .optimizely .ab .event .ForwardingEventProcessor ;
4648import com .optimizely .ab .event .internal .payload .EventBatch ;
4749import com .optimizely .ab .notification .NotificationCenter ;
4850import com .optimizely .ab .notification .UpdateConfigNotification ;
@@ -66,6 +68,8 @@ public class OptimizelyManager {
6668 private final long datafileDownloadInterval ;
6769 private final long eventDispatchInterval ;
6870 @ Nullable private EventHandler eventHandler = null ;
71+ @ Nullable private EventProcessor eventProcessor = null ;
72+ @ Nullable private NotificationCenter notificationCenter = null ;
6973 @ Nullable private ErrorHandler errorHandler ;
7074 @ NonNull private Logger logger ;
7175 @ Nullable private final String projectId ;
@@ -85,7 +89,9 @@ public class OptimizelyManager {
8589 @ Nullable ErrorHandler errorHandler ,
8690 long eventDispatchInterval ,
8791 @ NonNull EventHandler eventHandler ,
88- @ NonNull UserProfileService userProfileService ) {
92+ @ Nullable EventProcessor eventProcessor ,
93+ @ NonNull UserProfileService userProfileService ,
94+ @ NonNull NotificationCenter notificationCenter ) {
8995
9096 if (projectId == null && sdkKey == null ) {
9197 logger .error ("projectId and sdkKey are both null!" );
@@ -103,8 +109,10 @@ public class OptimizelyManager {
103109 this .datafileHandler = datafileHandler ;
104110 this .eventDispatchInterval = eventDispatchInterval ;
105111 this .eventHandler = eventHandler ;
112+ this .eventProcessor = eventProcessor ;
106113 this .errorHandler = errorHandler ;
107114 this .userProfileService = userProfileService ;
115+ this .notificationCenter = notificationCenter ;
108116 }
109117
110118 @ VisibleForTesting
@@ -501,6 +509,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
501509 Optimizely .Builder builder = Optimizely .builder ();
502510
503511 builder .withEventHandler (eventHandler );
512+ builder .withEventProcessor (eventProcessor );
504513
505514 if (datafileHandler instanceof DefaultDatafileHandler ) {
506515 DefaultDatafileHandler handler = (DefaultDatafileHandler )datafileHandler ;
@@ -519,7 +528,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
519528 }
520529
521530 builder .withUserProfileService (userProfileService );
522-
531+ builder . withNotificationCenter ( notificationCenter );
523532 Optimizely optimizely = builder .build ();
524533 return new OptimizelyClient (optimizely , LoggerFactory .getLogger (OptimizelyClient .class ));
525534 }
@@ -644,6 +653,8 @@ public static class Builder {
644653 @ Nullable private Logger logger = null ;
645654 @ Nullable private EventHandler eventHandler = null ;
646655 @ Nullable private ErrorHandler errorHandler = null ;
656+ @ Nullable private EventProcessor eventProcessor = null ;
657+ @ Nullable private NotificationCenter notificationCenter = null ;
647658 @ Nullable private UserProfileService userProfileService = null ;
648659 @ Nullable private String sdkKey = null ;
649660 @ Nullable private DatafileConfig datafileConfig = null ;
@@ -748,6 +759,16 @@ public Builder withDatafileConfig(DatafileConfig datafileConfig) {
748759 return this ;
749760 }
750761
762+ public Builder withEventProcessor (EventProcessor eventProcessor ) {
763+ this .eventProcessor = eventProcessor ;
764+ return this ;
765+ }
766+
767+ public Builder withNotificationCenter (NotificationCenter notificationCenter ) {
768+ this .notificationCenter = notificationCenter ;
769+ return this ;
770+ }
771+
751772 /**
752773 * Get a new {@link Builder} instance to create {@link OptimizelyManager} with.
753774 * @param context the application context used to create default service if not provided.
@@ -791,6 +812,14 @@ public OptimizelyManager build(Context context) {
791812 eventHandler = DefaultEventHandler .getInstance (context );
792813 }
793814
815+ if (notificationCenter == null ) {
816+ notificationCenter = new NotificationCenter ();
817+ }
818+
819+ if (eventProcessor == null ) {
820+ eventProcessor = new ForwardingEventProcessor (eventHandler , notificationCenter );
821+ }
822+
794823 if (projectId == null && sdkKey == null ) {
795824 logger .error ("ProjectId and SDKKey cannot both be null" );
796825 return null ;
@@ -804,7 +833,9 @@ public OptimizelyManager build(Context context) {
804833 errorHandler ,
805834 eventDispatchInterval ,
806835 eventHandler ,
807- userProfileService );
836+ eventProcessor ,
837+ userProfileService ,
838+ notificationCenter );
808839 }
809840 }
810841}
0 commit comments