NekoX/TMessagesProj/src/main/java/org/telegram/messenger/NotificationCenter.java

306 lines
14 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.x.x.
2013-10-25 17:19:00 +02: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).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2013-10-25 17:19:00 +02:00
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
import android.util.SparseArray;
2015-05-21 23:27:27 +02:00
2013-10-25 17:19:00 +02:00
import java.util.ArrayList;
public class NotificationCenter {
2015-01-02 23:15:07 +01:00
private static int totalEvents = 1;
public static final int didReceivedNewMessages = totalEvents++;
public static final int updateInterfaces = totalEvents++;
public static final int dialogsNeedReload = totalEvents++;
public static final int closeChats = totalEvents++;
public static final int messagesDeleted = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int historyCleared = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int messagesRead = totalEvents++;
public static final int messagesDidLoaded = totalEvents++;
public static final int messageReceivedByAck = totalEvents++;
public static final int messageReceivedByServer = totalEvents++;
public static final int messageSendError = totalEvents++;
public static final int contactsDidLoaded = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int contactsImported = totalEvents++;
public static final int hasNewContactsToImport = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int chatDidCreated = totalEvents++;
public static final int chatDidFailCreate = totalEvents++;
public static final int chatInfoDidLoaded = totalEvents++;
2015-09-24 22:52:02 +02:00
public static final int chatInfoCantLoad = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int mediaDidLoaded = totalEvents++;
public static final int mediaCountDidLoaded = totalEvents++;
public static final int encryptedChatUpdated = totalEvents++;
public static final int messagesReadEncrypted = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int encryptedChatCreated = totalEvents++;
public static final int dialogPhotosLoaded = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int removeAllMessagesFromDialog = totalEvents++;
public static final int notificationsSettingsUpdated = totalEvents++;
public static final int pushMessagesUpdated = totalEvents++;
public static final int blockedUsersDidLoaded = totalEvents++;
public static final int openedChatChanged = totalEvents++;
public static final int stopEncodingService = totalEvents++;
public static final int didCreatedNewDeleteTask = totalEvents++;
public static final int mainUserInfoChanged = totalEvents++;
public static final int privacyRulesUpdated = totalEvents++;
public static final int updateMessageMedia = totalEvents++;
public static final int recentImagesDidLoaded = totalEvents++;
2015-01-09 13:50:15 +01:00
public static final int replaceMessagesObjects = totalEvents++;
public static final int didSetPasscode = totalEvents++;
2015-04-09 20:00:14 +02:00
public static final int didSetTwoStepPassword = totalEvents++;
2017-03-31 01:58:05 +02:00
public static final int didRemovedTwoStepPassword = totalEvents++;
public static final int screenStateChanged = totalEvents++;
public static final int didLoadedReplyMessages = totalEvents++;
2016-03-16 13:26:32 +01:00
public static final int didLoadedPinnedMessage = totalEvents++;
2015-04-09 20:00:14 +02:00
public static final int newSessionReceived = totalEvents++;
public static final int didReceivedWebpages = totalEvents++;
public static final int didReceivedWebpagesInUpdates = totalEvents++;
public static final int stickersDidLoaded = totalEvents++;
2016-10-11 13:57:01 +02:00
public static final int featuredStickersDidLoaded = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int groupStickersDidLoaded = totalEvents++;
public static final int didReplacedPhotoInMemCache = totalEvents++;
public static final int messagesReadContent = totalEvents++;
2015-06-29 19:12:11 +02:00
public static final int botInfoDidLoaded = totalEvents++;
2016-03-16 13:26:32 +01:00
public static final int userInfoDidLoaded = totalEvents++;
2015-06-29 19:12:11 +02:00
public static final int botKeyboardDidLoaded = totalEvents++;
public static final int chatSearchResultsAvailable = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int chatSearchResultsLoading = totalEvents++;
2015-07-22 20:56:37 +02:00
public static final int musicDidLoaded = totalEvents++;
2015-12-09 19:27:52 +01:00
public static final int needShowAlert = totalEvents++;
2015-09-24 22:52:02 +02:00
public static final int didUpdatedMessagesViews = totalEvents++;
public static final int needReloadRecentDialogsSearch = totalEvents++;
public static final int locationPermissionGranted = totalEvents++;
2016-03-16 13:26:32 +01:00
public static final int peerSettingsDidLoaded = totalEvents++;
2016-04-22 15:49:00 +02:00
public static final int wasUnableToFindCurrentLocation = totalEvents++;
2016-05-25 23:49:47 +02:00
public static final int reloadHints = totalEvents++;
public static final int reloadInlineHints = totalEvents++;
public static final int newDraftReceived = totalEvents++;
2016-10-11 13:57:01 +02:00
public static final int recentDocumentsDidLoaded = totalEvents++;
public static final int cameraInitied = totalEvents++;
public static final int needReloadArchivedStickers = totalEvents++;
public static final int didSetNewWallpapper = totalEvents++;
2017-03-31 01:58:05 +02:00
public static final int archivedStickersCountDidLoaded = totalEvents++;
public static final int paymentFinished = totalEvents++;
2017-07-08 18:32:04 +02:00
public static final int reloadInterface = totalEvents++;
public static final int suggestedLangpack = totalEvents++;
public static final int channelRightsUpdated = totalEvents++;
public static final int proxySettingsChanged = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int openArticle = totalEvents++;
public static final int updateMentionsCount = totalEvents++;
public static final int liveLocationsChanged = totalEvents++;
public static final int liveLocationsCacheChanged = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int httpFileDidLoaded = totalEvents++;
public static final int httpFileDidFailedLoad = totalEvents++;
2015-02-01 19:51:02 +01:00
public static final int messageThumbGenerated = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int didSetNewTheme = totalEvents++;
2015-02-01 19:51:02 +01:00
2015-01-02 23:15:07 +01:00
public static final int wallpapersDidLoaded = totalEvents++;
public static final int closeOtherAppActivities = totalEvents++;
public static final int didUpdatedConnectionState = totalEvents++;
public static final int didReceiveSmsCode = totalEvents++;
2016-03-06 02:49:31 +01:00
public static final int didReceiveCall = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int emojiDidLoaded = totalEvents++;
public static final int appDidLogout = totalEvents++;
public static final int FileDidUpload = totalEvents++;
public static final int FileDidFailUpload = totalEvents++;
public static final int FileUploadProgressChanged = totalEvents++;
public static final int FileLoadProgressChanged = totalEvents++;
public static final int FileDidLoaded = totalEvents++;
public static final int FileDidFailedLoad = totalEvents++;
public static final int FilePreparingStarted = totalEvents++;
public static final int FileNewChunkAvailable = totalEvents++;
public static final int FilePreparingFailed = totalEvents++;
2017-07-08 18:32:04 +02:00
public static final int messagePlayingProgressDidChanged = totalEvents++;
public static final int messagePlayingDidReset = totalEvents++;
public static final int messagePlayingPlayStateChanged = totalEvents++;
public static final int messagePlayingDidStarted = totalEvents++;
2015-02-01 19:51:02 +01:00
public static final int recordProgressChanged = totalEvents++;
public static final int recordStarted = totalEvents++;
public static final int recordStartError = totalEvents++;
public static final int recordStopped = totalEvents++;
public static final int screenshotTook = totalEvents++;
public static final int albumsDidLoaded = totalEvents++;
public static final int audioDidSent = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int audioRouteChanged = totalEvents++;
2017-03-31 01:58:05 +02:00
public static final int didStartedCall = totalEvents++;
public static final int didEndedCall = totalEvents++;
public static final int closeInCallActivity = totalEvents++;
2015-09-24 22:52:02 +02:00
private SparseArray<ArrayList<Object>> observers = new SparseArray<>();
private SparseArray<ArrayList<Object>> removeAfterBroadcast = new SparseArray<>();
private SparseArray<ArrayList<Object>> addAfterBroadcast = new SparseArray<>();
2015-05-21 23:27:27 +02:00
private ArrayList<DelayedPost> delayedPosts = new ArrayList<>(10);
private int broadcasting = 0;
2015-05-21 23:27:27 +02:00
private boolean animationInProgress;
2016-04-22 15:49:00 +02:00
private int[] allowedNotifications;
2015-05-21 23:27:27 +02:00
public interface NotificationCenterDelegate {
void didReceivedNotification(int id, Object... args);
}
private class DelayedPost {
private DelayedPost(int id, Object[] args) {
this.id = id;
this.args = args;
}
private int id;
private Object[] args;
}
2013-10-25 17:19:00 +02:00
private static volatile NotificationCenter Instance = null;
2015-05-21 23:27:27 +02:00
public static NotificationCenter getInstance() {
NotificationCenter localInstance = Instance;
if (localInstance == null) {
synchronized (NotificationCenter.class) {
localInstance = Instance;
if (localInstance == null) {
Instance = localInstance = new NotificationCenter();
}
}
}
return localInstance;
}
2016-04-22 15:49:00 +02:00
public void setAllowedNotificationsDutingAnimation(int notifications[]) {
allowedNotifications = notifications;
}
2015-05-21 23:27:27 +02:00
public void setAnimationInProgress(boolean value) {
animationInProgress = value;
if (!animationInProgress && !delayedPosts.isEmpty()) {
2017-07-23 19:29:17 +02:00
for (int a = 0; a < delayedPosts.size(); a++) {
DelayedPost delayedPost = delayedPosts.get(a);
2015-05-21 23:27:27 +02:00
postNotificationNameInternal(delayedPost.id, true, delayedPost.args);
}
delayedPosts.clear();
}
2013-10-25 17:19:00 +02:00
}
2016-05-25 23:49:47 +02:00
public boolean isAnimationInProgress() {
return animationInProgress;
}
2013-10-25 17:19:00 +02:00
public void postNotificationName(int id, Object... args) {
2015-05-21 23:27:27 +02:00
boolean allowDuringAnimation = false;
2016-04-22 15:49:00 +02:00
if (allowedNotifications != null) {
for (int a = 0; a < allowedNotifications.length; a++) {
if (allowedNotifications[a] == id) {
allowDuringAnimation = true;
break;
}
}
2015-05-21 23:27:27 +02:00
}
postNotificationNameInternal(id, allowDuringAnimation, args);
}
public void postNotificationNameInternal(int id, boolean allowDuringAnimation, Object... args) {
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
throw new RuntimeException("postNotificationName allowed only from MAIN thread");
}
}
if (!allowDuringAnimation && animationInProgress) {
DelayedPost delayedPost = new DelayedPost(id, args);
delayedPosts.add(delayedPost);
if (BuildVars.DEBUG_VERSION) {
2017-03-31 01:58:05 +02:00
FileLog.e("delay post notification " + id + " with args count = " + args.length);
2013-10-25 17:19:00 +02:00
}
2015-05-21 23:27:27 +02:00
return;
}
broadcasting++;
ArrayList<Object> objects = observers.get(id);
2015-09-24 22:52:02 +02:00
if (objects != null && !objects.isEmpty()) {
for (int a = 0; a < objects.size(); a++) {
Object obj = objects.get(a);
2015-05-21 23:27:27 +02:00
((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
}
}
broadcasting--;
if (broadcasting == 0) {
2015-09-24 22:52:02 +02:00
if (removeAfterBroadcast.size() != 0) {
for (int a = 0; a < removeAfterBroadcast.size(); a++) {
int key = removeAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = removeAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
removeObserver(arrayList.get(b), key);
}
2013-10-25 17:19:00 +02:00
}
2015-05-21 23:27:27 +02:00
removeAfterBroadcast.clear();
}
2015-09-24 22:52:02 +02:00
if (addAfterBroadcast.size() != 0) {
for (int a = 0; a < addAfterBroadcast.size(); a++) {
int key = addAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = addAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
addObserver(arrayList.get(b), key);
}
}
2015-05-21 23:27:27 +02:00
addAfterBroadcast.clear();
}
2013-10-25 17:19:00 +02:00
}
}
public void addObserver(Object observer, int id) {
2015-05-21 23:27:27 +02:00
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
throw new RuntimeException("addObserver allowed only from MAIN thread");
}
2013-10-25 17:19:00 +02:00
}
2015-05-21 23:27:27 +02:00
if (broadcasting != 0) {
2015-09-24 22:52:02 +02:00
ArrayList<Object> arrayList = addAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
addAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
2015-05-21 23:27:27 +02:00
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects == null) {
observers.put(id, (objects = new ArrayList<>()));
}
if (objects.contains(observer)) {
return;
}
objects.add(observer);
2013-10-25 17:19:00 +02:00
}
public void removeObserver(Object observer, int id) {
2015-05-21 23:27:27 +02:00
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
throw new RuntimeException("removeObserver allowed only from MAIN thread");
2013-10-25 17:19:00 +02:00
}
2015-05-21 23:27:27 +02:00
}
if (broadcasting != 0) {
2015-09-24 22:52:02 +02:00
ArrayList<Object> arrayList = removeAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
removeAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
2015-05-21 23:27:27 +02:00
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects != null) {
objects.remove(observer);
2013-10-25 17:19:00 +02:00
}
}
}