Permalink
Browse files

Providing mechanisms to allow non Android use of the library.

  • Loading branch information...
william-ferguson-au authored and greenrobot committed Jan 30, 2016
1 parent 2c01ac0 commit 7a0949fcfbc60e37dfc7800baa28e3581ed1624d
@@ -21,7 +21,7 @@
*
* @author Markus
*/
class AsyncPoster implements Runnable {
class AsyncPoster implements Runnable, Poster {
private final PendingPostQueue queue;
private final EventBus eventBus;
@@ -15,14 +15,14 @@
*/
package org.greenrobot.eventbus;
import android.util.Log;
import org.greenrobot.eventbus.log.EBLog;
/**
* Posts events in background.
*
* @author Markus
*/
final class BackgroundPoster implements Runnable {
final class BackgroundPoster implements Runnable, Poster {
private final PendingPostQueue queue;
private final EventBus eventBus;
@@ -64,7 +64,7 @@ public void run() {
eventBus.invokeSubscriber(pendingPost);
}
} catch (InterruptedException e) {
Log.w("Event", Thread.currentThread().getName() + " was interruppted", e);
EBLog.w(Thread.currentThread().getName() + " was interruppted", e);
}
} finally {
executorRunning = false;
@@ -15,8 +15,12 @@
*/
package org.greenrobot.eventbus;
import android.os.Looper;
import android.util.Log;
import org.greenrobot.eventbus.log.AndroidLog;
import org.greenrobot.eventbus.log.EBLog;
import org.greenrobot.eventbus.log.SystemOutLog;
import org.greenrobot.eventbus.util.AndroidMTCalculator;
import org.greenrobot.eventbus.util.MainThreadCalculator;
import org.greenrobot.eventbus.util.NonAndroidMTCalculator;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
@@ -59,11 +63,12 @@ protected PostingThreadState initialValue() {
}
};
private final HandlerPoster mainThreadPoster;
private final Poster mainThreadPoster;
private final BackgroundPoster backgroundPoster;
private final AsyncPoster asyncPoster;
private final SubscriberMethodFinder subscriberMethodFinder;
private final ExecutorService executorService;
private final MainThreadCalculator mtCalculator;
private final boolean throwSubscriberException;
private final boolean logSubscriberExceptions;
@@ -105,10 +110,15 @@ public EventBus() {
}
EventBus(EventBusBuilder builder) {
if (builder.logTarget == null) {
EBLog.setLogTarget(builder.nonAndroidEnvironment ? new SystemOutLog() : new AndroidLog(TAG));
} else {
EBLog.setLogTarget(builder.logTarget);
}
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
mainThreadPoster = (builder.nonAndroidEnvironment ? new SyncPoster(this) : new HandlerPoster(this, 10));
backgroundPoster = new BackgroundPoster(this);
asyncPoster = new AsyncPoster(this);
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
@@ -121,6 +131,7 @@ public EventBus() {
throwSubscriberException = builder.throwSubscriberException;
eventInheritance = builder.eventInheritance;
executorService = builder.executorService;
mtCalculator = (builder.nonAndroidEnvironment ? new NonAndroidMTCalculator() : new AndroidMTCalculator());
}
/**
@@ -196,7 +207,7 @@ private void checkPostStickyEventToSubscription(Subscription newSubscription, Ob
if (stickyEvent != null) {
// If the subscriber is trying to abort the event, it will fail (event is not tracked in posting state)
// --> Strange corner case, which we don't take care of here.
postToSubscription(newSubscription, stickyEvent, Looper.getMainLooper() == Looper.myLooper());
postToSubscription(newSubscription, stickyEvent, mtCalculator.isMainThread());
}
}
@@ -230,7 +241,7 @@ public synchronized void unregister(Object subscriber) {
}
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
EBLog.w("Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
@@ -241,7 +252,7 @@ public void post(Object event) {
eventQueue.add(event);
if (!postingState.isPosting) {
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isMainThread = mtCalculator.isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
@@ -374,7 +385,7 @@ private void postSingleEvent(Object event, PostingThreadState postingState) thro
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
EBLog.d("No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
@@ -494,18 +505,18 @@ private void handleSubscriberException(Subscription subscription, Object event,
if (event instanceof SubscriberExceptionEvent) {
if (logSubscriberExceptions) {
// Don't send another SubscriberExceptionEvent to avoid infinite event recursion, just log
Log.e(TAG, "SubscriberExceptionEvent subscriber " + subscription.subscriber.getClass()
EBLog.e("SubscriberExceptionEvent subscriber " + subscription.subscriber.getClass()
+ " threw an exception", cause);
SubscriberExceptionEvent exEvent = (SubscriberExceptionEvent) event;
Log.e(TAG, "Initial event " + exEvent.causingEvent + " caused exception in "
EBLog.e("Initial event " + exEvent.causingEvent + " caused exception in "
+ exEvent.causingSubscriber, exEvent.throwable);
}
} else {
if (throwSubscriberException) {
throw new EventBusException("Invoking subscriber failed", cause);
}
if (logSubscriberExceptions) {
Log.e(TAG, "Could not dispatch event: " + event.getClass() + " to subscribing class "
EBLog.e("Could not dispatch event: " + event.getClass() + " to subscribing class "
+ subscription.subscriber.getClass(), cause);
}
if (sendSubscriberExceptionEvent) {
@@ -518,7 +529,7 @@ private void handleSubscriberException(Subscription subscription, Object event,
/** For ThreadLocal, much faster to set (and get multiple values). */
final static class PostingThreadState {
final List<Object> eventQueue = new ArrayList<Object>();
final List<Object> eventQueue = new ArrayList<>();
boolean isPosting;
boolean isMainThread;
Subscription subscription;
@@ -17,6 +17,8 @@
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;
import org.greenrobot.eventbus.log.GenericLog;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
@@ -40,6 +42,8 @@
ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;
List<Class<?>> skipMethodVerificationForClasses;
List<SubscriberInfoIndex> subscriberInfoIndexes;
GenericLog logTarget;
boolean nonAndroidEnvironment;
EventBusBuilder() {
}
@@ -137,6 +141,22 @@ public EventBusBuilder addIndex(SubscriberInfoIndex index) {
return this;
}
/**
* Set a specific log handler for all EventBus logging.
*
* By default all logging is via {@link android.util.Log} but if you want to use EventBus
* outside the Android environment then you will need to provide another log target.
*/
public EventBusBuilder logger(GenericLog logTarget) {
this.logTarget = logTarget;
return this;
}
public EventBusBuilder nonAndroidEnvironment(boolean nonAndroidEnvironment) {
this.nonAndroidEnvironment = nonAndroidEnvironment;
return this;
}
/**
* Installs the default EventBus returned by {@link EventBus#getDefault()} using this builders' values. Must be
* done only once before the first usage of the default EventBus.
@@ -20,21 +20,21 @@
import android.os.Message;
import android.os.SystemClock;
final class HandlerPoster extends Handler {
final class HandlerPoster extends Handler implements Poster {
private final PendingPostQueue queue;
private final int maxMillisInsideHandleMessage;
private final EventBus eventBus;
private boolean handlerActive;
HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {
super(looper);
HandlerPoster(EventBus eventBus, int maxMillisInsideHandleMessage) {
super(Looper.getMainLooper());
this.eventBus = eventBus;
this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;
queue = new PendingPostQueue();
}
void enqueue(Subscription subscription, Object event) {
public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
queue.enqueue(pendingPost);
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.eventbus;
/**
* Posts events.
*
* @author William Ferguson
*/
interface Poster {
/**
* Enqueue an event to be posted for a particular subscription.
*
* @param subscription Subscription which will receive the event.
* @param event Event that will be posted to subscribers.
*/
void enqueue(Subscription subscription, Object event);
}
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.eventbus;
/**
* Posts events in the current Thread.
*
* @author William Ferguson
*/
class SyncPoster implements Poster {
private final EventBus eventBus;
SyncPoster(EventBus eventBus) {
this.eventBus = eventBus;
}
public void enqueue(Subscription subscription, Object event) {
eventBus.invokeSubscriber(subscription, event);
}
}
@@ -0,0 +1,95 @@
/*
* Copyright (c) Xandar IP 2013.
*
* All Rights Reserved
* No part of this application may be reproduced, copied, modified or adapted, without the prior written consent
* of the author, unless otherwise indicated for stand-alone materials.
*
* Contact support@xandar.com.au for copyright requests.
*/
package org.greenrobot.eventbus.log;
import android.util.Log;
/**
* Logs to the Android log.
*/
public final class AndroidLog implements GenericLog {
private final String tag;
public AndroidLog(String tag) {
this.tag = tag;
}
@Override
public void v(String msg) {
Log.v(tag, msg);
}
@Override
public void v(String msg, Throwable tr) {
Log.v(tag, msg, tr);
}
@Override
public void d(String msg) {
Log.d(tag, msg);
}
@Override
public void d(String msg, Throwable tr) {
Log.d(tag, msg, tr);
}
@Override
public void i(String msg) {
Log.i(tag, msg);
}
@Override
public void i(String msg, Throwable tr) {
Log.i(tag, msg, tr);
}
@Override
public void w(String msg) {
Log.w(tag, msg);
}
@Override
public void w(String msg, Throwable tr) {
Log.w(tag, msg, tr);
}
@Override
public void w(Throwable tr) {
Log.w(tag, tr);
}
@Override
public void e(String msg) {
Log.e(tag, msg);
}
@Override
public void e(String msg, Throwable tr) {
Log.e(tag, msg, tr);
}
@Override
public void wtf(String msg) {
Log.wtf(tag, msg);
}
@Override
public void wtf(Throwable tr) {
Log.wtf(tag, tr);
}
@Override
public void wtf(String msg, Throwable tr) {
Log.wtf(tag, msg, tr);
}
}
Oops, something went wrong.

0 comments on commit 7a0949f

Please sign in to comment.