use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadStickers.
public void loadStickers(int type, boolean cache, boolean force, boolean scheduleIfLoading) {
if (loadingStickers[type]) {
if (scheduleIfLoading) {
scheduledLoadStickers[type] = () -> loadStickers(type, false, force, false);
}
return;
}
if (type == TYPE_FEATURED) {
if (featuredStickerSets.isEmpty() || !getMessagesController().preloadFeaturedStickers) {
return;
}
} else if (type != TYPE_EMOJI) {
loadArchivedStickersCount(type, cache);
}
loadingStickers[type] = true;
if (cache) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
ArrayList<TLRPC.TL_messages_stickerSet> newStickerArray = null;
int date = 0;
long hash = 0;
SQLiteCursor cursor = null;
try {
cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT data, date, hash FROM stickers_v2 WHERE id = " + (type + 1));
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
newStickerArray = new ArrayList<>();
int count = data.readInt32(false);
for (int a = 0; a < count; a++) {
TLRPC.TL_messages_stickerSet stickerSet = TLRPC.TL_messages_stickerSet.TLdeserialize(data, data.readInt32(false), false);
newStickerArray.add(stickerSet);
}
data.reuse();
}
date = cursor.intValue(1);
hash = calcStickersHash(newStickerArray);
}
} catch (Throwable e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
processLoadedStickers(type, newStickerArray, true, date, hash);
});
} else {
if (type == TYPE_FEATURED) {
TLRPC.TL_messages_allStickers response = new TLRPC.TL_messages_allStickers();
response.hash = loadFeaturedHash;
for (int a = 0, size = featuredStickerSets.size(); a < size; a++) {
response.sets.add(featuredStickerSets.get(a).set);
}
processLoadStickersResponse(type, response);
} else if (type == TYPE_EMOJI) {
TLRPC.TL_messages_getStickerSet req = new TLRPC.TL_messages_getStickerSet();
req.stickerset = new TLRPC.TL_inputStickerSetAnimatedEmoji();
getConnectionsManager().sendRequest(req, (response, error) -> {
if (response instanceof TLRPC.TL_messages_stickerSet) {
ArrayList<TLRPC.TL_messages_stickerSet> newStickerArray = new ArrayList<>();
newStickerArray.add((TLRPC.TL_messages_stickerSet) response);
processLoadedStickers(type, newStickerArray, false, (int) (System.currentTimeMillis() / 1000), calcStickersHash(newStickerArray));
} else {
processLoadedStickers(type, null, false, (int) (System.currentTimeMillis() / 1000), 0);
}
});
} else {
TLObject req;
long hash;
if (type == TYPE_IMAGE) {
req = new TLRPC.TL_messages_getAllStickers();
hash = ((TLRPC.TL_messages_getAllStickers) req).hash = force ? 0 : loadHash[type];
} else {
req = new TLRPC.TL_messages_getMaskStickers();
hash = ((TLRPC.TL_messages_getMaskStickers) req).hash = force ? 0 : loadHash[type];
}
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (response instanceof TLRPC.TL_messages_allStickers) {
processLoadStickersResponse(type, (TLRPC.TL_messages_allStickers) response);
} else {
processLoadedStickers(type, null, false, (int) (System.currentTimeMillis() / 1000), hash);
}
}));
}
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method saveDraft.
public void saveDraft(long dialogId, int threadId, TLRPC.DraftMessage draft, TLRPC.Message replyToMessage, boolean fromServer) {
SharedPreferences.Editor editor = draftPreferences.edit();
MessagesController messagesController = getMessagesController();
if (draft == null || draft instanceof TLRPC.TL_draftMessageEmpty) {
{
SparseArray<TLRPC.DraftMessage> threads = drafts.get(dialogId);
if (threads != null) {
threads.remove(threadId);
if (threads.size() == 0) {
drafts.remove(dialogId);
}
}
}
{
SparseArray<TLRPC.Message> threads = draftMessages.get(dialogId);
if (threads != null) {
threads.remove(threadId);
if (threads.size() == 0) {
draftMessages.remove(dialogId);
}
}
}
if (threadId == 0) {
draftPreferences.edit().remove("" + dialogId).remove("r_" + dialogId).commit();
} else {
draftPreferences.edit().remove("t_" + dialogId + "_" + threadId).remove("rt_" + dialogId + "_" + threadId).commit();
}
messagesController.removeDraftDialogIfNeed(dialogId);
} else {
SparseArray<TLRPC.DraftMessage> threads = drafts.get(dialogId);
if (threads == null) {
threads = new SparseArray<>();
drafts.put(dialogId, threads);
}
threads.put(threadId, draft);
if (threadId == 0) {
messagesController.putDraftDialogIfNeed(dialogId, draft);
}
try {
SerializedData serializedData = new SerializedData(draft.getObjectSize());
draft.serializeToStream(serializedData);
editor.putString(threadId == 0 ? ("" + dialogId) : ("t_" + dialogId + "_" + threadId), Utilities.bytesToHex(serializedData.toByteArray()));
serializedData.cleanup();
} catch (Exception e) {
FileLog.e(e);
}
}
SparseArray<TLRPC.Message> threads = draftMessages.get(dialogId);
if (replyToMessage == null) {
if (threads != null) {
threads.remove(threadId);
if (threads.size() == 0) {
draftMessages.remove(dialogId);
}
}
if (threadId == 0) {
editor.remove("r_" + dialogId);
} else {
editor.remove("rt_" + dialogId + "_" + threadId);
}
} else {
if (threads == null) {
threads = new SparseArray<>();
draftMessages.put(dialogId, threads);
}
threads.put(threadId, replyToMessage);
SerializedData serializedData = new SerializedData(replyToMessage.getObjectSize());
replyToMessage.serializeToStream(serializedData);
editor.putString(threadId == 0 ? ("r_" + dialogId) : ("rt_" + dialogId + "_" + threadId), Utilities.bytesToHex(serializedData.toByteArray()));
serializedData.cleanup();
}
editor.commit();
if (fromServer && threadId == 0) {
if (draft != null && draft.reply_to_msg_id != 0 && replyToMessage == null) {
TLRPC.User user = null;
TLRPC.Chat chat = null;
if (DialogObject.isUserDialog(dialogId)) {
user = getMessagesController().getUser(dialogId);
} else {
chat = getMessagesController().getChat(-dialogId);
}
if (user != null || chat != null) {
long channelId = ChatObject.isChannel(chat) ? chat.id : 0;
int messageId = draft.reply_to_msg_id;
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
TLRPC.Message message = null;
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE mid = %d and uid = %d", messageId, dialogId));
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
}
}
cursor.dispose();
if (message == null) {
if (channelId != 0) {
TLRPC.TL_channels_getMessages req = new TLRPC.TL_channels_getMessages();
req.channel = getMessagesController().getInputChannel(channelId);
req.id.add(messageId);
getConnectionsManager().sendRequest(req, (response, error) -> {
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
if (!messagesRes.messages.isEmpty()) {
saveDraftReplyMessage(dialogId, threadId, messagesRes.messages.get(0));
}
}
});
} else {
TLRPC.TL_messages_getMessages req = new TLRPC.TL_messages_getMessages();
req.id.add(messageId);
getConnectionsManager().sendRequest(req, (response, error) -> {
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
if (!messagesRes.messages.isEmpty()) {
saveDraftReplyMessage(dialogId, threadId, messagesRes.messages.get(0));
}
}
});
}
} else {
saveDraftReplyMessage(dialogId, threadId, message);
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
}
getNotificationCenter().postNotificationName(NotificationCenter.newDraftReceived, dialogId);
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadReplyMessagesForMessages.
public void loadReplyMessagesForMessages(ArrayList<MessageObject> messages, long dialogId, boolean scheduled, Runnable callback) {
if (DialogObject.isEncryptedDialog(dialogId)) {
ArrayList<Long> replyMessages = new ArrayList<>();
LongSparseArray<ArrayList<MessageObject>> replyMessageRandomOwners = new LongSparseArray<>();
for (int a = 0; a < messages.size(); a++) {
MessageObject messageObject = messages.get(a);
if (messageObject == null) {
continue;
}
if (messageObject.isReply() && messageObject.replyMessageObject == null) {
long id = messageObject.messageOwner.reply_to.reply_to_random_id;
ArrayList<MessageObject> messageObjects = replyMessageRandomOwners.get(id);
if (messageObjects == null) {
messageObjects = new ArrayList<>();
replyMessageRandomOwners.put(id, messageObjects);
}
messageObjects.add(messageObject);
if (!replyMessages.contains(id)) {
replyMessages.add(id);
}
}
}
if (replyMessages.isEmpty()) {
if (callback != null) {
callback.run();
}
return;
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
ArrayList<MessageObject> loadedMessages = new ArrayList<>();
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT m.data, m.mid, m.date, r.random_id FROM randoms_v2 as r INNER JOIN messages_v2 as m ON r.mid = m.mid AND r.uid = m.uid WHERE r.random_id IN(%s)", TextUtils.join(",", replyMessages)));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
message.id = cursor.intValue(1);
message.date = cursor.intValue(2);
message.dialog_id = dialogId;
long value = cursor.longValue(3);
ArrayList<MessageObject> arrayList = replyMessageRandomOwners.get(value);
replyMessageRandomOwners.remove(value);
if (arrayList != null) {
MessageObject messageObject = new MessageObject(currentAccount, message, false, false);
loadedMessages.add(messageObject);
for (int b = 0; b < arrayList.size(); b++) {
MessageObject object = arrayList.get(b);
object.replyMessageObject = messageObject;
object.messageOwner.reply_to = new TLRPC.TL_messageReplyHeader();
object.messageOwner.reply_to.reply_to_msg_id = messageObject.getId();
}
}
}
}
cursor.dispose();
if (replyMessageRandomOwners.size() != 0) {
for (int b = 0; b < replyMessageRandomOwners.size(); b++) {
ArrayList<MessageObject> arrayList = replyMessageRandomOwners.valueAt(b);
for (int a = 0; a < arrayList.size(); a++) {
TLRPC.Message message = arrayList.get(a).messageOwner;
if (message.reply_to != null) {
message.reply_to.reply_to_random_id = 0;
}
}
}
}
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.replyMessagesDidLoad, dialogId, loadedMessages, null));
if (callback != null) {
callback.run();
}
} catch (Exception e) {
FileLog.e(e);
}
});
} else {
LongSparseArray<SparseArray<ArrayList<MessageObject>>> replyMessageOwners = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> dialogReplyMessagesIds = new LongSparseArray<>();
for (int a = 0; a < messages.size(); a++) {
MessageObject messageObject = messages.get(a);
if (messageObject == null) {
continue;
}
if (messageObject.getId() > 0 && messageObject.isReply()) {
int messageId = messageObject.messageOwner.reply_to.reply_to_msg_id;
long channelId = 0;
if (messageObject.messageOwner.reply_to.reply_to_peer_id != null) {
if (messageObject.messageOwner.reply_to.reply_to_peer_id.channel_id != 0) {
channelId = messageObject.messageOwner.reply_to.reply_to_peer_id.channel_id;
}
} else if (messageObject.messageOwner.peer_id.channel_id != 0) {
channelId = messageObject.messageOwner.peer_id.channel_id;
}
if (messageObject.replyMessageObject != null) {
if (messageObject.replyMessageObject.messageOwner == null || messageObject.replyMessageObject.messageOwner.peer_id == null || messageObject.messageOwner instanceof TLRPC.TL_messageEmpty) {
continue;
}
if (messageObject.replyMessageObject.messageOwner.peer_id.channel_id == channelId) {
continue;
}
}
SparseArray<ArrayList<MessageObject>> sparseArray = replyMessageOwners.get(dialogId);
ArrayList<Integer> ids = dialogReplyMessagesIds.get(channelId);
if (sparseArray == null) {
sparseArray = new SparseArray<>();
replyMessageOwners.put(dialogId, sparseArray);
}
if (ids == null) {
ids = new ArrayList<>();
dialogReplyMessagesIds.put(channelId, ids);
}
ArrayList<MessageObject> arrayList = sparseArray.get(messageId);
if (arrayList == null) {
arrayList = new ArrayList<>();
sparseArray.put(messageId, arrayList);
if (!ids.contains(messageId)) {
ids.add(messageId);
}
}
arrayList.add(messageObject);
}
}
if (replyMessageOwners.isEmpty()) {
if (callback != null) {
callback.run();
}
return;
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
ArrayList<TLRPC.Message> result = new ArrayList<>();
ArrayList<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> chats = new ArrayList<>();
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
for (int b = 0, N2 = replyMessageOwners.size(); b < N2; b++) {
long did = replyMessageOwners.keyAt(b);
SparseArray<ArrayList<MessageObject>> owners = replyMessageOwners.valueAt(b);
ArrayList<Integer> ids = dialogReplyMessagesIds.get(did);
if (ids == null) {
continue;
}
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid, date, uid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", TextUtils.join(",", ids), dialogId));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
message.id = cursor.intValue(1);
message.date = cursor.intValue(2);
message.dialog_id = dialogId;
MessagesStorage.addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
result.add(message);
long channelId = message.peer_id != null ? message.peer_id.channel_id : 0;
ArrayList<Integer> mids = dialogReplyMessagesIds.get(channelId);
if (mids != null) {
mids.remove((Integer) message.id);
if (mids.isEmpty()) {
dialogReplyMessagesIds.remove(channelId);
}
}
}
}
cursor.dispose();
}
if (!usersToLoad.isEmpty()) {
getMessagesStorage().getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
if (!chatsToLoad.isEmpty()) {
getMessagesStorage().getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
broadcastReplyMessages(result, replyMessageOwners, users, chats, dialogId, true);
if (!dialogReplyMessagesIds.isEmpty()) {
for (int a = 0, N = dialogReplyMessagesIds.size(); a < N; a++) {
long channelId = dialogReplyMessagesIds.keyAt(a);
if (channelId != 0) {
TLRPC.TL_channels_getMessages req = new TLRPC.TL_channels_getMessages();
req.channel = getMessagesController().getInputChannel(channelId);
req.id = dialogReplyMessagesIds.valueAt(a);
getConnectionsManager().sendRequest(req, (response, error) -> {
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
for (int i = 0; i < messagesRes.messages.size(); i++) {
TLRPC.Message message = messagesRes.messages.get(i);
if (message.dialog_id == 0) {
message.dialog_id = dialogId;
}
}
MessageObject.fixMessagePeer(messagesRes.messages, channelId);
ImageLoader.saveMessagesThumbs(messagesRes.messages);
broadcastReplyMessages(messagesRes.messages, replyMessageOwners, messagesRes.users, messagesRes.chats, dialogId, false);
getMessagesStorage().putUsersAndChats(messagesRes.users, messagesRes.chats, true, true);
saveReplyMessages(replyMessageOwners, messagesRes.messages, scheduled);
}
if (callback != null) {
AndroidUtilities.runOnUIThread(callback);
}
});
} else {
TLRPC.TL_messages_getMessages req = new TLRPC.TL_messages_getMessages();
req.id = dialogReplyMessagesIds.valueAt(a);
getConnectionsManager().sendRequest(req, (response, error) -> {
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
for (int i = 0; i < messagesRes.messages.size(); i++) {
TLRPC.Message message = messagesRes.messages.get(i);
if (message.dialog_id == 0) {
message.dialog_id = dialogId;
}
}
ImageLoader.saveMessagesThumbs(messagesRes.messages);
broadcastReplyMessages(messagesRes.messages, replyMessageOwners, messagesRes.users, messagesRes.chats, dialogId, false);
getMessagesStorage().putUsersAndChats(messagesRes.users, messagesRes.chats, true, true);
saveReplyMessages(replyMessageOwners, messagesRes.messages, scheduled);
}
if (callback != null) {
AndroidUtilities.runOnUIThread(callback);
}
});
}
}
} else {
if (callback != null) {
AndroidUtilities.runOnUIThread(callback);
}
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method putSetToCache.
private void putSetToCache(TLRPC.TL_messages_stickerSet set) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
SQLiteDatabase database = getMessagesStorage().getDatabase();
SQLitePreparedStatement state = database.executeFast("REPLACE INTO web_recent_v3 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
state.requery();
state.bindString(1, "s_" + set.set.id);
state.bindInteger(2, 6);
state.bindString(3, "");
state.bindString(4, "");
state.bindString(5, "");
state.bindInteger(6, 0);
state.bindInteger(7, 0);
state.bindInteger(8, 0);
state.bindInteger(9, 0);
NativeByteBuffer data = new NativeByteBuffer(set.getObjectSize());
set.serializeToStream(data);
state.bindByteBuffer(10, data);
state.step();
data.reuse();
state.dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method putFeaturedStickersToCache.
private void putFeaturedStickersToCache(ArrayList<TLRPC.StickerSetCovered> stickers, ArrayList<Long> unreadStickers, int date, long hash) {
ArrayList<TLRPC.StickerSetCovered> stickersFinal = stickers != null ? new ArrayList<>(stickers) : null;
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
if (stickersFinal != null) {
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO stickers_featured VALUES(?, ?, ?, ?, ?)");
state.requery();
int size = 4;
for (int a = 0; a < stickersFinal.size(); a++) {
size += stickersFinal.get(a).getObjectSize();
}
NativeByteBuffer data = new NativeByteBuffer(size);
NativeByteBuffer data2 = new NativeByteBuffer(4 + unreadStickers.size() * 8);
data.writeInt32(stickersFinal.size());
for (int a = 0; a < stickersFinal.size(); a++) {
stickersFinal.get(a).serializeToStream(data);
}
data2.writeInt32(unreadStickers.size());
for (int a = 0; a < unreadStickers.size(); a++) {
data2.writeInt64(unreadStickers.get(a));
}
state.bindInteger(1, 1);
state.bindByteBuffer(2, data);
state.bindByteBuffer(3, data2);
state.bindInteger(4, date);
state.bindLong(5, hash);
state.step();
data.reuse();
data2.reuse();
state.dispose();
} else {
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("UPDATE stickers_featured SET date = ?");
state.requery();
state.bindInteger(1, date);
state.step();
state.dispose();
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations