diff --git a/checkstyle.xml b/checkstyle.xml
index 869ec7c..0e805cc 100644
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -107,7 +107,7 @@
-
+
diff --git a/otto-android/pom.xml b/otto-android/pom.xml
new file mode 100644
index 0000000..b9d24b9
--- /dev/null
+++ b/otto-android/pom.xml
@@ -0,0 +1,71 @@
+
+
+
+ 4.0.0
+
+
+ com.squareup
+ otto-parent
+ 1.3.9-SNAPSHOT
+ ../pom.xml
+
+
+ com.squareup
+ otto-android
+ jar
+ Otto Android
+
+
+
+ com.squareup
+ otto
+ ${project.version}
+
+
+ com.google.android
+ android
+ provided
+
+
+
+ junit
+ junit
+ test
+
+
+ org.easytesting
+ fest-assert-core
+ test
+
+
+
+
+ ${binary.prefix}-${project.artifactId}-${project.version}
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+
+
+
+
diff --git a/otto-android/src/main/java/com/squareup/otto/ThreadEnforcerAndroid.java b/otto-android/src/main/java/com/squareup/otto/ThreadEnforcerAndroid.java
new file mode 100644
index 0000000..7a02967
--- /dev/null
+++ b/otto-android/src/main/java/com/squareup/otto/ThreadEnforcerAndroid.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2012 Square, Inc.
+ *
+ * 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 com.squareup.otto;
+
+import android.os.Looper;
+
+/**
+ * Enforces a thread confinement policy for methods on a particular event bus.
+ *
+ * This class contains Android specific Thread enforcers.
+ *
+ * @author Jake Wharton
+ */
+public interface ThreadEnforcerAndroid extends ThreadEnforcer {
+
+ /** A {@link ThreadEnforcer} that confines {@link Bus} methods to the main thread. */
+ ThreadEnforcer MAIN = new ThreadEnforcer() {
+ @Override public void enforce(Bus bus) {
+ if (Looper.myLooper() != Looper.getMainLooper()) {
+ throw new IllegalStateException("Event bus " + bus + " accessed from non-main thread " + Looper.myLooper());
+ }
+ }
+ };
+
+}
diff --git a/otto-android/src/test/java/com/squareup/otto/ThreadEnforcerTest.java b/otto-android/src/test/java/com/squareup/otto/ThreadEnforcerTest.java
new file mode 100644
index 0000000..3b98925
--- /dev/null
+++ b/otto-android/src/test/java/com/squareup/otto/ThreadEnforcerTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2012 Square, Inc.
+ *
+ * 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 com.squareup.otto;
+
+import org.junit.Test;
+
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertTrue;
+
+public class ThreadEnforcerTest {
+
+ private static class RecordingThreadEnforcer implements ThreadEnforcer {
+ boolean called = false;
+
+ @Override public void enforce(Bus bus) {
+ called = true;
+ }
+ }
+
+ @Test public void enforerCalledForRegister() {
+ RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
+ Bus bus = new Bus(enforcer);
+
+ assertFalse(enforcer.called);
+ bus.register(this);
+ assertTrue(enforcer.called);
+ }
+
+ @Test public void enforcerCalledForPost() {
+ RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
+ Bus bus = new Bus(enforcer);
+
+ assertFalse(enforcer.called);
+ bus.post(this);
+ assertTrue(enforcer.called);
+ }
+
+ @Test public void enforcerCalledForUnregister() {
+ RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
+ Bus bus = new Bus(enforcer);
+
+ assertFalse(enforcer.called);
+ bus.unregister(this);
+ assertTrue(enforcer.called);
+ }
+
+}
diff --git a/otto-sample/pom.xml b/otto-sample/pom.xml
index 4b9cc89..7bb16b5 100644
--- a/otto-sample/pom.xml
+++ b/otto-sample/pom.xml
@@ -44,6 +44,11 @@
otto
${project.version}
+
+ com.squareup
+ otto-android
+ ${project.version}
+
diff --git a/otto-sample/src/main/java/com/squareup/otto/sample/BusProvider.java b/otto-sample/src/main/java/com/squareup/otto/sample/BusProvider.java
index 9ab71c1..32f8ca5 100644
--- a/otto-sample/src/main/java/com/squareup/otto/sample/BusProvider.java
+++ b/otto-sample/src/main/java/com/squareup/otto/sample/BusProvider.java
@@ -17,13 +17,14 @@
package com.squareup.otto.sample;
import com.squareup.otto.Bus;
+import com.squareup.otto.ThreadEnforcerAndroid;
/**
* Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means
* such as through injection directly into interested classes.
*/
public final class BusProvider {
- private static final Bus BUS = new Bus();
+ private static final Bus BUS = new Bus(ThreadEnforcerAndroid.MAIN);
public static Bus getInstance() {
return BUS;
diff --git a/otto/pom.xml b/otto/pom.xml
index a3b82ef..55861ea 100644
--- a/otto/pom.xml
+++ b/otto/pom.xml
@@ -30,12 +30,6 @@
Otto
-
- com.google.android
- android
- provided
-
-
junit
junit
diff --git a/otto/src/main/java/com/squareup/otto/Bus.java b/otto/src/main/java/com/squareup/otto/Bus.java
index 0ab6620..b860606 100644
--- a/otto/src/main/java/com/squareup/otto/Bus.java
+++ b/otto/src/main/java/com/squareup/otto/Bus.java
@@ -66,7 +66,7 @@
* Handlers should not, in general, throw. If they do, the Bus will wrap the exception and
* re-throw it.
*
- *
The Bus by default enforces that all interactions occur on the main thread. You can provide an alternate
+ *
The Bus does not by default enforces any thread. You can provide an alternate
* enforcement by passing a {@link ThreadEnforcer} to the constructor.
*
*
Producer Methods
@@ -119,18 +119,18 @@ public class Bus {
}
};
- /** Creates a new Bus named "default" that enforces actions on the main thread. */
+ /** Creates a new Bus named "default" that does not enforce actions on a given thread. */
public Bus() {
this(DEFAULT_IDENTIFIER);
}
/**
- * Creates a new Bus with the given {@code identifier} that enforces actions on the main thread.
+ * Creates a new Bus with the given {@code identifier} that does not enforce actions on a given thread.
*
* @param identifier a brief name for this bus, for debugging purposes. Should be a valid Java identifier.
*/
public Bus(String identifier) {
- this(ThreadEnforcer.MAIN, identifier);
+ this(ThreadEnforcer.ANY, identifier);
}
/**
diff --git a/otto/src/main/java/com/squareup/otto/ThreadEnforcer.java b/otto/src/main/java/com/squareup/otto/ThreadEnforcer.java
index 446c207..c25b876 100644
--- a/otto/src/main/java/com/squareup/otto/ThreadEnforcer.java
+++ b/otto/src/main/java/com/squareup/otto/ThreadEnforcer.java
@@ -16,8 +16,6 @@
package com.squareup.otto;
-import android.os.Looper;
-
/**
* Enforces a thread confinement policy for methods on a particular event bus.
*
@@ -32,21 +30,10 @@ public interface ThreadEnforcer {
*/
void enforce(Bus bus);
-
/** A {@link ThreadEnforcer} that does no verification. */
ThreadEnforcer ANY = new ThreadEnforcer() {
@Override public void enforce(Bus bus) {
// Allow any thread.
}
};
-
- /** A {@link ThreadEnforcer} that confines {@link Bus} methods to the main thread. */
- ThreadEnforcer MAIN = new ThreadEnforcer() {
- @Override public void enforce(Bus bus) {
- if (Looper.myLooper() != Looper.getMainLooper()) {
- throw new IllegalStateException("Event bus " + bus + " accessed from non-main thread " + Looper.myLooper());
- }
- }
- };
-
}
diff --git a/pom.xml b/pom.xml
index f6bcbba..29290c7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,6 +34,7 @@
otto
+ otto-android
otto-sample
diff --git a/website/index.html b/website/index.html
index 8cec1ab..869df0a 100644
--- a/website/index.html
+++ b/website/index.html
@@ -105,11 +105,14 @@ Producing
Thread Enforcement
Since at times it may be ambiguous on which thread you are receiving callbacks, Otto provides an
- enforcement mechanism to ensure you are always called on the thread you desire. By default, all interaction
- with an instance is confined to the main thread.
+ enforcement mechanism to ensure you are always called on the thread you desire. By default, no enforcement
+ is performed.
// Both of these are functionally equivalent.
Bus bus1 = new Bus();
-Bus bus2 = new Bus(ThreadEnforcer.MAIN);
+Bus bus2 = new Bus(ThreadEnforcer.ANY);
+ // To enforce Android Main Thread use the following.
+Bus bus2 = new Bus(ThreadEnforcerAndroid.MAIN);
+
If you are not concerned on which thread interaction is occurring, instantiate a bus instance with
ThreadEnforcer.ANY. You can also provide your own implementation of the
ThreadEnforcer interface if you need additional functionality or validation.
@@ -121,6 +124,7 @@ Download
Gradle
dependencies {
compile 'com.squareup:otto:+'
+ compile 'com.squareup:otto-android:+'
}
Maven
@@ -128,6 +132,11 @@ Maven
<groupId>com.squareup</groupId>
<artifactId>otto</artifactId>
<version>(insert latest version)</version>
+</dependency>
+<dependency>
+ <groupId>com.squareup</groupId>
+ <artifactId>otto-android</artifactId>
+ <version>(insert latest version)</version>
</dependency>
Once you've installed Otto, add the following lines to your proguard-project.txt file:
-keepattributes *Annotation*