Android Open Source - Android-MyStarterApp Scoped Bus






From Project

Back to project page Android-MyStarterApp.

License

The source code is released under:

Apache License

If you think the Android project Android-MyStarterApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package co.kaush.mystarterapp.app.network;
/*  w w w.  jav  a  2  s.c  o  m*/
import android.os.Handler;
import android.os.Looper;

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;

import java.util.HashSet;
import java.util.Set;

public class ScopedBus {

    private static final Bus _bus = new Bus(ThreadEnforcer.ANY);
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final Set<Object> objects = new HashSet<Object>();
    private boolean active;

    public void register(Object obj) {
        objects.add(obj);
        if (active) {
            _bus.register(obj);
        }
    }

    public void unregister(Object obj) {
        objects.remove(obj);
        if (active) {
            _bus.unregister(obj);
        }
    }

    public void paused() {
        active = false;
        for (Object obj : objects) {
            _bus.unregister(obj);
        }
    }

    public void resumed() {
        active = true;
        for (Object obj : objects) {
            _bus.register(obj);
        }
    }

    public void post(Object event) {
        _bus.post(event);
    }

    public void postOnUiThread(final Object event) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            post(event);
        } else {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    post(event);
                }
            });
        }
    }
}




Java Source Code List

co.kaush.mystarterapp.app.BaseApplication.java
co.kaush.mystarterapp.app.CrashReportingTree.java
co.kaush.mystarterapp.app.SampleEspressoTest.java
co.kaush.mystarterapp.app.data.modules.BaseModule.java
co.kaush.mystarterapp.app.data.modules.Modules.java
co.kaush.mystarterapp.app.data.modules.Modules.java
co.kaush.mystarterapp.app.network.BaseHandler.java
co.kaush.mystarterapp.app.network.ScopedBus.java
co.kaush.mystarterapp.app.pojos.SamplePojo.java
co.kaush.mystarterapp.app.services.BaseIntentService.java
co.kaush.mystarterapp.app.services.QueueClearableIntentService.java
co.kaush.mystarterapp.app.ui.activities.BaseActivity.java
co.kaush.mystarterapp.app.ui.activities.SampleActivity.java
co.kaush.mystarterapp.app.ui.adapters.MyBaseAdapter.java
co.kaush.mystarterapp.app.ui.adapters.ReuseCachedViewAdapter.java
co.kaush.mystarterapp.app.ui.fragments.BaseFragment.java
co.kaush.mystarterapp.app.ui.viewholder.ViewHolder.java
co.kaush.mystarterapp.app.utils.IntentUtils.java
co.kaush.mystarterapp.app.utils.LogUtils.java
com.micromobs.pkk.CrashReportingTree.java