Skip to content

Commit 21c6b56

Browse files
committed
refactorings
1 parent b8a44d8 commit 21c6b56

File tree

14 files changed

+95
-140
lines changed

14 files changed

+95
-140
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public EventBus() {
141141
*/
142142
public void register(Object subscriber) {
143143

144-
if (AndroidDependenciesDetector.isAndroidSDKAvailable() && !AndroidDependenciesDetector.isAndroidSDKProxyImplAvailable()) {
144+
if (AndroidDependenciesDetector.isAndroidSDKAvailable() && !AndroidDependenciesDetector.areAndroidComponentsAvailable()) {
145145
//should crash user's app if the user (developer) has not imported the android compatibility library
146146
throw new RuntimeException("Looks like you are using the latest version of EventBus on Android " +
147147
"without importing the EventBus for Android compatibility library. Please import it on app/build.gradle!");

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

+3-3
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.AndroidSDKProxy;
18+
import org.greenrobot.eventbus.android.AndroidComponents;
1919
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;
2020
import java.util.ArrayList;
2121
import java.util.List;
@@ -162,8 +162,8 @@ Logger getLogger() {
162162
MainThreadSupport getMainThreadSupport() {
163163
if (mainThreadSupport != null) {
164164
return mainThreadSupport;
165-
} else if (AndroidSDKProxy.isAvailable()) {
166-
return AndroidSDKProxy.get().defaultMainThreadSupport;
165+
} else if (AndroidComponents.areAvailable()) {
166+
return AndroidComponents.get().defaultMainThreadSupport;
167167
} else {
168168
return null;
169169
}

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

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

18-
import org.greenrobot.eventbus.android.AndroidLogger;
19-
import org.greenrobot.eventbus.android.AndroidSDKProxy;
18+
import org.greenrobot.eventbus.android.AndroidComponents;
2019
import java.util.logging.Level;
2120

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

6463
class Default {
6564
public static Logger get() {
66-
if (AndroidSDKProxy.isAvailable()) {
67-
return new AndroidLogger(AndroidSDKProxy.get(), "EventBus");
65+
if (AndroidComponents.areAvailable()) {
66+
return AndroidComponents.get().logger;
6867
}
6968

7069
return new SystemOutLogger();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.greenrobot.eventbus.android;
2+
3+
import org.greenrobot.eventbus.Logger;
4+
import org.greenrobot.eventbus.MainThreadSupport;
5+
6+
public abstract class AndroidComponents {
7+
8+
private static final AndroidComponents implementation;
9+
10+
static {
11+
implementation = AndroidDependenciesDetector.isAndroidSDKAvailable()
12+
? AndroidDependenciesDetector.instantiateAndroidComponents()
13+
: null;
14+
}
15+
16+
public static boolean areAvailable() {
17+
return implementation != null;
18+
}
19+
20+
public static AndroidComponents get() {
21+
return implementation;
22+
}
23+
24+
public final Logger logger;
25+
public final MainThreadSupport defaultMainThreadSupport;
26+
27+
public AndroidComponents(Logger logger, MainThreadSupport defaultMainThreadSupport) {
28+
this.logger = logger;
29+
this.defaultMainThreadSupport = defaultMainThreadSupport;
30+
}
31+
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@ public static boolean isAndroidSDKAvailable() {
2222
return false;
2323
}
2424

25-
public static boolean isAndroidSDKProxyImplAvailable() {
25+
private static final String ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME = "org.greenrobot.eventbus.android.AndroidComponentsImpl";
26+
27+
public static boolean areAndroidComponentsAvailable() {
2628

2729
try {
28-
Class.forName("org.greenrobot.eventbus.android.AndroidSDKProxyImpl");
30+
Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);
2931
return true;
3032
}
3133
catch (ClassNotFoundException ex) {
3234
return false;
3335
}
3436
}
3537

36-
public static AndroidSDKProxy instantiateAndroidSDKProxy() {
38+
public static AndroidComponents instantiateAndroidComponents() {
3739

3840
try {
39-
Class<?> impl = Class.forName("org.greenrobot.eventbus.android.AndroidSDKProxyImpl");
40-
return (AndroidSDKProxy) impl.getConstructor().newInstance();
41+
Class<?> impl = Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);
42+
return (AndroidComponents) impl.getConstructor().newInstance();
4143
}
4244
catch (Throwable ex) {
4345
return null;

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

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.greenrobot.eventbus;
2+
3+
import org.greenrobot.eventbus.android.AndroidComponents;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class AndroidComponentsAvailabilityTest {
10+
11+
@Test
12+
public void shouldBeAvailable() {
13+
assertTrue(AndroidComponents.areAvailable());
14+
assertNotNull(AndroidComponents.get());
15+
}
16+
}

EventBusTest/src/org/greenrobot/eventbus/AndroidSDKProxyTest.java

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.greenrobot.eventbus;
2+
3+
import org.greenrobot.eventbus.android.AndroidComponents;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertNull;
8+
9+
public class AndroidComponentsAvailabilityOnJavaTest
10+
{
11+
12+
@Test
13+
public void shouldNotBeAvailable() {
14+
assertFalse(AndroidComponents.areAvailable());
15+
assertNull(AndroidComponents.get());
16+
}
17+
}

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

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.greenrobot.eventbus.android;
2+
3+
public class AndroidComponentsImpl extends AndroidComponents {
4+
5+
public AndroidComponentsImpl() {
6+
super(new AndroidLogger("EventBus"), new DefaultAndroidMainThreadSupport());
7+
}
8+
}

eventbus-android/src/main/java/org/greenrobot/eventbus/android/AndroidLogProxyImpl.java

-36
This file was deleted.

EventBus/src/org/greenrobot/eventbus/android/AndroidLogger.java renamed to eventbus-android/src/main/java/org/greenrobot/eventbus/android/AndroidLogger.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,46 @@
1515
*/
1616
package org.greenrobot.eventbus.android;
1717

18+
import android.util.Log;
1819
import org.greenrobot.eventbus.Logger;
1920
import org.greenrobot.eventbus.util.ExceptionStackTraceUtils;
2021
import java.util.logging.Level;
2122

2223
public class AndroidLogger implements Logger {
2324

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

27-
public AndroidLogger(AndroidSDKProxy androidSDK, String tag) {
28-
this.androidSDK = androidSDK;
27+
public AndroidLogger(String tag) {
2928
this.tag = tag;
3029
}
3130

3231
public void log(Level level, String msg) {
3332
if (level != Level.OFF) {
34-
androidSDK.log.println(mapLevel(level), tag, msg);
33+
Log.println(mapLevel(level), tag, msg);
3534
}
3635
}
3736

3837
public void log(Level level, String msg, Throwable th) {
3938
if (level != Level.OFF) {
4039
// That's how Log does it internally
41-
androidSDK.log.println(mapLevel(level), tag, msg + "\n" + ExceptionStackTraceUtils.getStackTraceAsString(th));
40+
Log.println(mapLevel(level), tag, msg + "\n" + ExceptionStackTraceUtils.getStackTraceAsString(th));
4241
}
4342
}
4443

4544
private int mapLevel(Level level) {
4645
int value = level.intValue();
4746
if (value < 800) { // below INFO
4847
if (value < 500) { // below FINE
49-
return androidSDK.log.getVerboseLevelId();
48+
return Log.VERBOSE;
5049
} else {
51-
return androidSDK.log.getDebugLevelId();
50+
return Log.DEBUG;
5251
}
5352
} else if (value < 900) { // below WARNING
54-
return androidSDK.log.getInfoLevelId();
53+
return Log.INFO;
5554
} else if (value < 1000) { // below ERROR
56-
return androidSDK.log.getWarnLevelId();
55+
return Log.WARN;
5756
} else {
58-
return androidSDK.log.getErrorLevelId();
57+
return Log.ERROR;
5958
}
6059
}
6160
}

eventbus-android/src/main/java/org/greenrobot/eventbus/android/AndroidSDKProxyImpl.java

-8
This file was deleted.

0 commit comments

Comments
 (0)