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

com.yy.androidlib.util.notification.Notification 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 java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

import android.util.Log;

public class Notification implements InvocationHandler {
    private final Map observers;
    private Handler mainHandler;
    private T observerProxy = null;
    private Class callback;

    public Notification(Class callback, Handler handler, Map observers) {
        this.callback = callback;
        this.mainHandler = handler;
        this.observers = observers;
    }

    public Object invoke(Object proxy, final Method method, final Object[] args) {
        mainHandler.post(new Runnable() {
            public void run() {
                doInvoke(method, args);
            }
        });
        return null;
    }

    private void doInvoke(Method method, Object[] args) {
        for (Object observer : observers.keySet()) {
            if (callback.isInstance(observer)) {
                try {
                    method.invoke(observer, args);
                } catch (Exception e) {
                    Log.e(NotificationCenter.TAG, "invoke error, method: " + method.getName(), e);
                }
            }
        }
    }

    public T getObserver() {
        if (observerProxy == null) {
            observerProxy = (T) Proxy.newProxyInstance(callback.getClassLoader(), new Class[]{callback}, this);
        }
        return observerProxy;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy