All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.androidlib.util.notification.NotificationCenter Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
package com.yy.androidlib.util.notification;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * IOS NSNotificationCenter for Android
 */
public enum NotificationCenter {
    INSTANCE;

    private static final String TAG = "notification";
    private Map, Notification> notificationMap;
    private long mainThreadId;
    private Handler handler;
    private Set observers;

    NotificationCenter() {
        notificationMap = new HashMap, Notification>();
        observers = new HashSet();
        Looper mainLooper = Looper.getMainLooper();
        handler = new Handler(mainLooper);
        mainThreadId = mainLooper.getThread().getId();
    }

    /**
     * add observer
     *
     * @param observer
     */
    public void addObserver(final Object observer) {
        if (isMainThread()) {
            doAddObserver(observer);
        } else {
            Log.w(TAG, String.format("trying to add observer in non main thread: %s", observer.getClass()));
            handler.post(new Runnable() {
                public void run() {
                    doAddObserver(observer);
                }
            });
        }
    }

    private boolean isMainThread() {
        return Thread.currentThread().getId() == mainThreadId;
    }

    private void doAddObserver(Object observer) {
        observers.add(observer);
    }

    public void removeObserver(final Object observer) {
        if (isMainThread()) {
            doRemoveObserver(observer);
        } else {
            Log.w(TAG, String.format("trying to remove observer in non main thread: %s", observer.getClass()));
            removeObserverLater(observer);
        }
    }

    /**
     * observer will be removed later
     * @param observer
     */
    private void removeObserverLater(final Object observer) {
        handler.post(new Runnable() {
            public void run() {
                doRemoveObserver(observer);
            }
        });
    }

    private void doRemoveObserver(Object observer) {
        observers.remove(observer);
    }

    /**
     * @param callback
     * @return not null
     */
    private  Notification getNotification(Class callback) {
        Notification notification = notificationMap.get(callback);
        if (notification == null) {
            notification = addNotification(callback);
        }
        return notification;
    }

    private  Notification addNotification(Class callback) {
        Notification notification = notificationMap.get(callback);
        if (notification == null) {
            notification = new Notification(callback, handler, observers);
            notificationMap.put(callback, notification);
        }
        return notification;
    }

    public  T getObserver(Class callback) {
        return getNotification(callback).getObserver();
    }

    public void removeAll() {
        observers.clear();
    }
}