diff --git a/README-zh-CN.md b/README-zh-CN.md index ed6b8ea..d24e4c4 100644 --- a/README-zh-CN.md +++ b/README-zh-CN.md @@ -51,7 +51,7 @@ public void showText(String text) { ``` dependencies { - compile 'xiaofei.library:hermes-eventbus:0.1.1' + compile 'xiaofei.library:hermes-eventbus:0.2.0' } ``` @@ -61,7 +61,7 @@ dependencies { xiaofei.library hermes-eventbus - 0.1.1 + 0.2.0 pom ``` diff --git a/README.md b/README.md index de6f26f..7f0242d 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Add the following into your gradle file: ``` dependencies { - compile 'xiaofei.library:hermes-eventbus:0.1.6' + compile 'xiaofei.library:hermes-eventbus:0.2.0' } ``` @@ -74,7 +74,7 @@ For maven, please use the following: xiaofei.library hermes-eventbus - 0.1.6 + 0.2.0 pom ``` diff --git a/hermes-eventbus/build.gradle b/hermes-eventbus/build.gradle index b4320bc..d25474f 100644 --- a/hermes-eventbus/build.gradle +++ b/hermes-eventbus/build.gradle @@ -13,7 +13,7 @@ ext { siteUrl = 'https://github.com/Xiaofei-it/HermesEventBus-OriginalRepo' gitUrl = 'https://github.com/Xiaofei-it/HermesEventBus-OriginalRepo.git' - libraryVersion = '0.1.6' + libraryVersion = '0.2.0' developerId = 'Xiaofei-it' developerName = 'Eric Zhao' @@ -31,8 +31,8 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 23 - versionCode 2 - versionName "0.1.6" + versionCode 3 + versionName "0.2.0" } buildTypes { release { @@ -48,7 +48,7 @@ dependencies { compile 'com.android.support:appcompat-v7:23.3.0' compile 'org.greenrobot:eventbus:3.0.0' compile 'xiaofei.library:hermes:0.6.1' - compile 'xiaofei.library:concurrent-utils:0.1.3' + compile 'xiaofei.library:concurrent-utils:0.1.4' } apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle' diff --git a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/HermesEventBus.java b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/HermesEventBus.java index ebab131..2684e34 100644 --- a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/HermesEventBus.java +++ b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/HermesEventBus.java @@ -25,7 +25,7 @@ import org.greenrobot.eventbus.EventBus; -import xiaofei.library.concurrentutils.ObjectCanary; +import xiaofei.library.concurrentutils.ObjectCanary2; import xiaofei.library.concurrentutils.util.Action; import xiaofei.library.concurrentutils.util.Function; import xiaofei.library.hermes.Hermes; @@ -40,10 +40,6 @@ public class HermesEventBus { private static final String HERMES_SERVICE_DISCONNECTED = "Hermes service disconnected!"; - private static final String UNKNOWN_ERROR = "An unknown error occurred. Please report this to the author."; - - private static final int STATE_UNDEFINED = -1; - private static final int STATE_DISCONNECTED = 0; private static final int STATE_CONNECTING = 1; @@ -58,39 +54,33 @@ public class HermesEventBus { private volatile boolean mMainProcess; - private volatile ObjectCanary mRemoteApis; + private volatile ObjectCanary2 mRemoteApis; private volatile MainService mMainApis; - private volatile int mState = STATE_UNDEFINED; + private volatile int mState = STATE_DISCONNECTED; /** - * TODO * * 1. Consider more about the interleaving, especially when the service is being connected or disconnected. * - * 2. I should solve the following problems in the future: + * 2. Pay attention to the following cases: * * (1) Before the connection succeeds, e1, e2 and e3 are put into the queue. * Then when the connection succeeds, they are posted one by one. * However, after e1 is posted, we post another event e4. - * How can I guarantee that e4 is posted after e3? + * I should guarantee that e4 is posted after e3. * * (2) Before the connection succeeds, some sticky events (e1, e2 and e3) * are put into the queue. * Then when the connection succeeds, they are posted one by one. * However, after e1 is posted, we get a sticky event. - * How can I guarantee that we get e3 rather than e1? - * - * I have made some modifications in the concurrent library but has not imported it - * into HermesEventBus. - * Work remains to be done in the future. - * + * I should guarantee that we get e3 rather than e1. */ private HermesEventBus() { mEventBus = EventBus.getDefault(); - mRemoteApis = new ObjectCanary(); + mRemoteApis = new ObjectCanary2(); } public static HermesEventBus getDefault() { @@ -122,7 +112,7 @@ private static String getCurrentProcessName(Context context) { } public void init(Context context) { - mContext = context; + mContext = context.getApplicationContext(); mMainProcess = isMainProcess(context.getApplicationContext()); if (mMainProcess) { Hermes.init(context); @@ -137,7 +127,7 @@ public void init(Context context) { } public void connectApp(Context context, String packageName) { - mContext = context; + mContext = context.getApplicationContext(); mMainProcess = false; mState = STATE_CONNECTING; Hermes.setHermesListener(new HermesListener()); @@ -169,17 +159,13 @@ private void actionInternal(final Action action) { } else { if (mState == STATE_DISCONNECTED) { Log.w(TAG, HERMES_SERVICE_DISCONNECTED); - } else if (mState == STATE_CONNECTING) { - mRemoteApis.actionNonNullNonBlocking(new Action() { + } else { + mRemoteApis.action(new Action() { @Override public void call(IMainService o) { action.call(o); } }); - } else if (mState == STATE_CONNECTED) { - action.call(mRemoteApis.get()); - } else { - throw new IllegalStateException(UNKNOWN_ERROR); } } } @@ -191,17 +177,13 @@ private T calculateInternal(final Function functi if (mState == STATE_DISCONNECTED) { Log.w(TAG, HERMES_SERVICE_DISCONNECTED); return null; - } else if (mState == STATE_CONNECTING) { - return mRemoteApis.calculateNonNull(new Function() { + } else { + return mRemoteApis.calculate(new Function() { @Override public T call(IMainService o) { return function.call(o); } }); - } else if (mState == STATE_CONNECTED) { - return function.call(mRemoteApis.get()); - } else { - throw new IllegalStateException(UNKNOWN_ERROR); } } } @@ -311,14 +293,16 @@ public void call(IMainService o) { @Override public void onHermesDisconnected(Class service) { // Log.v(TAG, "Hermes disconnected in Process " + Process.myPid()); + mState = STATE_DISCONNECTED; mRemoteApis.action(new Action() { @Override public void call(IMainService o) { o.unregister(Process.myPid()); } }); - mRemoteApis.set(null); - mState = STATE_DISCONNECTED; + // I deleted the statement which assigns null to mRemoteApis. + // Then, if the service is disconnected, the pending events will still be posted + // but this process will not receive them any more. } } diff --git a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/ISubService.java b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/ISubService.java index f73c302..3420c86 100644 --- a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/ISubService.java +++ b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/ISubService.java @@ -32,10 +32,4 @@ public interface ISubService { @MethodId("cancelEventDelivery") void cancelEventDelivery(Object event); - @MethodId("removeStickyEvent") - boolean removeStickyEvent(Object event); - - @MethodId("removeAllStickyEvents") - void removeAllStickyEvents(); - } diff --git a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/SubService.java b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/SubService.java index a149105..311475b 100644 --- a/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/SubService.java +++ b/hermes-eventbus/src/main/java/xiaofei/library/hermeseventbus/SubService.java @@ -52,18 +52,6 @@ public void post(Object event) { mEventBus.post(event); } - @MethodId("removeStickyEvent") - @Override - public boolean removeStickyEvent(Object event) { - return mEventBus.removeStickyEvent(event); - } - - @MethodId("removeAllStickyEvents") - @Override - public void removeAllStickyEvents() { - mEventBus.removeAllStickyEvents(); - } - @MethodId("cancelEventDelivery") @Override public void cancelEventDelivery(Object event) {