Skip to content

Commit b8a44d8

Browse files
committed
android sdk proxy implementation
1 parent 9153576 commit b8a44d8

24 files changed

+273
-148
lines changed

EventBus/src/org/greenrobot/eventbus/EventBus.java

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.greenrobot.eventbus;
1717

18+
import org.greenrobot.eventbus.android.AndroidDependenciesDetector;
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.util.ArrayList;
2021
import java.util.HashMap;
@@ -139,6 +140,13 @@ public EventBus() {
139140
* ThreadMode} and priority.
140141
*/
141142
public void register(Object subscriber) {
143+
144+
if (AndroidDependenciesDetector.isAndroidSDKAvailable() && !AndroidDependenciesDetector.isAndroidSDKProxyImplAvailable()) {
145+
//should crash user's app if the user (developer) has not imported the android compatibility library
146+
throw new RuntimeException("Looks like you are using the latest version of EventBus on Android " +
147+
"without importing the EventBus for Android compatibility library. Please import it on app/build.gradle!");
148+
}
149+
142150
Class<?> subscriberClass = subscriber.getClass();
143151
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
144152
synchronized (this) {

EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.greenrobot.eventbus;
1717

18-
import org.greenrobot.eventbus.android.AndroidSDK;
18+
import org.greenrobot.eventbus.android.AndroidSDKProxy;
1919
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;
2020
import java.util.ArrayList;
2121
import java.util.List;
@@ -162,9 +162,8 @@ Logger getLogger() {
162162
MainThreadSupport getMainThreadSupport() {
163163
if (mainThreadSupport != null) {
164164
return mainThreadSupport;
165-
} else if (AndroidSDK.isAvailable()) {
166-
return new MainThreadSupport.AndroidHandlerMainThreadSupport(
167-
AndroidSDK.get().looper, AndroidSDK.get().systemClock);
165+
} else if (AndroidSDKProxy.isAvailable()) {
166+
return AndroidSDKProxy.get().defaultMainThreadSupport;
168167
} else {
169168
return null;
170169
}

EventBus/src/org/greenrobot/eventbus/Logger.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.greenrobot.eventbus;
1717

1818
import org.greenrobot.eventbus.android.AndroidLogger;
19-
import org.greenrobot.eventbus.android.AndroidSDK;
19+
import org.greenrobot.eventbus.android.AndroidSDKProxy;
2020
import java.util.logging.Level;
2121

2222
public interface Logger {
@@ -63,8 +63,8 @@ public void log(Level level, String msg, Throwable th) {
6363

6464
class Default {
6565
public static Logger get() {
66-
if (AndroidSDK.isAvailable()) {
67-
return new AndroidLogger(AndroidSDK.get(), "EventBus");
66+
if (AndroidSDKProxy.isAvailable()) {
67+
return new AndroidLogger(AndroidSDKProxy.get(), "EventBus");
6868
}
6969

7070
return new SystemOutLogger();

EventBus/src/org/greenrobot/eventbus/MainThreadSupport.java

-24
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.greenrobot.eventbus;
1717

18-
import org.greenrobot.eventbus.android.AndroidSDK;
19-
2018
/**
2119
* Interface to the "main" thread, which can be whatever you like. Typically on Android, Android's main thread is used.
2220
*/
@@ -25,26 +23,4 @@ public interface MainThreadSupport {
2523
boolean isMainThread();
2624

2725
Poster createPoster(EventBus eventBus);
28-
29-
class AndroidHandlerMainThreadSupport implements MainThreadSupport {
30-
31-
private final AndroidSDK.Looper looper;
32-
private final AndroidSDK.SystemClock systemClock;
33-
34-
public AndroidHandlerMainThreadSupport(AndroidSDK.Looper looper, AndroidSDK.SystemClock systemClock) {
35-
this.looper = looper;
36-
this.systemClock = systemClock;
37-
}
38-
39-
@Override
40-
public boolean isMainThread() {
41-
return looper == AndroidSDK.Looper.myLooper();
42-
}
43-
44-
@Override
45-
public Poster createPoster(EventBus eventBus) {
46-
return new HandlerPoster(eventBus, looper, systemClock, 10);
47-
}
48-
}
49-
5026
}

EventBus/src/org/greenrobot/eventbus/Poster.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author William Ferguson
2222
*/
23-
interface Poster {
23+
public interface Poster {
2424

2525
/**
2626
* Enqueue an event to be posted for a particular subscription.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.greenrobot.eventbus.android;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
@SuppressWarnings("TryWithIdenticalCatches")
7+
public class AndroidDependenciesDetector {
8+
9+
public static boolean isAndroidSDKAvailable() {
10+
11+
try {
12+
Class<?> looperClass = Class.forName("android.os.Looper");
13+
Method getMainLooper = looperClass.getDeclaredMethod("getMainLooper");
14+
Object mainLooper = getMainLooper.invoke(null);
15+
return mainLooper != null;
16+
}
17+
catch (ClassNotFoundException ignored) {}
18+
catch (NoSuchMethodException ignored) {}
19+
catch (IllegalAccessException ignored) {}
20+
catch (InvocationTargetException ignored) {}
21+
22+
return false;
23+
}
24+
25+
public static boolean isAndroidSDKProxyImplAvailable() {
26+
27+
try {
28+
Class.forName("org.greenrobot.eventbus.android.AndroidSDKProxyImpl");
29+
return true;
30+
}
31+
catch (ClassNotFoundException ex) {
32+
return false;
33+
}
34+
}
35+
36+
public static AndroidSDKProxy instantiateAndroidSDKProxy() {
37+
38+
try {
39+
Class<?> impl = Class.forName("org.greenrobot.eventbus.android.AndroidSDKProxyImpl");
40+
return (AndroidSDKProxy) impl.getConstructor().newInstance();
41+
}
42+
catch (Throwable ex) {
43+
return null;
44+
}
45+
}
46+
}

EventBus/src/org/greenrobot/eventbus/android/AndroidLogger.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
public class AndroidLogger implements Logger {
2323

24-
private final AndroidSDK androidSDK;
24+
private final AndroidSDKProxy androidSDK;
2525
private final String tag;
2626

27-
public AndroidLogger(AndroidSDK androidSDK, String tag) {
27+
public AndroidLogger(AndroidSDKProxy androidSDK, String tag) {
2828
this.androidSDK = androidSDK;
2929
this.tag = tag;
3030
}
@@ -46,16 +46,16 @@ private int mapLevel(Level level) {
4646
int value = level.intValue();
4747
if (value < 800) { // below INFO
4848
if (value < 500) { // below FINE
49-
return androidSDK.log.getLogLevels().verbose;
49+
return androidSDK.log.getVerboseLevelId();
5050
} else {
51-
return androidSDK.log.getLogLevels().debug;
51+
return androidSDK.log.getDebugLevelId();
5252
}
5353
} else if (value < 900) { // below WARNING
54-
return androidSDK.log.getLogLevels().info;
54+
return androidSDK.log.getInfoLevelId();
5555
} else if (value < 1000) { // below ERROR
56-
return androidSDK.log.getLogLevels().warn;
56+
return androidSDK.log.getWarnLevelId();
5757
} else {
58-
return androidSDK.log.getLogLevels().error;
58+
return androidSDK.log.getErrorLevelId();
5959
}
6060
}
6161
}

EventBus/src/org/greenrobot/eventbus/android/AndroidSDK.java

-97
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.greenrobot.eventbus.android;
2+
3+
import org.greenrobot.eventbus.MainThreadSupport;
4+
5+
public abstract class AndroidSDKProxy {
6+
7+
private static final AndroidSDKProxy implementation;
8+
9+
static {
10+
implementation = AndroidDependenciesDetector.isAndroidSDKAvailable()
11+
? AndroidDependenciesDetector.instantiateAndroidSDKProxy()
12+
: null;
13+
}
14+
15+
public static boolean isAvailable() {
16+
return implementation != null;
17+
}
18+
19+
public static AndroidSDKProxy get() {
20+
return implementation;
21+
}
22+
23+
public final LogProxy log;
24+
public final MainThreadSupport defaultMainThreadSupport;
25+
26+
public AndroidSDKProxy(LogProxy log, MainThreadSupport defaultMainThreadSupport) {
27+
this.log = log;
28+
this.defaultMainThreadSupport = defaultMainThreadSupport;
29+
}
30+
31+
interface LogProxy {
32+
33+
void println(int priority, String tag, String msg);
34+
35+
int getVerboseLevelId();
36+
int getDebugLevelId();
37+
int getInfoLevelId();
38+
int getWarnLevelId();
39+
int getErrorLevelId();
40+
}
41+
}

EventBusPerformance/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apply plugin: 'com.android.application'
1313

1414
dependencies {
1515
implementation project(':eventbus')
16+
implementation project(':eventbus-android')
1617
annotationProcessor project(':eventbus-annotation-processor')
1718
implementation 'com.squareup:otto:1.3.8'
1819
}

EventBusTest/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apply plugin: 'com.android.application'
1313

1414
dependencies {
1515
androidTestImplementation project(':eventbus')
16+
androidTestImplementation project(':eventbus-android')
1617
androidTestImplementation project(':EventBusTestJava')
1718
androidTestAnnotationProcessor project(':eventbus-annotation-processor')
1819
// Trying to repro bug:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.greenrobot.eventbus;
2+
3+
import org.greenrobot.eventbus.android.AndroidSDKProxy;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class AndroidSDKProxyTest {
10+
11+
@Test
12+
public void shouldBeAvailable() {
13+
assertTrue(AndroidSDKProxy.isAvailable());
14+
assertNotNull(AndroidSDKProxy.get());
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.greenrobot.eventbus;
2+
3+
import org.greenrobot.eventbus.android.AndroidSDKProxy;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertNull;
8+
9+
public class AndroidSDKAvailabilityTest {
10+
11+
@Test
12+
public void shouldNotBeAvailable() {
13+
assertFalse(AndroidSDKProxy.isAvailable());
14+
assertNull(AndroidSDKProxy.get());
15+
}
16+
}

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusBuilderTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.junit.Assert;
1919
import org.junit.Test;
2020

21-
import static org.junit.Assert.assertEquals;
2221
import static org.junit.Assert.fail;
2322

2423
/**

eventbus-android/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)