Android Open Source - mobilib Mbl Event Center






From Project

Back to project page mobilib.

License

The source code is released under:

MIT License

If you think the Android project mobilib 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 com.datdo.mobilib.event;
/*ww w .  j a  v a2  s.  c om*/
import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.datdo.mobilib.event.MblWeakArrayList.MblWeakArrayListCallback;
import com.datdo.mobilib.util.MblUtils;


/**
 * <pre>
 * Class to post in-app events between objects.
 * This class keeps a {@link WeakReference} to listeners to prevent memory leak.
 * If you want to create an anonymous listener, please use {@link MblStrongEventListener}, otherwise the listener will be destroyed by GC.
 * </pre>
 */
public class MblEventCenter {
    private static final Map<String, MblWeakArrayList<MblEventListener>> sEventListenerMap = new ConcurrentHashMap<String, MblWeakArrayList<MblEventListener>>();

    /**
     * <pre>
     * Register a listener for multiple events.
     * </pre>
     * @param names event names
     */
    public static void addListener(MblEventListener listener, String[] names) {
        if (!MblUtils.isEmpty(names)) {
            for (String n : names) {
                addListener(listener, n);
            }
        }
    }

    /**
     * <pre>
     * Register a listener for one event.
     * </pre>
     * @param name
     */
    public static void addListener(MblEventListener listener, String name) {
        MblWeakArrayList<MblEventListener> listeners = null;

        if(sEventListenerMap.containsKey(name)) {
            listeners = sEventListenerMap.get(name);
        } else {
            listeners = new MblWeakArrayList<MblEventListener>();
            sEventListenerMap.put(name, listeners);
        }
        if(listeners.contains(listener)) return;

        listeners.add(listener);
    }

    /**
     * <pre>
     * Remove listener from one event
     * </pre>
     * @param name event name
     */
    public static void removeListenerFromEvent(MblEventListener listener, String name) {
        if(!sEventListenerMap.containsKey(name)) return;

        MblWeakArrayList<MblEventListener> listeners = null;

        listeners = sEventListenerMap.get(name);
        if(!listeners.contains(listener)) return;

        listeners.remove(listener);

        if(listeners.isEmpty()) {
            sEventListenerMap.remove(name);
        }
    }

    /**
     * <pre>
     * Remove listener from all events.
     * </pre>
     */
    public static void removeListenerFromAllEvents(MblEventListener listener) {
        Set<String> keys = sEventListenerMap.keySet();
        for (String aKey : keys) {
            removeListenerFromEvent(listener, aKey);
        }
    }

    /**
     * <pre>
     * Post event to all listeners.
     * The callback method {@link MblEventListener#onEvent(Object, String, Object...)} is invoked in main thread.
     * </pre>
     * @param sender object that posts the event
     * @param name event name
     * @param args data attached to event
     */
    public static void postEvent(final Object sender, final String name, final Object... args) {
        if(!sEventListenerMap.containsKey(name)) return;

        MblWeakArrayList<MblEventListener> listeners;
        listeners = sEventListenerMap.get(name);
        listeners.iterateWithCallback(new MblWeakArrayListCallback<MblEventListener>() {
            @Override
            public void onInterate(final MblEventListener listener) {
                MblUtils.executeOnMainThread(new Runnable() {
                    @Override
                    public void run() {
                        listener.onEvent(sender, name, args);
                    }
                });
            }
        });
    }

    /**
     * <pre>
     * Get argument object from argument array safely (no {@link NullPointerException} or {@link IndexOutOfBoundsException})
     * This method is designated for {@link MblEventListener#onEvent(Object, String, Object...)}
     * </pre>
     * @param index specific index of argument
     * @param args argument array
     * @return argument object if index is valid, otherwise return null
     */
    public static Object getArgAt(int index, Object...args) {
        return args != null && args.length > index ? args[index] : null;
    }
}




Java Source Code List

com.datdo.mobilib.api.DBBase.java
com.datdo.mobilib.api.DBHelper.java
com.datdo.mobilib.api.MblApi.java
com.datdo.mobilib.api.MblCache.java
com.datdo.mobilib.api.MblException.java
com.datdo.mobilib.api.MblSSLCertificateUtils.java
com.datdo.mobilib.base.MblActivityPlugin.java
com.datdo.mobilib.base.MblBaseActionBarActivity.java
com.datdo.mobilib.base.MblBaseActivity.java
com.datdo.mobilib.base.MblBaseAdapter.java
com.datdo.mobilib.base.MblBaseApplication.java
com.datdo.mobilib.base.MblBaseFragmentActivity.java
com.datdo.mobilib.base.MblDecorView.java
com.datdo.mobilib.base.MblNetworkStatusChangedReceiver.java
com.datdo.mobilib.event.MblCommonEvents.java
com.datdo.mobilib.event.MblEventCenter.java
com.datdo.mobilib.event.MblEventListener.java
com.datdo.mobilib.event.MblStrongEventListener.java
com.datdo.mobilib.event.MblWeakArrayList.java
com.datdo.mobilib.imageinput.MblAutoResizeSquareImageView.java
com.datdo.mobilib.imageinput.MblDataInputActivity.java
com.datdo.mobilib.imageinput.MblImageFolderScanner.java
com.datdo.mobilib.imageinput.MblImageInput.java
com.datdo.mobilib.imageinput.MblImagePickingScanEngine.java
com.datdo.mobilib.imageinput.MblPickImageActivity.java
com.datdo.mobilib.imageinput.MblPickImageGridViewAdapter.java
com.datdo.mobilib.imageinput.MblTakeImageActivity.java
com.datdo.mobilib.util.MblAsyncTask.java
com.datdo.mobilib.util.MblImageLoader.java
com.datdo.mobilib.util.MblLinkMovementMethod.java
com.datdo.mobilib.util.MblUrlRecognizer.java
com.datdo.mobilib.util.MblUtils.java
com.datdo.mobilib.widget.MblHorizontalViewPager.java
com.datdo.mobilib.widget.MblListViewWithScrollableItems.java
com.datdo.mobilib.widget.MblSequenceImage.java
com.datdo.mobilib.widget.MblSideMenuEnabledLayout.java
com.datdo.mobilib.widget.MblTouchImageView.java