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

602 lines
27 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.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).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
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
2019-05-14 14:08:05 +02:00
import androidx.annotation.UiThread;
2020-12-24 06:36:01 +01:00
import android.os.SystemClock;
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;
import java.util.HashMap;
2020-10-30 11:26:29 +01:00
import java.util.HashSet;
2013-10-25 17:19:00 +02:00
public class NotificationCenter {
2015-01-02 23:15:07 +01:00
private static int totalEvents = 1;
2019-01-23 18:03:33 +01:00
public static final int didReceiveNewMessages = totalEvents++;
2015-01-02 23:15:07 +01:00
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++;
2020-09-30 15:48:47 +02:00
public static final int threadMessagesRead = totalEvents++;
public static final int commentsRead = totalEvents++;
public static final int changeRepliesCounter = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int messagesDidLoad = totalEvents++;
2020-12-23 08:48:30 +01:00
public static final int messagesDidLoadWithoutProcess = totalEvents++;
public static final int loadingMessagesFailed = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int messageReceivedByAck = totalEvents++;
public static final int messageReceivedByServer = totalEvents++;
public static final int messageSendError = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int contactsDidLoad = 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++;
2019-01-23 18:03:33 +01:00
public static final int chatInfoDidLoad = totalEvents++;
2015-09-24 22:52:02 +02:00
public static final int chatInfoCantLoad = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int mediaDidLoad = totalEvents++;
public static final int mediaCountDidLoad = totalEvents++;
public static final int mediaCountsDidLoad = totalEvents++;
2015-01-02 23:15:07 +01:00
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++;
2020-07-26 10:03:38 +02:00
public static final int reloadDialogPhotos = totalEvents++;
2019-05-14 14:08:05 +02:00
public static final int folderBecomeEmpty = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int removeAllMessagesFromDialog = totalEvents++;
public static final int notificationsSettingsUpdated = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int blockedUsersDidLoad = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int openedChatChanged = 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++;
2015-01-09 13:50:15 +01:00
public static final int replaceMessagesObjects = totalEvents++;
public static final int didSetPasscode = totalEvents++;
public static final int twoStepPasswordChanged = totalEvents++;
public static final int didSetOrRemoveTwoStepPassword = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int didRemoveTwoStepPassword = totalEvents++;
public static final int replyMessagesDidLoad = totalEvents++;
2020-10-30 11:26:29 +01:00
public static final int didLoadPinnedMessages = 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++;
2019-01-23 18:03:33 +01:00
public static final int stickersDidLoad = totalEvents++;
2020-04-24 11:21:58 +02:00
public static final int diceStickersDidLoad = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int featuredStickersDidLoad = totalEvents++;
public static final int groupStickersDidLoad = totalEvents++;
public static final int messagesReadContent = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int botInfoDidLoad = totalEvents++;
public static final int userInfoDidLoad = totalEvents++;
2020-10-30 11:26:29 +01:00
public static final int pinnedInfoDidLoad = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int botKeyboardDidLoad = totalEvents++;
2015-06-29 19:12:11 +02:00
public static final int chatSearchResultsAvailable = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int chatSearchResultsLoading = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int musicDidLoad = totalEvents++;
2020-07-26 10:03:38 +02:00
public static final int moreMusicDidLoad = totalEvents++;
2015-12-09 19:27:52 +01:00
public static final int needShowAlert = totalEvents++;
2019-07-18 15:01:39 +02:00
public static final int needShowPlayServicesAlert = totalEvents++;
2020-02-13 21:45:02 +01:00
public static final int didUpdateMessagesViews = totalEvents++;
2015-09-24 22:52:02 +02:00
public static final int needReloadRecentDialogsSearch = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int peerSettingsDidLoad = 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++;
2019-01-23 18:03:33 +01:00
public static final int recentDocumentsDidLoad = totalEvents++;
2020-03-30 14:00:09 +02:00
public static final int needAddArchivedStickers = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int archivedStickersCountDidLoad = totalEvents++;
2017-03-31 01:58:05 +02:00
public static final int paymentFinished = totalEvents++;
2017-07-08 18:32:04 +02:00
public static final int channelRightsUpdated = totalEvents++;
2017-12-08 18:35:59 +01:00
public static final int openArticle = totalEvents++;
public static final int updateMentionsCount = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int didUpdatePollResults = totalEvents++;
public static final int chatOnlineCountDidLoad = totalEvents++;
2019-03-03 21:40:48 +01:00
public static final int videoLoadingStateChanged = totalEvents++;
2019-07-18 15:01:39 +02:00
public static final int newPeopleNearbyAvailable = totalEvents++;
public static final int stopAllHeavyOperations = totalEvents++;
public static final int startAllHeavyOperations = totalEvents++;
2019-08-22 01:53:26 +02:00
public static final int sendingMessagesChanged = totalEvents++;
2019-09-10 12:56:11 +02:00
public static final int didUpdateReactions = totalEvents++;
public static final int didVerifyMessagesStickers = totalEvents++;
2019-09-10 12:56:11 +02:00
public static final int scheduledMessagesUpdated = totalEvents++;
2020-07-26 10:03:38 +02:00
public static final int newSuggestionsAvailable = totalEvents++;
2020-12-23 08:48:30 +01:00
public static final int didLoadChatInviter = totalEvents++;
public static final int didLoadChatAdmins = totalEvents++;
2021-01-28 15:15:51 +01:00
public static final int historyImportProgressChanged = totalEvents++;
public static final int dialogDeleted = totalEvents++;
2015-01-02 23:15:07 +01:00
2019-12-31 14:08:08 +01:00
public static final int walletPendingTransactionsChanged = totalEvents++;
public static final int walletSyncProgressChanged = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int httpFileDidLoad = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int httpFileDidFailedLoad = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int didUpdateConnectionState = totalEvents++;
2015-01-02 23:15:07 +01:00
public static final int FileDidUpload = totalEvents++;
public static final int FileDidFailUpload = totalEvents++;
public static final int FileUploadProgressChanged = totalEvents++;
public static final int FileLoadProgressChanged = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int fileDidLoad = totalEvents++;
2019-09-10 12:56:11 +02:00
public static final int fileDidFailToLoad = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int filePreparingStarted = totalEvents++;
public static final int fileNewChunkAvailable = totalEvents++;
public static final int filePreparingFailed = totalEvents++;
2015-01-02 23:15:07 +01:00
2018-07-30 04:07:02 +02:00
public static final int dialogsUnreadCounterChanged = 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++;
2019-01-23 18:03:33 +01:00
public static final int messagePlayingDidStart = totalEvents++;
2018-08-27 10:33:11 +02:00
public static final int messagePlayingDidSeek = totalEvents++;
2019-03-03 21:40:48 +01:00
public static final int messagePlayingGoingToStop = 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++;
2019-01-23 18:03:33 +01:00
public static final int albumsDidLoad = totalEvents++;
2015-02-01 19:51:02 +01:00
public static final int audioDidSent = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int audioRecordTooShort = 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++;
2020-12-23 08:48:30 +01:00
public static final int groupCallUpdated = totalEvents++;
2021-01-28 15:15:51 +01:00
public static final int applyGroupCallVisibleParticipants = totalEvents++;
2020-12-23 08:48:30 +01:00
public static final int groupCallTypingsUpdated = totalEvents++;
2020-01-05 12:50:11 +01:00
public static final int didEndCall = totalEvents++;
2017-03-31 01:58:05 +02:00
public static final int closeInCallActivity = totalEvents++;
2020-12-23 08:48:30 +01:00
public static final int groupCallVisibilityChanged = totalEvents++;
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
public static final int appDidLogout = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int configLoaded = totalEvents++;
public static final int needDeleteDialog = totalEvents++;
2019-05-14 14:08:05 +02:00
public static final int newEmojiSuggestionsAvailable = totalEvents++;
2019-09-10 12:56:11 +02:00
public static final int themeUploadedToServer = totalEvents++;
public static final int themeUploadError = totalEvents++;
2020-03-30 14:00:09 +02:00
public static final int dialogFiltersUpdated = totalEvents++;
public static final int filterSettingsUpdated = totalEvents++;
public static final int suggestedFiltersLoaded = totalEvents++;
2018-07-30 04:07:02 +02:00
//global
public static final int pushMessagesUpdated = totalEvents++;
public static final int stopEncodingService = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int wallpapersDidLoad = totalEvents++;
2019-02-08 03:30:32 +01:00
public static final int wallpapersNeedReload = totalEvents++;
2018-07-30 04:07:02 +02:00
public static final int didReceiveSmsCode = totalEvents++;
public static final int didReceiveCall = totalEvents++;
2019-01-23 18:03:33 +01:00
public static final int emojiDidLoad = totalEvents++;
2018-07-30 04:07:02 +02:00
public static final int closeOtherAppActivities = totalEvents++;
public static final int cameraInitied = totalEvents++;
public static final int didReplacedPhotoInMemCache = totalEvents++;
public static final int didSetNewTheme = totalEvents++;
2019-06-04 12:14:50 +02:00
public static final int themeListUpdated = totalEvents++;
2019-12-31 14:08:08 +01:00
public static final int didApplyNewTheme = totalEvents++;
public static final int themeAccentListUpdated = totalEvents++;
public static final int needCheckSystemBarColors = totalEvents++;
public static final int needShareTheme = totalEvents++;
2018-07-30 04:07:02 +02:00
public static final int needSetDayNightTheme = totalEvents++;
2019-09-10 12:56:11 +02:00
public static final int goingToPreviewTheme = totalEvents++;
2018-07-30 04:07:02 +02:00
public static final int locationPermissionGranted = totalEvents++;
public static final int reloadInterface = totalEvents++;
public static final int suggestedLangpack = totalEvents++;
public static final int didSetNewWallpapper = totalEvents++;
public static final int proxySettingsChanged = totalEvents++;
public static final int proxyCheckDone = totalEvents++;
public static final int liveLocationsChanged = totalEvents++;
2019-07-18 15:01:39 +02:00
public static final int newLocationAvailable = totalEvents++;
2018-07-30 04:07:02 +02:00
public static final int liveLocationsCacheChanged = totalEvents++;
public static final int notificationsCountUpdated = totalEvents++;
public static final int playerDidStartPlaying = totalEvents++;
public static final int closeSearchByActiveAction = totalEvents++;
2019-12-31 14:08:08 +01:00
public static final int messagePlayingSpeedChanged = totalEvents++;
public static final int screenStateChanged = totalEvents++;
2020-12-24 06:36:01 +01:00
public static final int didClearDatabase = totalEvents++;
2020-08-14 18:58:22 +02:00
public static final int voipServiceCreated = totalEvents++;
2020-12-23 08:48:30 +01:00
public static final int webRtcMicAmplitudeEvent = totalEvents++;
public static final int webRtcSpeakerAmplitudeEvent = totalEvents++;
2021-02-23 12:53:38 +01:00
public static final int showBulletin = totalEvents++;
2018-07-30 04:07:02 +02:00
2019-12-31 14:08:08 +01:00
private SparseArray<ArrayList<NotificationCenterDelegate>> observers = new SparseArray<>();
private SparseArray<ArrayList<NotificationCenterDelegate>> removeAfterBroadcast = new SparseArray<>();
private SparseArray<ArrayList<NotificationCenterDelegate>> addAfterBroadcast = new SparseArray<>();
2015-05-21 23:27:27 +02:00
private ArrayList<DelayedPost> delayedPosts = new ArrayList<>(10);
2020-10-30 11:26:29 +01:00
private ArrayList<Runnable> delayedRunnables = new ArrayList<>(10);
private ArrayList<Runnable> delayedRunnablesTmp = new ArrayList<>(10);
2020-07-26 10:03:38 +02:00
private ArrayList<DelayedPost> delayedPostsTmp = new ArrayList<>(10);
private ArrayList<PostponeNotificationCallback> postponeCallbackList = new ArrayList<>(10);
2020-12-24 06:36:01 +01:00
private Runnable checkForExpiredNotifications;
private int broadcasting = 0;
2015-05-21 23:27:27 +02:00
private int animationInProgressCount;
private int animationInProgressPointer = 1;
2020-10-30 11:26:29 +01:00
HashSet<Integer> heavyOperationsCounter = new HashSet<>();
2020-12-24 06:36:01 +01:00
private final HashMap<Integer, AllowedNotifications> allowedNotifications = new HashMap<>();
2016-04-22 15:49:00 +02:00
2015-05-21 23:27:27 +02:00
public interface NotificationCenterDelegate {
2018-07-30 04:07:02 +02:00
void didReceivedNotification(int id, int account, Object... args);
2015-05-21 23:27:27 +02:00
}
private static class DelayedPost {
2015-05-21 23:27:27 +02:00
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
2018-07-30 04:07:02 +02:00
private int currentAccount;
2019-07-18 15:01:39 +02:00
private int currentHeavyOperationFlags;
private static volatile NotificationCenter[] Instance = new NotificationCenter[UserConfig.MAX_ACCOUNT_COUNT];
2018-07-30 04:07:02 +02:00
private static volatile NotificationCenter globalInstance;
2015-05-21 23:27:27 +02:00
2018-07-30 04:07:02 +02:00
@UiThread
public static NotificationCenter getInstance(int num) {
NotificationCenter localInstance = Instance[num];
if (localInstance == null) {
synchronized (NotificationCenter.class) {
2018-07-30 04:07:02 +02:00
localInstance = Instance[num];
if (localInstance == null) {
2018-07-30 04:07:02 +02:00
Instance[num] = localInstance = new NotificationCenter(num);
}
}
}
return localInstance;
}
2018-07-30 04:07:02 +02:00
@UiThread
public static NotificationCenter getGlobalInstance() {
NotificationCenter localInstance = globalInstance;
if (localInstance == null) {
synchronized (NotificationCenter.class) {
localInstance = globalInstance;
if (localInstance == null) {
globalInstance = localInstance = new NotificationCenter(-1);
}
}
}
return localInstance;
}
public NotificationCenter(int account) {
currentAccount = account;
}
public int setAnimationInProgress(int oldIndex, int[] allowedNotifications) {
2020-10-30 11:26:29 +01:00
return setAnimationInProgress(oldIndex, allowedNotifications, true);
}
public int setAnimationInProgress(int oldIndex, int[] allowedNotifications, boolean stopHeavyOperations) {
onAnimationFinish(oldIndex);
2020-10-30 11:26:29 +01:00
if (heavyOperationsCounter.isEmpty() && stopHeavyOperations) {
2020-10-31 22:13:37 +01:00
getGlobalInstance().postNotificationName(stopAllHeavyOperations, 512);
}
animationInProgressCount++;
animationInProgressPointer++;
2020-10-30 11:26:29 +01:00
if (stopHeavyOperations) {
heavyOperationsCounter.add(animationInProgressPointer);
}
2020-12-24 06:36:01 +01:00
AllowedNotifications notifications = new AllowedNotifications();
notifications.allowedIds = allowedNotifications;
this.allowedNotifications.put(animationInProgressPointer, notifications);
if (checkForExpiredNotifications == null) {
AndroidUtilities.runOnUIThread(checkForExpiredNotifications = this::checkForExpiredNotifications, 1017);
}
return animationInProgressPointer;
2016-04-22 15:49:00 +02:00
}
2020-12-24 06:36:01 +01:00
private void checkForExpiredNotifications() {
checkForExpiredNotifications = null;
if (this.allowedNotifications.isEmpty()) {
return;
}
long minTime = Long.MAX_VALUE;
long currentTime = SystemClock.elapsedRealtime();
ArrayList<Integer> expiredIndices = null;
for (HashMap.Entry<Integer, AllowedNotifications> entry : this.allowedNotifications.entrySet()) {
AllowedNotifications allowedNotification = entry.getValue();
if (currentTime - allowedNotification.time > 1000) {
if (expiredIndices == null) {
expiredIndices = new ArrayList<>();
}
expiredIndices.add(entry.getKey());
} else {
minTime = Math.min(allowedNotification.time, minTime);
}
2020-12-24 06:36:01 +01:00
}
if (expiredIndices != null) {
for (int i = 0; i < expiredIndices.size(); i++) {
onAnimationFinish(expiredIndices.get(i));
}
}
if (minTime != Long.MAX_VALUE) {
long time = 1017 - (currentTime - minTime);
AndroidUtilities.runOnUIThread(() -> checkForExpiredNotifications = this::checkForExpiredNotifications, Math.max(17, time));
2019-07-18 15:01:39 +02:00
}
}
2020-12-24 06:36:01 +01:00
public void updateAllowedNotifications(int transitionAnimationIndex, int[] allowedNotifications) {
AllowedNotifications notifications = this.allowedNotifications.get(transitionAnimationIndex);
if (notifications != null) {
2020-12-24 06:36:01 +01:00
notifications.allowedIds = allowedNotifications;
}
}
public void onAnimationFinish(int index) {
AllowedNotifications allowed = allowedNotifications.remove(index);
if (allowed != null) {
animationInProgressCount--;
2020-10-30 11:26:29 +01:00
if (!heavyOperationsCounter.isEmpty()) {
heavyOperationsCounter.remove(index);
if (heavyOperationsCounter.isEmpty()) {
NotificationCenter.getGlobalInstance().postNotificationName(startAllHeavyOperations, 512);
}
}
if (animationInProgressCount == 0) {
2020-07-26 10:03:38 +02:00
runDelayedNotifications();
2015-05-21 23:27:27 +02:00
}
}
2020-12-24 06:36:01 +01:00
if (checkForExpiredNotifications != null && allowedNotifications.isEmpty()) {
AndroidUtilities.cancelRunOnUIThread(checkForExpiredNotifications);
checkForExpiredNotifications = null;
}
2013-10-25 17:19:00 +02:00
}
2020-07-26 10:03:38 +02:00
public void runDelayedNotifications() {
if (!delayedPosts.isEmpty()) {
delayedPostsTmp.clear();
delayedPostsTmp.addAll(delayedPosts);
delayedPosts.clear();
for (int a = 0; a < delayedPostsTmp.size(); a++) {
DelayedPost delayedPost = delayedPostsTmp.get(a);
postNotificationNameInternal(delayedPost.id, true, delayedPost.args);
}
delayedPostsTmp.clear();
}
2020-10-30 11:26:29 +01:00
if (!delayedRunnables.isEmpty()) {
delayedRunnablesTmp.clear();
delayedRunnablesTmp.addAll(delayedRunnables);
delayedRunnables.clear();
for (int a = 0; a < delayedRunnablesTmp.size(); a++) {
delayedRunnablesTmp.get(a).run();
}
delayedRunnablesTmp.clear();
}
2020-07-26 10:03:38 +02:00
}
2016-05-25 23:49:47 +02:00
public boolean isAnimationInProgress() {
return animationInProgressCount > 0;
2016-05-25 23:49:47 +02:00
}
2019-07-18 15:01:39 +02:00
public int getCurrentHeavyOperationFlags() {
return currentHeavyOperationFlags;
}
2021-03-19 11:25:58 +01:00
public ArrayList<NotificationCenterDelegate> getObservers(int id) {
return observers.get(id);
}
2013-10-25 17:19:00 +02:00
public void postNotificationName(int id, Object... args) {
2020-07-26 10:03:38 +02:00
boolean allowDuringAnimation = id == startAllHeavyOperations || id == stopAllHeavyOperations || id == didReplacedPhotoInMemCache;
2020-12-24 06:36:01 +01:00
ArrayList<Integer> expiredIndices = null;
if (!allowDuringAnimation && !allowedNotifications.isEmpty()) {
int size = allowedNotifications.size();
int allowedCount = 0;
2020-12-24 06:36:01 +01:00
long currentTime = SystemClock.elapsedRealtime();
for (HashMap.Entry<Integer, AllowedNotifications> entry : allowedNotifications.entrySet()) {
AllowedNotifications allowedNotification = entry.getValue();
if (currentTime - allowedNotification.time > 1000) {
if (expiredIndices == null) {
expiredIndices = new ArrayList<>();
}
expiredIndices.add(entry.getKey());
}
int[] allowed = allowedNotification.allowedIds;
if (allowed != null) {
for (int a = 0; a < allowed.length; a++) {
if (allowed[a] == id) {
allowedCount++;
break;
}
}
} else {
2016-04-22 15:49:00 +02:00
break;
}
}
allowDuringAnimation = size == allowedCount;
2015-05-21 23:27:27 +02:00
}
2019-07-18 15:01:39 +02:00
if (id == startAllHeavyOperations) {
Integer flags = (Integer) args[0];
2020-10-30 11:26:29 +01:00
currentHeavyOperationFlags &= ~flags;
2019-07-18 15:01:39 +02:00
} else if (id == stopAllHeavyOperations) {
Integer flags = (Integer) args[0];
currentHeavyOperationFlags |= flags;
}
2015-05-21 23:27:27 +02:00
postNotificationNameInternal(id, allowDuringAnimation, args);
2020-12-24 06:36:01 +01:00
if (expiredIndices != null) {
for (int i = 0; i < expiredIndices.size(); i++) {
onAnimationFinish(expiredIndices.get(i));
}
}
2015-05-21 23:27:27 +02:00
}
2018-07-30 04:07:02 +02:00
@UiThread
2015-05-21 23:27:27 +02:00
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 && isAnimationInProgress()) {
2015-05-21 23:27:27 +02:00
DelayedPost delayedPost = new DelayedPost(id, args);
delayedPosts.add(delayedPost);
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
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;
}
2020-07-26 10:03:38 +02:00
if (!postponeCallbackList.isEmpty()) {
for (int i = 0; i < postponeCallbackList.size(); i++) {
if (postponeCallbackList.get(i).needPostpone(id, currentAccount, args)) {
delayedPosts.add(new DelayedPost(id, args));
return;
}
}
}
2015-05-21 23:27:27 +02:00
broadcasting++;
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> 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++) {
2020-06-06 08:07:06 +02:00
NotificationCenterDelegate obj = objects.get(a);
2019-12-31 14:08:08 +01:00
obj.didReceivedNotification(id, currentAccount, args);
2015-05-21 23:27:27 +02:00
}
}
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);
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> arrayList = removeAfterBroadcast.get(key);
2015-09-24 22:52:02 +02:00
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);
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> arrayList = addAfterBroadcast.get(key);
2015-09-24 22:52:02 +02:00
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
}
}
2019-12-31 14:08:08 +01:00
public void addObserver(NotificationCenterDelegate 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) {
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> arrayList = addAfterBroadcast.get(id);
2015-09-24 22:52:02 +02:00
if (arrayList == null) {
arrayList = new ArrayList<>();
addAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
2015-05-21 23:27:27 +02:00
return;
}
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> objects = observers.get(id);
2015-05-21 23:27:27 +02:00
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
}
2019-12-31 14:08:08 +01:00
public void removeObserver(NotificationCenterDelegate 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) {
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> arrayList = removeAfterBroadcast.get(id);
2015-09-24 22:52:02 +02:00
if (arrayList == null) {
arrayList = new ArrayList<>();
removeAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
2015-05-21 23:27:27 +02:00
return;
}
2019-12-31 14:08:08 +01:00
ArrayList<NotificationCenterDelegate> objects = observers.get(id);
2015-05-21 23:27:27 +02:00
if (objects != null) {
objects.remove(observer);
2013-10-25 17:19:00 +02:00
}
}
2019-01-23 18:03:33 +01:00
public boolean hasObservers(int id) {
return observers.indexOfKey(id) >= 0;
}
2020-07-26 10:03:38 +02:00
public void addPostponeNotificationsCallback(PostponeNotificationCallback callback) {
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
throw new RuntimeException("PostponeNotificationsCallback allowed only from MAIN thread");
}
}
if (!postponeCallbackList.contains(callback)) {
postponeCallbackList.add(callback);
}
}
public void removePostponeNotificationsCallback(PostponeNotificationCallback callback) {
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
throw new RuntimeException("removePostponeNotificationsCallback allowed only from MAIN thread");
}
}
if (postponeCallbackList.remove(callback)) {
runDelayedNotifications();
}
}
public interface PostponeNotificationCallback {
boolean needPostpone(int id, int currentAccount, Object[] args);
}
2020-10-30 11:26:29 +01:00
public void doOnIdle(Runnable runnable) {
if (isAnimationInProgress()) {
delayedRunnables.add(runnable);
} else {
runnable.run();
}
}
2020-12-24 06:36:01 +01:00
private static class AllowedNotifications {
int[] allowedIds;
final long time;
private AllowedNotifications() {
time = SystemClock.elapsedRealtime();
}
}
2013-10-25 17:19:00 +02:00
}