From 461b4fc0134210a612c73d35a11418c55bca9332 Mon Sep 17 00:00:00 2001 From: xynngh Date: Sat, 4 Jul 2020 12:19:39 +0400 Subject: [PATCH] Lazily init EventBus --- .../yetanothercallblocker/App.java | 10 ----- .../yetanothercallblocker/DebugActivity.java | 5 +-- .../yetanothercallblocker/EventHandler.java | 38 ---------------- .../yetanothercallblocker/EventUtils.java | 44 ++++++++++++++++++- .../yetanothercallblocker/MainActivity.java | 7 ++- 5 files changed, 47 insertions(+), 57 deletions(-) delete mode 100644 app/src/main/java/dummydomain/yetanothercallblocker/EventHandler.java diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/App.java b/app/src/main/java/dummydomain/yetanothercallblocker/App.java index dd689b1..2b82b35 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/App.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/App.java @@ -5,8 +5,6 @@ import android.app.Application; import android.content.Context; import android.os.Build; -import org.greenrobot.eventbus.EventBus; - import dummydomain.yetanothercallblocker.data.Config; public class App extends Application { @@ -35,14 +33,6 @@ public class App extends Application { settings = new Settings(getDeviceProtectedStorageContext()); settings.init(); - EventBus.builder() - .throwSubscriberException(BuildConfig.DEBUG) - .sendNoSubscriberEvent(false) - .addIndex(new EventBusIndex()) - .installDefaultEventBus(); - - EventHandler.create(this); - Config.init(getDeviceProtectedStorageContext(), settings); } diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/DebugActivity.java b/app/src/main/java/dummydomain/yetanothercallblocker/DebugActivity.java index 2606ed7..bd081c0 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/DebugActivity.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/DebugActivity.java @@ -9,7 +9,6 @@ import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.core.util.Pair; -import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -41,12 +40,12 @@ public class DebugActivity extends AppCompatActivity { protected void onStart() { super.onStart(); - EventBus.getDefault().register(this); + EventUtils.register(this); } @Override protected void onStop() { - EventBus.getDefault().unregister(this); + EventUtils.unregister(this); super.onStop(); } diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/EventHandler.java b/app/src/main/java/dummydomain/yetanothercallblocker/EventHandler.java deleted file mode 100644 index 21d58f3..0000000 --- a/app/src/main/java/dummydomain/yetanothercallblocker/EventHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -package dummydomain.yetanothercallblocker; - -import android.annotation.SuppressLint; -import android.content.Context; - -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.SubscriberExceptionEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EventHandler { - - private static final Logger LOG = LoggerFactory.getLogger(EventHandler.class); - - @SuppressLint("StaticFieldLeak") // same lifecycle - private static EventHandler instance; - - private Context context; - - public static EventHandler create(Context context) { - EventHandler instance = new EventHandler(context); - - EventBus.getDefault().register(instance); - - return EventHandler.instance = instance; - } - - private EventHandler(Context context) { - this.context = context; - } - - @Subscribe - public void onSubscriberExceptionEvent(SubscriberExceptionEvent event) { - LOG.warn("onSubscriberExceptionEvent()", event.throwable); - } - -} diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/EventUtils.java b/app/src/main/java/dummydomain/yetanothercallblocker/EventUtils.java index 9d93f7f..9a57042 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/EventUtils.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/EventUtils.java @@ -1,9 +1,33 @@ package dummydomain.yetanothercallblocker; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.SubscriberExceptionEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class EventUtils { + private static final Logger LOG = LoggerFactory.getLogger(EventUtils.class); + + private static EventUtils instance; + + private static class Holder { + static final EventBus bus = createBus(); + } + + public static EventBus bus() { + return Holder.bus; + } + + public static void register(Object subscriber) { + bus().register(subscriber); + } + + public static void unregister(Object subscriber) { + bus().unregister(subscriber); + } + public static void postEvent(Object event) { bus().post(event); } @@ -16,8 +40,24 @@ public class EventUtils { bus().removeStickyEvent(event); } - private static EventBus bus() { - return EventBus.getDefault(); + private static EventBus createBus() { + EventBus bus = EventBus.builder() + .throwSubscriberException(BuildConfig.DEBUG) + .sendNoSubscriberEvent(false) + .addIndex(new EventBusIndex()) + .installDefaultEventBus(); + + instance = new EventUtils(); + bus.register(instance); + + return bus; + } + + private EventUtils() {} + + @Subscribe + public void onSubscriberExceptionEvent(SubscriberExceptionEvent event) { + LOG.warn("onSubscriberExceptionEvent()", event.throwable); } } diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java index 4017dbc..cbd86c5 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java @@ -15,7 +15,6 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; -import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -92,7 +91,7 @@ public class MainActivity extends AppCompatActivity { protected void onStart() { super.onStart(); - EventBus.getDefault().register(this); + EventUtils.register(this); startCheckMainDbTask(); @@ -103,7 +102,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onStop() { - EventBus.getDefault().unregister(this); + EventUtils.unregister(this); super.onStop(); } @@ -144,7 +143,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onPostExecute(Boolean result) { - if (!result && EventBus.getDefault().getStickyEvent(MainDbDownloadingEvent.class) == null) { + if (!result && EventUtils.bus().getStickyEvent(MainDbDownloadingEvent.class) == null) { showNoMainDbDialog(); } }