List of usage examples for android.view ViewTreeObserver addOnPreDrawListener
public void addOnPreDrawListener(OnPreDrawListener listener)
From source file:org.telegram.ui.ChatActivity.java
private void fixLayout() { if (chatListView != null) { ViewTreeObserver obs = chatListView.getViewTreeObserver(); obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override//from w w w . j ava 2s.c om public boolean onPreDraw() { if (parentActivity == null) { chatListView.getViewTreeObserver().removeOnPreDrawListener(this); return false; } WindowManager manager = (WindowManager) ApplicationLoader.applicationContext .getSystemService(Activity.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); int rotation = display.getRotation(); int height; int currentActionBarHeight = parentActivity.getSupportActionBar().getHeight(); if (currentActionBarHeight != Utilities.dp(48) && currentActionBarHeight != Utilities.dp(40)) { height = currentActionBarHeight; } else { height = Utilities.dp(48); if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) { height = Utilities.dp(40); } } if (avatarImageView != null) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) avatarImageView .getLayoutParams(); params.width = height; params.height = height; avatarImageView.setLayoutParams(params); } chatListView.getViewTreeObserver().removeOnPreDrawListener(this); if (currentEncryptedChat != null) { TextView title = (TextView) parentActivity.findViewById(R.id.action_bar_title); if (title == null) { final int subtitleId = parentActivity.getResources().getIdentifier("action_bar_title", "id", "android"); title = (TextView) parentActivity.findViewById(subtitleId); } if (title != null) { title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_white, 0, 0, 0); title.setCompoundDrawablePadding(Utilities.dp(4)); } } return false; } }); } }
From source file:com.android.launcher2.Launcher.java
public void onWindowVisibilityChanged(int visibility) { mVisible = visibility == View.VISIBLE; updateRunning();/* w ww .j av a2s. c om*/ // The following code used to be in onResume, but it turns out onResume is called when // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged // is a more appropriate event to handle if (mVisible) { mAppsCustomizeTabHost.onWindowVisible(); if (!mWorkspaceLoading) { final ViewTreeObserver observer = mWorkspace.getViewTreeObserver(); // We want to let Launcher draw itself at least once before we force it to build // layers on all the workspace pages, so that transitioning to Launcher from other // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to // a true draw so we wait until the second preDraw call to be safe observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { // We delay the layer building a bit in order to give // other message processing a time to run. In particular // this avoids a delay in hiding the IME if it was // currently shown, because doing that may involve // some communication back with the app. mWorkspace.postDelayed(mBuildLayersRunnable, 500); observer.removeOnPreDrawListener(this); return true; } }); } // When Launcher comes back to foreground, a different Activity might be responsible for // the app market intent, so refresh the icon updateAppMarketIcon(); clearTypedText(); } }
From source file:org.telepatch.ui.ChatActivity.java
@SuppressWarnings("unchecked") @Override//w w w.j a v a 2 s. c om public void didReceivedNotification(int id, final Object... args) { if (id == NotificationCenter.messagesDidLoaded) { long did = (Long) args[0]; if (did == dialog_id) { loadsCount++; int count = (Integer) args[1]; boolean isCache = (Boolean) args[3]; int fnid = (Integer) args[4]; int last_unread_date = (Integer) args[7]; boolean forwardLoad = (Boolean) args[8]; boolean wasUnread = false; boolean positionToUnread = false; if (fnid != 0) { first_unread_id = fnid; last_unread_id = (Integer) args[5]; unread_to_load = (Integer) args[6]; positionToUnread = true; } ArrayList<MessageObject> messArr = (ArrayList<MessageObject>) args[2]; int newRowsCount = 0; unread_end_reached = last_unread_id == 0; if (loadsCount == 1 && messArr.size() > 20) { loadsCount++; } if (firstLoading) { if (!unread_end_reached) { messages.clear(); messagesByDays.clear(); messagesDict.clear(); if (currentEncryptedChat == null) { maxMessageId = Integer.MAX_VALUE; minMessageId = Integer.MIN_VALUE; } else { maxMessageId = Integer.MIN_VALUE; minMessageId = Integer.MAX_VALUE; } maxDate = Integer.MIN_VALUE; minDate = 0; } firstLoading = false; } for (int a = 0; a < messArr.size(); a++) { MessageObject obj = messArr.get(a); if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } if (obj.messageOwner.id > 0) { maxMessageId = Math.min(obj.messageOwner.id, maxMessageId); minMessageId = Math.max(obj.messageOwner.id, minMessageId); } else if (currentEncryptedChat != null) { maxMessageId = Math.max(obj.messageOwner.id, maxMessageId); minMessageId = Math.min(obj.messageOwner.id, minMessageId); } maxDate = Math.max(maxDate, obj.messageOwner.date); if (minDate == 0 || obj.messageOwner.date < minDate) { minDate = obj.messageOwner.date; } if (obj.type < 0) { continue; } if (!obj.isOut() && obj.isUnread()) { wasUnread = true; } messagesDict.put(obj.messageOwner.id, obj); ArrayList<MessageObject> dayArray = messagesByDays.get(obj.dateKey); if (dayArray == null) { dayArray = new ArrayList<MessageObject>(); messagesByDays.put(obj.dateKey, dayArray); TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = LocaleController.formatDateChat(obj.messageOwner.date); dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.type = 10; dateObj.contentType = 4; if (forwardLoad) { messages.add(0, dateObj); } else { messages.add(dateObj); } newRowsCount++; } newRowsCount++; dayArray.add(obj); if (forwardLoad) { messages.add(0, obj); } else { messages.add(messages.size() - 1, obj); } if (!forwardLoad) { if (obj.messageOwner.id == first_unread_id) { TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = ""; dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.contentType = dateObj.type = 6; boolean dateAdded = true; if (a != messArr.size() - 1) { MessageObject next = messArr.get(a + 1); dateAdded = !next.dateKey.equals(obj.dateKey); } messages.add(messages.size() - (dateAdded ? 0 : 1), dateObj); unreadMessageObject = dateObj; newRowsCount++; } if (obj.messageOwner.id == last_unread_id) { unread_end_reached = true; } } } if (unread_end_reached) { first_unread_id = 0; last_unread_id = 0; } if (forwardLoad) { if (messArr.size() != count) { unread_end_reached = true; first_unread_id = 0; last_unread_id = 0; } chatAdapter.notifyDataSetChanged(); loadingForward = false; } else { if (messArr.size() != count) { if (isCache) { cacheEndReaced = true; if (currentEncryptedChat != null || isBroadcast) { endReached = true; } } else { cacheEndReaced = true; endReached = true; } } loading = false; if (chatListView != null) { if (first || scrollToTopOnResume) { chatAdapter.notifyDataSetChanged(); if (positionToUnread && unreadMessageObject != null) { if (messages.get(messages.size() - 1) == unreadMessageObject) { chatListView.setSelectionFromTop(0, AndroidUtilities.dp(-11)); } else { chatListView.setSelectionFromTop( messages.size() - messages.indexOf(unreadMessageObject), AndroidUtilities.dp(-11)); } ViewTreeObserver obs = chatListView.getViewTreeObserver(); obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (!messages.isEmpty()) { if (messages.get(messages.size() - 1) == unreadMessageObject) { chatListView.setSelectionFromTop(0, AndroidUtilities.dp(-11)); } else { chatListView.setSelectionFromTop( messages.size() - messages.indexOf(unreadMessageObject), AndroidUtilities.dp(-11)); } } chatListView.getViewTreeObserver().removeOnPreDrawListener(this); return false; } }); chatListView.invalidate(); showPagedownButton(true, true); } else { chatListView.post(new Runnable() { @Override public void run() { chatListView.setSelectionFromTop(messages.size() - 1, -100000 - chatListView.getPaddingTop()); } }); } } else { int firstVisPos = chatListView.getLastVisiblePosition(); View firstVisView = chatListView.getChildAt(chatListView.getChildCount() - 1); int top = ((firstVisView == null) ? 0 : firstVisView.getTop()) - chatListView.getPaddingTop(); chatAdapter.notifyDataSetChanged(); chatListView.setSelectionFromTop(firstVisPos + newRowsCount - (endReached ? 1 : 0), top); } if (paused) { scrollToTopOnResume = true; if (positionToUnread && unreadMessageObject != null) { scrollToTopUnReadOnResume = true; } } if (first) { if (chatListView.getEmptyView() == null) { chatListView.setEmptyView(emptyViewContainer); } } } else { scrollToTopOnResume = true; if (positionToUnread && unreadMessageObject != null) { scrollToTopUnReadOnResume = true; } } } if (first && messages.size() > 0) { if (last_unread_id != 0) { MessagesController.getInstance().markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, last_unread_id, 0, last_unread_date, wasUnread, false); } else { MessagesController.getInstance().markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, minMessageId, 0, maxDate, wasUnread, false); } first = false; } if (progressView != null) { progressView.setVisibility(View.GONE); } } } else if (id == NotificationCenter.emojiDidLoaded) { if (chatListView != null) { chatListView.invalidateViews(); } } else if (id == NotificationCenter.updateInterfaces) { int updateMask = (Integer) args[0]; if ((updateMask & MessagesController.UPDATE_MASK_NAME) != 0 || (updateMask & MessagesController.UPDATE_MASK_STATUS) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_NAME) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_MEMBERS) != 0) { //TODO non aggiorno niente se sto effettuando una ricerca if (!searching) { updateSubtitle(); updateOnlineCount(); } } if ((updateMask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_AVATAR) != 0 || (updateMask & MessagesController.UPDATE_MASK_NAME) != 0) { checkAndUpdateAvatar(); updateVisibleRows(); } if ((updateMask & MessagesController.UPDATE_MASK_USER_PRINT) != 0) { CharSequence printString = MessagesController.getInstance().printingStrings.get(dialog_id); if (lastPrintString != null && printString == null || lastPrintString == null && printString != null || lastPrintString != null && printString != null && !lastPrintString.equals(printString)) { //TODO non aggiorno niente se sto effettuando una ricerca if (!searching) { updateSubtitle(); } } } if ((updateMask & MessagesController.UPDATE_MASK_USER_PHONE) != 0) { updateContactStatus(); } } else if (id == NotificationCenter.didReceivedNewMessages) { long did = (Long) args[0]; if (did == dialog_id) { boolean updateChat = false; boolean hasFromMe = false; ArrayList<MessageObject> arr = (ArrayList<MessageObject>) args[1]; if (currentEncryptedChat != null && arr.size() == 1) { MessageObject obj = arr.get(0); if (currentEncryptedChat != null && obj.isOut() && obj.messageOwner.action != null && obj.messageOwner.action instanceof TLRPC.TL_messageEncryptedAction && obj.messageOwner.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL && getParentActivity() != null) { TLRPC.TL_decryptedMessageActionSetMessageTTL action = (TLRPC.TL_decryptedMessageActionSetMessageTTL) obj.messageOwner.action.encryptedAction; if (AndroidUtilities.getPeerLayerVersion(currentEncryptedChat.layer) < 17 && currentEncryptedChat.ttl > 0 && currentEncryptedChat.ttl <= 60) { AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); builder.setTitle(LocaleController.getString("AppName", R.string.AppName)); builder.setPositiveButton(R.string.OK, null); builder.setMessage(LocaleController.formatString("CompatibilityChat", R.string.CompatibilityChat, currentUser.first_name, currentUser.first_name)); showAlertDialog(builder); } } } if (!unread_end_reached) { int currentMaxDate = Integer.MIN_VALUE; int currentMinMsgId = Integer.MIN_VALUE; if (currentEncryptedChat != null) { currentMinMsgId = Integer.MAX_VALUE; } boolean currentMarkAsRead = false; for (MessageObject obj : arr) { if (currentEncryptedChat != null && obj.messageOwner.action != null && obj.messageOwner.action instanceof TLRPC.TL_messageEncryptedAction && obj.messageOwner.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL && timerButton != null) { TLRPC.TL_decryptedMessageActionSetMessageTTL action = (TLRPC.TL_decryptedMessageActionSetMessageTTL) obj.messageOwner.action.encryptedAction; timerButton.setTime(action.ttl_seconds); } if (obj.isOut() && obj.isSending()) { scrollToLastMessage(); return; } if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } currentMaxDate = Math.max(currentMaxDate, obj.messageOwner.date); if (obj.messageOwner.id > 0) { currentMinMsgId = Math.max(obj.messageOwner.id, currentMinMsgId); } else if (currentEncryptedChat != null) { currentMinMsgId = Math.min(obj.messageOwner.id, currentMinMsgId); } if (!obj.isOut() && obj.isUnread()) { unread_to_load++; currentMarkAsRead = true; } if (obj.type == 10 || obj.type == 11) { updateChat = true; } } if (currentMarkAsRead) { if (paused) { readWhenResume = true; readWithDate = currentMaxDate; readWithMid = currentMinMsgId; } else { if (messages.size() > 0) { MessagesController.getInstance().markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, currentMinMsgId, 0, currentMaxDate, true, false); } } } updateVisibleRows(); } else { boolean markAsRead = false; int oldCount = messages.size(); for (MessageObject obj : arr) { if (currentEncryptedChat != null && obj.messageOwner.action != null && obj.messageOwner.action instanceof TLRPC.TL_messageEncryptedAction && obj.messageOwner.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL && timerButton != null) { TLRPC.TL_decryptedMessageActionSetMessageTTL action = (TLRPC.TL_decryptedMessageActionSetMessageTTL) obj.messageOwner.action.encryptedAction; timerButton.setTime(action.ttl_seconds); } if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } if (minDate == 0 || obj.messageOwner.date < minDate) { minDate = obj.messageOwner.date; } if (obj.isOut()) { removeUnreadPlane(false); hasFromMe = true; } if (!obj.isOut() && unreadMessageObject != null) { unread_to_load++; } if (obj.messageOwner.id > 0) { maxMessageId = Math.min(obj.messageOwner.id, maxMessageId); minMessageId = Math.max(obj.messageOwner.id, minMessageId); } else if (currentEncryptedChat != null) { maxMessageId = Math.max(obj.messageOwner.id, maxMessageId); minMessageId = Math.min(obj.messageOwner.id, minMessageId); } maxDate = Math.max(maxDate, obj.messageOwner.date); messagesDict.put(obj.messageOwner.id, obj); ArrayList<MessageObject> dayArray = messagesByDays.get(obj.dateKey); if (dayArray == null) { dayArray = new ArrayList<MessageObject>(); messagesByDays.put(obj.dateKey, dayArray); TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = LocaleController.formatDateChat(obj.messageOwner.date); dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.type = 10; dateObj.contentType = 4; messages.add(0, dateObj); } if (!obj.isOut() && obj.isUnread()) { if (!paused) { obj.setIsRead(); } markAsRead = true; } dayArray.add(0, obj); messages.add(0, obj); if (obj.type == 10 || obj.type == 11) { updateChat = true; } } if (progressView != null) { progressView.setVisibility(View.GONE); } if (chatAdapter != null) { chatAdapter.notifyDataSetChanged(); } else { scrollToTopOnResume = true; } if (chatListView != null && chatAdapter != null) { int lastVisible = chatListView.getLastVisiblePosition(); if (endReached) { lastVisible++; } if (lastVisible == oldCount || hasFromMe) { if (!firstLoading) { if (paused) { scrollToTopOnResume = true; } else { chatListView.post(new Runnable() { @Override public void run() { chatListView.setSelectionFromTop(messages.size() - 1, -100000 - chatListView.getPaddingTop()); } }); } } } else { showPagedownButton(true, true); } } else { scrollToTopOnResume = true; } if (markAsRead) { if (paused) { readWhenResume = true; readWithDate = maxDate; readWithMid = minMessageId; } else { MessagesController.getInstance().markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, minMessageId, 0, maxDate, true, false); } } } //TODO non aggiorno niente se sto effettuando una ricerca if (updateChat && !searching) { updateSubtitle(); checkAndUpdateAvatar(); } } } else if (id == NotificationCenter.closeChats) { if (args != null && args.length > 0) { long did = (Long) args[0]; if (did == dialog_id) { finishFragment(); } } else { removeSelfFromStack(); } } else if (id == NotificationCenter.messagesRead) { ArrayList<Integer> markAsReadMessages = (ArrayList<Integer>) args[0]; boolean updated = false; for (Integer ids : markAsReadMessages) { MessageObject obj = messagesDict.get(ids); if (obj != null) { obj.setIsRead(); updated = true; } } if (updated) { updateVisibleRows(); } } else if (id == NotificationCenter.messagesDeleted) { ArrayList<Integer> markAsDeletedMessages = (ArrayList<Integer>) args[0]; boolean updated = false; for (Integer ids : markAsDeletedMessages) { MessageObject obj = messagesDict.get(ids); if (obj != null) { int index = messages.indexOf(obj); if (index != -1) { messages.remove(index); messagesDict.remove(ids); ArrayList<MessageObject> dayArr = messagesByDays.get(obj.dateKey); dayArr.remove(obj); if (dayArr.isEmpty()) { messagesByDays.remove(obj.dateKey); messages.remove(index); } updated = true; } } } if (messages.isEmpty()) { if (!endReached && !loading) { progressView.setVisibility(View.GONE); chatListView.setEmptyView(null); if (currentEncryptedChat == null) { maxMessageId = Integer.MAX_VALUE; minMessageId = Integer.MIN_VALUE; } else { maxMessageId = Integer.MIN_VALUE; minMessageId = Integer.MAX_VALUE; } maxDate = Integer.MIN_VALUE; minDate = 0; MessagesController.getInstance().loadMessages(dialog_id, 30, 0, !cacheEndReaced, minDate, classGuid, false, false, null); loading = true; } } if (updated && chatAdapter != null) { removeUnreadPlane(false); chatAdapter.notifyDataSetChanged(); } } else if (id == NotificationCenter.messageReceivedByServer) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { Integer newMsgId = (Integer) args[1]; TLRPC.Message newMsgObj = (TLRPC.Message) args[2]; if (newMsgObj != null) { obj.messageOwner.media = newMsgObj.media; obj.generateThumbs(true, 1); } messagesDict.remove(msgId); messagesDict.put(newMsgId, obj); obj.messageOwner.id = newMsgId; obj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT; updateVisibleRows(); } } else if (id == NotificationCenter.messageReceivedByAck) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { obj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT; updateVisibleRows(); } } else if (id == NotificationCenter.messageSendError) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { obj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR; updateVisibleRows(); } } else if (id == NotificationCenter.chatInfoDidLoaded) { int chatId = (Integer) args[0]; if (currentChat != null && chatId == currentChat.id) { info = (TLRPC.ChatParticipants) args[1]; updateOnlineCount(); if (isBroadcast) { SendMessagesHelper.getInstance().setCurrentChatInfo(info); } } } else if (id == NotificationCenter.contactsDidLoaded) { updateContactStatus(); updateSubtitle(); } else if (id == NotificationCenter.encryptedChatUpdated) { TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat) args[0]; if (currentEncryptedChat != null && chat.id == currentEncryptedChat.id) { currentEncryptedChat = chat; updateContactStatus(); updateSecretStatus(); } } else if (id == NotificationCenter.messagesReadedEncrypted) { int encId = (Integer) args[0]; if (currentEncryptedChat != null && currentEncryptedChat.id == encId) { int date = (Integer) args[1]; boolean started = false; for (MessageObject obj : messages) { if (!obj.isOut()) { continue; } else if (obj.isOut() && !obj.isUnread()) { break; } if (obj.messageOwner.date <= date) { obj.setIsRead(); } } updateVisibleRows(); } } else if (id == NotificationCenter.audioDidReset) { Integer mid = (Integer) args[0]; if (chatListView != null) { int count = chatListView.getChildCount(); for (int a = 0; a < count; a++) { View view = chatListView.getChildAt(a); if (view instanceof ChatAudioCell) { ChatAudioCell cell = (ChatAudioCell) view; if (cell.getMessageObject() != null && cell.getMessageObject().messageOwner.id == mid) { cell.updateButtonState(); break; } } } } } else if (id == NotificationCenter.audioProgressDidChanged) { Integer mid = (Integer) args[0]; if (chatListView != null) { int count = chatListView.getChildCount(); for (int a = 0; a < count; a++) { View view = chatListView.getChildAt(a); if (view instanceof ChatAudioCell) { ChatAudioCell cell = (ChatAudioCell) view; if (cell.getMessageObject() != null && cell.getMessageObject().messageOwner.id == mid) { cell.updateProgress(); break; } } } } } else if (id == NotificationCenter.removeAllMessagesFromDialog) { long did = (Long) args[0]; if (dialog_id == did) { messages.clear(); messagesByDays.clear(); messagesDict.clear(); progressView.setVisibility(View.GONE); chatListView.setEmptyView(emptyViewContainer); if (currentEncryptedChat == null) { maxMessageId = Integer.MAX_VALUE; minMessageId = Integer.MIN_VALUE; } else { maxMessageId = Integer.MIN_VALUE; minMessageId = Integer.MAX_VALUE; } maxDate = Integer.MIN_VALUE; minDate = 0; selectedMessagesIds.clear(); selectedMessagesCanCopyIds.clear(); actionBarLayer.hideActionMode(); chatAdapter.notifyDataSetChanged(); } } else if (id == NotificationCenter.screenshotTook) { updateInformationForScreenshotDetector(); } else if (id == NotificationCenter.blockedUsersDidLoaded) { if (currentUser != null) { boolean oldValue = userBlocked; userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id); if (oldValue != userBlocked) { updateBottomOverlay(); } } } else if (id == NotificationCenter.FileNewChunkAvailable) { MessageObject messageObject = (MessageObject) args[0]; long finalSize = (Long) args[2]; if (finalSize != 0 && dialog_id == messageObject.getDialogId()) { MessageObject currentObject = messagesDict.get(messageObject.messageOwner.id); if (currentObject != null) { currentObject.messageOwner.media.video.size = (int) finalSize; updateVisibleRows(); } } } else if (id == NotificationCenter.didCreatedNewDeleteTask) { SparseArray<ArrayList<Integer>> mids = (SparseArray<ArrayList<Integer>>) args[0]; boolean changed = false; for (int i = 0; i < mids.size(); i++) { int key = mids.keyAt(i); ArrayList<Integer> arr = mids.get(key); for (Integer mid : arr) { MessageObject messageObject = messagesDict.get(mid); if (messageObject != null) { messageObject.messageOwner.destroyTime = key; changed = true; } } } if (changed) { updateVisibleRows(); } } else if (id == NotificationCenter.audioDidStarted) { MessageObject messageObject = (MessageObject) args[0]; sendSecretMessageRead(messageObject); } }
From source file:org.telegram.ui.ChatActivity.java
@SuppressWarnings("unchecked") @Override//from w ww . j a v a2 s . co m public void didReceivedNotification(int id, final Object... args) { if (id == MessagesController.messagesDidLoaded) { long did = (Long) args[0]; if (did == dialog_id) { int offset = (Integer) args[1]; int count = (Integer) args[2]; boolean isCache = (Boolean) args[4]; int fnid = (Integer) args[5]; int last_unread_date = (Integer) args[8]; boolean forwardLoad = (Boolean) args[9]; boolean wasUnread = false; boolean positionToUnread = false; if (fnid != 0) { first_unread_id = (Integer) args[5]; last_unread_id = (Integer) args[6]; unread_to_load = (Integer) args[7]; positionToUnread = true; } ArrayList<MessageObject> messArr = (ArrayList<MessageObject>) args[3]; int newRowsCount = 0; unread_end_reached = last_unread_id == 0; for (int a = 0; a < messArr.size(); a++) { MessageObject obj = messArr.get(a); if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } if (obj.messageOwner.id > 0) { maxMessageId = Math.min(obj.messageOwner.id, maxMessageId); minMessageId = Math.max(obj.messageOwner.id, minMessageId); } else if (currentEncryptedChat != null) { minMessageId = Math.min(obj.messageOwner.id, minMessageId); } maxDate = Math.max(maxDate, obj.messageOwner.date); if (minDate == 0 || obj.messageOwner.date < minDate) { minDate = obj.messageOwner.date; } if (!obj.messageOwner.out && obj.messageOwner.unread) { wasUnread = true; } messagesDict.put(obj.messageOwner.id, obj); ArrayList<MessageObject> dayArray = messagesByDays.get(obj.dateKey); if (dayArray == null) { dayArray = new ArrayList<MessageObject>(); messagesByDays.put(obj.dateKey, dayArray); TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = Utilities.formatDateChat(obj.messageOwner.date); dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.type = 10; if (forwardLoad) { messages.add(0, dateObj); } else { messages.add(dateObj); } newRowsCount++; } newRowsCount++; dayArray.add(obj); if (forwardLoad) { messages.add(0, obj); } else { messages.add(messages.size() - 1, obj); } if (!forwardLoad) { if (obj.messageOwner.id == first_unread_id) { TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = ""; dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.type = 15; boolean dateAdded = true; if (a != messArr.size() - 1) { MessageObject next = messArr.get(a + 1); dateAdded = !next.dateKey.equals(obj.dateKey); } messages.add(messages.size() - (dateAdded ? 0 : 1), dateObj); unreadMessageObject = dateObj; newRowsCount++; } if (obj.messageOwner.id == last_unread_id) { unread_end_reached = true; } } } if (unread_end_reached) { first_unread_id = 0; last_unread_id = 0; } if (forwardLoad) { if (messArr.size() != count) { unread_end_reached = true; first_unread_id = 0; last_unread_id = 0; } chatAdapter.notifyDataSetChanged(); loadingForward = false; } else { if (messArr.size() != count) { if (isCache) { cacheEndReaced = true; if (currentEncryptedChat != null) { endReached = true; } } else { cacheEndReaced = true; endReached = true; } } loading = false; if (chatListView != null) { if (first || scrollToTopOnResume) { chatAdapter.notifyDataSetChanged(); if (positionToUnread && unreadMessageObject != null) { if (messages.get(messages.size() - 1) == unreadMessageObject) { chatListView.setSelectionFromTop(0, Utilities.dp(-11)); } else { chatListView.setSelectionFromTop( messages.size() - messages.indexOf(unreadMessageObject), Utilities.dp(-11)); } ViewTreeObserver obs = chatListView.getViewTreeObserver(); obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (messages.get(messages.size() - 1) == unreadMessageObject) { chatListView.setSelectionFromTop(0, Utilities.dp(-11)); } else { chatListView.setSelectionFromTop( messages.size() - messages.indexOf(unreadMessageObject), Utilities.dp(-11)); } chatListView.getViewTreeObserver().removeOnPreDrawListener(this); return false; } }); chatListView.invalidate(); showPagedownButton(true, true); } else { chatListView.post(new Runnable() { @Override public void run() { chatListView.setSelectionFromTop(messages.size() - 1, -10000 - chatListView.getPaddingTop()); } }); } } else { int firstVisPos = chatListView.getLastVisiblePosition(); View firstVisView = chatListView.getChildAt(chatListView.getChildCount() - 1); int top = ((firstVisView == null) ? 0 : firstVisView.getTop()) - chatListView.getPaddingTop(); chatAdapter.notifyDataSetChanged(); chatListView.setSelectionFromTop(firstVisPos + newRowsCount, top); } if (paused) { scrollToTopOnResume = true; if (positionToUnread && unreadMessageObject != null) { scrollToTopUnReadOnResume = true; } } if (first) { if (chatListView.getEmptyView() == null) { if (currentEncryptedChat == null) { chatListView.setEmptyView(emptyView); } else { chatListView.setEmptyView(secretChatPlaceholder); } } } } else { scrollToTopOnResume = true; if (positionToUnread && unreadMessageObject != null) { scrollToTopUnReadOnResume = true; } } } if (first && messages.size() > 0) { if (last_unread_id != 0) { MessagesController.Instance.markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, last_unread_id, 0, last_unread_date, wasUnread); } else { MessagesController.Instance.markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, minMessageId, 0, maxDate, wasUnread); } first = false; } if (progressView != null) { progressView.setVisibility(View.GONE); } } } else if (id == 999) { if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { chatListView.invalidateViews(); } } if (emojiView != null) { emojiView.invalidateViews(); } } else if (id == MessagesController.updateInterfaces) { int updateMask = (Integer) args[0]; if ((updateMask & MessagesController.UPDATE_MASK_NAME) != 0 || (updateMask & MessagesController.UPDATE_MASK_STATUS) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_NAME) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_MEMBERS) != 0) { updateSubtitle(); updateOnlineCount(); } if ((updateMask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (updateMask & MessagesController.UPDATE_MASK_CHAT_AVATAR) != 0 || (updateMask & MessagesController.UPDATE_MASK_NAME) != 0) { checkAndUpdateAvatar(); if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } } if ((updateMask & MessagesController.UPDATE_MASK_USER_PRINT) != 0) { CharSequence printString = MessagesController.Instance.printingStrings.get(dialog_id); if (lastPrintString != null && printString == null || lastPrintString == null && printString != null || lastPrintString != null && printString != null && !lastPrintString.equals(printString)) { updateSubtitle(); } } if ((updateMask & MessagesController.UPDATE_MASK_USER_PHONE) != 0) { updateContactStatus(); } } else if (id == MessagesController.didReceivedNewMessages) { long did = (Long) args[0]; if (did == dialog_id) { boolean updateChat = false; ArrayList<MessageObject> arr = (ArrayList<MessageObject>) args[1]; if (!unread_end_reached) { int currentMaxDate = Integer.MIN_VALUE; int currentMinMsgId = Integer.MIN_VALUE; if (currentEncryptedChat != null) { currentMinMsgId = Integer.MAX_VALUE; } boolean currentMarkAsRead = false; for (MessageObject obj : arr) { if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } currentMaxDate = Math.max(currentMaxDate, obj.messageOwner.date); if (obj.messageOwner.id > 0) { currentMinMsgId = Math.max(obj.messageOwner.id, currentMinMsgId); } else if (currentEncryptedChat != null) { currentMinMsgId = Math.min(obj.messageOwner.id, currentMinMsgId); } if (!obj.messageOwner.out && obj.messageOwner.unread) { unread_to_load++; currentMarkAsRead = true; } if (obj.type == 10 || obj.type == 11) { updateChat = true; } } if (currentMarkAsRead) { if (paused) { readWhenResume = true; readWithDate = currentMaxDate; readWithMid = currentMinMsgId; } else { if (messages.size() > 0) { MessagesController.Instance.markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, currentMinMsgId, 0, currentMaxDate, true); } } } updateVisibleRows(); } else { boolean markAsRead = false; int oldCount = messages.size(); for (MessageObject obj : arr) { if (messagesDict.containsKey(obj.messageOwner.id)) { continue; } if (minDate == 0 || obj.messageOwner.date < minDate) { minDate = obj.messageOwner.date; } if (obj.messageOwner.attachPath != null && obj.messageOwner.attachPath.length() != 0) { progressBarMap.put(obj.messageOwner.attachPath, null); } if (obj.messageOwner.out) { removeUnreadPlane(false); } if (!obj.messageOwner.out && unreadMessageObject != null) { unread_to_load++; } if (obj.messageOwner.id > 0) { maxMessageId = Math.min(obj.messageOwner.id, maxMessageId); minMessageId = Math.max(obj.messageOwner.id, minMessageId); } else if (currentEncryptedChat != null) { minMessageId = Math.min(obj.messageOwner.id, minMessageId); } maxDate = Math.max(maxDate, obj.messageOwner.date); messagesDict.put(obj.messageOwner.id, obj); ArrayList<MessageObject> dayArray = messagesByDays.get(obj.dateKey); if (dayArray == null) { dayArray = new ArrayList<MessageObject>(); messagesByDays.put(obj.dateKey, dayArray); TLRPC.Message dateMsg = new TLRPC.Message(); dateMsg.message = Utilities.formatDateChat(obj.messageOwner.date); dateMsg.id = 0; MessageObject dateObj = new MessageObject(dateMsg, null); dateObj.type = 10; messages.add(0, dateObj); } if (!obj.messageOwner.out && obj.messageOwner.unread) { obj.messageOwner.unread = false; markAsRead = true; } dayArray.add(0, obj); messages.add(0, obj); if (obj.type == 10 || obj.type == 11) { updateChat = true; } } if (progressView != null) { progressView.setVisibility(View.GONE); } if (chatAdapter != null) { chatAdapter.notifyDataSetChanged(); } else { scrollToTopOnResume = true; } if (chatListView != null && chatAdapter != null) { int lastVisible = chatListView.getLastVisiblePosition(); if (endReached) { lastVisible++; } if (lastVisible == oldCount) { if (paused) { scrollToTopOnResume = true; } else { chatListView.post(new Runnable() { @Override public void run() { chatListView.setSelectionFromTop(messages.size() - 1, -10000 - chatListView.getPaddingTop()); } }); } } else { showPagedownButton(true, true); } } else { scrollToTopOnResume = true; } if (markAsRead) { if (paused) { readWhenResume = true; readWithDate = maxDate; readWithMid = minMessageId; } else { MessagesController.Instance.markDialogAsRead(dialog_id, messages.get(0).messageOwner.id, minMessageId, 0, maxDate, true); } } } if (updateChat) { updateSubtitle(); checkAndUpdateAvatar(); } } } else if (id == MessagesController.closeChats) { if (messsageEditText != null && messsageEditText.isFocused()) { Utilities.hideKeyboard(messsageEditText); } removeSelfFromStack(); } else if (id == MessagesController.messagesReaded) { ArrayList<Integer> markAsReadMessages = (ArrayList<Integer>) args[0]; boolean updated = false; for (Integer ids : markAsReadMessages) { MessageObject obj = messagesDict.get(ids); if (obj != null) { obj.messageOwner.unread = false; updated = true; } } if (updated) { if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } } } else if (id == MessagesController.messagesDeleted) { ArrayList<Integer> markAsDeletedMessages = (ArrayList<Integer>) args[0]; boolean updated = false; for (Integer ids : markAsDeletedMessages) { MessageObject obj = messagesDict.get(ids); if (obj != null) { int index = messages.indexOf(obj); if (index != -1) { messages.remove(index); messagesDict.remove(ids); ArrayList<MessageObject> dayArr = messagesByDays.get(obj.dateKey); dayArr.remove(obj); if (dayArr.isEmpty()) { messagesByDays.remove(obj.dateKey); if (index != -1) { messages.remove(index); } } updated = true; } } } if (messages.isEmpty()) { if (!endReached && !loading) { progressView.setVisibility(View.VISIBLE); chatListView.setEmptyView(null); maxMessageId = Integer.MAX_VALUE; minMessageId = Integer.MIN_VALUE; maxDate = Integer.MIN_VALUE; minDate = 0; MessagesController.Instance.loadMessages(dialog_id, 0, 30, 0, !cacheEndReaced, minDate, classGuid, false, false); loading = true; } } if (updated && chatAdapter != null) { removeUnreadPlane(false); chatAdapter.notifyDataSetChanged(); } } else if (id == MessagesController.messageReceivedByServer) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { Integer newMsgId = (Integer) args[1]; messagesDict.remove(msgId); messagesDict.put(newMsgId, obj); obj.messageOwner.id = newMsgId; obj.messageOwner.send_state = MessagesController.MESSAGE_SEND_STATE_SENT; if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } if (obj.messageOwner.attachPath != null && obj.messageOwner.attachPath.length() != 0) { progressBarMap.remove(obj.messageOwner.attachPath); } } } else if (id == MessagesController.messageReceivedByAck) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { if (obj.messageOwner.attachPath != null && obj.messageOwner.attachPath.length() != 0) { progressBarMap.remove(obj.messageOwner.attachPath); } obj.messageOwner.send_state = MessagesController.MESSAGE_SEND_STATE_SENT; if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } } } else if (id == MessagesController.messageSendError) { Integer msgId = (Integer) args[0]; MessageObject obj = messagesDict.get(msgId); if (obj != null) { obj.messageOwner.send_state = MessagesController.MESSAGE_SEND_STATE_SEND_ERROR; if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } if (obj.messageOwner.attachPath != null && obj.messageOwner.attachPath.length() != 0) { progressBarMap.remove(obj.messageOwner.attachPath); } } } else if (id == 997) { MessagesController.Instance.sendMessage((Double) args[0], (Double) args[1], dialog_id); if (chatListView != null) { chatListView.setSelection(messages.size() + 1); scrollToTopOnResume = true; } } else if (id == MessagesController.chatInfoDidLoaded) { int chatId = (Integer) args[0]; if (currentChat != null && chatId == currentChat.id) { info = (TLRPC.ChatParticipants) args[1]; updateOnlineCount(); } } else if (id == FileLoader.FileUploadProgressChanged) { String location = (String) args[0]; ProgressBar bar; if ((bar = progressBarMap.get(location)) != null) { Float progress = (Float) args[1]; bar.setProgress((int) (progress * 100)); } } else if (id == FileLoader.FileDidFailedLoad) { String location = (String) args[0]; if (loadingFile.containsKey(location)) { loadingFile.remove(location); if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } } } else if (id == FileLoader.FileDidLoaded) { String location = (String) args[0]; if (loadingFile.containsKey(location)) { loadingFile.remove(location); if (animationInProgress) { invalidateAfterAnimation = true; } else { if (chatListView != null) { updateVisibleRows(); } } } } else if (id == FileLoader.FileLoadProgressChanged) { String location = (String) args[0]; ArrayList<ProgressBar> arr = loadingFile.get(location); if (arr != null) { Float progress = (Float) args[1]; for (ProgressBar bar : arr) { bar.setProgress((int) (progress * 100)); } } } else if (id == MessagesController.contactsDidLoaded) { updateContactStatus(); updateSubtitle(); } else if (id == MessagesController.encryptedChatUpdated) { TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat) args[0]; if (currentEncryptedChat != null && chat.id == currentEncryptedChat.id) { currentEncryptedChat = chat; updateContactStatus(); updateSecretStatus(); } } else if (id == MessagesController.messagesReadedEncrypted) { int encId = (Integer) args[0]; if (currentEncryptedChat != null && currentEncryptedChat.id == encId) { int date = (Integer) args[1]; boolean started = false; for (MessageObject obj : messages) { if (!obj.messageOwner.out) { continue; } else if (obj.messageOwner.out && !obj.messageOwner.unread) { break; } if (obj.messageOwner.date <= date) { obj.messageOwner.unread = false; } } if (chatListView != null) { updateVisibleRows(); } } } }