Search in sources :

Example 71 with TLObject

use of org.telegram.tgnet.TLObject in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method performSendDelayedMessage.

private void performSendDelayedMessage(final DelayedMessage message, int index) {
    if (message.type == 0) {
        if (message.httpLocation != null) {
            putToDelayedMessages(message.httpLocation, message);
            ImageLoader.getInstance().loadHttpFile(message.httpLocation, "file", currentAccount);
        } else {
            if (message.sendRequest != null) {
                String location = FileLoader.getPathToAttach(message.photoSize).toString();
                putToDelayedMessages(location, message);
                getFileLoader().uploadFile(location, false, true, ConnectionsManager.FileTypePhoto);
                putToUploadingMessages(message.obj);
            } else {
                String location = FileLoader.getPathToAttach(message.photoSize).toString();
                if (message.sendEncryptedRequest != null && message.photoSize.location.dc_id != 0) {
                    File file = new File(location);
                    if (!file.exists()) {
                        location = FileLoader.getPathToAttach(message.photoSize, true).toString();
                        file = new File(location);
                    }
                    if (!file.exists()) {
                        putToDelayedMessages(FileLoader.getAttachFileName(message.photoSize), message);
                        getFileLoader().loadFile(ImageLocation.getForObject(message.photoSize, message.locationParent), message.parentObject, "jpg", 2, 0);
                        return;
                    }
                }
                putToDelayedMessages(location, message);
                getFileLoader().uploadFile(location, true, true, ConnectionsManager.FileTypePhoto);
                putToUploadingMessages(message.obj);
            }
        }
    } else if (message.type == 1) {
        if (message.videoEditedInfo != null && message.videoEditedInfo.needConvert()) {
            String location = message.obj.messageOwner.attachPath;
            TLRPC.Document document = message.obj.getDocument();
            if (location == null) {
                location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + document.id + ".mp4";
            }
            putToDelayedMessages(location, message);
            MediaController.getInstance().scheduleVideoConvert(message.obj);
            putToUploadingMessages(message.obj);
        } else {
            if (message.videoEditedInfo != null) {
                if (message.videoEditedInfo.file != null) {
                    TLRPC.InputMedia media;
                    if (message.sendRequest instanceof TLRPC.TL_messages_sendMedia) {
                        media = ((TLRPC.TL_messages_sendMedia) message.sendRequest).media;
                    } else {
                        media = ((TLRPC.TL_messages_editMessage) message.sendRequest).media;
                    }
                    media.file = message.videoEditedInfo.file;
                    message.videoEditedInfo.file = null;
                } else if (message.videoEditedInfo.encryptedFile != null) {
                    TLRPC.TL_decryptedMessage decryptedMessage = (TLRPC.TL_decryptedMessage) message.sendEncryptedRequest;
                    decryptedMessage.media.size = (int) message.videoEditedInfo.estimatedSize;
                    decryptedMessage.media.key = message.videoEditedInfo.key;
                    decryptedMessage.media.iv = message.videoEditedInfo.iv;
                    getSecretChatHelper().performSendEncryptedRequest(decryptedMessage, message.obj.messageOwner, message.encryptedChat, message.videoEditedInfo.encryptedFile, message.originalPath, message.obj);
                    message.videoEditedInfo.encryptedFile = null;
                    return;
                }
            }
            if (message.sendRequest != null) {
                TLRPC.InputMedia media;
                if (message.sendRequest instanceof TLRPC.TL_messages_sendMedia) {
                    media = ((TLRPC.TL_messages_sendMedia) message.sendRequest).media;
                } else {
                    media = ((TLRPC.TL_messages_editMessage) message.sendRequest).media;
                }
                if (media.file == null) {
                    String location = message.obj.messageOwner.attachPath;
                    TLRPC.Document document = message.obj.getDocument();
                    if (location == null) {
                        location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + document.id + ".mp4";
                    }
                    putToDelayedMessages(location, message);
                    if (message.obj.videoEditedInfo != null && message.obj.videoEditedInfo.needConvert()) {
                        getFileLoader().uploadFile(location, false, false, document.size, ConnectionsManager.FileTypeVideo, false);
                    } else {
                        getFileLoader().uploadFile(location, false, false, ConnectionsManager.FileTypeVideo);
                    }
                    putToUploadingMessages(message.obj);
                } else {
                    String location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + message.photoSize.location.volume_id + "_" + message.photoSize.location.local_id + ".jpg";
                    putToDelayedMessages(location, message);
                    getFileLoader().uploadFile(location, false, true, ConnectionsManager.FileTypePhoto);
                    putToUploadingMessages(message.obj);
                }
            } else {
                String location = message.obj.messageOwner.attachPath;
                TLRPC.Document document = message.obj.getDocument();
                if (location == null) {
                    location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + document.id + ".mp4";
                }
                if (message.sendEncryptedRequest != null && document.dc_id != 0) {
                    File file = new File(location);
                    if (!file.exists()) {
                        putToDelayedMessages(FileLoader.getAttachFileName(document), message);
                        getFileLoader().loadFile(document, message.parentObject, 2, 0);
                        return;
                    }
                }
                putToDelayedMessages(location, message);
                if (message.obj.videoEditedInfo != null && message.obj.videoEditedInfo.needConvert()) {
                    getFileLoader().uploadFile(location, true, false, document.size, ConnectionsManager.FileTypeVideo, false);
                } else {
                    getFileLoader().uploadFile(location, true, false, ConnectionsManager.FileTypeVideo);
                }
                putToUploadingMessages(message.obj);
            }
        }
    } else if (message.type == 2) {
        if (message.httpLocation != null) {
            putToDelayedMessages(message.httpLocation, message);
            ImageLoader.getInstance().loadHttpFile(message.httpLocation, "gif", currentAccount);
        } else {
            if (message.sendRequest != null) {
                TLRPC.InputMedia media;
                if (message.sendRequest instanceof TLRPC.TL_messages_sendMedia) {
                    media = ((TLRPC.TL_messages_sendMedia) message.sendRequest).media;
                } else {
                    media = ((TLRPC.TL_messages_editMessage) message.sendRequest).media;
                }
                if (media.file == null) {
                    String location = message.obj.messageOwner.attachPath;
                    putToDelayedMessages(location, message);
                    getFileLoader().uploadFile(location, message.sendRequest == null, false, ConnectionsManager.FileTypeFile);
                    putToUploadingMessages(message.obj);
                } else if (media.thumb == null && message.photoSize != null && !(message.photoSize instanceof TLRPC.TL_photoStrippedSize)) {
                    String location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + message.photoSize.location.volume_id + "_" + message.photoSize.location.local_id + ".jpg";
                    putToDelayedMessages(location, message);
                    getFileLoader().uploadFile(location, false, true, ConnectionsManager.FileTypePhoto);
                    putToUploadingMessages(message.obj);
                }
            } else {
                String location = message.obj.messageOwner.attachPath;
                TLRPC.Document document = message.obj.getDocument();
                if (message.sendEncryptedRequest != null && document.dc_id != 0) {
                    File file = new File(location);
                    if (!file.exists()) {
                        putToDelayedMessages(FileLoader.getAttachFileName(document), message);
                        getFileLoader().loadFile(document, message.parentObject, 2, 0);
                        return;
                    }
                }
                putToDelayedMessages(location, message);
                getFileLoader().uploadFile(location, true, false, ConnectionsManager.FileTypeFile);
                putToUploadingMessages(message.obj);
            }
        }
    } else if (message.type == 3) {
        String location = message.obj.messageOwner.attachPath;
        putToDelayedMessages(location, message);
        getFileLoader().uploadFile(location, message.sendRequest == null, true, ConnectionsManager.FileTypeAudio);
        putToUploadingMessages(message.obj);
    } else if (message.type == 4) {
        boolean add = index < 0;
        if (message.performMediaUpload) {
            if (index < 0) {
                index = message.messageObjects.size() - 1;
            }
            MessageObject messageObject = message.messageObjects.get(index);
            if (messageObject.getDocument() != null) {
                if (message.videoEditedInfo != null) {
                    String location = messageObject.messageOwner.attachPath;
                    TLRPC.Document document = messageObject.getDocument();
                    if (location == null) {
                        location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + document.id + ".mp4";
                    }
                    putToDelayedMessages(location, message);
                    message.extraHashMap.put(messageObject, location);
                    message.extraHashMap.put(location + "_i", messageObject);
                    if (message.photoSize != null && message.photoSize.location != null) {
                        message.extraHashMap.put(location + "_t", message.photoSize);
                    }
                    MediaController.getInstance().scheduleVideoConvert(messageObject);
                    message.obj = messageObject;
                    putToUploadingMessages(messageObject);
                } else {
                    TLRPC.Document document = messageObject.getDocument();
                    String documentLocation = messageObject.messageOwner.attachPath;
                    if (documentLocation == null) {
                        documentLocation = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + document.id + ".mp4";
                    }
                    if (message.sendRequest != null) {
                        TLRPC.TL_messages_sendMultiMedia request = (TLRPC.TL_messages_sendMultiMedia) message.sendRequest;
                        TLRPC.InputMedia media = request.multi_media.get(index).media;
                        if (media.file == null) {
                            putToDelayedMessages(documentLocation, message);
                            message.extraHashMap.put(messageObject, documentLocation);
                            message.extraHashMap.put(documentLocation, media);
                            message.extraHashMap.put(documentLocation + "_i", messageObject);
                            if (message.photoSize != null && message.photoSize.location != null) {
                                message.extraHashMap.put(documentLocation + "_t", message.photoSize);
                            }
                            if (messageObject.videoEditedInfo != null && messageObject.videoEditedInfo.needConvert()) {
                                getFileLoader().uploadFile(documentLocation, false, false, document.size, ConnectionsManager.FileTypeVideo, false);
                            } else {
                                getFileLoader().uploadFile(documentLocation, false, false, ConnectionsManager.FileTypeVideo);
                            }
                            putToUploadingMessages(messageObject);
                        } else if (message.photoSize != null) {
                            String location = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + message.photoSize.location.volume_id + "_" + message.photoSize.location.local_id + ".jpg";
                            putToDelayedMessages(location, message);
                            message.extraHashMap.put(location + "_o", documentLocation);
                            message.extraHashMap.put(messageObject, location);
                            message.extraHashMap.put(location, media);
                            getFileLoader().uploadFile(location, false, true, ConnectionsManager.FileTypePhoto);
                            putToUploadingMessages(messageObject);
                        }
                    } else {
                        TLRPC.TL_messages_sendEncryptedMultiMedia request = (TLRPC.TL_messages_sendEncryptedMultiMedia) message.sendEncryptedRequest;
                        putToDelayedMessages(documentLocation, message);
                        message.extraHashMap.put(messageObject, documentLocation);
                        message.extraHashMap.put(documentLocation, request.files.get(index));
                        message.extraHashMap.put(documentLocation + "_i", messageObject);
                        if (message.photoSize != null && message.photoSize.location != null) {
                            message.extraHashMap.put(documentLocation + "_t", message.photoSize);
                        }
                        if (messageObject.videoEditedInfo != null && messageObject.videoEditedInfo.needConvert()) {
                            getFileLoader().uploadFile(documentLocation, true, false, document.size, ConnectionsManager.FileTypeVideo, false);
                        } else {
                            getFileLoader().uploadFile(documentLocation, true, false, ConnectionsManager.FileTypeVideo);
                        }
                        putToUploadingMessages(messageObject);
                    }
                }
                message.videoEditedInfo = null;
                message.photoSize = null;
            } else {
                if (message.httpLocation != null) {
                    putToDelayedMessages(message.httpLocation, message);
                    message.extraHashMap.put(messageObject, message.httpLocation);
                    message.extraHashMap.put(message.httpLocation, messageObject);
                    ImageLoader.getInstance().loadHttpFile(message.httpLocation, "file", currentAccount);
                    message.httpLocation = null;
                } else {
                    TLObject inputMedia;
                    if (message.sendRequest != null) {
                        TLRPC.TL_messages_sendMultiMedia request = (TLRPC.TL_messages_sendMultiMedia) message.sendRequest;
                        inputMedia = request.multi_media.get(index).media;
                    } else {
                        TLRPC.TL_messages_sendEncryptedMultiMedia request = (TLRPC.TL_messages_sendEncryptedMultiMedia) message.sendEncryptedRequest;
                        inputMedia = request.files.get(index);
                    }
                    String location = FileLoader.getPathToAttach(message.photoSize).toString();
                    putToDelayedMessages(location, message);
                    message.extraHashMap.put(location, inputMedia);
                    message.extraHashMap.put(messageObject, location);
                    getFileLoader().uploadFile(location, message.sendEncryptedRequest != null, true, ConnectionsManager.FileTypePhoto);
                    putToUploadingMessages(messageObject);
                    message.photoSize = null;
                }
            }
            message.performMediaUpload = false;
        } else if (!message.messageObjects.isEmpty()) {
            putToSendingMessages(message.messageObjects.get(message.messageObjects.size() - 1).messageOwner, message.finalGroupMessage != 0);
        }
        sendReadyToSendGroup(message, add, true);
    } else if (message.type == 5) {
        String key = "stickerset_" + message.obj.getId();
        TLRPC.TL_messages_getStickerSet req = new TLRPC.TL_messages_getStickerSet();
        req.stickerset = (TLRPC.InputStickerSet) message.parentObject;
        getConnectionsManager().sendRequest(req, (response, error) -> {
            AndroidUtilities.runOnUIThread(() -> {
                boolean found = false;
                if (response != null) {
                    TLRPC.TL_messages_stickerSet set = (TLRPC.TL_messages_stickerSet) response;
                    getMediaDataController().storeTempStickerSet(set);
                    TLRPC.TL_documentAttributeSticker_layer55 attributeSticker = (TLRPC.TL_documentAttributeSticker_layer55) message.locationParent;
                    attributeSticker.stickerset = new TLRPC.TL_inputStickerSetShortName();
                    attributeSticker.stickerset.short_name = set.set.short_name;
                    found = true;
                }
                ArrayList<DelayedMessage> arrayList = delayedMessages.remove(key);
                if (arrayList != null && !arrayList.isEmpty()) {
                    if (found) {
                        getMessagesStorage().replaceMessageIfExists(arrayList.get(0).obj.messageOwner, null, null, false);
                    }
                    getSecretChatHelper().performSendEncryptedRequest((TLRPC.DecryptedMessage) message.sendEncryptedRequest, message.obj.messageOwner, message.encryptedChat, null, null, message.obj);
                }
            });
        });
        putToDelayedMessages(key, message);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) LinearLayout(android.widget.LinearLayout) SparseLongArray(org.telegram.messenger.support.SparseLongArray) Bundle(android.os.Bundle) Uri(android.net.Uri) ImageView(android.widget.ImageView) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) LocationListener(android.location.LocationListener) Bulletin(org.telegram.ui.Components.Bulletin) ByteBuffer(java.nio.ByteBuffer) AudioInfo(org.telegram.messenger.audioinfo.AudioInfo) JSONObject(org.json.JSONObject) MediaCodecInfo(android.media.MediaCodecInfo) Locale(java.util.Locale) MediaStore(android.provider.MediaStore) UiThread(androidx.annotation.UiThread) ChatMessageCell(org.telegram.ui.Cells.ChatMessageCell) BaseFragment(org.telegram.ui.ActionBar.BaseFragment) BitmapDrawable(android.graphics.drawable.BitmapDrawable) PorterDuff(android.graphics.PorterDuff) ConnectionsManager(org.telegram.tgnet.ConnectionsManager) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SparseArray(android.util.SparseArray) CountDownLatch(java.util.concurrent.CountDownLatch) TextView(android.widget.TextView) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Location(android.location.Location) LocationManager(android.location.LocationManager) ChatActivity(org.telegram.ui.ChatActivity) Context(android.content.Context) MediaMetadataRetriever(android.media.MediaMetadataRetriever) Theme(org.telegram.ui.ActionBar.Theme) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RequestDelegate(org.telegram.tgnet.RequestDelegate) TwoStepVerificationActivity(org.telegram.ui.TwoStepVerificationActivity) Intent(android.content.Intent) BitmapFactory(android.graphics.BitmapFactory) MediaPlayer(android.media.MediaPlayer) SystemClock(android.os.SystemClock) HashMap(java.util.HashMap) AlertsCreator(org.telegram.ui.Components.AlertsCreator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TLRPC(org.telegram.tgnet.TLRPC) Point(org.telegram.ui.Components.Point) Toast(android.widget.Toast) PaymentFormActivity(org.telegram.ui.PaymentFormActivity) TLObject(org.telegram.tgnet.TLObject) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) Build(android.os.Build) SerializedData(org.telegram.tgnet.SerializedData) LongSparseArray(androidx.collection.LongSparseArray) TwoStepVerificationSetupActivity(org.telegram.ui.TwoStepVerificationSetupActivity) FileOutputStream(java.io.FileOutputStream) TextUtils(android.text.TextUtils) InputContentInfoCompat(androidx.core.view.inputmethod.InputContentInfoCompat) LayoutHelper(org.telegram.ui.Components.LayoutHelper) FileInputStream(java.io.FileInputStream) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Gravity(android.view.Gravity) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) Base64(android.util.Base64) ClipDescription(android.content.ClipDescription) AlertDialog(org.telegram.ui.ActionBar.AlertDialog) FileChannel(java.nio.channels.FileChannel) MimeTypeMap(android.webkit.MimeTypeMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) TLRPC(org.telegram.tgnet.TLRPC) TLObject(org.telegram.tgnet.TLObject) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 72 with TLObject

use of org.telegram.tgnet.TLObject in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method performSendMessageRequest.

protected void performSendMessageRequest(final TLObject req, final MessageObject msgObj, final String originalPath, DelayedMessage parentMessage, boolean check, DelayedMessage delayedMessage, Object parentObject, HashMap<String, String> params, boolean scheduled) {
    if (!(req instanceof TLRPC.TL_messages_editMessage)) {
        if (check) {
            DelayedMessage maxDelayedMessage = findMaxDelayedMessageForMessageId(msgObj.getId(), msgObj.getDialogId());
            if (maxDelayedMessage != null) {
                maxDelayedMessage.addDelayedRequest(req, msgObj, originalPath, parentObject, delayedMessage, parentMessage != null ? parentMessage.scheduled : false);
                if (parentMessage != null && parentMessage.requests != null) {
                    maxDelayedMessage.requests.addAll(parentMessage.requests);
                }
                return;
            }
        }
    }
    final TLRPC.Message newMsgObj = msgObj.messageOwner;
    putToSendingMessages(newMsgObj, scheduled);
    newMsgObj.reqId = getConnectionsManager().sendRequest(req, (response, error) -> {
        if (error != null && (req instanceof TLRPC.TL_messages_sendMedia || req instanceof TLRPC.TL_messages_editMessage) && FileRefController.isFileRefError(error.text)) {
            if (parentObject != null) {
                getFileRefController().requestReference(parentObject, req, msgObj, originalPath, parentMessage, check, delayedMessage, scheduled);
                return;
            } else if (delayedMessage != null) {
                AndroidUtilities.runOnUIThread(() -> {
                    removeFromSendingMessages(newMsgObj.id, scheduled);
                    if (req instanceof TLRPC.TL_messages_sendMedia) {
                        TLRPC.TL_messages_sendMedia request = (TLRPC.TL_messages_sendMedia) req;
                        if (request.media instanceof TLRPC.TL_inputMediaPhoto) {
                            request.media = delayedMessage.inputUploadMedia;
                        } else if (request.media instanceof TLRPC.TL_inputMediaDocument) {
                            request.media = delayedMessage.inputUploadMedia;
                        }
                    } else if (req instanceof TLRPC.TL_messages_editMessage) {
                        TLRPC.TL_messages_editMessage request = (TLRPC.TL_messages_editMessage) req;
                        if (request.media instanceof TLRPC.TL_inputMediaPhoto) {
                            request.media = delayedMessage.inputUploadMedia;
                        } else if (request.media instanceof TLRPC.TL_inputMediaDocument) {
                            request.media = delayedMessage.inputUploadMedia;
                        }
                    }
                    delayedMessage.performMediaUpload = true;
                    performSendDelayedMessage(delayedMessage);
                });
                return;
            }
        }
        if (req instanceof TLRPC.TL_messages_editMessage) {
            AndroidUtilities.runOnUIThread(() -> {
                if (error == null) {
                    final String attachPath = newMsgObj.attachPath;
                    final TLRPC.Updates updates = (TLRPC.Updates) response;
                    ArrayList<TLRPC.Update> updatesArr = ((TLRPC.Updates) response).updates;
                    TLRPC.Message message = null;
                    for (int a = 0; a < updatesArr.size(); a++) {
                        TLRPC.Update update = updatesArr.get(a);
                        if (update instanceof TLRPC.TL_updateEditMessage) {
                            final TLRPC.TL_updateEditMessage newMessage = (TLRPC.TL_updateEditMessage) update;
                            message = newMessage.message;
                            break;
                        } else if (update instanceof TLRPC.TL_updateEditChannelMessage) {
                            final TLRPC.TL_updateEditChannelMessage newMessage = (TLRPC.TL_updateEditChannelMessage) update;
                            message = newMessage.message;
                            break;
                        } else if (update instanceof TLRPC.TL_updateNewScheduledMessage) {
                            final TLRPC.TL_updateNewScheduledMessage newMessage = (TLRPC.TL_updateNewScheduledMessage) update;
                            message = newMessage.message;
                            break;
                        }
                    }
                    if (message != null) {
                        ImageLoader.saveMessageThumbs(message);
                        updateMediaPaths(msgObj, message, message.id, originalPath, false);
                    }
                    Utilities.stageQueue.postRunnable(() -> {
                        getMessagesController().processUpdates(updates, false);
                        AndroidUtilities.runOnUIThread(() -> {
                            processSentMessage(newMsgObj.id);
                            removeFromSendingMessages(newMsgObj.id, scheduled);
                        });
                    });
                    if (MessageObject.isVideoMessage(newMsgObj) || MessageObject.isRoundVideoMessage(newMsgObj) || MessageObject.isNewGifMessage(newMsgObj)) {
                        stopVideoService(attachPath);
                    }
                } else {
                    AlertsCreator.processError(currentAccount, error, null, req);
                    if (MessageObject.isVideoMessage(newMsgObj) || MessageObject.isRoundVideoMessage(newMsgObj) || MessageObject.isNewGifMessage(newMsgObj)) {
                        stopVideoService(newMsgObj.attachPath);
                    }
                    removeFromSendingMessages(newMsgObj.id, scheduled);
                    revertEditingMessageObject(msgObj);
                }
            });
        } else {
            AndroidUtilities.runOnUIThread(() -> {
                boolean currentSchedule = scheduled;
                boolean isSentError = false;
                if (error == null) {
                    final int oldId = newMsgObj.id;
                    final ArrayList<TLRPC.Message> sentMessages = new ArrayList<>();
                    final String attachPath = newMsgObj.attachPath;
                    final int existFlags;
                    boolean scheduledOnline = newMsgObj.date == 0x7FFFFFFE;
                    if (response instanceof TLRPC.TL_updateShortSentMessage) {
                        final TLRPC.TL_updateShortSentMessage res = (TLRPC.TL_updateShortSentMessage) response;
                        updateMediaPaths(msgObj, null, res.id, null, false);
                        existFlags = msgObj.getMediaExistanceFlags();
                        newMsgObj.local_id = newMsgObj.id = res.id;
                        newMsgObj.date = res.date;
                        newMsgObj.entities = res.entities;
                        newMsgObj.out = res.out;
                        if ((res.flags & 33554432) != 0) {
                            newMsgObj.ttl_period = res.ttl_period;
                            newMsgObj.flags |= 33554432;
                        }
                        if (res.media != null) {
                            newMsgObj.media = res.media;
                            newMsgObj.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
                            ImageLoader.saveMessageThumbs(newMsgObj);
                        }
                        if ((res.media instanceof TLRPC.TL_messageMediaGame || res.media instanceof TLRPC.TL_messageMediaInvoice) && !TextUtils.isEmpty(res.message)) {
                            newMsgObj.message = res.message;
                        }
                        if (!newMsgObj.entities.isEmpty()) {
                            newMsgObj.flags |= TLRPC.MESSAGE_FLAG_HAS_ENTITIES;
                        }
                        currentSchedule = false;
                        if (!currentSchedule) {
                            Integer value = getMessagesController().dialogs_read_outbox_max.get(newMsgObj.dialog_id);
                            if (value == null) {
                                value = getMessagesStorage().getDialogReadMax(newMsgObj.out, newMsgObj.dialog_id);
                                getMessagesController().dialogs_read_outbox_max.put(newMsgObj.dialog_id, value);
                            }
                            newMsgObj.unread = value < newMsgObj.id;
                        }
                        Utilities.stageQueue.postRunnable(() -> getMessagesController().processNewDifferenceParams(-1, res.pts, res.date, res.pts_count));
                        sentMessages.add(newMsgObj);
                    } else if (response instanceof TLRPC.Updates) {
                        final TLRPC.Updates updates = (TLRPC.Updates) response;
                        ArrayList<TLRPC.Update> updatesArr = ((TLRPC.Updates) response).updates;
                        TLRPC.Message message = null;
                        LongSparseArray<SparseArray<TLRPC.MessageReplies>> channelReplies = null;
                        for (int a = 0; a < updatesArr.size(); a++) {
                            TLRPC.Update update = updatesArr.get(a);
                            if (update instanceof TLRPC.TL_updateNewMessage) {
                                final TLRPC.TL_updateNewMessage newMessage = (TLRPC.TL_updateNewMessage) update;
                                sentMessages.add(message = newMessage.message);
                                Utilities.stageQueue.postRunnable(() -> getMessagesController().processNewDifferenceParams(-1, newMessage.pts, -1, newMessage.pts_count));
                                updatesArr.remove(a);
                                break;
                            } else if (update instanceof TLRPC.TL_updateNewChannelMessage) {
                                final TLRPC.TL_updateNewChannelMessage newMessage = (TLRPC.TL_updateNewChannelMessage) update;
                                long channelId = MessagesController.getUpdateChannelId(newMessage);
                                TLRPC.Chat chat = getMessagesController().getChat(channelId);
                                if ((chat == null || chat.megagroup) && newMessage.message.reply_to != null && (newMessage.message.reply_to.reply_to_top_id != 0 || newMessage.message.reply_to.reply_to_msg_id != 0)) {
                                    if (channelReplies == null) {
                                        channelReplies = new LongSparseArray<>();
                                    }
                                    long did = MessageObject.getDialogId(newMessage.message);
                                    SparseArray<TLRPC.MessageReplies> replies = channelReplies.get(did);
                                    if (replies == null) {
                                        replies = new SparseArray<>();
                                        channelReplies.put(did, replies);
                                    }
                                    int id = newMessage.message.reply_to.reply_to_top_id != 0 ? newMessage.message.reply_to.reply_to_top_id : newMessage.message.reply_to.reply_to_msg_id;
                                    TLRPC.MessageReplies messageReplies = replies.get(id);
                                    if (messageReplies == null) {
                                        messageReplies = new TLRPC.TL_messageReplies();
                                        replies.put(id, messageReplies);
                                    }
                                    if (newMessage.message.from_id != null) {
                                        messageReplies.recent_repliers.add(0, newMessage.message.from_id);
                                    }
                                    messageReplies.replies++;
                                }
                                sentMessages.add(message = newMessage.message);
                                Utilities.stageQueue.postRunnable(() -> getMessagesController().processNewChannelDifferenceParams(newMessage.pts, newMessage.pts_count, newMessage.message.peer_id.channel_id));
                                updatesArr.remove(a);
                                break;
                            } else if (update instanceof TLRPC.TL_updateNewScheduledMessage) {
                                final TLRPC.TL_updateNewScheduledMessage newMessage = (TLRPC.TL_updateNewScheduledMessage) update;
                                sentMessages.add(message = newMessage.message);
                                updatesArr.remove(a);
                                break;
                            }
                        }
                        if (channelReplies != null) {
                            getMessagesStorage().putChannelViews(null, null, channelReplies, true);
                            getNotificationCenter().postNotificationName(NotificationCenter.didUpdateMessagesViews, null, null, channelReplies, true);
                        }
                        if (message != null) {
                            MessageObject.getDialogId(message);
                            if (scheduledOnline && message.date != 0x7FFFFFFE) {
                                currentSchedule = false;
                            }
                            ImageLoader.saveMessageThumbs(message);
                            if (!currentSchedule) {
                                Integer value = getMessagesController().dialogs_read_outbox_max.get(message.dialog_id);
                                if (value == null) {
                                    value = getMessagesStorage().getDialogReadMax(message.out, message.dialog_id);
                                    getMessagesController().dialogs_read_outbox_max.put(message.dialog_id, value);
                                }
                                message.unread = value < message.id;
                            }
                            msgObj.messageOwner.post_author = message.post_author;
                            if ((message.flags & 33554432) != 0) {
                                msgObj.messageOwner.ttl_period = message.ttl_period;
                                msgObj.messageOwner.flags |= 33554432;
                            }
                            msgObj.messageOwner.entities = message.entities;
                            updateMediaPaths(msgObj, message, message.id, originalPath, false);
                            existFlags = msgObj.getMediaExistanceFlags();
                            newMsgObj.id = message.id;
                        } else {
                            isSentError = true;
                            existFlags = 0;
                        }
                        Utilities.stageQueue.postRunnable(() -> getMessagesController().processUpdates(updates, false));
                    } else {
                        existFlags = 0;
                    }
                    if (MessageObject.isLiveLocationMessage(newMsgObj) && newMsgObj.via_bot_id == 0 && TextUtils.isEmpty(newMsgObj.via_bot_name)) {
                        getLocationController().addSharingLocation(newMsgObj);
                    }
                    if (!isSentError) {
                        getStatsController().incrementSentItemsCount(ApplicationLoader.getCurrentNetworkType(), StatsController.TYPE_MESSAGES, 1);
                        newMsgObj.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
                        if (scheduled && !currentSchedule) {
                            ArrayList<Integer> messageIds = new ArrayList<>();
                            messageIds.add(oldId);
                            getMessagesController().deleteMessages(messageIds, null, null, newMsgObj.dialog_id, false, true);
                            getMessagesStorage().getStorageQueue().postRunnable(() -> {
                                getMessagesStorage().putMessages(sentMessages, true, false, false, 0, false);
                                AndroidUtilities.runOnUIThread(() -> {
                                    ArrayList<MessageObject> messageObjects = new ArrayList<>();
                                    messageObjects.add(new MessageObject(msgObj.currentAccount, msgObj.messageOwner, true, true));
                                    getMessagesController().updateInterfaceWithMessages(newMsgObj.dialog_id, messageObjects, false);
                                    getMediaDataController().increasePeerRaiting(newMsgObj.dialog_id);
                                    processSentMessage(oldId);
                                    removeFromSendingMessages(oldId, scheduled);
                                });
                                if (MessageObject.isVideoMessage(newMsgObj) || MessageObject.isRoundVideoMessage(newMsgObj) || MessageObject.isNewGifMessage(newMsgObj)) {
                                    stopVideoService(attachPath);
                                }
                            });
                        } else {
                            getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, newMsgObj.id, newMsgObj, newMsgObj.dialog_id, 0L, existFlags, scheduled);
                            getMessagesStorage().getStorageQueue().postRunnable(() -> {
                                getMessagesStorage().updateMessageStateAndId(newMsgObj.random_id, MessageObject.getPeerId(newMsgObj.peer_id), oldId, newMsgObj.id, 0, false, scheduled ? 1 : 0);
                                getMessagesStorage().putMessages(sentMessages, true, false, false, 0, scheduled);
                                AndroidUtilities.runOnUIThread(() -> {
                                    getMediaDataController().increasePeerRaiting(newMsgObj.dialog_id);
                                    getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, newMsgObj.id, newMsgObj, newMsgObj.dialog_id, 0L, existFlags, scheduled);
                                    processSentMessage(oldId);
                                    removeFromSendingMessages(oldId, scheduled);
                                });
                                if (MessageObject.isVideoMessage(newMsgObj) || MessageObject.isRoundVideoMessage(newMsgObj) || MessageObject.isNewGifMessage(newMsgObj)) {
                                    stopVideoService(attachPath);
                                }
                            });
                        }
                    }
                } else {
                    AlertsCreator.processError(currentAccount, error, null, req);
                    isSentError = true;
                }
                if (isSentError) {
                    getMessagesStorage().markMessageAsSendError(newMsgObj, scheduled);
                    newMsgObj.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR;
                    getNotificationCenter().postNotificationName(NotificationCenter.messageSendError, newMsgObj.id);
                    processSentMessage(newMsgObj.id);
                    if (MessageObject.isVideoMessage(newMsgObj) || MessageObject.isRoundVideoMessage(newMsgObj) || MessageObject.isNewGifMessage(newMsgObj)) {
                        stopVideoService(newMsgObj.attachPath);
                    }
                    removeFromSendingMessages(newMsgObj.id, scheduled);
                }
            });
        }
    }, () -> {
        final int msg_id = newMsgObj.id;
        AndroidUtilities.runOnUIThread(() -> {
            newMsgObj.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
            getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByAck, msg_id);
        });
    }, ConnectionsManager.RequestFlagCanCompress | ConnectionsManager.RequestFlagInvokeAfter | (req instanceof TLRPC.TL_messages_sendMessage ? ConnectionsManager.RequestFlagNeedQuickAck : 0));
    if (parentMessage != null) {
        parentMessage.sendDelayedRequests();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) LinearLayout(android.widget.LinearLayout) SparseLongArray(org.telegram.messenger.support.SparseLongArray) Bundle(android.os.Bundle) Uri(android.net.Uri) ImageView(android.widget.ImageView) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) LocationListener(android.location.LocationListener) Bulletin(org.telegram.ui.Components.Bulletin) ByteBuffer(java.nio.ByteBuffer) AudioInfo(org.telegram.messenger.audioinfo.AudioInfo) JSONObject(org.json.JSONObject) MediaCodecInfo(android.media.MediaCodecInfo) Locale(java.util.Locale) MediaStore(android.provider.MediaStore) UiThread(androidx.annotation.UiThread) ChatMessageCell(org.telegram.ui.Cells.ChatMessageCell) BaseFragment(org.telegram.ui.ActionBar.BaseFragment) BitmapDrawable(android.graphics.drawable.BitmapDrawable) PorterDuff(android.graphics.PorterDuff) ConnectionsManager(org.telegram.tgnet.ConnectionsManager) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SparseArray(android.util.SparseArray) CountDownLatch(java.util.concurrent.CountDownLatch) TextView(android.widget.TextView) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Location(android.location.Location) LocationManager(android.location.LocationManager) ChatActivity(org.telegram.ui.ChatActivity) Context(android.content.Context) MediaMetadataRetriever(android.media.MediaMetadataRetriever) Theme(org.telegram.ui.ActionBar.Theme) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RequestDelegate(org.telegram.tgnet.RequestDelegate) TwoStepVerificationActivity(org.telegram.ui.TwoStepVerificationActivity) Intent(android.content.Intent) BitmapFactory(android.graphics.BitmapFactory) MediaPlayer(android.media.MediaPlayer) SystemClock(android.os.SystemClock) HashMap(java.util.HashMap) AlertsCreator(org.telegram.ui.Components.AlertsCreator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TLRPC(org.telegram.tgnet.TLRPC) Point(org.telegram.ui.Components.Point) Toast(android.widget.Toast) PaymentFormActivity(org.telegram.ui.PaymentFormActivity) TLObject(org.telegram.tgnet.TLObject) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) Build(android.os.Build) SerializedData(org.telegram.tgnet.SerializedData) LongSparseArray(androidx.collection.LongSparseArray) TwoStepVerificationSetupActivity(org.telegram.ui.TwoStepVerificationSetupActivity) FileOutputStream(java.io.FileOutputStream) TextUtils(android.text.TextUtils) InputContentInfoCompat(androidx.core.view.inputmethod.InputContentInfoCompat) LayoutHelper(org.telegram.ui.Components.LayoutHelper) FileInputStream(java.io.FileInputStream) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Gravity(android.view.Gravity) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) Base64(android.util.Base64) ClipDescription(android.content.ClipDescription) AlertDialog(org.telegram.ui.ActionBar.AlertDialog) FileChannel(java.nio.channels.FileChannel) MimeTypeMap(android.webkit.MimeTypeMap) InputStream(java.io.InputStream) LongSparseArray(androidx.collection.LongSparseArray) ArrayList(java.util.ArrayList) TLRPC(org.telegram.tgnet.TLRPC) Point(org.telegram.ui.Components.Point) SparseArray(android.util.SparseArray) LongSparseArray(androidx.collection.LongSparseArray)

Example 73 with TLObject

use of org.telegram.tgnet.TLObject in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method editMessage.

public void editMessage(MessageObject messageObject, TLRPC.TL_photo photo, VideoEditedInfo videoEditedInfo, TLRPC.TL_document document, String path, HashMap<String, String> params, boolean retry, Object parentObject) {
    if (messageObject == null) {
        return;
    }
    if (params == null) {
        params = new HashMap<>();
    }
    TLRPC.Message newMsg = messageObject.messageOwner;
    messageObject.cancelEditing = false;
    try {
        int type = -1;
        DelayedMessage delayedMessage = null;
        long peer = messageObject.getDialogId();
        boolean supportsSendingNewEntities = true;
        if (DialogObject.isEncryptedDialog(peer)) {
            int encryptedId = DialogObject.getEncryptedChatId(peer);
            TLRPC.EncryptedChat encryptedChat = getMessagesController().getEncryptedChat(encryptedId);
            if (encryptedChat == null || AndroidUtilities.getPeerLayerVersion(encryptedChat.layer) < 101) {
                supportsSendingNewEntities = false;
            }
        }
        if (retry) {
            if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaWebPage || messageObject.messageOwner.media == null || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaEmpty) {
                type = 1;
            } else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
                photo = (TLRPC.TL_photo) messageObject.messageOwner.media.photo;
                type = 2;
            } else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
                document = (TLRPC.TL_document) messageObject.messageOwner.media.document;
                if (MessageObject.isVideoDocument(document) || videoEditedInfo != null) {
                    type = 3;
                } else {
                    type = 7;
                }
                videoEditedInfo = messageObject.videoEditedInfo;
            }
            params = newMsg.params;
            if (parentObject == null && params != null && params.containsKey("parentObject")) {
                parentObject = params.get("parentObject");
            }
            messageObject.editingMessage = newMsg.message;
            messageObject.editingMessageEntities = newMsg.entities;
            path = newMsg.attachPath;
        } else {
            messageObject.previousMedia = newMsg.media;
            messageObject.previousMessage = newMsg.message;
            messageObject.previousMessageEntities = newMsg.entities;
            messageObject.previousAttachPath = newMsg.attachPath;
            TLRPC.MessageMedia media = newMsg.media;
            if (media == null) {
                media = new TLRPC.TL_messageMediaEmpty();
            }
            SerializedData serializedDataCalc = new SerializedData(true);
            writePreviousMessageData(newMsg, serializedDataCalc);
            SerializedData prevMessageData = new SerializedData(serializedDataCalc.length());
            writePreviousMessageData(newMsg, prevMessageData);
            if (params == null) {
                params = new HashMap<>();
            }
            params.put("prevMedia", Base64.encodeToString(prevMessageData.toByteArray(), Base64.DEFAULT));
            prevMessageData.cleanup();
            if (photo != null) {
                newMsg.media = new TLRPC.TL_messageMediaPhoto();
                newMsg.media.flags |= 3;
                newMsg.media.photo = photo;
                type = 2;
                if (path != null && path.length() > 0 && path.startsWith("http")) {
                    newMsg.attachPath = path;
                } else {
                    TLRPC.FileLocation location1 = photo.sizes.get(photo.sizes.size() - 1).location;
                    newMsg.attachPath = FileLoader.getPathToAttach(location1, true).toString();
                }
            } else if (document != null) {
                newMsg.media = new TLRPC.TL_messageMediaDocument();
                newMsg.media.flags |= 3;
                newMsg.media.document = document;
                if (MessageObject.isVideoDocument(document) || videoEditedInfo != null) {
                    type = 3;
                } else {
                    type = 7;
                }
                if (videoEditedInfo != null) {
                    String ve = videoEditedInfo.getString();
                    params.put("ve", ve);
                }
                newMsg.attachPath = path;
            } else {
                type = 1;
            }
            newMsg.params = params;
            newMsg.send_state = MessageObject.MESSAGE_SEND_STATE_EDITING;
        }
        if (newMsg.attachPath == null) {
            newMsg.attachPath = "";
        }
        newMsg.local_id = 0;
        if ((messageObject.type == 3 || videoEditedInfo != null || messageObject.type == 2) && !TextUtils.isEmpty(newMsg.attachPath)) {
            messageObject.attachPathExists = true;
        }
        if (messageObject.videoEditedInfo != null && videoEditedInfo == null) {
            videoEditedInfo = messageObject.videoEditedInfo;
        }
        if (!retry) {
            if (messageObject.editingMessage != null) {
                String oldMessge = newMsg.message;
                newMsg.message = messageObject.editingMessage.toString();
                messageObject.caption = null;
                if (type == 1) {
                    if (messageObject.editingMessageEntities != null) {
                        newMsg.entities = messageObject.editingMessageEntities;
                        newMsg.flags |= 128;
                    } else if (!TextUtils.equals(oldMessge, newMsg.message)) {
                        newMsg.flags &= ~128;
                    }
                } else {
                    if (messageObject.editingMessageEntities != null) {
                        newMsg.entities = messageObject.editingMessageEntities;
                        newMsg.flags |= 128;
                    } else {
                        CharSequence[] message = new CharSequence[] { messageObject.editingMessage };
                        ArrayList<TLRPC.MessageEntity> entities = getMediaDataController().getEntities(message, supportsSendingNewEntities);
                        if (entities != null && !entities.isEmpty()) {
                            newMsg.entities = entities;
                            newMsg.flags |= 128;
                        } else if (!TextUtils.equals(oldMessge, newMsg.message)) {
                            newMsg.flags &= ~128;
                        }
                    }
                    messageObject.generateCaption();
                }
            }
            ArrayList<TLRPC.Message> arr = new ArrayList<>();
            arr.add(newMsg);
            getMessagesStorage().putMessages(arr, false, true, false, 0, messageObject.scheduled);
            messageObject.type = -1;
            messageObject.setType();
            if (type == 1) {
                if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
                    messageObject.generateCaption();
                } else {
                    messageObject.resetLayout();
                    messageObject.checkLayout();
                }
            }
            messageObject.createMessageSendInfo();
            ArrayList<MessageObject> arrayList = new ArrayList<>();
            arrayList.add(messageObject);
            getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, peer, arrayList);
        }
        String originalPath = null;
        if (params != null && params.containsKey("originalPath")) {
            originalPath = params.get("originalPath");
        }
        boolean performMediaUpload = false;
        if (type >= 1 && type <= 3 || type >= 5 && type <= 8) {
            TLRPC.InputMedia inputMedia = null;
            if (type == 1) {
            } else if (type == 2) {
                TLRPC.TL_inputMediaUploadedPhoto uploadedPhoto = new TLRPC.TL_inputMediaUploadedPhoto();
                if (params != null) {
                    String masks = params.get("masks");
                    if (masks != null) {
                        SerializedData serializedData = new SerializedData(Utilities.hexToBytes(masks));
                        int count = serializedData.readInt32(false);
                        for (int a = 0; a < count; a++) {
                            uploadedPhoto.stickers.add(TLRPC.InputDocument.TLdeserialize(serializedData, serializedData.readInt32(false), false));
                        }
                        uploadedPhoto.flags |= 1;
                        serializedData.cleanup();
                    }
                }
                if (photo.access_hash == 0) {
                    inputMedia = uploadedPhoto;
                    performMediaUpload = true;
                } else {
                    TLRPC.TL_inputMediaPhoto media = new TLRPC.TL_inputMediaPhoto();
                    media.id = new TLRPC.TL_inputPhoto();
                    media.id.id = photo.id;
                    media.id.access_hash = photo.access_hash;
                    media.id.file_reference = photo.file_reference;
                    if (media.id.file_reference == null) {
                        media.id.file_reference = new byte[0];
                    }
                    inputMedia = media;
                }
                delayedMessage = new DelayedMessage(peer);
                delayedMessage.type = 0;
                delayedMessage.obj = messageObject;
                delayedMessage.originalPath = originalPath;
                delayedMessage.parentObject = parentObject;
                delayedMessage.inputUploadMedia = uploadedPhoto;
                delayedMessage.performMediaUpload = performMediaUpload;
                if (path != null && path.length() > 0 && path.startsWith("http")) {
                    delayedMessage.httpLocation = path;
                } else {
                    delayedMessage.photoSize = photo.sizes.get(photo.sizes.size() - 1);
                    delayedMessage.locationParent = photo;
                }
            } else if (type == 3) {
                TLRPC.TL_inputMediaUploadedDocument uploadedDocument = new TLRPC.TL_inputMediaUploadedDocument();
                if (params != null) {
                    String masks = params.get("masks");
                    if (masks != null) {
                        SerializedData serializedData = new SerializedData(Utilities.hexToBytes(masks));
                        int count = serializedData.readInt32(false);
                        for (int a = 0; a < count; a++) {
                            uploadedDocument.stickers.add(TLRPC.InputDocument.TLdeserialize(serializedData, serializedData.readInt32(false), false));
                        }
                        uploadedDocument.flags |= 1;
                        serializedData.cleanup();
                    }
                }
                uploadedDocument.mime_type = document.mime_type;
                uploadedDocument.attributes = document.attributes;
                if (!messageObject.isGif() && (videoEditedInfo == null || !videoEditedInfo.muted)) {
                    uploadedDocument.nosound_video = true;
                    if (BuildVars.DEBUG_VERSION) {
                        FileLog.d("nosound_video = true");
                    }
                }
                if (document.access_hash == 0) {
                    inputMedia = uploadedDocument;
                    performMediaUpload = true;
                } else {
                    TLRPC.TL_inputMediaDocument media = new TLRPC.TL_inputMediaDocument();
                    media.id = new TLRPC.TL_inputDocument();
                    media.id.id = document.id;
                    media.id.access_hash = document.access_hash;
                    media.id.file_reference = document.file_reference;
                    if (media.id.file_reference == null) {
                        media.id.file_reference = new byte[0];
                    }
                    inputMedia = media;
                }
                delayedMessage = new DelayedMessage(peer);
                delayedMessage.type = 1;
                delayedMessage.obj = messageObject;
                delayedMessage.originalPath = originalPath;
                delayedMessage.parentObject = parentObject;
                delayedMessage.inputUploadMedia = uploadedDocument;
                delayedMessage.performMediaUpload = performMediaUpload;
                if (!document.thumbs.isEmpty()) {
                    TLRPC.PhotoSize photoSize = document.thumbs.get(0);
                    if (!(photoSize instanceof TLRPC.TL_photoStrippedSize)) {
                        delayedMessage.photoSize = photoSize;
                        delayedMessage.locationParent = document;
                    }
                }
                delayedMessage.videoEditedInfo = videoEditedInfo;
            } else if (type == 7) {
                boolean http = false;
                TLRPC.InputMedia uploadedDocument = new TLRPC.TL_inputMediaUploadedDocument();
                uploadedDocument.mime_type = document.mime_type;
                uploadedDocument.attributes = document.attributes;
                if (document.access_hash == 0) {
                    inputMedia = uploadedDocument;
                    performMediaUpload = uploadedDocument instanceof TLRPC.TL_inputMediaUploadedDocument;
                } else {
                    TLRPC.TL_inputMediaDocument media = new TLRPC.TL_inputMediaDocument();
                    media.id = new TLRPC.TL_inputDocument();
                    media.id.id = document.id;
                    media.id.access_hash = document.access_hash;
                    media.id.file_reference = document.file_reference;
                    if (media.id.file_reference == null) {
                        media.id.file_reference = new byte[0];
                    }
                    inputMedia = media;
                }
                if (!http) {
                    delayedMessage = new DelayedMessage(peer);
                    delayedMessage.originalPath = originalPath;
                    delayedMessage.type = 2;
                    delayedMessage.obj = messageObject;
                    if (!document.thumbs.isEmpty()) {
                        TLRPC.PhotoSize photoSize = document.thumbs.get(0);
                        if (!(photoSize instanceof TLRPC.TL_photoStrippedSize)) {
                            delayedMessage.photoSize = photoSize;
                            delayedMessage.locationParent = document;
                        }
                    }
                    delayedMessage.parentObject = parentObject;
                    delayedMessage.inputUploadMedia = uploadedDocument;
                    delayedMessage.performMediaUpload = performMediaUpload;
                }
            }
            TLObject reqSend;
            TLRPC.TL_messages_editMessage request = new TLRPC.TL_messages_editMessage();
            request.id = messageObject.getId();
            request.peer = getMessagesController().getInputPeer(peer);
            if (inputMedia != null) {
                request.flags |= 16384;
                request.media = inputMedia;
            }
            if (messageObject.scheduled) {
                request.schedule_date = messageObject.messageOwner.date;
                request.flags |= 32768;
            }
            if (messageObject.editingMessage != null) {
                request.message = messageObject.editingMessage.toString();
                request.flags |= 2048;
                request.no_webpage = !messageObject.editingMessageSearchWebPage;
                if (messageObject.editingMessageEntities != null) {
                    request.entities = messageObject.editingMessageEntities;
                    request.flags |= 8;
                } else {
                    CharSequence[] message = new CharSequence[] { messageObject.editingMessage };
                    ArrayList<TLRPC.MessageEntity> entities = getMediaDataController().getEntities(message, supportsSendingNewEntities);
                    if (entities != null && !entities.isEmpty()) {
                        request.entities = entities;
                        request.flags |= 8;
                    }
                }
                messageObject.editingMessage = null;
                messageObject.editingMessageEntities = null;
            }
            if (delayedMessage != null) {
                delayedMessage.sendRequest = request;
            }
            reqSend = request;
            if (type == 1) {
                performSendMessageRequest(reqSend, messageObject, null, delayedMessage, parentObject, params, messageObject.scheduled);
            } else if (type == 2) {
                if (performMediaUpload) {
                    performSendDelayedMessage(delayedMessage);
                } else {
                    performSendMessageRequest(reqSend, messageObject, originalPath, null, true, delayedMessage, parentObject, params, messageObject.scheduled);
                }
            } else if (type == 3) {
                if (performMediaUpload) {
                    performSendDelayedMessage(delayedMessage);
                } else {
                    performSendMessageRequest(reqSend, messageObject, originalPath, delayedMessage, parentObject, params, messageObject.scheduled);
                }
            } else if (type == 6) {
                performSendMessageRequest(reqSend, messageObject, originalPath, delayedMessage, parentObject, params, messageObject.scheduled);
            } else if (type == 7) {
                if (performMediaUpload) {
                    performSendDelayedMessage(delayedMessage);
                } else {
                    performSendMessageRequest(reqSend, messageObject, originalPath, delayedMessage, parentObject, params, messageObject.scheduled);
                }
            } else if (type == 8) {
                if (performMediaUpload) {
                    performSendDelayedMessage(delayedMessage);
                } else {
                    performSendMessageRequest(reqSend, messageObject, originalPath, delayedMessage, parentObject, params, messageObject.scheduled);
                }
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
        revertEditingMessageObject(messageObject);
    }
}
Also used : ArrayList(java.util.ArrayList) TLRPC(org.telegram.tgnet.TLRPC) SerializedData(org.telegram.tgnet.SerializedData) Point(org.telegram.ui.Components.Point) TLObject(org.telegram.tgnet.TLObject)

Example 74 with TLObject

use of org.telegram.tgnet.TLObject in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method sendMessage.

private void sendMessage(String message, String caption, TLRPC.MessageMedia location, TLRPC.TL_photo photo, VideoEditedInfo videoEditedInfo, TLRPC.User user, TLRPC.TL_document document, TLRPC.TL_game game, TLRPC.TL_messageMediaPoll poll, TLRPC.TL_messageMediaInvoice invoice, long peer, String path, MessageObject replyToMsg, MessageObject replyToTopMsg, TLRPC.WebPage webPage, boolean searchLinks, MessageObject retryMessageObject, ArrayList<TLRPC.MessageEntity> entities, TLRPC.ReplyMarkup replyMarkup, HashMap<String, String> params, boolean notify, int scheduleDate, int ttl, Object parentObject, MessageObject.SendAnimationData sendAnimationData) {
    if (user != null && user.phone == null) {
        return;
    }
    if (peer == 0) {
        return;
    }
    if (message == null && caption == null) {
        caption = "";
    }
    String originalPath = null;
    if (params != null && params.containsKey("originalPath")) {
        originalPath = params.get("originalPath");
    }
    TLRPC.Message newMsg = null;
    MessageObject newMsgObj = null;
    DelayedMessage delayedMessage = null;
    int type = -1;
    boolean isChannel = false;
    boolean forceNoSoundVideo = false;
    TLRPC.Peer fromPeer = null;
    String rank = null;
    long linkedToGroup = 0;
    TLRPC.EncryptedChat encryptedChat = null;
    TLRPC.InputPeer sendToPeer = !DialogObject.isEncryptedDialog(peer) ? getMessagesController().getInputPeer(peer) : null;
    long myId = getUserConfig().getClientUserId();
    if (DialogObject.isEncryptedDialog(peer)) {
        encryptedChat = getMessagesController().getEncryptedChat(DialogObject.getEncryptedChatId(peer));
        if (encryptedChat == null) {
            if (retryMessageObject != null) {
                getMessagesStorage().markMessageAsSendError(retryMessageObject.messageOwner, retryMessageObject.scheduled);
                retryMessageObject.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR;
                getNotificationCenter().postNotificationName(NotificationCenter.messageSendError, retryMessageObject.getId());
                processSentMessage(retryMessageObject.getId());
            }
            return;
        }
    } else if (sendToPeer instanceof TLRPC.TL_inputPeerChannel) {
        TLRPC.Chat chat = getMessagesController().getChat(sendToPeer.channel_id);
        TLRPC.ChatFull chatFull = getMessagesController().getChatFull(chat.id);
        isChannel = chat != null && !chat.megagroup;
        if (isChannel && chat.has_link && chatFull != null) {
            linkedToGroup = chatFull.linked_chat_id;
        }
        fromPeer = getMessagesController().getPeer(ChatObject.getSendAsPeerId(chat, chatFull, true));
    }
    try {
        if (retryMessageObject != null) {
            newMsg = retryMessageObject.messageOwner;
            if (parentObject == null && params != null && params.containsKey("parentObject")) {
                parentObject = params.get("parentObject");
            }
            if (retryMessageObject.isForwarded() || params != null && params.containsKey("fwd_id")) {
                type = 4;
            } else {
                if (retryMessageObject.isDice()) {
                    type = 11;
                    message = retryMessageObject.getDiceEmoji();
                    caption = "";
                } else if (retryMessageObject.type == 0 || retryMessageObject.isAnimatedEmoji()) {
                    if (retryMessageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
                    // game = retryMessageObject.messageOwner.media.game;
                    } else {
                        message = newMsg.message;
                    }
                    type = 0;
                } else if (retryMessageObject.type == 4) {
                    location = newMsg.media;
                    type = 1;
                } else if (retryMessageObject.type == 1) {
                    photo = (TLRPC.TL_photo) newMsg.media.photo;
                    if (retryMessageObject.messageOwner.message != null) {
                        caption = retryMessageObject.messageOwner.message;
                    }
                    type = 2;
                } else if (retryMessageObject.type == 3 || retryMessageObject.type == 5 || retryMessageObject.videoEditedInfo != null) {
                    type = 3;
                    document = (TLRPC.TL_document) newMsg.media.document;
                    if (retryMessageObject.messageOwner.message != null) {
                        caption = retryMessageObject.messageOwner.message;
                    }
                } else if (retryMessageObject.type == 12) {
                    user = new TLRPC.TL_userRequest_old2();
                    user.phone = newMsg.media.phone_number;
                    user.first_name = newMsg.media.first_name;
                    user.last_name = newMsg.media.last_name;
                    TLRPC.TL_restrictionReason reason = new TLRPC.TL_restrictionReason();
                    reason.platform = "";
                    reason.reason = "";
                    reason.text = newMsg.media.vcard;
                    user.restriction_reason.add(reason);
                    user.id = newMsg.media.user_id;
                    type = 6;
                } else if (retryMessageObject.type == 8 || retryMessageObject.type == 9 || retryMessageObject.type == MessageObject.TYPE_STICKER || retryMessageObject.type == 14 || retryMessageObject.type == MessageObject.TYPE_ANIMATED_STICKER) {
                    document = (TLRPC.TL_document) newMsg.media.document;
                    type = 7;
                    if (retryMessageObject.messageOwner.message != null) {
                        caption = retryMessageObject.messageOwner.message;
                    }
                } else if (retryMessageObject.type == 2) {
                    document = (TLRPC.TL_document) newMsg.media.document;
                    type = 8;
                    if (retryMessageObject.messageOwner.message != null) {
                        caption = retryMessageObject.messageOwner.message;
                    }
                } else if (retryMessageObject.type == MessageObject.TYPE_POLL) {
                    poll = (TLRPC.TL_messageMediaPoll) newMsg.media;
                    type = 10;
                }
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                }
                if (newMsg.media.ttl_seconds > 0) {
                    ttl = newMsg.media.ttl_seconds;
                }
            }
        } else {
            boolean canSendStickers = true;
            if (DialogObject.isChatDialog(peer)) {
                TLRPC.Chat chat = getMessagesController().getChat(-peer);
                canSendStickers = ChatObject.canSendStickers(chat);
            }
            if (message != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                if (encryptedChat != null && webPage instanceof TLRPC.TL_webPagePending) {
                    if (webPage.url != null) {
                        TLRPC.WebPage newWebPage = new TLRPC.TL_webPageUrlPending();
                        newWebPage.url = webPage.url;
                        webPage = newWebPage;
                    } else {
                        webPage = null;
                    }
                }
                if (canSendStickers && message.length() < 30 && webPage == null && (entities == null || entities.isEmpty()) && getMessagesController().diceEmojies.contains(message.replace("\ufe0f", "")) && encryptedChat == null && scheduleDate == 0) {
                    TLRPC.TL_messageMediaDice mediaDice = new TLRPC.TL_messageMediaDice();
                    mediaDice.emoticon = message;
                    mediaDice.value = -1;
                    newMsg.media = mediaDice;
                    type = 11;
                    caption = "";
                } else {
                    if (webPage == null) {
                        newMsg.media = new TLRPC.TL_messageMediaEmpty();
                    } else {
                        newMsg.media = new TLRPC.TL_messageMediaWebPage();
                        newMsg.media.webpage = webPage;
                    }
                    if (params != null && params.containsKey("query_id")) {
                        type = 9;
                    } else {
                        type = 0;
                    }
                    newMsg.message = message;
                }
            } else if (poll != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                newMsg.media = poll;
                type = 10;
            } else if (location != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                newMsg.media = location;
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                } else {
                    type = 1;
                }
            } else if (photo != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                newMsg.media = new TLRPC.TL_messageMediaPhoto();
                newMsg.media.flags |= 3;
                if (entities != null) {
                    newMsg.entities = entities;
                }
                if (ttl != 0) {
                    newMsg.ttl = newMsg.media.ttl_seconds = ttl;
                    newMsg.media.flags |= 4;
                }
                newMsg.media.photo = photo;
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                } else {
                    type = 2;
                }
                if (path != null && path.length() > 0 && path.startsWith("http")) {
                    newMsg.attachPath = path;
                } else {
                    TLRPC.FileLocation location1 = photo.sizes.get(photo.sizes.size() - 1).location;
                    newMsg.attachPath = FileLoader.getPathToAttach(location1, true).toString();
                }
            } else if (game != null) {
                newMsg = new TLRPC.TL_message();
                newMsg.media = new TLRPC.TL_messageMediaGame();
                newMsg.media.game = game;
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                }
            } else if (invoice != null) {
                newMsg = new TLRPC.TL_message();
                newMsg.media = invoice;
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                }
            } else if (user != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                newMsg.media = new TLRPC.TL_messageMediaContact();
                newMsg.media.phone_number = user.phone;
                newMsg.media.first_name = user.first_name;
                newMsg.media.last_name = user.last_name;
                newMsg.media.user_id = user.id;
                if (!user.restriction_reason.isEmpty() && user.restriction_reason.get(0).text.startsWith("BEGIN:VCARD")) {
                    newMsg.media.vcard = user.restriction_reason.get(0).text;
                } else {
                    newMsg.media.vcard = "";
                }
                if (newMsg.media.first_name == null) {
                    user.first_name = newMsg.media.first_name = "";
                }
                if (newMsg.media.last_name == null) {
                    user.last_name = newMsg.media.last_name = "";
                }
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                } else {
                    type = 6;
                }
            } else if (document != null) {
                if (encryptedChat != null) {
                    newMsg = new TLRPC.TL_message_secret();
                } else {
                    newMsg = new TLRPC.TL_message();
                }
                if (DialogObject.isChatDialog(peer)) {
                    if (!canSendStickers) {
                        for (int a = 0, N = document.attributes.size(); a < N; a++) {
                            if (document.attributes.get(a) instanceof TLRPC.TL_documentAttributeAnimated) {
                                document.attributes.remove(a);
                                forceNoSoundVideo = true;
                                break;
                            }
                        }
                    }
                }
                newMsg.media = new TLRPC.TL_messageMediaDocument();
                newMsg.media.flags |= 3;
                if (ttl != 0) {
                    newMsg.ttl = newMsg.media.ttl_seconds = ttl;
                    newMsg.media.flags |= 4;
                }
                newMsg.media.document = document;
                if (params != null && params.containsKey("query_id")) {
                    type = 9;
                } else if (!MessageObject.isVideoSticker(document) && (MessageObject.isVideoDocument(document) || MessageObject.isRoundVideoDocument(document) || videoEditedInfo != null)) {
                    type = 3;
                } else if (MessageObject.isVoiceDocument(document)) {
                    type = 8;
                } else {
                    type = 7;
                }
                if (videoEditedInfo != null) {
                    String ve = videoEditedInfo.getString();
                    if (params == null) {
                        params = new HashMap<>();
                    }
                    params.put("ve", ve);
                }
                if (encryptedChat != null && document.dc_id > 0 && !MessageObject.isStickerDocument(document) && !MessageObject.isAnimatedStickerDocument(document, true)) {
                    newMsg.attachPath = FileLoader.getPathToAttach(document).toString();
                } else {
                    newMsg.attachPath = path;
                }
                if (encryptedChat != null && (MessageObject.isStickerDocument(document) || MessageObject.isAnimatedStickerDocument(document, true))) {
                    for (int a = 0; a < document.attributes.size(); a++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(a);
                        if (attribute instanceof TLRPC.TL_documentAttributeSticker) {
                            document.attributes.remove(a);
                            TLRPC.TL_documentAttributeSticker_layer55 attributeSticker = new TLRPC.TL_documentAttributeSticker_layer55();
                            document.attributes.add(attributeSticker);
                            attributeSticker.alt = attribute.alt;
                            if (attribute.stickerset != null) {
                                String name;
                                if (attribute.stickerset instanceof TLRPC.TL_inputStickerSetShortName) {
                                    name = attribute.stickerset.short_name;
                                } else {
                                    name = getMediaDataController().getStickerSetName(attribute.stickerset.id);
                                }
                                if (!TextUtils.isEmpty(name)) {
                                    attributeSticker.stickerset = new TLRPC.TL_inputStickerSetShortName();
                                    attributeSticker.stickerset.short_name = name;
                                } else {
                                    if (attribute.stickerset instanceof TLRPC.TL_inputStickerSetID) {
                                        delayedMessage = new DelayedMessage(peer);
                                        delayedMessage.encryptedChat = encryptedChat;
                                        delayedMessage.locationParent = attributeSticker;
                                        delayedMessage.type = 5;
                                        delayedMessage.parentObject = attribute.stickerset;
                                    }
                                    attributeSticker.stickerset = new TLRPC.TL_inputStickerSetEmpty();
                                }
                            } else {
                                attributeSticker.stickerset = new TLRPC.TL_inputStickerSetEmpty();
                            }
                            break;
                        }
                    }
                }
            }
            if (entities != null && !entities.isEmpty()) {
                newMsg.entities = entities;
                newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_ENTITIES;
            }
            if (caption != null) {
                newMsg.message = caption;
            } else if (newMsg.message == null) {
                newMsg.message = "";
            }
            if (newMsg.attachPath == null) {
                newMsg.attachPath = "";
            }
            newMsg.local_id = newMsg.id = getUserConfig().getNewMessageId();
            newMsg.out = true;
            if (isChannel && sendToPeer != null) {
                newMsg.from_id = new TLRPC.TL_peerChannel();
                newMsg.from_id.channel_id = sendToPeer.channel_id;
            } else if (fromPeer != null) {
                newMsg.from_id = fromPeer;
                if (rank != null) {
                    newMsg.post_author = rank;
                    newMsg.flags |= 65536;
                }
            } else {
                newMsg.from_id = new TLRPC.TL_peerUser();
                newMsg.from_id.user_id = myId;
                newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
            }
            getUserConfig().saveConfig(false);
        }
        newMsg.silent = !notify || MessagesController.getNotificationsSettings(currentAccount).getBoolean("silent_" + peer, false);
        if (newMsg.random_id == 0) {
            newMsg.random_id = getNextRandomId();
        }
        if (params != null && params.containsKey("bot")) {
            if (encryptedChat != null) {
                newMsg.via_bot_name = params.get("bot_name");
                if (newMsg.via_bot_name == null) {
                    newMsg.via_bot_name = "";
                }
            } else {
                newMsg.via_bot_id = Utilities.parseInt(params.get("bot"));
            }
            newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_BOT_ID;
        }
        newMsg.params = params;
        if (retryMessageObject == null || !retryMessageObject.resendAsIs) {
            newMsg.date = scheduleDate != 0 ? scheduleDate : getConnectionsManager().getCurrentTime();
            if (sendToPeer instanceof TLRPC.TL_inputPeerChannel) {
                if (scheduleDate == 0 && isChannel) {
                    newMsg.views = 1;
                    newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_VIEWS;
                }
                TLRPC.Chat chat = getMessagesController().getChat(sendToPeer.channel_id);
                if (chat != null) {
                    if (chat.megagroup) {
                        newMsg.unread = true;
                    } else {
                        newMsg.post = true;
                        if (chat.signatures) {
                            newMsg.from_id = new TLRPC.TL_peerUser();
                            newMsg.from_id.user_id = myId;
                        }
                    }
                }
            } else {
                newMsg.unread = true;
            }
        }
        newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
        newMsg.dialog_id = peer;
        if (replyToMsg != null) {
            newMsg.reply_to = new TLRPC.TL_messageReplyHeader();
            if (encryptedChat != null && replyToMsg.messageOwner.random_id != 0) {
                newMsg.reply_to.reply_to_random_id = replyToMsg.messageOwner.random_id;
                newMsg.flags |= TLRPC.MESSAGE_FLAG_REPLY;
            } else {
                newMsg.flags |= TLRPC.MESSAGE_FLAG_REPLY;
            }
            newMsg.reply_to.reply_to_msg_id = replyToMsg.getId();
            if (replyToTopMsg != null && replyToTopMsg != replyToMsg) {
                newMsg.reply_to.reply_to_top_id = replyToTopMsg.getId();
                newMsg.reply_to.flags |= 2;
            }
        }
        if (linkedToGroup != 0) {
            newMsg.replies = new TLRPC.TL_messageReplies();
            newMsg.replies.comments = true;
            newMsg.replies.channel_id = linkedToGroup;
            newMsg.replies.flags |= 1;
            newMsg.flags |= 8388608;
        }
        if (replyMarkup != null && encryptedChat == null) {
            newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_MARKUP;
            newMsg.reply_markup = replyMarkup;
        }
        if (!DialogObject.isEncryptedDialog(peer)) {
            newMsg.peer_id = getMessagesController().getPeer(peer);
            if (DialogObject.isUserDialog(peer)) {
                TLRPC.User sendToUser = getMessagesController().getUser(peer);
                if (sendToUser == null) {
                    processSentMessage(newMsg.id);
                    return;
                }
                if (sendToUser.bot) {
                    newMsg.unread = false;
                }
            }
        } else {
            newMsg.peer_id = new TLRPC.TL_peerUser();
            if (encryptedChat.participant_id == myId) {
                newMsg.peer_id.user_id = encryptedChat.admin_id;
            } else {
                newMsg.peer_id.user_id = encryptedChat.participant_id;
            }
            if (ttl != 0) {
                newMsg.ttl = ttl;
            } else {
                newMsg.ttl = encryptedChat.ttl;
                if (newMsg.ttl != 0 && newMsg.media != null) {
                    newMsg.media.ttl_seconds = newMsg.ttl;
                    newMsg.media.flags |= 4;
                }
            }
            if (newMsg.ttl != 0 && newMsg.media.document != null) {
                if (MessageObject.isVoiceMessage(newMsg)) {
                    int duration = 0;
                    for (int a = 0; a < newMsg.media.document.attributes.size(); a++) {
                        TLRPC.DocumentAttribute attribute = newMsg.media.document.attributes.get(a);
                        if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
                            duration = attribute.duration;
                            break;
                        }
                    }
                    newMsg.ttl = Math.max(newMsg.ttl, duration + 1);
                } else if (MessageObject.isVideoMessage(newMsg) || MessageObject.isRoundVideoMessage(newMsg)) {
                    int duration = 0;
                    for (int a = 0; a < newMsg.media.document.attributes.size(); a++) {
                        TLRPC.DocumentAttribute attribute = newMsg.media.document.attributes.get(a);
                        if (attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            duration = attribute.duration;
                            break;
                        }
                    }
                    newMsg.ttl = Math.max(newMsg.ttl, duration + 1);
                }
            }
        }
        if (MessageObject.isVoiceMessage(newMsg) || MessageObject.isRoundVideoMessage(newMsg)) {
            newMsg.media_unread = true;
        }
        if (newMsg.from_id == null) {
            newMsg.from_id = newMsg.peer_id;
        }
        newMsg.send_state = MessageObject.MESSAGE_SEND_STATE_SENDING;
        long groupId = 0;
        boolean isFinalGroupMedia = false;
        if (params != null) {
            String groupIdStr = params.get("groupId");
            if (groupIdStr != null) {
                groupId = Utilities.parseLong(groupIdStr);
                newMsg.grouped_id = groupId;
                newMsg.flags |= 131072;
            }
            isFinalGroupMedia = params.get("final") != null;
        }
        newMsgObj = new MessageObject(currentAccount, newMsg, replyToMsg, true, true);
        newMsgObj.sendAnimationData = sendAnimationData;
        newMsgObj.wasJustSent = true;
        newMsgObj.scheduled = scheduleDate != 0;
        if (!newMsgObj.isForwarded() && (newMsgObj.type == 3 || videoEditedInfo != null || newMsgObj.type == 2) && !TextUtils.isEmpty(newMsg.attachPath)) {
            newMsgObj.attachPathExists = true;
        }
        if (newMsgObj.videoEditedInfo != null && videoEditedInfo == null) {
            videoEditedInfo = newMsgObj.videoEditedInfo;
        }
        if (groupId == 0) {
            ArrayList<MessageObject> objArr = new ArrayList<>();
            objArr.add(newMsgObj);
            ArrayList<TLRPC.Message> arr = new ArrayList<>();
            arr.add(newMsg);
            MessagesStorage.getInstance(currentAccount).putMessages(arr, false, true, false, 0, scheduleDate != 0);
            MessagesController.getInstance(currentAccount).updateInterfaceWithMessages(peer, objArr, scheduleDate != 0);
            if (scheduleDate == 0) {
                NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.dialogsNeedReload);
            }
        } else {
            String key = "group_" + groupId;
            ArrayList<DelayedMessage> arrayList = delayedMessages.get(key);
            if (arrayList != null) {
                delayedMessage = arrayList.get(0);
            }
            if (delayedMessage == null) {
                delayedMessage = new DelayedMessage(peer);
                delayedMessage.initForGroup(groupId);
                delayedMessage.encryptedChat = encryptedChat;
                delayedMessage.scheduled = scheduleDate != 0;
            }
            delayedMessage.performMediaUpload = false;
            delayedMessage.photoSize = null;
            delayedMessage.videoEditedInfo = null;
            delayedMessage.httpLocation = null;
            if (isFinalGroupMedia) {
                delayedMessage.finalGroupMessage = newMsg.id;
            }
        }
        if (BuildVars.LOGS_ENABLED) {
            if (sendToPeer != null) {
                FileLog.d("send message user_id = " + sendToPeer.user_id + " chat_id = " + sendToPeer.chat_id + " channel_id = " + sendToPeer.channel_id + " access_hash = " + sendToPeer.access_hash + " notify = " + notify + " silent = " + MessagesController.getNotificationsSettings(currentAccount).getBoolean("silent_" + peer, false));
            }
        }
        boolean performMediaUpload = false;
        if (type == 0 || type == 9 && message != null && encryptedChat != null) {
            if (encryptedChat == null) {
                TLRPC.TL_messages_sendMessage reqSend = new TLRPC.TL_messages_sendMessage();
                reqSend.message = message;
                reqSend.clear_draft = retryMessageObject == null;
                reqSend.silent = newMsg.silent;
                reqSend.peer = sendToPeer;
                reqSend.random_id = newMsg.random_id;
                if (newMsg.from_id != null) {
                    reqSend.send_as = getMessagesController().getInputPeer(newMsg.from_id);
                }
                if (newMsg.reply_to != null && newMsg.reply_to.reply_to_msg_id != 0) {
                    reqSend.flags |= 1;
                    reqSend.reply_to_msg_id = newMsg.reply_to.reply_to_msg_id;
                }
                if (!searchLinks) {
                    reqSend.no_webpage = true;
                }
                if (entities != null && !entities.isEmpty()) {
                    reqSend.entities = entities;
                    reqSend.flags |= 8;
                }
                if (scheduleDate != 0) {
                    reqSend.schedule_date = scheduleDate;
                    reqSend.flags |= 1024;
                }
                performSendMessageRequest(reqSend, newMsgObj, null, null, parentObject, params, scheduleDate != 0);
                if (retryMessageObject == null) {
                    getMediaDataController().cleanDraft(peer, replyToTopMsg != null ? replyToTopMsg.getId() : 0, false);
                }
            } else {
                TLRPC.TL_decryptedMessage reqSend;
                reqSend = new TLRPC.TL_decryptedMessage();
                reqSend.ttl = newMsg.ttl;
                if (entities != null && !entities.isEmpty()) {
                    reqSend.entities = entities;
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_ENTITIES;
                }
                if (newMsg.reply_to != null && newMsg.reply_to.reply_to_random_id != 0) {
                    reqSend.reply_to_random_id = newMsg.reply_to.reply_to_random_id;
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_REPLY;
                }
                if (params != null && params.get("bot_name") != null) {
                    reqSend.via_bot_name = params.get("bot_name");
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_BOT_ID;
                }
                reqSend.silent = newMsg.silent;
                reqSend.random_id = newMsg.random_id;
                reqSend.message = message;
                if (webPage != null && webPage.url != null) {
                    reqSend.media = new TLRPC.TL_decryptedMessageMediaWebPage();
                    reqSend.media.url = webPage.url;
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
                } else {
                    reqSend.media = new TLRPC.TL_decryptedMessageMediaEmpty();
                }
                getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, null, null, newMsgObj);
                if (retryMessageObject == null) {
                    getMediaDataController().cleanDraft(peer, replyToTopMsg != null ? replyToTopMsg.getId() : 0, false);
                }
            }
        } else if (type >= 1 && type <= 3 || type >= 5 && type <= 8 || type == 9 && encryptedChat != null || type == 10 || type == 11) {
            if (encryptedChat == null) {
                TLRPC.InputMedia inputMedia = null;
                if (type == 1) {
                    if (location instanceof TLRPC.TL_messageMediaVenue) {
                        inputMedia = new TLRPC.TL_inputMediaVenue();
                        inputMedia.address = location.address;
                        inputMedia.title = location.title;
                        inputMedia.provider = location.provider;
                        inputMedia.venue_id = location.venue_id;
                        inputMedia.venue_type = "";
                    } else if (location instanceof TLRPC.TL_messageMediaGeoLive) {
                        inputMedia = new TLRPC.TL_inputMediaGeoLive();
                        inputMedia.period = location.period;
                        inputMedia.flags |= 2;
                        if (location.heading != 0) {
                            inputMedia.heading = location.heading;
                            inputMedia.flags |= 4;
                        }
                        if (location.proximity_notification_radius != 0) {
                            inputMedia.proximity_notification_radius = location.proximity_notification_radius;
                            inputMedia.flags |= 8;
                        }
                    } else {
                        inputMedia = new TLRPC.TL_inputMediaGeoPoint();
                    }
                    inputMedia.geo_point = new TLRPC.TL_inputGeoPoint();
                    inputMedia.geo_point.lat = location.geo.lat;
                    inputMedia.geo_point._long = location.geo._long;
                } else if (type == 2 || type == 9 && photo != null) {
                    TLRPC.TL_inputMediaUploadedPhoto uploadedPhoto = new TLRPC.TL_inputMediaUploadedPhoto();
                    if (ttl != 0) {
                        newMsg.ttl = uploadedPhoto.ttl_seconds = ttl;
                        uploadedPhoto.flags |= 2;
                    }
                    if (params != null) {
                        String masks = params.get("masks");
                        if (masks != null) {
                            SerializedData serializedData = new SerializedData(Utilities.hexToBytes(masks));
                            int count = serializedData.readInt32(false);
                            for (int a = 0; a < count; a++) {
                                uploadedPhoto.stickers.add(TLRPC.InputDocument.TLdeserialize(serializedData, serializedData.readInt32(false), false));
                            }
                            uploadedPhoto.flags |= 1;
                            serializedData.cleanup();
                        }
                    }
                    if (photo.access_hash == 0) {
                        inputMedia = uploadedPhoto;
                        performMediaUpload = true;
                    } else {
                        TLRPC.TL_inputMediaPhoto media = new TLRPC.TL_inputMediaPhoto();
                        media.id = new TLRPC.TL_inputPhoto();
                        media.id.id = photo.id;
                        media.id.access_hash = photo.access_hash;
                        media.id.file_reference = photo.file_reference;
                        if (media.id.file_reference == null) {
                            media.id.file_reference = new byte[0];
                        }
                        inputMedia = media;
                    }
                    if (delayedMessage == null) {
                        delayedMessage = new DelayedMessage(peer);
                        delayedMessage.type = 0;
                        delayedMessage.obj = newMsgObj;
                        delayedMessage.originalPath = originalPath;
                        delayedMessage.scheduled = scheduleDate != 0;
                    }
                    delayedMessage.inputUploadMedia = uploadedPhoto;
                    delayedMessage.performMediaUpload = performMediaUpload;
                    if (path != null && path.length() > 0 && path.startsWith("http")) {
                        delayedMessage.httpLocation = path;
                    } else {
                        delayedMessage.photoSize = photo.sizes.get(photo.sizes.size() - 1);
                        delayedMessage.locationParent = photo;
                    }
                } else if (type == 3) {
                    TLRPC.TL_inputMediaUploadedDocument uploadedDocument = new TLRPC.TL_inputMediaUploadedDocument();
                    uploadedDocument.mime_type = document.mime_type;
                    uploadedDocument.attributes = document.attributes;
                    if (forceNoSoundVideo || !MessageObject.isRoundVideoDocument(document) && (videoEditedInfo == null || !videoEditedInfo.muted && !videoEditedInfo.roundVideo)) {
                        uploadedDocument.nosound_video = true;
                        if (BuildVars.DEBUG_VERSION) {
                            FileLog.d("nosound_video = true");
                        }
                    }
                    if (ttl != 0) {
                        newMsg.ttl = uploadedDocument.ttl_seconds = ttl;
                        uploadedDocument.flags |= 2;
                    }
                    if (params != null) {
                        String masks = params.get("masks");
                        if (masks != null) {
                            SerializedData serializedData = new SerializedData(Utilities.hexToBytes(masks));
                            int count = serializedData.readInt32(false);
                            for (int a = 0; a < count; a++) {
                                uploadedDocument.stickers.add(TLRPC.InputDocument.TLdeserialize(serializedData, serializedData.readInt32(false), false));
                            }
                            uploadedDocument.flags |= 1;
                            serializedData.cleanup();
                        }
                    }
                    if (document.access_hash == 0) {
                        inputMedia = uploadedDocument;
                        performMediaUpload = true;
                    } else {
                        TLRPC.TL_inputMediaDocument media = new TLRPC.TL_inputMediaDocument();
                        media.id = new TLRPC.TL_inputDocument();
                        media.id.id = document.id;
                        media.id.access_hash = document.access_hash;
                        media.id.file_reference = document.file_reference;
                        if (media.id.file_reference == null) {
                            media.id.file_reference = new byte[0];
                        }
                        if (params != null && params.containsKey("query")) {
                            media.query = params.get("query");
                            media.flags |= 2;
                        }
                        inputMedia = media;
                    }
                    if (delayedMessage == null) {
                        delayedMessage = new DelayedMessage(peer);
                        delayedMessage.type = 1;
                        delayedMessage.obj = newMsgObj;
                        delayedMessage.originalPath = originalPath;
                        delayedMessage.parentObject = parentObject;
                        delayedMessage.scheduled = scheduleDate != 0;
                    }
                    delayedMessage.inputUploadMedia = uploadedDocument;
                    delayedMessage.performMediaUpload = performMediaUpload;
                    if (!document.thumbs.isEmpty()) {
                        TLRPC.PhotoSize photoSize = document.thumbs.get(0);
                        if (!(photoSize instanceof TLRPC.TL_photoStrippedSize)) {
                            delayedMessage.photoSize = photoSize;
                            delayedMessage.locationParent = document;
                        }
                    }
                    delayedMessage.videoEditedInfo = videoEditedInfo;
                } else if (type == 6) {
                    inputMedia = new TLRPC.TL_inputMediaContact();
                    inputMedia.phone_number = user.phone;
                    inputMedia.first_name = user.first_name;
                    inputMedia.last_name = user.last_name;
                    if (!user.restriction_reason.isEmpty() && user.restriction_reason.get(0).text.startsWith("BEGIN:VCARD")) {
                        inputMedia.vcard = user.restriction_reason.get(0).text;
                    } else {
                        inputMedia.vcard = "";
                    }
                } else if (type == 7 || type == 9) {
                    boolean http = false;
                    TLRPC.InputMedia uploadedMedia;
                    if (originalPath != null || path != null || document.access_hash == 0) {
                        uploadedMedia = new TLRPC.TL_inputMediaUploadedDocument();
                        if (ttl != 0) {
                            newMsg.ttl = uploadedMedia.ttl_seconds = ttl;
                            uploadedMedia.flags |= 2;
                        }
                        if (forceNoSoundVideo || !TextUtils.isEmpty(path) && path.toLowerCase().endsWith("mp4") && (params == null || params.containsKey("forceDocument"))) {
                            uploadedMedia.nosound_video = true;
                        }
                        uploadedMedia.force_file = params != null && params.containsKey("forceDocument");
                        uploadedMedia.mime_type = document.mime_type;
                        uploadedMedia.attributes = document.attributes;
                    } else {
                        uploadedMedia = null;
                    }
                    if (document.access_hash == 0) {
                        inputMedia = uploadedMedia;
                        performMediaUpload = uploadedMedia instanceof TLRPC.TL_inputMediaUploadedDocument;
                    } else {
                        TLRPC.TL_inputMediaDocument media = new TLRPC.TL_inputMediaDocument();
                        media.id = new TLRPC.TL_inputDocument();
                        media.id.id = document.id;
                        media.id.access_hash = document.access_hash;
                        media.id.file_reference = document.file_reference;
                        if (media.id.file_reference == null) {
                            media.id.file_reference = new byte[0];
                        }
                        if (params != null && params.containsKey("query")) {
                            media.query = params.get("query");
                            media.flags |= 2;
                        }
                        inputMedia = media;
                    }
                    if (!http && uploadedMedia != null) {
                        if (delayedMessage == null) {
                            delayedMessage = new DelayedMessage(peer);
                            delayedMessage.type = 2;
                            delayedMessage.obj = newMsgObj;
                            delayedMessage.originalPath = originalPath;
                            delayedMessage.parentObject = parentObject;
                            delayedMessage.scheduled = scheduleDate != 0;
                        }
                        delayedMessage.inputUploadMedia = uploadedMedia;
                        delayedMessage.performMediaUpload = performMediaUpload;
                        if (!document.thumbs.isEmpty()) {
                            TLRPC.PhotoSize photoSize = document.thumbs.get(0);
                            if (!(photoSize instanceof TLRPC.TL_photoStrippedSize)) {
                                delayedMessage.photoSize = photoSize;
                                delayedMessage.locationParent = document;
                            }
                        }
                    }
                } else if (type == 8) {
                    TLRPC.TL_inputMediaUploadedDocument uploadedDocument = new TLRPC.TL_inputMediaUploadedDocument();
                    uploadedDocument.mime_type = document.mime_type;
                    uploadedDocument.attributes = document.attributes;
                    if (ttl != 0) {
                        newMsg.ttl = uploadedDocument.ttl_seconds = ttl;
                        uploadedDocument.flags |= 2;
                    }
                    if (document.access_hash == 0) {
                        inputMedia = uploadedDocument;
                        performMediaUpload = true;
                    } else {
                        TLRPC.TL_inputMediaDocument media = new TLRPC.TL_inputMediaDocument();
                        media.id = new TLRPC.TL_inputDocument();
                        media.id.id = document.id;
                        media.id.access_hash = document.access_hash;
                        media.id.file_reference = document.file_reference;
                        if (media.id.file_reference == null) {
                            media.id.file_reference = new byte[0];
                        }
                        if (params != null && params.containsKey("query")) {
                            media.query = params.get("query");
                            media.flags |= 2;
                        }
                        inputMedia = media;
                    }
                    delayedMessage = new DelayedMessage(peer);
                    delayedMessage.type = 3;
                    delayedMessage.obj = newMsgObj;
                    delayedMessage.parentObject = parentObject;
                    delayedMessage.inputUploadMedia = uploadedDocument;
                    delayedMessage.performMediaUpload = performMediaUpload;
                    delayedMessage.scheduled = scheduleDate != 0;
                } else if (type == 10) {
                    TLRPC.TL_inputMediaPoll inputMediaPoll = new TLRPC.TL_inputMediaPoll();
                    inputMediaPoll.poll = poll.poll;
                    if (params != null && params.containsKey("answers")) {
                        byte[] answers = Utilities.hexToBytes(params.get("answers"));
                        if (answers.length > 0) {
                            for (int a = 0; a < answers.length; a++) {
                                inputMediaPoll.correct_answers.add(new byte[] { answers[a] });
                            }
                            inputMediaPoll.flags |= 1;
                        }
                    }
                    if (poll.results != null && !TextUtils.isEmpty(poll.results.solution)) {
                        inputMediaPoll.solution = poll.results.solution;
                        inputMediaPoll.solution_entities = poll.results.solution_entities;
                        inputMediaPoll.flags |= 2;
                    }
                    inputMedia = inputMediaPoll;
                } else if (type == 11) {
                    TLRPC.TL_inputMediaDice inputMediaDice = new TLRPC.TL_inputMediaDice();
                    inputMediaDice.emoticon = message;
                    inputMedia = inputMediaDice;
                }
                TLObject reqSend;
                if (groupId != 0) {
                    TLRPC.TL_messages_sendMultiMedia request;
                    if (delayedMessage.sendRequest != null) {
                        request = (TLRPC.TL_messages_sendMultiMedia) delayedMessage.sendRequest;
                    } else {
                        request = new TLRPC.TL_messages_sendMultiMedia();
                        request.peer = sendToPeer;
                        request.silent = newMsg.silent;
                        if (newMsg.reply_to != null && newMsg.reply_to.reply_to_msg_id != 0) {
                            request.flags |= 1;
                            request.reply_to_msg_id = newMsg.reply_to.reply_to_msg_id;
                        }
                        if (scheduleDate != 0) {
                            request.schedule_date = scheduleDate;
                            request.flags |= 1024;
                        }
                        delayedMessage.sendRequest = request;
                    }
                    delayedMessage.messageObjects.add(newMsgObj);
                    delayedMessage.parentObjects.add(parentObject);
                    delayedMessage.locations.add(delayedMessage.photoSize);
                    delayedMessage.videoEditedInfos.add(delayedMessage.videoEditedInfo);
                    delayedMessage.httpLocations.add(delayedMessage.httpLocation);
                    delayedMessage.inputMedias.add(delayedMessage.inputUploadMedia);
                    delayedMessage.messages.add(newMsg);
                    delayedMessage.originalPaths.add(originalPath);
                    TLRPC.TL_inputSingleMedia inputSingleMedia = new TLRPC.TL_inputSingleMedia();
                    inputSingleMedia.random_id = newMsg.random_id;
                    inputSingleMedia.media = inputMedia;
                    inputSingleMedia.message = caption;
                    if (entities != null && !entities.isEmpty()) {
                        inputSingleMedia.entities = entities;
                        inputSingleMedia.flags |= 1;
                    }
                    request.multi_media.add(inputSingleMedia);
                    reqSend = request;
                } else {
                    TLRPC.TL_messages_sendMedia request = new TLRPC.TL_messages_sendMedia();
                    request.peer = sendToPeer;
                    request.silent = newMsg.silent;
                    if (newMsg.reply_to != null && newMsg.reply_to.reply_to_msg_id != 0) {
                        request.flags |= 1;
                        request.reply_to_msg_id = newMsg.reply_to.reply_to_msg_id;
                    }
                    request.random_id = newMsg.random_id;
                    if (newMsg.from_id != null) {
                        request.send_as = getMessagesController().getInputPeer(newMsg.from_id);
                    }
                    request.media = inputMedia;
                    request.message = caption;
                    if (entities != null && !entities.isEmpty()) {
                        request.entities = entities;
                        request.flags |= 8;
                    }
                    if (scheduleDate != 0) {
                        request.schedule_date = scheduleDate;
                        request.flags |= 1024;
                    }
                    if (delayedMessage != null) {
                        delayedMessage.sendRequest = request;
                    }
                    reqSend = request;
                }
                if (groupId != 0) {
                    performSendDelayedMessage(delayedMessage);
                } else if (type == 1) {
                    performSendMessageRequest(reqSend, newMsgObj, null, delayedMessage, parentObject, params, scheduleDate != 0);
                } else if (type == 2) {
                    if (performMediaUpload) {
                        performSendDelayedMessage(delayedMessage);
                    } else {
                        performSendMessageRequest(reqSend, newMsgObj, originalPath, null, true, delayedMessage, parentObject, params, scheduleDate != 0);
                    }
                } else if (type == 3) {
                    if (performMediaUpload) {
                        performSendDelayedMessage(delayedMessage);
                    } else {
                        performSendMessageRequest(reqSend, newMsgObj, originalPath, delayedMessage, parentObject, params, scheduleDate != 0);
                    }
                } else if (type == 6) {
                    performSendMessageRequest(reqSend, newMsgObj, originalPath, delayedMessage, parentObject, params, scheduleDate != 0);
                } else if (type == 7) {
                    if (performMediaUpload && delayedMessage != null) {
                        performSendDelayedMessage(delayedMessage);
                    } else {
                        performSendMessageRequest(reqSend, newMsgObj, originalPath, delayedMessage, parentObject, params, scheduleDate != 0);
                    }
                } else if (type == 8) {
                    if (performMediaUpload) {
                        performSendDelayedMessage(delayedMessage);
                    } else {
                        performSendMessageRequest(reqSend, newMsgObj, originalPath, delayedMessage, parentObject, params, scheduleDate != 0);
                    }
                } else if (type == 10 || type == 11) {
                    performSendMessageRequest(reqSend, newMsgObj, originalPath, delayedMessage, parentObject, params, scheduleDate != 0);
                }
            } else {
                TLRPC.TL_decryptedMessage reqSend;
                if (AndroidUtilities.getPeerLayerVersion(encryptedChat.layer) >= 73) {
                    reqSend = new TLRPC.TL_decryptedMessage();
                    if (groupId != 0) {
                        reqSend.grouped_id = groupId;
                        reqSend.flags |= 131072;
                    }
                } else {
                    reqSend = new TLRPC.TL_decryptedMessage_layer45();
                }
                reqSend.ttl = newMsg.ttl;
                if (entities != null && !entities.isEmpty()) {
                    reqSend.entities = entities;
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_ENTITIES;
                }
                if (newMsg.reply_to != null && newMsg.reply_to.reply_to_random_id != 0) {
                    reqSend.reply_to_random_id = newMsg.reply_to.reply_to_random_id;
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_REPLY;
                }
                reqSend.silent = newMsg.silent;
                reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
                if (params != null && params.get("bot_name") != null) {
                    reqSend.via_bot_name = params.get("bot_name");
                    reqSend.flags |= TLRPC.MESSAGE_FLAG_HAS_BOT_ID;
                }
                reqSend.random_id = newMsg.random_id;
                reqSend.message = "";
                if (type == 1) {
                    if (location instanceof TLRPC.TL_messageMediaVenue) {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaVenue();
                        reqSend.media.address = location.address;
                        reqSend.media.title = location.title;
                        reqSend.media.provider = location.provider;
                        reqSend.media.venue_id = location.venue_id;
                    } else {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaGeoPoint();
                    }
                    reqSend.media.lat = location.geo.lat;
                    reqSend.media._long = location.geo._long;
                    getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, null, null, newMsgObj);
                } else if (type == 2 || type == 9 && photo != null) {
                    TLRPC.PhotoSize small = photo.sizes.get(0);
                    TLRPC.PhotoSize big = photo.sizes.get(photo.sizes.size() - 1);
                    ImageLoader.fillPhotoSizeWithBytes(small);
                    reqSend.media = new TLRPC.TL_decryptedMessageMediaPhoto();
                    reqSend.media.caption = caption;
                    if (small.bytes != null) {
                        ((TLRPC.TL_decryptedMessageMediaPhoto) reqSend.media).thumb = small.bytes;
                    } else {
                        ((TLRPC.TL_decryptedMessageMediaPhoto) reqSend.media).thumb = new byte[0];
                    }
                    reqSend.media.thumb_h = small.h;
                    reqSend.media.thumb_w = small.w;
                    reqSend.media.w = big.w;
                    reqSend.media.h = big.h;
                    reqSend.media.size = big.size;
                    if (big.location.key == null || groupId != 0) {
                        if (delayedMessage == null) {
                            delayedMessage = new DelayedMessage(peer);
                            delayedMessage.encryptedChat = encryptedChat;
                            delayedMessage.type = 0;
                            delayedMessage.originalPath = originalPath;
                            delayedMessage.sendEncryptedRequest = reqSend;
                            delayedMessage.obj = newMsgObj;
                            if (params != null && params.containsKey("parentObject")) {
                                delayedMessage.parentObject = params.get("parentObject");
                            } else {
                                delayedMessage.parentObject = parentObject;
                            }
                            delayedMessage.performMediaUpload = true;
                            delayedMessage.scheduled = scheduleDate != 0;
                        }
                        if (!TextUtils.isEmpty(path) && path.startsWith("http")) {
                            delayedMessage.httpLocation = path;
                        } else {
                            delayedMessage.photoSize = photo.sizes.get(photo.sizes.size() - 1);
                            delayedMessage.locationParent = photo;
                        }
                        if (groupId == 0) {
                            performSendDelayedMessage(delayedMessage);
                        }
                    } else {
                        TLRPC.TL_inputEncryptedFile encryptedFile = new TLRPC.TL_inputEncryptedFile();
                        encryptedFile.id = big.location.volume_id;
                        encryptedFile.access_hash = big.location.secret;
                        reqSend.media.key = big.location.key;
                        reqSend.media.iv = big.location.iv;
                        getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, encryptedFile, null, newMsgObj);
                    }
                } else if (type == 3) {
                    TLRPC.PhotoSize thumb = getThumbForSecretChat(document.thumbs);
                    ImageLoader.fillPhotoSizeWithBytes(thumb);
                    if (MessageObject.isNewGifDocument(document) || MessageObject.isRoundVideoDocument(document)) {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaDocument();
                        reqSend.media.attributes = document.attributes;
                        if (thumb != null && thumb.bytes != null) {
                            ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = thumb.bytes;
                        } else {
                            ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = new byte[0];
                        }
                    } else {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaVideo();
                        if (thumb != null && thumb.bytes != null) {
                            ((TLRPC.TL_decryptedMessageMediaVideo) reqSend.media).thumb = thumb.bytes;
                        } else {
                            ((TLRPC.TL_decryptedMessageMediaVideo) reqSend.media).thumb = new byte[0];
                        }
                    }
                    reqSend.media.caption = caption;
                    reqSend.media.mime_type = "video/mp4";
                    reqSend.media.size = document.size;
                    for (int a = 0; a < document.attributes.size(); a++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(a);
                        if (attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            reqSend.media.w = attribute.w;
                            reqSend.media.h = attribute.h;
                            reqSend.media.duration = attribute.duration;
                            break;
                        }
                    }
                    reqSend.media.thumb_h = thumb.h;
                    reqSend.media.thumb_w = thumb.w;
                    if (document.key == null || groupId != 0) {
                        if (delayedMessage == null) {
                            delayedMessage = new DelayedMessage(peer);
                            delayedMessage.encryptedChat = encryptedChat;
                            delayedMessage.type = 1;
                            delayedMessage.sendEncryptedRequest = reqSend;
                            delayedMessage.originalPath = originalPath;
                            delayedMessage.obj = newMsgObj;
                            if (params != null && params.containsKey("parentObject")) {
                                delayedMessage.parentObject = params.get("parentObject");
                            } else {
                                delayedMessage.parentObject = parentObject;
                            }
                            delayedMessage.performMediaUpload = true;
                            delayedMessage.scheduled = scheduleDate != 0;
                        }
                        delayedMessage.videoEditedInfo = videoEditedInfo;
                        if (groupId == 0) {
                            performSendDelayedMessage(delayedMessage);
                        }
                    } else {
                        TLRPC.TL_inputEncryptedFile encryptedFile = new TLRPC.TL_inputEncryptedFile();
                        encryptedFile.id = document.id;
                        encryptedFile.access_hash = document.access_hash;
                        reqSend.media.key = document.key;
                        reqSend.media.iv = document.iv;
                        getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, encryptedFile, null, newMsgObj);
                    }
                } else if (type == 6) {
                    reqSend.media = new TLRPC.TL_decryptedMessageMediaContact();
                    reqSend.media.phone_number = user.phone;
                    reqSend.media.first_name = user.first_name;
                    reqSend.media.last_name = user.last_name;
                    reqSend.media.user_id = user.id;
                    getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, null, null, newMsgObj);
                } else if (type == 7 || type == 9 && document != null) {
                    if (document.access_hash != 0 && (MessageObject.isStickerDocument(document) || MessageObject.isAnimatedStickerDocument(document, true))) {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaExternalDocument();
                        reqSend.media.id = document.id;
                        reqSend.media.date = document.date;
                        reqSend.media.access_hash = document.access_hash;
                        reqSend.media.mime_type = document.mime_type;
                        reqSend.media.size = document.size;
                        reqSend.media.dc_id = document.dc_id;
                        reqSend.media.attributes = document.attributes;
                        TLRPC.PhotoSize thumb = getThumbForSecretChat(document.thumbs);
                        if (thumb != null) {
                            ((TLRPC.TL_decryptedMessageMediaExternalDocument) reqSend.media).thumb = thumb;
                        } else {
                            ((TLRPC.TL_decryptedMessageMediaExternalDocument) reqSend.media).thumb = new TLRPC.TL_photoSizeEmpty();
                            ((TLRPC.TL_decryptedMessageMediaExternalDocument) reqSend.media).thumb.type = "s";
                        }
                        if (delayedMessage != null && delayedMessage.type == 5) {
                            delayedMessage.sendEncryptedRequest = reqSend;
                            delayedMessage.obj = newMsgObj;
                            performSendDelayedMessage(delayedMessage);
                        } else {
                            getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, null, null, newMsgObj);
                        }
                    } else {
                        reqSend.media = new TLRPC.TL_decryptedMessageMediaDocument();
                        reqSend.media.attributes = document.attributes;
                        reqSend.media.caption = caption;
                        TLRPC.PhotoSize thumb = getThumbForSecretChat(document.thumbs);
                        if (thumb != null) {
                            ImageLoader.fillPhotoSizeWithBytes(thumb);
                            ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = thumb.bytes;
                            reqSend.media.thumb_h = thumb.h;
                            reqSend.media.thumb_w = thumb.w;
                        } else {
                            ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = new byte[0];
                            reqSend.media.thumb_h = 0;
                            reqSend.media.thumb_w = 0;
                        }
                        reqSend.media.size = document.size;
                        reqSend.media.mime_type = document.mime_type;
                        if (document.key == null || groupId != 0) {
                            if (delayedMessage == null) {
                                delayedMessage = new DelayedMessage(peer);
                                delayedMessage.encryptedChat = encryptedChat;
                                delayedMessage.type = 2;
                                delayedMessage.sendEncryptedRequest = reqSend;
                                delayedMessage.originalPath = originalPath;
                                delayedMessage.obj = newMsgObj;
                                if (params != null && params.containsKey("parentObject")) {
                                    delayedMessage.parentObject = params.get("parentObject");
                                } else {
                                    delayedMessage.parentObject = parentObject;
                                }
                                delayedMessage.performMediaUpload = true;
                                delayedMessage.scheduled = scheduleDate != 0;
                            }
                            if (path != null && path.length() > 0 && path.startsWith("http")) {
                                delayedMessage.httpLocation = path;
                            }
                            if (groupId == 0) {
                                performSendDelayedMessage(delayedMessage);
                            }
                        } else {
                            TLRPC.TL_inputEncryptedFile encryptedFile = new TLRPC.TL_inputEncryptedFile();
                            encryptedFile.id = document.id;
                            encryptedFile.access_hash = document.access_hash;
                            reqSend.media.key = document.key;
                            reqSend.media.iv = document.iv;
                            getSecretChatHelper().performSendEncryptedRequest(reqSend, newMsgObj.messageOwner, encryptedChat, encryptedFile, null, newMsgObj);
                        }
                    }
                } else if (type == 8) {
                    delayedMessage = new DelayedMessage(peer);
                    delayedMessage.encryptedChat = encryptedChat;
                    delayedMessage.sendEncryptedRequest = reqSend;
                    delayedMessage.obj = newMsgObj;
                    delayedMessage.type = 3;
                    delayedMessage.parentObject = parentObject;
                    delayedMessage.performMediaUpload = true;
                    delayedMessage.scheduled = scheduleDate != 0;
                    reqSend.media = new TLRPC.TL_decryptedMessageMediaDocument();
                    reqSend.media.attributes = document.attributes;
                    reqSend.media.caption = caption;
                    TLRPC.PhotoSize thumb = getThumbForSecretChat(document.thumbs);
                    if (thumb != null) {
                        ImageLoader.fillPhotoSizeWithBytes(thumb);
                        ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = thumb.bytes;
                        reqSend.media.thumb_h = thumb.h;
                        reqSend.media.thumb_w = thumb.w;
                    } else {
                        ((TLRPC.TL_decryptedMessageMediaDocument) reqSend.media).thumb = new byte[0];
                        reqSend.media.thumb_h = 0;
                        reqSend.media.thumb_w = 0;
                    }
                    reqSend.media.mime_type = document.mime_type;
                    reqSend.media.size = document.size;
                    delayedMessage.originalPath = originalPath;
                    performSendDelayedMessage(delayedMessage);
                }
                if (groupId != 0) {
                    TLRPC.TL_messages_sendEncryptedMultiMedia request;
                    if (delayedMessage.sendEncryptedRequest != null) {
                        request = (TLRPC.TL_messages_sendEncryptedMultiMedia) delayedMessage.sendEncryptedRequest;
                    } else {
                        request = new TLRPC.TL_messages_sendEncryptedMultiMedia();
                        delayedMessage.sendEncryptedRequest = request;
                    }
                    delayedMessage.messageObjects.add(newMsgObj);
                    delayedMessage.messages.add(newMsg);
                    delayedMessage.originalPaths.add(originalPath);
                    delayedMessage.performMediaUpload = true;
                    request.messages.add(reqSend);
                    TLRPC.TL_inputEncryptedFile encryptedFile = new TLRPC.TL_inputEncryptedFile();
                    encryptedFile.id = type == 3 || type == 7 ? 1 : 0;
                    request.files.add(encryptedFile);
                    performSendDelayedMessage(delayedMessage);
                }
                if (retryMessageObject == null) {
                    getMediaDataController().cleanDraft(peer, replyToTopMsg != null ? replyToTopMsg.getId() : 0, false);
                }
            }
        } else if (type == 4) {
            TLRPC.TL_messages_forwardMessages reqSend = new TLRPC.TL_messages_forwardMessages();
            reqSend.to_peer = sendToPeer;
            reqSend.with_my_score = retryMessageObject.messageOwner.with_my_score;
            if (params != null && params.containsKey("fwd_id")) {
                int fwdId = Utilities.parseInt(params.get("fwd_id"));
                reqSend.drop_author = true;
                long peerId = Utilities.parseLong(params.get("fwd_peer"));
                if (peerId < 0) {
                    TLRPC.Chat chat = getMessagesController().getChat(-peerId);
                    if (ChatObject.isChannel(chat)) {
                        reqSend.from_peer = new TLRPC.TL_inputPeerChannel();
                        reqSend.from_peer.channel_id = chat.id;
                        reqSend.from_peer.access_hash = chat.access_hash;
                    } else {
                        reqSend.from_peer = new TLRPC.TL_inputPeerEmpty();
                    }
                } else {
                    reqSend.from_peer = new TLRPC.TL_inputPeerEmpty();
                }
                reqSend.id.add(fwdId);
            } else {
                reqSend.from_peer = new TLRPC.TL_inputPeerEmpty();
            }
            reqSend.silent = newMsg.silent;
            if (scheduleDate != 0) {
                reqSend.schedule_date = scheduleDate;
                reqSend.flags |= 1024;
            }
            reqSend.random_id.add(newMsg.random_id);
            if (retryMessageObject.getId() >= 0) {
                reqSend.id.add(retryMessageObject.getId());
            } else {
                if (retryMessageObject.messageOwner.fwd_msg_id != 0) {
                    reqSend.id.add(retryMessageObject.messageOwner.fwd_msg_id);
                } else if (retryMessageObject.messageOwner.fwd_from != null) {
                    reqSend.id.add(retryMessageObject.messageOwner.fwd_from.channel_post);
                }
            }
            performSendMessageRequest(reqSend, newMsgObj, null, null, parentObject, params, scheduleDate != 0);
        } else if (type == 9) {
            TLRPC.TL_messages_sendInlineBotResult reqSend = new TLRPC.TL_messages_sendInlineBotResult();
            reqSend.peer = sendToPeer;
            reqSend.random_id = newMsg.random_id;
            if (newMsg.from_id != null) {
                reqSend.send_as = getMessagesController().getInputPeer(newMsg.from_id);
            }
            reqSend.hide_via = !params.containsKey("bot");
            if (newMsg.reply_to != null && newMsg.reply_to.reply_to_msg_id != 0) {
                reqSend.flags |= 1;
                reqSend.reply_to_msg_id = newMsg.reply_to.reply_to_msg_id;
            }
            reqSend.silent = newMsg.silent;
            if (scheduleDate != 0) {
                reqSend.schedule_date = scheduleDate;
                reqSend.flags |= 1024;
            }
            reqSend.query_id = Utilities.parseLong(params.get("query_id"));
            reqSend.id = params.get("id");
            if (retryMessageObject == null) {
                reqSend.clear_draft = true;
                getMediaDataController().cleanDraft(peer, replyToTopMsg != null ? replyToTopMsg.getId() : 0, false);
            }
            performSendMessageRequest(reqSend, newMsgObj, null, null, parentObject, params, scheduleDate != 0);
        }
    } catch (Exception e) {
        FileLog.e(e);
        getMessagesStorage().markMessageAsSendError(newMsg, scheduleDate != 0);
        if (newMsgObj != null) {
            newMsgObj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR;
        }
        getNotificationCenter().postNotificationName(NotificationCenter.messageSendError, newMsg.id);
        processSentMessage(newMsg.id);
    }
}
Also used : ArrayList(java.util.ArrayList) SerializedData(org.telegram.tgnet.SerializedData) TLObject(org.telegram.tgnet.TLObject) HashMap(java.util.HashMap) TLRPC(org.telegram.tgnet.TLRPC) Point(org.telegram.ui.Components.Point)

Example 75 with TLObject

use of org.telegram.tgnet.TLObject in project Telegram-FOSS by Telegram-FOSS-Team.

the class SendMessagesHelper method updateMediaPaths.

private void updateMediaPaths(MessageObject newMsgObj, TLRPC.Message sentMessage, int newMsgId, String originalPath, boolean post) {
    TLRPC.Message newMsg = newMsgObj.messageOwner;
    TLRPC.PhotoSize strippedNew = null;
    if (newMsg.media != null) {
        TLRPC.PhotoSize strippedOld = null;
        TLObject photoObject = null;
        if (newMsgObj.isLiveLocation() && sentMessage.media instanceof TLRPC.TL_messageMediaGeoLive) {
            newMsg.media.period = sentMessage.media.period;
        } else if (newMsgObj.isDice()) {
            TLRPC.TL_messageMediaDice mediaDice = (TLRPC.TL_messageMediaDice) newMsg.media;
            TLRPC.TL_messageMediaDice mediaDiceNew = (TLRPC.TL_messageMediaDice) sentMessage.media;
            mediaDice.value = mediaDiceNew.value;
        } else if (newMsg.media.photo != null) {
            strippedOld = FileLoader.getClosestPhotoSizeWithSize(newMsg.media.photo.sizes, 40);
            if (sentMessage != null && sentMessage.media != null && sentMessage.media.photo != null) {
                strippedNew = FileLoader.getClosestPhotoSizeWithSize(sentMessage.media.photo.sizes, 40);
            } else {
                strippedNew = strippedOld;
            }
            photoObject = newMsg.media.photo;
        } else if (newMsg.media.document != null) {
            strippedOld = FileLoader.getClosestPhotoSizeWithSize(newMsg.media.document.thumbs, 40);
            if (sentMessage != null && sentMessage.media != null && sentMessage.media.document != null) {
                strippedNew = FileLoader.getClosestPhotoSizeWithSize(sentMessage.media.document.thumbs, 40);
            } else {
                strippedNew = strippedOld;
            }
            photoObject = newMsg.media.document;
        } else if (newMsg.media.webpage != null) {
            if (newMsg.media.webpage.photo != null) {
                strippedOld = FileLoader.getClosestPhotoSizeWithSize(newMsg.media.webpage.photo.sizes, 40);
                if (sentMessage != null && sentMessage.media != null && sentMessage.media.webpage != null && sentMessage.media.webpage.photo != null) {
                    strippedNew = FileLoader.getClosestPhotoSizeWithSize(sentMessage.media.webpage.photo.sizes, 40);
                } else {
                    strippedNew = strippedOld;
                }
                photoObject = newMsg.media.webpage.photo;
            } else if (newMsg.media.webpage.document != null) {
                strippedOld = FileLoader.getClosestPhotoSizeWithSize(newMsg.media.webpage.document.thumbs, 40);
                if (sentMessage != null && sentMessage.media != null && sentMessage.media.webpage != null && sentMessage.media.webpage.document != null) {
                    strippedNew = FileLoader.getClosestPhotoSizeWithSize(sentMessage.media.webpage.document.thumbs, 40);
                } else {
                    strippedNew = strippedOld;
                }
                photoObject = newMsg.media.webpage.document;
            }
        }
        if (strippedNew instanceof TLRPC.TL_photoStrippedSize && strippedOld instanceof TLRPC.TL_photoStrippedSize) {
            String oldKey = "stripped" + FileRefController.getKeyForParentObject(newMsgObj);
            String newKey = null;
            if (sentMessage != null) {
                newKey = "stripped" + FileRefController.getKeyForParentObject(sentMessage);
            } else {
                newKey = "stripped" + "message" + newMsgId + "_" + newMsgObj.getChannelId() + "_" + newMsgObj.scheduled;
            }
            ImageLoader.getInstance().replaceImageInCache(oldKey, newKey, ImageLocation.getForObject(strippedNew, photoObject), post);
        }
    }
    if (sentMessage == null) {
        return;
    }
    if (sentMessage.media instanceof TLRPC.TL_messageMediaPhoto && sentMessage.media.photo != null && newMsg.media instanceof TLRPC.TL_messageMediaPhoto && newMsg.media.photo != null) {
        if (sentMessage.media.ttl_seconds == 0 && !newMsgObj.scheduled) {
            getMessagesStorage().putSentFile(originalPath, sentMessage.media.photo, 0, "sent_" + sentMessage.peer_id.channel_id + "_" + sentMessage.id);
        }
        if (newMsg.media.photo.sizes.size() == 1 && newMsg.media.photo.sizes.get(0).location instanceof TLRPC.TL_fileLocationUnavailable) {
            newMsg.media.photo.sizes = sentMessage.media.photo.sizes;
        } else {
            for (int b = 0; b < newMsg.media.photo.sizes.size(); b++) {
                TLRPC.PhotoSize size2 = newMsg.media.photo.sizes.get(b);
                if (size2 == null || size2.location == null || size2.type == null) {
                    continue;
                }
                boolean found = false;
                for (int a = 0; a < sentMessage.media.photo.sizes.size(); a++) {
                    TLRPC.PhotoSize size = sentMessage.media.photo.sizes.get(a);
                    if (size == null || size.location == null || size instanceof TLRPC.TL_photoSizeEmpty || size.type == null) {
                        continue;
                    }
                    if (size2.location.volume_id == Integer.MIN_VALUE && size.type.equals(size2.type) || size.w == size2.w && size.h == size2.h) {
                        found = true;
                        String fileName = size2.location.volume_id + "_" + size2.location.local_id;
                        String fileName2 = size.location.volume_id + "_" + size.location.local_id;
                        if (fileName.equals(fileName2)) {
                            break;
                        }
                        File cacheFile = new File(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName + ".jpg");
                        File cacheFile2;
                        if (sentMessage.media.ttl_seconds == 0 && (sentMessage.media.photo.sizes.size() == 1 || size.w > 90 || size.h > 90)) {
                            cacheFile2 = FileLoader.getPathToAttach(size);
                        } else {
                            cacheFile2 = new File(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName2 + ".jpg");
                        }
                        cacheFile.renameTo(cacheFile2);
                        ImageLoader.getInstance().replaceImageInCache(fileName, fileName2, ImageLocation.getForPhoto(size, sentMessage.media.photo), post);
                        size2.location = size.location;
                        size2.size = size.size;
                        break;
                    }
                }
                if (!found) {
                    String fileName = size2.location.volume_id + "_" + size2.location.local_id;
                    File cacheFile = new File(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName + ".jpg");
                    cacheFile.delete();
                    if ("s".equals(size2.type) && strippedNew != null) {
                        newMsg.media.photo.sizes.set(b, strippedNew);
                        ImageLocation location = ImageLocation.getForPhoto(strippedNew, sentMessage.media.photo);
                        ImageLoader.getInstance().replaceImageInCache(fileName, location.getKey(sentMessage, null, false), location, post);
                    }
                }
            }
        }
        newMsg.message = sentMessage.message;
        sentMessage.attachPath = newMsg.attachPath;
        newMsg.media.photo.id = sentMessage.media.photo.id;
        newMsg.media.photo.dc_id = sentMessage.media.photo.dc_id;
        newMsg.media.photo.access_hash = sentMessage.media.photo.access_hash;
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaDocument && sentMessage.media.document != null && newMsg.media instanceof TLRPC.TL_messageMediaDocument && newMsg.media.document != null) {
        if (sentMessage.media.ttl_seconds == 0 && (newMsgObj.videoEditedInfo == null || newMsgObj.videoEditedInfo.mediaEntities == null && TextUtils.isEmpty(newMsgObj.videoEditedInfo.paintPath) && newMsgObj.videoEditedInfo.cropState == null)) {
            boolean isVideo = MessageObject.isVideoMessage(sentMessage);
            if ((isVideo || MessageObject.isGifMessage(sentMessage)) && MessageObject.isGifDocument(sentMessage.media.document) == MessageObject.isGifDocument(newMsg.media.document)) {
                if (!newMsgObj.scheduled) {
                    getMessagesStorage().putSentFile(originalPath, sentMessage.media.document, 2, "sent_" + sentMessage.peer_id.channel_id + "_" + sentMessage.id);
                }
                if (isVideo) {
                    sentMessage.attachPath = newMsg.attachPath;
                }
            } else if (!MessageObject.isVoiceMessage(sentMessage) && !MessageObject.isRoundVideoMessage(sentMessage) && !newMsgObj.scheduled) {
                getMessagesStorage().putSentFile(originalPath, sentMessage.media.document, 1, "sent_" + sentMessage.peer_id.channel_id + "_" + sentMessage.id);
            }
        }
        TLRPC.PhotoSize size2 = FileLoader.getClosestPhotoSizeWithSize(newMsg.media.document.thumbs, 320);
        TLRPC.PhotoSize size = FileLoader.getClosestPhotoSizeWithSize(sentMessage.media.document.thumbs, 320);
        if (size2 != null && size2.location != null && size2.location.volume_id == Integer.MIN_VALUE && size != null && size.location != null && !(size instanceof TLRPC.TL_photoSizeEmpty) && !(size2 instanceof TLRPC.TL_photoSizeEmpty)) {
            String fileName = size2.location.volume_id + "_" + size2.location.local_id;
            String fileName2 = size.location.volume_id + "_" + size.location.local_id;
            if (!fileName.equals(fileName2)) {
                File cacheFile = new File(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName + ".jpg");
                File cacheFile2 = new File(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName2 + ".jpg");
                cacheFile.renameTo(cacheFile2);
                ImageLoader.getInstance().replaceImageInCache(fileName, fileName2, ImageLocation.getForDocument(size, sentMessage.media.document), post);
                size2.location = size.location;
                size2.size = size.size;
            }
        } else if (size != null && size2 != null && MessageObject.isStickerMessage(sentMessage) && size2.location != null) {
            size.location = size2.location;
        } else if (size2 == null || size2 != null && size2.location instanceof TLRPC.TL_fileLocationUnavailable || size2 instanceof TLRPC.TL_photoSizeEmpty) {
            newMsg.media.document.thumbs = sentMessage.media.document.thumbs;
        }
        newMsg.media.document.dc_id = sentMessage.media.document.dc_id;
        newMsg.media.document.id = sentMessage.media.document.id;
        newMsg.media.document.access_hash = sentMessage.media.document.access_hash;
        byte[] oldWaveform = null;
        for (int a = 0; a < newMsg.media.document.attributes.size(); a++) {
            TLRPC.DocumentAttribute attribute = newMsg.media.document.attributes.get(a);
            if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
                oldWaveform = attribute.waveform;
                break;
            }
        }
        newMsg.media.document.attributes = sentMessage.media.document.attributes;
        if (oldWaveform != null) {
            for (int a = 0; a < newMsg.media.document.attributes.size(); a++) {
                TLRPC.DocumentAttribute attribute = newMsg.media.document.attributes.get(a);
                if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
                    attribute.waveform = oldWaveform;
                    attribute.flags |= 4;
                }
            }
        }
        newMsg.media.document.size = sentMessage.media.document.size;
        newMsg.media.document.mime_type = sentMessage.media.document.mime_type;
        if ((sentMessage.flags & TLRPC.MESSAGE_FLAG_FWD) == 0 && MessageObject.isOut(sentMessage)) {
            if (MessageObject.isNewGifDocument(sentMessage.media.document)) {
                boolean save;
                if (MessageObject.isDocumentHasAttachedStickers(sentMessage.media.document)) {
                    save = getMessagesController().saveGifsWithStickers;
                } else {
                    save = true;
                }
                if (save) {
                    getMediaDataController().addRecentGif(sentMessage.media.document, sentMessage.date);
                }
            } else if (MessageObject.isStickerDocument(sentMessage.media.document) || MessageObject.isAnimatedStickerDocument(sentMessage.media.document, true)) {
                getMediaDataController().addRecentSticker(MediaDataController.TYPE_IMAGE, sentMessage, sentMessage.media.document, sentMessage.date, false);
            }
        }
        if (newMsg.attachPath != null && newMsg.attachPath.startsWith(FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE).getAbsolutePath())) {
            File cacheFile = new File(newMsg.attachPath);
            File cacheFile2 = FileLoader.getPathToAttach(sentMessage.media.document, sentMessage.media.ttl_seconds != 0);
            if (!cacheFile.renameTo(cacheFile2)) {
                if (cacheFile.exists()) {
                    sentMessage.attachPath = newMsg.attachPath;
                } else {
                    newMsgObj.attachPathExists = false;
                }
                newMsgObj.mediaExists = cacheFile2.exists();
                sentMessage.message = newMsg.message;
            } else {
                if (MessageObject.isVideoMessage(sentMessage)) {
                    newMsgObj.attachPathExists = true;
                } else {
                    newMsgObj.mediaExists = newMsgObj.attachPathExists;
                    newMsgObj.attachPathExists = false;
                    newMsg.attachPath = "";
                    if (originalPath != null && originalPath.startsWith("http")) {
                        getMessagesStorage().addRecentLocalFile(originalPath, cacheFile2.toString(), newMsg.media.document);
                    }
                }
            }
        } else {
            sentMessage.attachPath = newMsg.attachPath;
            sentMessage.message = newMsg.message;
        }
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaContact && newMsg.media instanceof TLRPC.TL_messageMediaContact) {
        newMsg.media = sentMessage.media;
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaWebPage) {
        newMsg.media = sentMessage.media;
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaGeo) {
        sentMessage.media.geo.lat = newMsg.media.geo.lat;
        sentMessage.media.geo._long = newMsg.media.geo._long;
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaGame || sentMessage.media instanceof TLRPC.TL_messageMediaInvoice) {
        newMsg.media = sentMessage.media;
        if (!TextUtils.isEmpty(sentMessage.message)) {
            newMsg.entities = sentMessage.entities;
            newMsg.message = sentMessage.message;
        }
        if (sentMessage.reply_markup != null) {
            newMsg.reply_markup = sentMessage.reply_markup;
            newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_MARKUP;
        }
    } else if (sentMessage.media instanceof TLRPC.TL_messageMediaPoll) {
        newMsg.media = sentMessage.media;
    }
}
Also used : TLRPC(org.telegram.tgnet.TLRPC) Point(org.telegram.ui.Components.Point) TLObject(org.telegram.tgnet.TLObject) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

TLObject (org.telegram.tgnet.TLObject)85 TLRPC (org.telegram.tgnet.TLRPC)79 Paint (android.graphics.Paint)36 ArrayList (java.util.ArrayList)36 TextPaint (android.text.TextPaint)25 SuppressLint (android.annotation.SuppressLint)22 ConnectionsManager (org.telegram.tgnet.ConnectionsManager)22 File (java.io.File)21 HashMap (java.util.HashMap)20 Context (android.content.Context)19 Build (android.os.Build)19 TextUtils (android.text.TextUtils)19 MessageObject (org.telegram.messenger.MessageObject)19 Theme (org.telegram.ui.ActionBar.Theme)19 BaseFragment (org.telegram.ui.ActionBar.BaseFragment)18 LongSparseArray (androidx.collection.LongSparseArray)17 AlertDialog (org.telegram.ui.ActionBar.AlertDialog)17 Bundle (android.os.Bundle)16 SpannableStringBuilder (android.text.SpannableStringBuilder)16 ChatObject (org.telegram.messenger.ChatObject)16