PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/0072016/Telegram
Java | 276 lines | 242 code | 27 blank | 7 comment | 51 complexity | d9232a8326478581d3780bcdb70cbbac MD5 | raw file
  1. /*
  2. * This is the source code of Telegram for Android v. 3.x.x.
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2013-2016.
  7. */
  8. package org.telegram.messenger;
  9. import android.util.SparseArray;
  10. import java.util.ArrayList;
  11. public class NotificationCenter {
  12. private static int totalEvents = 1;
  13. public static final int didReceivedNewMessages = totalEvents++;
  14. public static final int updateInterfaces = totalEvents++;
  15. public static final int dialogsNeedReload = totalEvents++;
  16. public static final int closeChats = totalEvents++;
  17. public static final int messagesDeleted = totalEvents++;
  18. public static final int messagesRead = totalEvents++;
  19. public static final int messagesDidLoaded = totalEvents++;
  20. public static final int messageReceivedByAck = totalEvents++;
  21. public static final int messageReceivedByServer = totalEvents++;
  22. public static final int messageSendError = totalEvents++;
  23. public static final int contactsDidLoaded = totalEvents++;
  24. public static final int chatDidCreated = totalEvents++;
  25. public static final int chatDidFailCreate = totalEvents++;
  26. public static final int chatInfoDidLoaded = totalEvents++;
  27. public static final int chatInfoCantLoad = totalEvents++;
  28. public static final int mediaDidLoaded = totalEvents++;
  29. public static final int mediaCountDidLoaded = totalEvents++;
  30. public static final int encryptedChatUpdated = totalEvents++;
  31. public static final int messagesReadEncrypted = totalEvents++;
  32. public static final int encryptedChatCreated = totalEvents++;
  33. public static final int userPhotosLoaded = totalEvents++;
  34. public static final int removeAllMessagesFromDialog = totalEvents++;
  35. public static final int notificationsSettingsUpdated = totalEvents++;
  36. public static final int pushMessagesUpdated = totalEvents++;
  37. public static final int blockedUsersDidLoaded = totalEvents++;
  38. public static final int openedChatChanged = totalEvents++;
  39. public static final int stopEncodingService = totalEvents++;
  40. public static final int didCreatedNewDeleteTask = totalEvents++;
  41. public static final int mainUserInfoChanged = totalEvents++;
  42. public static final int privacyRulesUpdated = totalEvents++;
  43. public static final int updateMessageMedia = totalEvents++;
  44. public static final int recentImagesDidLoaded = totalEvents++;
  45. public static final int replaceMessagesObjects = totalEvents++;
  46. public static final int didSetPasscode = totalEvents++;
  47. public static final int didSetTwoStepPassword = totalEvents++;
  48. public static final int screenStateChanged = totalEvents++;
  49. public static final int didLoadedReplyMessages = totalEvents++;
  50. public static final int didLoadedPinnedMessage = totalEvents++;
  51. public static final int newSessionReceived = totalEvents++;
  52. public static final int didReceivedWebpages = totalEvents++;
  53. public static final int didReceivedWebpagesInUpdates = totalEvents++;
  54. public static final int stickersDidLoaded = totalEvents++;
  55. public static final int didReplacedPhotoInMemCache = totalEvents++;
  56. public static final int messagesReadContent = totalEvents++;
  57. public static final int botInfoDidLoaded = totalEvents++;
  58. public static final int userInfoDidLoaded = totalEvents++;
  59. public static final int botKeyboardDidLoaded = totalEvents++;
  60. public static final int chatSearchResultsAvailable = totalEvents++;
  61. public static final int musicDidLoaded = totalEvents++;
  62. public static final int needShowAlert = totalEvents++;
  63. public static final int didUpdatedMessagesViews = totalEvents++;
  64. public static final int needReloadRecentDialogsSearch = totalEvents++;
  65. public static final int locationPermissionGranted = totalEvents++;
  66. public static final int peerSettingsDidLoaded = totalEvents++;
  67. public static final int wasUnableToFindCurrentLocation = totalEvents++;
  68. public static final int reloadHints = totalEvents++;
  69. public static final int reloadInlineHints = totalEvents++;
  70. public static final int httpFileDidLoaded = totalEvents++;
  71. public static final int httpFileDidFailedLoad = totalEvents++;
  72. public static final int messageThumbGenerated = totalEvents++;
  73. public static final int wallpapersDidLoaded = totalEvents++;
  74. public static final int closeOtherAppActivities = totalEvents++;
  75. public static final int didUpdatedConnectionState = totalEvents++;
  76. public static final int didReceiveSmsCode = totalEvents++;
  77. public static final int didReceiveCall = totalEvents++;
  78. public static final int emojiDidLoaded = totalEvents++;
  79. public static final int appDidLogout = totalEvents++;
  80. public static final int FileDidUpload = totalEvents++;
  81. public static final int FileDidFailUpload = totalEvents++;
  82. public static final int FileUploadProgressChanged = totalEvents++;
  83. public static final int FileLoadProgressChanged = totalEvents++;
  84. public static final int FileDidLoaded = totalEvents++;
  85. public static final int FileDidFailedLoad = totalEvents++;
  86. public static final int FilePreparingStarted = totalEvents++;
  87. public static final int FileNewChunkAvailable = totalEvents++;
  88. public static final int FilePreparingFailed = totalEvents++;
  89. public static final int audioProgressDidChanged = totalEvents++;
  90. public static final int audioDidReset = totalEvents++;
  91. public static final int audioPlayStateChanged = totalEvents++;
  92. public static final int recordProgressChanged = totalEvents++;
  93. public static final int recordStarted = totalEvents++;
  94. public static final int recordStartError = totalEvents++;
  95. public static final int recordStopped = totalEvents++;
  96. public static final int screenshotTook = totalEvents++;
  97. public static final int albumsDidLoaded = totalEvents++;
  98. public static final int audioDidSent = totalEvents++;
  99. public static final int audioDidStarted = totalEvents++;
  100. public static final int audioRouteChanged = totalEvents++;
  101. private SparseArray<ArrayList<Object>> observers = new SparseArray<>();
  102. private SparseArray<ArrayList<Object>> removeAfterBroadcast = new SparseArray<>();
  103. private SparseArray<ArrayList<Object>> addAfterBroadcast = new SparseArray<>();
  104. private ArrayList<DelayedPost> delayedPosts = new ArrayList<>(10);
  105. private int broadcasting = 0;
  106. private boolean animationInProgress;
  107. private int[] allowedNotifications;
  108. public interface NotificationCenterDelegate {
  109. void didReceivedNotification(int id, Object... args);
  110. }
  111. private class DelayedPost {
  112. private DelayedPost(int id, Object[] args) {
  113. this.id = id;
  114. this.args = args;
  115. }
  116. private int id;
  117. private Object[] args;
  118. }
  119. private static volatile NotificationCenter Instance = null;
  120. public static NotificationCenter getInstance() {
  121. NotificationCenter localInstance = Instance;
  122. if (localInstance == null) {
  123. synchronized (NotificationCenter.class) {
  124. localInstance = Instance;
  125. if (localInstance == null) {
  126. Instance = localInstance = new NotificationCenter();
  127. }
  128. }
  129. }
  130. return localInstance;
  131. }
  132. public void setAllowedNotificationsDutingAnimation(int notifications[]) {
  133. allowedNotifications = notifications;
  134. }
  135. public void setAnimationInProgress(boolean value) {
  136. animationInProgress = value;
  137. if (!animationInProgress && !delayedPosts.isEmpty()) {
  138. for (DelayedPost delayedPost : delayedPosts) {
  139. postNotificationNameInternal(delayedPost.id, true, delayedPost.args);
  140. }
  141. delayedPosts.clear();
  142. }
  143. }
  144. public boolean isAnimationInProgress() {
  145. return animationInProgress;
  146. }
  147. public void postNotificationName(int id, Object... args) {
  148. boolean allowDuringAnimation = false;
  149. if (allowedNotifications != null) {
  150. for (int a = 0; a < allowedNotifications.length; a++) {
  151. if (allowedNotifications[a] == id) {
  152. allowDuringAnimation = true;
  153. break;
  154. }
  155. }
  156. }
  157. postNotificationNameInternal(id, allowDuringAnimation, args);
  158. }
  159. public void postNotificationNameInternal(int id, boolean allowDuringAnimation, Object... args) {
  160. if (BuildVars.DEBUG_VERSION) {
  161. if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
  162. throw new RuntimeException("postNotificationName allowed only from MAIN thread");
  163. }
  164. }
  165. if (!allowDuringAnimation && animationInProgress) {
  166. DelayedPost delayedPost = new DelayedPost(id, args);
  167. delayedPosts.add(delayedPost);
  168. if (BuildVars.DEBUG_VERSION) {
  169. FileLog.e("tmessages", "delay post notification " + id + " with args count = " + args.length);
  170. }
  171. return;
  172. }
  173. broadcasting++;
  174. ArrayList<Object> objects = observers.get(id);
  175. if (objects != null && !objects.isEmpty()) {
  176. for (int a = 0; a < objects.size(); a++) {
  177. Object obj = objects.get(a);
  178. ((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
  179. }
  180. }
  181. broadcasting--;
  182. if (broadcasting == 0) {
  183. if (removeAfterBroadcast.size() != 0) {
  184. for (int a = 0; a < removeAfterBroadcast.size(); a++) {
  185. int key = removeAfterBroadcast.keyAt(a);
  186. ArrayList<Object> arrayList = removeAfterBroadcast.get(key);
  187. for (int b = 0; b < arrayList.size(); b++) {
  188. removeObserver(arrayList.get(b), key);
  189. }
  190. }
  191. removeAfterBroadcast.clear();
  192. }
  193. if (addAfterBroadcast.size() != 0) {
  194. for (int a = 0; a < addAfterBroadcast.size(); a++) {
  195. int key = addAfterBroadcast.keyAt(a);
  196. ArrayList<Object> arrayList = addAfterBroadcast.get(key);
  197. for (int b = 0; b < arrayList.size(); b++) {
  198. addObserver(arrayList.get(b), key);
  199. }
  200. }
  201. addAfterBroadcast.clear();
  202. }
  203. }
  204. }
  205. public void addObserver(Object observer, int id) {
  206. if (BuildVars.DEBUG_VERSION) {
  207. if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
  208. throw new RuntimeException("addObserver allowed only from MAIN thread");
  209. }
  210. }
  211. if (broadcasting != 0) {
  212. ArrayList<Object> arrayList = addAfterBroadcast.get(id);
  213. if (arrayList == null) {
  214. arrayList = new ArrayList<>();
  215. addAfterBroadcast.put(id, arrayList);
  216. }
  217. arrayList.add(observer);
  218. return;
  219. }
  220. ArrayList<Object> objects = observers.get(id);
  221. if (objects == null) {
  222. observers.put(id, (objects = new ArrayList<>()));
  223. }
  224. if (objects.contains(observer)) {
  225. return;
  226. }
  227. objects.add(observer);
  228. }
  229. public void removeObserver(Object observer, int id) {
  230. if (BuildVars.DEBUG_VERSION) {
  231. if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) {
  232. throw new RuntimeException("removeObserver allowed only from MAIN thread");
  233. }
  234. }
  235. if (broadcasting != 0) {
  236. ArrayList<Object> arrayList = removeAfterBroadcast.get(id);
  237. if (arrayList == null) {
  238. arrayList = new ArrayList<>();
  239. removeAfterBroadcast.put(id, arrayList);
  240. }
  241. arrayList.add(observer);
  242. return;
  243. }
  244. ArrayList<Object> objects = observers.get(id);
  245. if (objects != null) {
  246. objects.remove(observer);
  247. }
  248. }
  249. }