Lazily init EventBus

This commit is contained in:
xynngh 2020-07-04 12:19:39 +04:00
parent 00acf43ffa
commit 461b4fc013
5 changed files with 47 additions and 57 deletions

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}