2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* 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 {
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
final private HashMap<Integer, ArrayList<Object>> observers = new HashMap<Integer, ArrayList<Object>>();
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
final private HashMap<Integer, Object> removeAfterBroadcast = new HashMap<Integer, Object>();
|
2014-02-04 18:36:55 +00:00
|
|
|
final private HashMap<Integer, Object> addAfterBroadcast = new HashMap<Integer, Object>();
|
|
|
|
|
|
|
|
private boolean broadcasting = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
private static volatile NotificationCenter Instance = null;
|
|
|
|
public static NotificationCenter getInstance() {
|
|
|
|
NotificationCenter localInstance = Instance;
|
|
|
|
if (localInstance == null) {
|
|
|
|
synchronized (NotificationCenter.class) {
|
|
|
|
localInstance = Instance;
|
|
|
|
if (localInstance == null) {
|
|
|
|
Instance = localInstance = new NotificationCenter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return localInstance;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public interface NotificationCenterDelegate {
|
|
|
|
public abstract void didReceivedNotification(int id, Object... args);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postNotificationName(int id, Object... args) {
|
|
|
|
synchronized (observers) {
|
|
|
|
broadcasting = true;
|
|
|
|
ArrayList<Object> objects = observers.get(id);
|
|
|
|
if (objects != null) {
|
|
|
|
for (Object obj : objects) {
|
|
|
|
((NotificationCenterDelegate)obj).didReceivedNotification(id, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
broadcasting = false;
|
|
|
|
if (!removeAfterBroadcast.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Integer, Object> entry : removeAfterBroadcast.entrySet()) {
|
|
|
|
removeObserver(entry.getValue(), entry.getKey());
|
|
|
|
}
|
|
|
|
removeAfterBroadcast.clear();
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
if (!addAfterBroadcast.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Integer, Object> entry : addAfterBroadcast.entrySet()) {
|
|
|
|
addObserver(entry.getValue(), entry.getKey());
|
|
|
|
}
|
|
|
|
addAfterBroadcast.clear();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addObserver(Object observer, int id) {
|
|
|
|
synchronized (observers) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (broadcasting) {
|
|
|
|
addAfterBroadcast.put(id, observer);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
ArrayList<Object> objects = observers.get(id);
|
|
|
|
if (objects == null) {
|
2014-02-04 18:36:55 +00:00
|
|
|
observers.put(id, (objects = new ArrayList<Object>()));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
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<Object> objects = observers.get(id);
|
|
|
|
if (objects != null) {
|
|
|
|
objects.remove(observer);
|
|
|
|
if (objects.size() == 0) {
|
|
|
|
observers.remove(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|