/* * This is the source code of Telegram for Android v. 1.3.2. * It is licensed under GNU GPL v. 2 or later. * You should have received a copy of the license in this archive (see LICENSE). * * Copyright Nikolai Kudashov, 2013. */ package org.telegram.messenger; import java.util.ArrayList; import java.util.HashMap; public class NotificationCenter { public static NotificationCenter Instance = new NotificationCenter(); final private HashMap> observers = new HashMap>(); final private HashMap memCache = new HashMap(); final private HashMap removeAfterBroadcast = new HashMap(); final private HashMap addAfterBroadcast = new HashMap(); private boolean broadcasting = false; public interface NotificationCenterDelegate { public abstract void didReceivedNotification(int id, Object... args); } public void addToMemCache(int id, Object object) { addToMemCache(String.valueOf(id), object); } public void addToMemCache(String id, Object object) { memCache.put(id, object); } public Object getFromMemCache(int id) { return getFromMemCache(String.valueOf(id), null); } public Object getFromMemCache(String id, Object defaultValue) { Object obj = memCache.get(id); if (obj != null) { memCache.remove(id); return obj; } return defaultValue; } public void postNotificationName(int id, Object... args) { synchronized (observers) { broadcasting = true; ArrayList objects = observers.get(id); if (objects != null) { for (Object obj : objects) { ((NotificationCenterDelegate)obj).didReceivedNotification(id, args); } } broadcasting = false; if (!removeAfterBroadcast.isEmpty()) { for (HashMap.Entry entry : removeAfterBroadcast.entrySet()) { removeObserver(entry.getValue(), entry.getKey()); } removeAfterBroadcast.clear(); } if (!addAfterBroadcast.isEmpty()) { for (HashMap.Entry entry : addAfterBroadcast.entrySet()) { addObserver(entry.getValue(), entry.getKey()); } addAfterBroadcast.clear(); } } } public void addObserver(Object observer, int id) { synchronized (observers) { if (broadcasting) { addAfterBroadcast.put(id, observer); return; } ArrayList objects = observers.get(id); if (objects == null) { observers.put(id, (objects = new ArrayList())); } if (objects.contains(observer)) { return; } objects.add(observer); } } public void removeObserver(Object observer, int id) { synchronized (observers) { if (broadcasting) { removeAfterBroadcast.put(id, observer); return; } ArrayList objects = observers.get(id); if (objects != null) { objects.remove(observer); if (objects.size() == 0) { observers.remove(id); } } } } }