From 07d5a226f9d7cf1b4db3a0f085c39756c64a41d5 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Mon, 13 Nov 2017 15:17:01 +0100 Subject: [PATCH 1/6] eventbus 3.2.0-SNAPSHOT --- EventBus/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EventBus/build.gradle b/EventBus/build.gradle index 16f57190..a268a19f 100644 --- a/EventBus/build.gradle +++ b/EventBus/build.gradle @@ -5,7 +5,7 @@ apply plugin: 'idea' archivesBaseName = 'eventbus' group = 'org.greenrobot' -version = '3.1.1' +version = '3.2.0-SNAPSHOT' sourceCompatibility = 1.7 def isSnapshot = version.endsWith('-SNAPSHOT') From 6348f40f9901f813451791f9d11181be6eb49a32 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Mon, 13 Nov 2017 15:16:03 +0100 Subject: [PATCH 2/6] Add EventBusLifecycleObserver used by EventBus.observe(lifecycleOwner). - Use gradle-apt-plugin to run lifecycle annotation processor. - Add provided dependency on android.arch.lifecycle:runtime:1.0.3, this might cause issues with ProGuard due to EventBus#observe(lifecycleOwner). --- EventBus/build.gradle | 14 +++++++++ .../src/org/greenrobot/eventbus/EventBus.java | 12 ++++++++ .../eventbus/EventBusLifecycleObserver.java | 29 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java diff --git a/EventBus/build.gradle b/EventBus/build.gradle index a268a19f..cc00933f 100644 --- a/EventBus/build.gradle +++ b/EventBus/build.gradle @@ -1,7 +1,18 @@ +buildscript { + repositories { + maven { url "https://plugins.gradle.org/m2/" } + } + + dependencies { + classpath "net.ltgt.gradle:gradle-apt-plugin:0.13" + } +} + apply plugin: 'java' apply plugin: 'maven' apply plugin: 'signing' apply plugin: 'idea' +apply plugin: 'net.ltgt.apt-idea' archivesBaseName = 'eventbus' group = 'org.greenrobot' @@ -18,6 +29,7 @@ if(isSnapshot) { repositories { mavenCentral() + google() } // Still unsupported, see http://issues.gradle.org/browse/GRADLE-784 @@ -32,6 +44,8 @@ dependencies { provided 'com.google.android:android-test:4.1.1.4' provided 'com.google.android:annotations:4.1.1.4' provided 'com.google.android:support-v4:r7' + provided "android.arch.lifecycle:runtime:1.0.3" + apt "android.arch.lifecycle:compiler:1.0.0" // deployerJars 'org.apache.maven.wagon:wagon-webdav-jackrabbit:2.4' deployerJars 'org.apache.maven.wagon:wagon-webdav:1.0-beta-2' } diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index 247cbb27..f4f639a5 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -15,6 +15,8 @@ */ package org.greenrobot.eventbus; +import android.arch.lifecycle.LifecycleOwner; + import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; @@ -127,6 +129,16 @@ public EventBus() { executorService = builder.executorService; } + /** + * Adds an observer to the given {@link LifecycleOwner}s lifecycle to register EventBus when + * started and unregister when stopped. + *

+ * Requires dependency on Android Architecture Components Lifecycle package. + */ + public void observe(LifecycleOwner lifecycleOwner) { + lifecycleOwner.getLifecycle().addObserver(new EventBusLifecycleObserver(lifecycleOwner, this)); + } + /** * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they * are no longer interested in receiving events. diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java b/EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java new file mode 100644 index 00000000..e8ce3274 --- /dev/null +++ b/EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java @@ -0,0 +1,29 @@ +package org.greenrobot.eventbus; + +import android.arch.lifecycle.Lifecycle.Event; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.OnLifecycleEvent; + + +public class EventBusLifecycleObserver implements LifecycleObserver { + + private final LifecycleOwner lifecycleOwner; + private final EventBus eventBus; + + public EventBusLifecycleObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) { + this.lifecycleOwner = lifecycleOwner; + this.eventBus = eventBus; + } + + @OnLifecycleEvent(Event.ON_START) + void start() { + eventBus.register(lifecycleOwner); + } + + @OnLifecycleEvent(Event.ON_STOP) + void stop() { + eventBus.unregister(lifecycleOwner); + } + +} From 2fe46814c1156bd3bb936c2acfe415002f8c941c Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 14 Nov 2017 09:48:55 +0100 Subject: [PATCH 3/6] Add lifecycle observer for onCreate/onDestroy events, EventBus method. - Link to register/unregister docs for further info. --- .../src/org/greenrobot/eventbus/EventBus.java | 24 ++++++++++++--- .../EventBusCreateDestroyObserver.java | 29 +++++++++++++++++++ ...er.java => EventBusStartStopObserver.java} | 4 +-- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java rename EventBus/src/org/greenrobot/eventbus/{EventBusLifecycleObserver.java => EventBusStartStopObserver.java} (83%) diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index f4f639a5..0d0872d8 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -130,13 +130,29 @@ public EventBus() { } /** - * Adds an observer to the given {@link LifecycleOwner}s lifecycle to register EventBus when - * started and unregister when stopped. + * Adds an observer to the given {@link LifecycleOwner}s lifecycle to automatically register + * EventBus during an onCreated event and unregister during an onDestroy event. *

* Requires dependency on Android Architecture Components Lifecycle package. + * + * @see #register(Object) + * @see #unregister(Object) + */ + public void observeCreateDestroy(LifecycleOwner lifecycleOwner) { + lifecycleOwner.getLifecycle().addObserver(new EventBusCreateDestroyObserver(lifecycleOwner, this)); + } + + /** + * Adds an observer to the given {@link LifecycleOwner}s lifecycle to automatically register + * EventBus during an onStart event and unregister during an onStop event. + *

+ * Requires dependency on Android Architecture Components Lifecycle package. + * + * @see #register(Object) + * @see #unregister(Object) */ - public void observe(LifecycleOwner lifecycleOwner) { - lifecycleOwner.getLifecycle().addObserver(new EventBusLifecycleObserver(lifecycleOwner, this)); + public void observeStartStop(LifecycleOwner lifecycleOwner) { + lifecycleOwner.getLifecycle().addObserver(new EventBusStartStopObserver(lifecycleOwner, this)); } /** diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java b/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java new file mode 100644 index 00000000..6921bf2d --- /dev/null +++ b/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java @@ -0,0 +1,29 @@ +package org.greenrobot.eventbus; + +import android.arch.lifecycle.Lifecycle.Event; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.OnLifecycleEvent; + + +public class EventBusCreateDestroyObserver implements LifecycleObserver { + + private final LifecycleOwner lifecycleOwner; + private final EventBus eventBus; + + public EventBusCreateDestroyObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) { + this.lifecycleOwner = lifecycleOwner; + this.eventBus = eventBus; + } + + @OnLifecycleEvent(Event.ON_CREATE) + void start() { + eventBus.register(lifecycleOwner); + } + + @OnLifecycleEvent(Event.ON_DESTROY) + void stop() { + eventBus.unregister(lifecycleOwner); + } + +} diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java b/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java similarity index 83% rename from EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java rename to EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java index e8ce3274..228a3b9d 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusLifecycleObserver.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java @@ -6,12 +6,12 @@ import android.arch.lifecycle.OnLifecycleEvent; -public class EventBusLifecycleObserver implements LifecycleObserver { +public class EventBusStartStopObserver implements LifecycleObserver { private final LifecycleOwner lifecycleOwner; private final EventBus eventBus; - public EventBusLifecycleObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) { + public EventBusStartStopObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) { this.lifecycleOwner = lifecycleOwner; this.eventBus = eventBus; } From b09eb1adafd7993958f3f3fb3eeb58b1974fa5d2 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 14 Nov 2017 10:26:23 +0100 Subject: [PATCH 4/6] Unify method names of lifecycle observers. --- .../greenrobot/eventbus/EventBusCreateDestroyObserver.java | 4 ++-- .../org/greenrobot/eventbus/EventBusStartStopObserver.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java b/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java index 6921bf2d..69dc1f04 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java @@ -17,12 +17,12 @@ public EventBusCreateDestroyObserver(LifecycleOwner lifecycleOwner, EventBus eve } @OnLifecycleEvent(Event.ON_CREATE) - void start() { + void register() { eventBus.register(lifecycleOwner); } @OnLifecycleEvent(Event.ON_DESTROY) - void stop() { + void unregister() { eventBus.unregister(lifecycleOwner); } diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java b/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java index 228a3b9d..52f21dfa 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java @@ -17,12 +17,12 @@ public EventBusStartStopObserver(LifecycleOwner lifecycleOwner, EventBus eventBu } @OnLifecycleEvent(Event.ON_START) - void start() { + void register() { eventBus.register(lifecycleOwner); } @OnLifecycleEvent(Event.ON_STOP) - void stop() { + void unregister() { eventBus.unregister(lifecycleOwner); } From e341dd1e485973ec01dede685b17e6c3116efcb1 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 14 Nov 2017 11:32:34 +0100 Subject: [PATCH 5/6] Instant. observers via reflection as interface may not be in classpath. --- .../src/org/greenrobot/eventbus/EventBus.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index 0d0872d8..a5343581 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -15,6 +15,7 @@ */ package org.greenrobot.eventbus; +import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.LifecycleOwner; import java.lang.reflect.InvocationTargetException; @@ -139,7 +140,7 @@ public EventBus() { * @see #unregister(Object) */ public void observeCreateDestroy(LifecycleOwner lifecycleOwner) { - lifecycleOwner.getLifecycle().addObserver(new EventBusCreateDestroyObserver(lifecycleOwner, this)); + createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.EventBusCreateDestroyObserver"); } /** @@ -152,7 +153,20 @@ public void observeCreateDestroy(LifecycleOwner lifecycleOwner) { * @see #unregister(Object) */ public void observeStartStop(LifecycleOwner lifecycleOwner) { - lifecycleOwner.getLifecycle().addObserver(new EventBusStartStopObserver(lifecycleOwner, this)); + createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.EventBusStartStopObserver"); + } + + private void createAndAddObserver(LifecycleOwner lifecycleOwner, String className) { + try { + Class observer = Class.forName(className); + LifecycleObserver instance = (LifecycleObserver) observer + .getDeclaredConstructor(LifecycleOwner.class, EventBus.class) + .newInstance(lifecycleOwner, this); + lifecycleOwner.getLifecycle().addObserver(instance); + } catch (Exception e) { + throw new EventBusException("Failed to create LifecycleObserver. " + + "Do you have a dependency on Android Architecture Components Lifecycles?", e); + } } /** From 9a601a1d2b34ae81d4cf15e7770b1f5d06adf023 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 14 Nov 2017 12:09:07 +0100 Subject: [PATCH 6/6] Move Lifecycle observers to android.lifecycle package. --- EventBus/src/org/greenrobot/eventbus/EventBus.java | 4 ++-- .../lifecycle}/EventBusCreateDestroyObserver.java | 4 +++- .../{ => android/lifecycle}/EventBusStartStopObserver.java | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) rename EventBus/src/org/greenrobot/eventbus/{ => android/lifecycle}/EventBusCreateDestroyObserver.java (89%) rename EventBus/src/org/greenrobot/eventbus/{ => android/lifecycle}/EventBusStartStopObserver.java (88%) diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index a5343581..312f1ffd 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -140,7 +140,7 @@ public EventBus() { * @see #unregister(Object) */ public void observeCreateDestroy(LifecycleOwner lifecycleOwner) { - createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.EventBusCreateDestroyObserver"); + createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.android.lifecycle.EventBusCreateDestroyObserver"); } /** @@ -153,7 +153,7 @@ public void observeCreateDestroy(LifecycleOwner lifecycleOwner) { * @see #unregister(Object) */ public void observeStartStop(LifecycleOwner lifecycleOwner) { - createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.EventBusStartStopObserver"); + createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.android.lifecycle.EventBusStartStopObserver"); } private void createAndAddObserver(LifecycleOwner lifecycleOwner, String className) { diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java b/EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusCreateDestroyObserver.java similarity index 89% rename from EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java rename to EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusCreateDestroyObserver.java index 69dc1f04..8fc0c0d3 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusCreateDestroyObserver.java +++ b/EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusCreateDestroyObserver.java @@ -1,10 +1,12 @@ -package org.greenrobot.eventbus; +package org.greenrobot.eventbus.android.lifecycle; import android.arch.lifecycle.Lifecycle.Event; import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.OnLifecycleEvent; +import org.greenrobot.eventbus.EventBus; + public class EventBusCreateDestroyObserver implements LifecycleObserver { diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java b/EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusStartStopObserver.java similarity index 88% rename from EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java rename to EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusStartStopObserver.java index 52f21dfa..2cd89d01 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusStartStopObserver.java +++ b/EventBus/src/org/greenrobot/eventbus/android/lifecycle/EventBusStartStopObserver.java @@ -1,10 +1,12 @@ -package org.greenrobot.eventbus; +package org.greenrobot.eventbus.android.lifecycle; import android.arch.lifecycle.Lifecycle.Event; import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.OnLifecycleEvent; +import org.greenrobot.eventbus.EventBus; + public class EventBusStartStopObserver implements LifecycleObserver {