use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MusicBrowserService method onLoadChildren.
@Override
public void onLoadChildren(String parentMediaId, Result<List<MediaBrowser.MediaItem>> result) {
if (!chatsLoaded) {
result.detach();
if (loadingChats) {
return;
}
loadingChats = true;
MessagesStorage messagesStorage = MessagesStorage.getInstance(currentAccount);
messagesStorage.getStorageQueue().postRunnable(() -> {
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
SQLiteCursor cursor = messagesStorage.getDatabase().queryFinalized(String.format(Locale.US, "SELECT DISTINCT uid FROM media_v4 WHERE uid != 0 AND mid > 0 AND type = %d", MediaDataController.MEDIA_MUSIC));
while (cursor.next()) {
long dialogId = cursor.longValue(0);
if (DialogObject.isEncryptedDialog(dialogId)) {
continue;
}
dialogs.add(dialogId);
if (DialogObject.isUserDialog(dialogId)) {
usersToLoad.add(dialogId);
} else {
chatsToLoad.add(-dialogId);
}
}
cursor.dispose();
if (!dialogs.isEmpty()) {
String ids = TextUtils.join(",", dialogs);
cursor = messagesStorage.getDatabase().queryFinalized(String.format(Locale.US, "SELECT uid, data, mid FROM media_v4 WHERE uid IN (%s) AND mid > 0 AND type = %d ORDER BY date DESC, mid DESC", ids, MediaDataController.MEDIA_MUSIC));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(1);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, UserConfig.getInstance(currentAccount).clientUserId);
data.reuse();
if (MessageObject.isMusicMessage(message)) {
long did = cursor.longValue(0);
message.id = cursor.intValue(2);
message.dialog_id = did;
ArrayList<MessageObject> arrayList = musicObjects.get(did);
ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(did);
if (arrayList == null) {
arrayList = new ArrayList<>();
musicObjects.put(did, arrayList);
arrayList1 = new ArrayList<>();
musicQueues.put(did, arrayList1);
}
MessageObject messageObject = new MessageObject(currentAccount, message, false, true);
arrayList.add(0, messageObject);
MediaDescription.Builder builder = new MediaDescription.Builder().setMediaId(did + "_" + arrayList.size());
builder.setTitle(messageObject.getMusicTitle());
builder.setSubtitle(messageObject.getMusicAuthor());
arrayList1.add(0, new MediaSession.QueueItem(builder.build(), arrayList1.size()));
}
}
}
cursor.dispose();
if (!usersToLoad.isEmpty()) {
ArrayList<TLRPC.User> usersArrayList = new ArrayList<>();
messagesStorage.getUsersInternal(TextUtils.join(",", usersToLoad), usersArrayList);
for (int a = 0; a < usersArrayList.size(); a++) {
TLRPC.User user = usersArrayList.get(a);
users.put(user.id, user);
}
}
if (!chatsToLoad.isEmpty()) {
ArrayList<TLRPC.Chat> chatsArrayList = new ArrayList<>();
messagesStorage.getChatsInternal(TextUtils.join(",", chatsToLoad), chatsArrayList);
for (int a = 0; a < chatsArrayList.size(); a++) {
TLRPC.Chat chat = chatsArrayList.get(a);
chats.put(chat.id, chat);
}
}
}
} catch (Exception e) {
FileLog.e(e);
}
AndroidUtilities.runOnUIThread(() -> {
chatsLoaded = true;
loadingChats = false;
loadChildrenImpl(parentMediaId, result);
if (lastSelectedDialog == 0 && !dialogs.isEmpty()) {
lastSelectedDialog = dialogs.get(0);
}
if (lastSelectedDialog != 0) {
ArrayList<MessageObject> arrayList = musicObjects.get(lastSelectedDialog);
ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(lastSelectedDialog);
if (arrayList != null && !arrayList.isEmpty()) {
mediaSession.setQueue(arrayList1);
if (lastSelectedDialog > 0) {
TLRPC.User user = users.get(lastSelectedDialog);
if (user != null) {
mediaSession.setQueueTitle(ContactsController.formatName(user.first_name, user.last_name));
} else {
mediaSession.setQueueTitle("DELETED USER");
}
} else {
TLRPC.Chat chat = chats.get(-lastSelectedDialog);
if (chat != null) {
mediaSession.setQueueTitle(chat.title);
} else {
mediaSession.setQueueTitle("DELETED CHAT");
}
}
MessageObject messageObject = arrayList.get(0);
MediaMetadata.Builder builder = new MediaMetadata.Builder();
builder.putLong(MediaMetadata.METADATA_KEY_DURATION, messageObject.getDuration() * 1000);
builder.putString(MediaMetadata.METADATA_KEY_ARTIST, messageObject.getMusicAuthor());
builder.putString(MediaMetadata.METADATA_KEY_TITLE, messageObject.getMusicTitle());
mediaSession.setMetadata(builder.build());
}
}
updatePlaybackState(null);
});
});
} else {
loadChildrenImpl(parentMediaId, result);
}
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class SecretChatHelper method resendMessages.
private void resendMessages(int startSeq, int endSeq, TLRPC.EncryptedChat encryptedChat) {
if (encryptedChat == null || endSeq - startSeq < 0) {
return;
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
int sSeq = startSeq;
if (encryptedChat.admin_id == getUserConfig().getClientUserId() && sSeq % 2 == 0) {
sSeq++;
}
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT uid FROM requested_holes WHERE uid = %d AND ((seq_out_start >= %d AND %d <= seq_out_end) OR (seq_out_start >= %d AND %d <= seq_out_end))", encryptedChat.id, sSeq, sSeq, endSeq, endSeq));
boolean exists = cursor.next();
cursor.dispose();
if (exists) {
return;
}
long dialog_id = DialogObject.makeEncryptedDialogId(encryptedChat.id);
SparseArray<TLRPC.Message> messagesToResend = new SparseArray<>();
ArrayList<TLRPC.Message> messages = new ArrayList<>();
for (int a = sSeq; a <= endSeq; a += 2) {
messagesToResend.put(a, null);
}
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT m.data, r.random_id, s.seq_in, s.seq_out, m.ttl, s.mid FROM messages_seq as s LEFT JOIN randoms_v2 as r ON r.mid = s.mid LEFT JOIN messages_v2 as m ON m.mid = s.mid WHERE m.uid = %d AND m.out = 1 AND s.seq_out >= %d AND s.seq_out <= %d ORDER BY seq_out ASC", dialog_id, sSeq, endSeq));
while (cursor.next()) {
TLRPC.Message message;
long random_id = cursor.longValue(1);
if (random_id == 0) {
random_id = Utilities.random.nextLong();
}
int seq_in = cursor.intValue(2);
int seq_out = cursor.intValue(3);
int mid = cursor.intValue(5);
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
message.random_id = random_id;
message.dialog_id = dialog_id;
message.seq_in = seq_in;
message.seq_out = seq_out;
message.ttl = cursor.intValue(4);
} else {
message = createDeleteMessage(mid, seq_out, seq_in, random_id, encryptedChat);
}
messages.add(message);
messagesToResend.remove(seq_out);
}
cursor.dispose();
if (messagesToResend.size() != 0) {
for (int a = 0; a < messagesToResend.size(); a++) {
int seq = messagesToResend.keyAt(a);
messages.add(createDeleteMessage(getUserConfig().getNewMessageId(), seq, seq + 1, Utilities.random.nextLong(), encryptedChat));
}
getUserConfig().saveConfig(false);
}
Collections.sort(messages, (lhs, rhs) -> AndroidUtilities.compare(lhs.seq_out, rhs.seq_out));
ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
encryptedChats.add(encryptedChat);
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
MessageObject messageObject = new MessageObject(currentAccount, message, false, true);
messageObject.resendAsIs = true;
getSendMessagesHelper().retrySendMessage(messageObject, true);
}
});
getSendMessagesHelper().processUnsentMessages(messages, null, new ArrayList<>(), new ArrayList<>(), encryptedChats);
getMessagesStorage().getDatabase().executeFast(String.format(Locale.US, "REPLACE INTO requested_holes VALUES(%d, %d, %d)", encryptedChat.id, sSeq, endSeq)).stepThis().dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class CacheControlActivity method clearDatabase.
private void clearDatabase() {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("LocalDatabaseClearTextTitle", R.string.LocalDatabaseClearTextTitle));
builder.setMessage(LocaleController.getString("LocalDatabaseClearText", R.string.LocalDatabaseClearText));
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
builder.setPositiveButton(LocaleController.getString("CacheClear", R.string.CacheClear), (dialogInterface, i) -> {
if (getParentActivity() == null) {
return;
}
final AlertDialog progressDialog = new AlertDialog(getParentActivity(), 3);
progressDialog.setCanCacnel(false);
progressDialog.showDelayed(500);
MessagesController.getInstance(currentAccount).clearQueryTime();
MessagesStorage.getInstance(currentAccount).getStorageQueue().postRunnable(() -> {
try {
SQLiteDatabase database = MessagesStorage.getInstance(currentAccount).getDatabase();
ArrayList<Long> dialogsToCleanup = new ArrayList<>();
database.executeFast("DELETE FROM reaction_mentions").stepThis().dispose();
database.executeFast("DELETE FROM reaction_mentions").stepThis().dispose();
SQLiteCursor cursor = database.queryFinalized("SELECT did FROM dialogs WHERE 1");
StringBuilder ids = new StringBuilder();
while (cursor.next()) {
long did = cursor.longValue(0);
if (!DialogObject.isEncryptedDialog(did)) {
dialogsToCleanup.add(did);
}
}
cursor.dispose();
SQLitePreparedStatement state5 = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
SQLitePreparedStatement state6 = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
database.beginTransaction();
for (int a = 0; a < dialogsToCleanup.size(); a++) {
Long did = dialogsToCleanup.get(a);
int messagesCount = 0;
cursor = database.queryFinalized("SELECT COUNT(mid) FROM messages_v2 WHERE uid = " + did);
if (cursor.next()) {
messagesCount = cursor.intValue(0);
}
cursor.dispose();
if (messagesCount <= 2) {
continue;
}
cursor = database.queryFinalized("SELECT last_mid_i, last_mid FROM dialogs WHERE did = " + did);
int messageId = -1;
if (cursor.next()) {
long last_mid_i = cursor.longValue(0);
long last_mid = cursor.longValue(1);
SQLiteCursor cursor2 = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + did + " AND mid IN (" + last_mid_i + "," + last_mid + ")");
try {
while (cursor2.next()) {
NativeByteBuffer data = cursor2.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
if (message != null) {
messageId = message.id;
message.readAttachPath(data, UserConfig.getInstance(currentAccount).clientUserId);
}
data.reuse();
}
}
} catch (Exception e) {
FileLog.e(e);
}
cursor2.dispose();
database.executeFast("DELETE FROM messages_v2 WHERE uid = " + did + " AND mid != " + last_mid_i + " AND mid != " + last_mid).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_v4 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
MediaDataController.getInstance(currentAccount).clearBotKeyboard(did, null);
if (messageId != -1) {
MessagesStorage.createFirstHoles(did, state5, state6, messageId);
}
}
cursor.dispose();
}
state5.dispose();
state6.dispose();
database.commitTransaction();
database.executeFast("PRAGMA journal_size_limit = 0").stepThis().dispose();
database.executeFast("VACUUM").stepThis().dispose();
database.executeFast("PRAGMA journal_size_limit = -1").stepThis().dispose();
} catch (Exception e) {
FileLog.e(e);
} finally {
AndroidUtilities.runOnUIThread(() -> {
try {
progressDialog.dismiss();
} catch (Exception e) {
FileLog.e(e);
}
if (listAdapter != null) {
databaseSize = MessagesStorage.getInstance(currentAccount).getDatabaseSize();
listAdapter.notifyDataSetChanged();
}
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didClearDatabase);
});
}
});
});
AlertDialog alertDialog = builder.create();
showDialog(alertDialog);
TextView button = (TextView) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
}
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class LocationController method loadSharingLocations.
private void loadSharingLocations() {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
final ArrayList<SharingLocationInfo> result = new ArrayList<>();
final ArrayList<TLRPC.User> users = new ArrayList<>();
final ArrayList<TLRPC.Chat> chats = new ArrayList<>();
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT uid, mid, date, period, message, proximity FROM sharing_locations WHERE 1");
while (cursor.next()) {
SharingLocationInfo info = new SharingLocationInfo();
info.did = cursor.longValue(0);
info.mid = cursor.intValue(1);
info.stopTime = cursor.intValue(2);
info.period = cursor.intValue(3);
info.proximityMeters = cursor.intValue(5);
info.account = currentAccount;
NativeByteBuffer data = cursor.byteBufferValue(4);
if (data != null) {
info.messageObject = new MessageObject(currentAccount, TLRPC.Message.TLdeserialize(data, data.readInt32(false), false), false, false);
MessagesStorage.addUsersAndChatsFromMessage(info.messageObject.messageOwner, usersToLoad, chatsToLoad);
data.reuse();
}
result.add(info);
if (DialogObject.isChatDialog(info.did)) {
if (!chatsToLoad.contains(-info.did)) {
chatsToLoad.add(-info.did);
}
} else if (DialogObject.isUserDialog(info.did)) {
if (!usersToLoad.contains(info.did)) {
usersToLoad.add(info.did);
}
}
}
cursor.dispose();
if (!chatsToLoad.isEmpty()) {
getMessagesStorage().getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
if (!usersToLoad.isEmpty()) {
getMessagesStorage().getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
} catch (Exception e) {
FileLog.e(e);
}
if (!result.isEmpty()) {
AndroidUtilities.runOnUIThread(() -> {
getMessagesController().putUsers(users, true);
getMessagesController().putChats(chats, true);
Utilities.stageQueue.postRunnable(() -> {
sharingLocations.addAll(result);
for (int a = 0; a < sharingLocations.size(); a++) {
SharingLocationInfo info = sharingLocations.get(a);
sharingLocationsMap.put(info.did, info);
}
AndroidUtilities.runOnUIThread(() -> {
sharingLocationsUI.addAll(result);
for (int a = 0; a < result.size(); a++) {
SharingLocationInfo info = result.get(a);
sharingLocationsMapUI.put(info.did, info);
}
startService();
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.liveLocationsChanged);
});
});
});
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getMessagesInternal.
public Runnable getMessagesInternal(long dialogId, long mergeDialogId, int count, int max_id, int offset_date, int minDate, int classGuid, int load_type, boolean scheduled, int replyMessageId, int loadIndex, boolean processMessages) {
TLRPC.TL_messages_messages res = new TLRPC.TL_messages_messages();
long currentUserId = getUserConfig().clientUserId;
int count_unread = 0;
int mentions_unread = 0;
int count_query = count;
int offset_query = 0;
int min_unread_id = 0;
int last_message_id = 0;
boolean queryFromServer = false;
int max_unread_date = 0;
int messageMaxId = max_id;
int max_id_query = max_id;
boolean unreadCountIsLocal = false;
int max_id_override = max_id;
boolean isEnd = false;
int num = dialogId == 777000 ? 10 : 1;
int messagesCount = 0;
long startLoadTime = SystemClock.elapsedRealtime();
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
LongSparseArray<SparseArray<ArrayList<TLRPC.Message>>> replyMessageOwners = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> dialogReplyMessagesIds = new LongSparseArray<>();
LongSparseArray<ArrayList<TLRPC.Message>> replyMessageRandomOwners = new LongSparseArray<>();
ArrayList<Long> replyMessageRandomIds = new ArrayList<>();
SQLiteCursor cursor;
String messageSelect = "SELECT m.read_state, m.data, m.send_state, m.mid, m.date, r.random_id, m.replydata, m.media, m.ttl, m.mention, m.imp, m.forwards, m.replies_data FROM messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid";
if (scheduled) {
isEnd = true;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT m.data, m.send_state, m.mid, m.date, r.random_id, m.replydata, m.ttl FROM scheduled_messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid WHERE m.uid = %d ORDER BY m.date DESC", dialogId));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.send_state = cursor.intValue(1);
message.id = cursor.intValue(2);
if (message.id > 0 && message.send_state != 0 && message.send_state != 3) {
message.send_state = 0;
}
if (dialogId == currentUserId) {
message.out = true;
message.unread = false;
} else {
message.unread = true;
}
message.readAttachPath(data, currentUserId);
data.reuse();
message.date = cursor.intValue(3);
message.dialog_id = dialogId;
if (message.ttl == 0) {
message.ttl = cursor.intValue(6);
}
res.messages.add(message);
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
if (message.reply_to != null && (message.reply_to.reply_to_msg_id != 0 || message.reply_to.reply_to_random_id != 0)) {
if (!cursor.isNull(5)) {
data = cursor.byteBufferValue(5);
if (data != null) {
message.replyMessage = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.replyMessage.readAttachPath(data, currentUserId);
data.reuse();
if (message.replyMessage != null) {
addUsersAndChatsFromMessage(message.replyMessage, usersToLoad, chatsToLoad);
}
}
}
if (message.replyMessage == null) {
if (message.reply_to.reply_to_msg_id != 0) {
addReplyMessages(message, replyMessageOwners, dialogReplyMessagesIds);
} else {
ArrayList<TLRPC.Message> messages = replyMessageRandomOwners.get(message.reply_to.reply_to_random_id);
if (messages == null) {
messages = new ArrayList<>();
replyMessageRandomOwners.put(message.reply_to.reply_to_random_id, messages);
}
if (!replyMessageRandomIds.contains(message.reply_to.reply_to_random_id)) {
replyMessageRandomIds.add(message.reply_to.reply_to_random_id);
}
messages.add(message);
}
}
}
}
}
cursor.dispose();
} else {
if (!DialogObject.isEncryptedDialog(dialogId)) {
if (load_type == 3 && minDate == 0) {
cursor = database.queryFinalized("SELECT inbox_max, unread_count, date, unread_count_i FROM dialogs WHERE did = " + dialogId);
if (cursor.next()) {
min_unread_id = Math.max(1, cursor.intValue(0)) + 1;
count_unread = cursor.intValue(1);
max_unread_date = cursor.intValue(2);
mentions_unread = cursor.intValue(3);
}
cursor.dispose();
} else if (load_type != 1 && load_type != 3 && load_type != 4 && minDate == 0) {
if (load_type == 2) {
cursor = database.queryFinalized("SELECT inbox_max, unread_count, date, unread_count_i FROM dialogs WHERE did = " + dialogId);
if (cursor.next()) {
messageMaxId = max_id_query = min_unread_id = Math.max(1, cursor.intValue(0));
count_unread = cursor.intValue(1);
max_unread_date = cursor.intValue(2);
mentions_unread = cursor.intValue(3);
queryFromServer = true;
if (dialogId == currentUserId) {
count_unread = 0;
}
}
cursor.dispose();
if (!queryFromServer) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid), max(date) FROM messages_v2 WHERE uid = %d AND out = 0 AND read_state IN(0,2) AND mid > 0", dialogId));
if (cursor.next()) {
min_unread_id = cursor.intValue(0);
max_unread_date = cursor.intValue(1);
}
cursor.dispose();
if (min_unread_id != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(*) FROM messages_v2 WHERE uid = %d AND mid >= %d AND out = 0 AND read_state IN(0,2)", dialogId, min_unread_id));
if (cursor.next()) {
count_unread = cursor.intValue(0);
}
cursor.dispose();
}
} else if (max_id_query == 0) {
int existingUnreadCount = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(*) FROM messages_v2 WHERE uid = %d AND mid > 0 AND out = 0 AND read_state IN(0,2)", dialogId));
if (cursor.next()) {
existingUnreadCount = cursor.intValue(0);
}
cursor.dispose();
if (existingUnreadCount == count_unread) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND out = 0 AND read_state IN(0,2) AND mid > 0", dialogId));
if (cursor.next()) {
messageMaxId = max_id_query = min_unread_id = cursor.intValue(0);
}
cursor.dispose();
}
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM messages_holes WHERE uid = %d AND start < %d AND end > %d", dialogId, max_id_query, max_id_query));
boolean containMessage = !cursor.next();
cursor.dispose();
if (containMessage) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND out = 0 AND read_state IN(0,2) AND mid > %d", dialogId, max_id_query));
if (cursor.next()) {
messageMaxId = max_id_query = cursor.intValue(0);
}
cursor.dispose();
}
}
}
if (count_query > count_unread || count_unread < num) {
count_query = Math.max(count_query, count_unread + 10);
if (count_unread < num) {
count_unread = 0;
min_unread_id = 0;
messageMaxId = 0;
last_message_id = 0;
queryFromServer = false;
}
} else {
offset_query = count_unread - count_query;
count_query += 10;
}
}
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM messages_holes WHERE uid = %d AND start IN (0, 1)", dialogId));
if (cursor.next()) {
isEnd = cursor.intValue(0) == 1;
} else {
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND mid > 0", dialogId));
if (cursor.next()) {
int mid = cursor.intValue(0);
if (mid != 0) {
SQLitePreparedStatement state = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
state.requery();
state.bindLong(1, dialogId);
state.bindInteger(2, 0);
state.bindInteger(3, mid);
state.step();
state.dispose();
}
}
}
cursor.dispose();
if (load_type == 3 || load_type == 4 || queryFromServer && load_type == 2) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(mid) FROM messages_v2 WHERE uid = %d AND mid > 0", dialogId));
if (cursor.next()) {
last_message_id = cursor.intValue(0);
}
cursor.dispose();
if (load_type == 4 && offset_date != 0) {
int startMid;
int endMid;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(mid) FROM messages_v2 WHERE uid = %d AND date <= %d AND mid > 0", dialogId, offset_date));
if (cursor.next()) {
startMid = cursor.intValue(0);
} else {
startMid = -1;
}
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND date >= %d AND mid > 0", dialogId, offset_date));
if (cursor.next()) {
endMid = cursor.intValue(0);
} else {
endMid = -1;
}
cursor.dispose();
if (startMid != -1 && endMid != -1) {
if (startMid == endMid) {
max_id_query = startMid;
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM messages_holes WHERE uid = %d AND start <= %d AND end > %d", dialogId, startMid, startMid));
if (cursor.next()) {
startMid = -1;
}
cursor.dispose();
if (startMid != -1) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM messages_holes WHERE uid = %d AND start <= %d AND end > %d", dialogId, endMid, endMid));
if (cursor.next()) {
endMid = -1;
}
cursor.dispose();
if (endMid != -1) {
max_id_override = endMid;
messageMaxId = max_id_query = endMid;
}
}
}
}
}
boolean containMessage = max_id_query != 0;
if (containMessage) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM messages_holes WHERE uid = %d AND start < %d AND end > %d", dialogId, max_id_query, max_id_query));
if (cursor.next()) {
containMessage = false;
}
cursor.dispose();
}
if (containMessage) {
int holeMessageMaxId = 0;
int holeMessageMinId = 1;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM messages_holes WHERE uid = %d AND start >= %d ORDER BY start ASC LIMIT 1", dialogId, max_id_query));
if (cursor.next()) {
holeMessageMaxId = cursor.intValue(0);
}
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT end FROM messages_holes WHERE uid = %d AND end <= %d ORDER BY end DESC LIMIT 1", dialogId, max_id_query));
if (cursor.next()) {
holeMessageMinId = cursor.intValue(0);
}
cursor.dispose();
if (holeMessageMaxId != 0 || holeMessageMinId != 1) {
if (holeMessageMaxId == 0) {
holeMessageMaxId = 1000000000;
}
cursor = database.queryFinalized(String.format(Locale.US, "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid <= %d AND (m.mid >= %d OR m.mid < 0) ORDER BY m.date DESC, m.mid DESC LIMIT %d) UNION " + "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid > %d AND (m.mid <= %d OR m.mid < 0) ORDER BY m.date ASC, m.mid ASC LIMIT %d)", dialogId, messageMaxId, holeMessageMinId, count_query / 2, dialogId, messageMaxId, holeMessageMaxId, count_query / 2));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid <= %d ORDER BY m.date DESC, m.mid DESC LIMIT %d) UNION " + "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid > %d ORDER BY m.date ASC, m.mid ASC LIMIT %d)", dialogId, messageMaxId, count_query / 2, dialogId, messageMaxId, count_query / 2));
}
} else {
if (load_type == 2) {
int existingUnreadCount = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(*) FROM messages_v2 WHERE uid = %d AND mid != 0 AND out = 0 AND read_state IN(0,2)", dialogId));
if (cursor.next()) {
existingUnreadCount = cursor.intValue(0);
}
cursor.dispose();
if (existingUnreadCount == count_unread) {
unreadCountIsLocal = true;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid <= %d ORDER BY m.date DESC, m.mid DESC LIMIT %d) UNION " + "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid > %d ORDER BY m.date ASC, m.mid ASC LIMIT %d)", dialogId, messageMaxId, count_query / 2, dialogId, messageMaxId, count_query / 2));
} else {
cursor = null;
}
} else {
cursor = null;
}
}
} else if (load_type == 1) {
int holeMessageId = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM messages_holes WHERE uid = %d AND (start >= %d AND start != 1 AND end != 1 OR start < %d AND end > %d) ORDER BY start ASC LIMIT 1", dialogId, max_id, max_id, max_id));
if (cursor.next()) {
holeMessageId = cursor.intValue(0);
}
cursor.dispose();
if (holeMessageId != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date >= %d AND m.mid > %d AND m.mid <= %d ORDER BY m.date ASC, m.mid ASC LIMIT %d", dialogId, minDate, messageMaxId, holeMessageId, count_query));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date >= %d AND m.mid > %d ORDER BY m.date ASC, m.mid ASC LIMIT %d", dialogId, minDate, messageMaxId, count_query));
}
} else if (minDate != 0) {
if (messageMaxId != 0) {
int holeMessageId = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT end FROM messages_holes WHERE uid = %d AND end <= %d ORDER BY end DESC LIMIT 1", dialogId, max_id));
if (cursor.next()) {
holeMessageId = cursor.intValue(0);
}
cursor.dispose();
if (holeMessageId != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date <= %d AND m.mid < %d AND (m.mid >= %d OR m.mid < 0) ORDER BY m.date DESC, m.mid DESC LIMIT %d", dialogId, minDate, messageMaxId, holeMessageId, count_query));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date <= %d AND m.mid < %d ORDER BY m.date DESC, m.mid DESC LIMIT %d", dialogId, minDate, messageMaxId, count_query));
}
} else {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date <= %d ORDER BY m.date DESC, m.mid DESC LIMIT %d,%d", dialogId, minDate, offset_query, count_query));
}
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(mid) FROM messages_v2 WHERE uid = %d AND mid > 0", dialogId));
if (cursor.next()) {
last_message_id = cursor.intValue(0);
}
cursor.dispose();
int holeMessageId = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(end) FROM messages_holes WHERE uid = %d", dialogId));
if (cursor.next()) {
holeMessageId = cursor.intValue(0);
}
cursor.dispose();
if (holeMessageId != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND (m.mid >= %d OR m.mid < 0) ORDER BY m.date DESC, m.mid DESC LIMIT %d,%d", dialogId, holeMessageId, offset_query, count_query));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d ORDER BY m.date DESC, m.mid DESC LIMIT %d,%d", dialogId, offset_query, count_query));
}
}
} else {
isEnd = true;
if (load_type == 3 && minDate == 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND mid < 0", dialogId));
if (cursor.next()) {
min_unread_id = cursor.intValue(0);
}
cursor.dispose();
int min_unread_id2 = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(mid), max(date) FROM messages_v2 WHERE uid = %d AND out = 0 AND read_state IN(0,2) AND mid < 0", dialogId));
if (cursor.next()) {
min_unread_id2 = cursor.intValue(0);
max_unread_date = cursor.intValue(1);
}
cursor.dispose();
if (min_unread_id2 != 0) {
min_unread_id = min_unread_id2;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(*) FROM messages_v2 WHERE uid = %d AND mid <= %d AND out = 0 AND read_state IN(0,2)", dialogId, min_unread_id2));
if (cursor.next()) {
count_unread = cursor.intValue(0);
}
cursor.dispose();
}
}
if (load_type == 3 || load_type == 4) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND mid < 0", dialogId));
if (cursor.next()) {
last_message_id = cursor.intValue(0);
}
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid <= %d ORDER BY m.mid DESC LIMIT %d) UNION " + "SELECT * FROM (" + messageSelect + " WHERE m.uid = %d AND m.mid > %d ORDER BY m.mid ASC LIMIT %d)", dialogId, messageMaxId, count_query / 2, dialogId, messageMaxId, count_query / 2));
} else if (load_type == 1) {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.mid < %d ORDER BY m.mid DESC LIMIT %d", dialogId, max_id, count_query));
} else if (minDate != 0) {
if (max_id != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.mid > %d ORDER BY m.mid ASC LIMIT %d", dialogId, max_id, count_query));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d AND m.date <= %d ORDER BY m.mid ASC LIMIT %d,%d", dialogId, minDate, offset_query, count_query));
}
} else {
if (load_type == 2) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM messages_v2 WHERE uid = %d AND mid < 0", dialogId));
if (cursor.next()) {
last_message_id = cursor.intValue(0);
}
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(mid), max(date) FROM messages_v2 WHERE uid = %d AND out = 0 AND read_state IN(0,2) AND mid < 0", dialogId));
if (cursor.next()) {
min_unread_id = cursor.intValue(0);
max_unread_date = cursor.intValue(1);
}
cursor.dispose();
if (min_unread_id != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(*) FROM messages_v2 WHERE uid = %d AND mid <= %d AND out = 0 AND read_state IN(0,2)", dialogId, min_unread_id));
if (cursor.next()) {
count_unread = cursor.intValue(0);
}
cursor.dispose();
}
}
if (count_query > count_unread || count_unread < num) {
count_query = Math.max(count_query, count_unread + 10);
if (count_unread < num) {
count_unread = 0;
min_unread_id = 0;
last_message_id = 0;
}
} else {
offset_query = count_unread - count_query;
count_query += 10;
}
cursor = database.queryFinalized(String.format(Locale.US, "" + messageSelect + " WHERE m.uid = %d ORDER BY m.mid ASC LIMIT %d,%d", dialogId, offset_query, count_query));
}
}
int minId = Integer.MAX_VALUE;
int maxId = Integer.MIN_VALUE;
ArrayList<Long> messageIdsToFix = null;
if (cursor != null) {
while (cursor.next()) {
messagesCount++;
if (!processMessages) {
continue;
}
NativeByteBuffer data = cursor.byteBufferValue(1);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.send_state = cursor.intValue(2);
long fullMid = cursor.longValue(3);
message.id = (int) fullMid;
if ((fullMid & 0xffffffff00000000L) == 0xffffffff00000000L && message.id > 0) {
if (messageIdsToFix == null) {
messageIdsToFix = new ArrayList<>();
}
messageIdsToFix.add(fullMid);
}
if (message.id > 0 && message.send_state != 0 && message.send_state != 3) {
message.send_state = 0;
}
if (dialogId == currentUserId) {
message.out = true;
}
message.readAttachPath(data, currentUserId);
data.reuse();
MessageObject.setUnreadFlags(message, cursor.intValue(0));
if (message.id > 0) {
minId = Math.min(message.id, minId);
maxId = Math.max(message.id, maxId);
}
message.date = cursor.intValue(4);
message.dialog_id = dialogId;
if ((message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
message.views = cursor.intValue(7);
message.forwards = cursor.intValue(11);
}
NativeByteBuffer repliesData = cursor.byteBufferValue(12);
if (repliesData != null) {
TLRPC.MessageReplies replies = TLRPC.MessageReplies.TLdeserialize(repliesData, repliesData.readInt32(false), false);
if (replies != null) {
message.replies = replies;
}
repliesData.reuse();
}
if (!DialogObject.isEncryptedDialog(dialogId) && message.ttl == 0) {
message.ttl = cursor.intValue(8);
}
if (cursor.intValue(9) != 0) {
message.mentioned = true;
}
int flags = cursor.intValue(10);
if ((flags & 1) != 0) {
message.stickerVerified = 0;
} else if ((flags & 2) != 0) {
message.stickerVerified = 2;
}
res.messages.add(message);
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
if (message.reply_to != null && (message.reply_to.reply_to_msg_id != 0 || message.reply_to.reply_to_random_id != 0)) {
if (!cursor.isNull(6)) {
data = cursor.byteBufferValue(6);
if (data != null) {
message.replyMessage = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.replyMessage.readAttachPath(data, currentUserId);
data.reuse();
if (message.replyMessage != null) {
addUsersAndChatsFromMessage(message.replyMessage, usersToLoad, chatsToLoad);
}
}
}
if (message.replyMessage == null) {
if (message.reply_to.reply_to_msg_id != 0) {
addReplyMessages(message, replyMessageOwners, dialogReplyMessagesIds);
} else {
ArrayList<TLRPC.Message> messages = replyMessageRandomOwners.get(message.reply_to.reply_to_random_id);
if (messages == null) {
messages = new ArrayList<>();
replyMessageRandomOwners.put(message.reply_to.reply_to_random_id, messages);
}
if (!replyMessageRandomIds.contains(message.reply_to.reply_to_random_id)) {
replyMessageRandomIds.add(message.reply_to.reply_to_random_id);
}
messages.add(message);
}
}
}
if (DialogObject.isEncryptedDialog(dialogId) && !cursor.isNull(5)) {
message.random_id = cursor.longValue(5);
}
if (MessageObject.isSecretMedia(message)) {
try {
SQLiteCursor cursor2 = database.queryFinalized(String.format(Locale.US, "SELECT date FROM enc_tasks_v4 WHERE mid = %d AND uid = %d AND media = 1", message.id, MessageObject.getDialogId(message)));
if (cursor2.next()) {
message.destroyTime = cursor2.intValue(0);
}
cursor2.dispose();
} catch (Exception e) {
FileLog.e(e);
}
}
}
}
cursor.dispose();
}
Collections.sort(res.messages, (lhs, rhs) -> {
if (lhs.id > 0 && rhs.id > 0) {
if (lhs.id > rhs.id) {
return -1;
} else if (lhs.id < rhs.id) {
return 1;
}
} else if (lhs.id < 0 && rhs.id < 0) {
if (lhs.id < rhs.id) {
return -1;
} else if (lhs.id > rhs.id) {
return 1;
}
} else {
if (lhs.date > rhs.date) {
return -1;
} else if (lhs.date < rhs.date) {
return 1;
}
}
return 0;
});
if (!DialogObject.isEncryptedDialog(dialogId)) {
if ((load_type == 3 || load_type == 4 || load_type == 2 && queryFromServer && !unreadCountIsLocal) && !res.messages.isEmpty()) {
if (!(minId <= max_id_query && maxId >= max_id_query)) {
usersToLoad.clear();
chatsToLoad.clear();
res.messages.clear();
}
}
if ((load_type == 4 || load_type == 3) && res.messages.size() == 1) {
res.messages.clear();
}
}
if (mentions_unread != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM messages_v2 WHERE uid = %d AND mention = 1 AND read_state IN(0, 1)", dialogId));
if (cursor.next()) {
if (mentions_unread != cursor.intValue(0)) {
mentions_unread *= -1;
}
} else {
mentions_unread *= -1;
}
cursor.dispose();
}
}
if (!replyMessageRandomOwners.isEmpty()) {
cursor = database.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(",", replyMessageRandomIds)));
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, currentUserId);
data.reuse();
message.id = cursor.intValue(1);
message.date = cursor.intValue(2);
message.dialog_id = dialogId;
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
long value = cursor.longValue(3);
ArrayList<TLRPC.Message> arrayList = replyMessageRandomOwners.get(value);
replyMessageRandomOwners.remove(value);
if (arrayList != null) {
for (int a = 0; a < arrayList.size(); a++) {
TLRPC.Message object = arrayList.get(a);
object.replyMessage = message;
if (object.reply_to != null) {
object.reply_to.reply_to_msg_id = message.id;
}
}
}
}
}
cursor.dispose();
for (int b = 0, N = replyMessageRandomOwners.size(); b < N; b++) {
ArrayList<TLRPC.Message> arrayList = replyMessageRandomOwners.valueAt(b);
for (int a = 0, N2 = arrayList.size(); a < N2; a++) {
TLRPC.Message message = arrayList.get(a);
if (message.reply_to != null) {
message.reply_to.reply_to_random_id = 0;
}
}
}
} else {
loadReplyMessages(replyMessageOwners, dialogReplyMessagesIds, usersToLoad, chatsToLoad);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), res.users);
}
if (!chatsToLoad.isEmpty()) {
getChatsInternal(TextUtils.join(",", chatsToLoad), res.chats);
}
} catch (Exception e) {
res.messages.clear();
res.chats.clear();
res.users.clear();
FileLog.e(e);
}
if (BuildVars.LOGS_ENABLED) {
FileLog.d("messages load time = " + (SystemClock.elapsedRealtime() - startLoadTime) + " for dialog = " + dialogId);
}
int countQueryFinal = count_query;
int maxIdOverrideFinal = max_id_override;
int minUnreadIdFinal = min_unread_id;
int lastMessageIdFinal = last_message_id;
boolean isEndFinal = isEnd;
boolean queryFromServerFinal = queryFromServer;
int mentionsUnreadFinal = mentions_unread;
int countUnreadFinal = count_unread;
int maxUnreadDateFinal = max_unread_date;
/*if (!scheduled && mergeDialogId != 0 && res.messages.size() < count && isEnd && load_type == 2) { TODO fix if end not reached
Runnable runnable = getMessagesInternal(mergeDialogId, 0, count, Integer.MAX_VALUE, 0, 0, classGuid, 0, false, false, loadIndex);
return () -> {
getMessagesController().processLoadedMessages(res, dialogId, mergeDialogId, countQueryFinal, maxIdOverrideFinal, offset_date, true, classGuid, minUnreadIdFinal, lastMessageIdFinal, countUnreadFinal, maxUnreadDateFinal, load_type, isChannel, isEndFinal, scheduled, -loadIndex, queryFromServerFinal, mentionsUnreadFinal);
runnable.run();
};
} else {*/
int finalMessagesCount = scheduled ? res.messages.size() : messagesCount;
return () -> getMessagesController().processLoadedMessages(res, finalMessagesCount, dialogId, mergeDialogId, countQueryFinal, maxIdOverrideFinal, offset_date, true, classGuid, minUnreadIdFinal, lastMessageIdFinal, countUnreadFinal, maxUnreadDateFinal, load_type, isEndFinal, scheduled ? 1 : 0, replyMessageId, loadIndex, queryFromServerFinal, mentionsUnreadFinal, processMessages);
// }
}
Aggregations