Search in sources :

Example 16 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method unpinAllDialogsExceptNew.

public void unpinAllDialogsExceptNew(ArrayList<Long> dids, int folderId) {
    storageQueue.postRunnable(() -> {
        try {
            ArrayList<Long> unpinnedDialogs = new ArrayList<>();
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT did, folder_id FROM dialogs WHERE pinned > 0 AND did NOT IN (%s)", TextUtils.join(",", dids)));
            while (cursor.next()) {
                long did = cursor.longValue(0);
                int fid = cursor.intValue(1);
                if (fid == folderId && !DialogObject.isEncryptedDialog(did) && !DialogObject.isFolderDialogId(did)) {
                    unpinnedDialogs.add(cursor.longValue(0));
                }
            }
            cursor.dispose();
            if (!unpinnedDialogs.isEmpty()) {
                SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET pinned = ? WHERE did = ?");
                for (int a = 0; a < unpinnedDialogs.size(); a++) {
                    long did = unpinnedDialogs.get(a);
                    state.requery();
                    state.bindInteger(1, 0);
                    state.bindLong(2, did);
                    state.step();
                }
                state.dispose();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 17 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method getUnsentMessages.

public void getUnsentMessages(int count) {
    storageQueue.postRunnable(() -> {
        try {
            SparseArray<TLRPC.Message> messageHashMap = new SparseArray<>();
            ArrayList<TLRPC.Message> messages = new ArrayList<>();
            ArrayList<TLRPC.Message> scheduledMessages = new ArrayList<>();
            ArrayList<TLRPC.User> users = new ArrayList<>();
            ArrayList<TLRPC.Chat> chats = new ArrayList<>();
            ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
            ArrayList<Long> usersToLoad = new ArrayList<>();
            ArrayList<Long> chatsToLoad = new ArrayList<>();
            ArrayList<Integer> encryptedChatIds = new ArrayList<>();
            SQLiteCursor cursor = database.queryFinalized("SELECT m.read_state, m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, s.seq_in, s.seq_out, m.ttl FROM messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid LEFT JOIN messages_seq as s ON m.mid = s.mid WHERE (m.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY m.mid DESC LIMIT " + count);
            while (cursor.next()) {
                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);
                    message.readAttachPath(data, getUserConfig().clientUserId);
                    data.reuse();
                    if (messageHashMap.indexOfKey(message.id) < 0) {
                        MessageObject.setUnreadFlags(message, cursor.intValue(0));
                        message.id = cursor.intValue(3);
                        message.date = cursor.intValue(4);
                        if (!cursor.isNull(5)) {
                            message.random_id = cursor.longValue(5);
                        }
                        message.dialog_id = cursor.longValue(6);
                        message.seq_in = cursor.intValue(7);
                        message.seq_out = cursor.intValue(8);
                        message.ttl = cursor.intValue(9);
                        messages.add(message);
                        messageHashMap.put(message.id, message);
                        if (DialogObject.isEncryptedDialog(message.dialog_id)) {
                            int encryptedChatId = DialogObject.getEncryptedChatId(message.dialog_id);
                            if (!encryptedChatIds.contains(encryptedChatId)) {
                                encryptedChatIds.add(encryptedChatId);
                            }
                        } else if (DialogObject.isUserDialog(message.dialog_id)) {
                            if (!usersToLoad.contains(message.dialog_id)) {
                                usersToLoad.add(message.dialog_id);
                            }
                        } else {
                            if (!chatsToLoad.contains(-message.dialog_id)) {
                                chatsToLoad.add(-message.dialog_id);
                            }
                        }
                        addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
                        if (message.send_state != 3 && (message.peer_id.channel_id == 0 && !MessageObject.isUnread(message) && !DialogObject.isEncryptedDialog(message.dialog_id) || message.id > 0)) {
                            message.send_state = 0;
                        }
                    }
                }
            }
            cursor.dispose();
            cursor = database.queryFinalized("SELECT m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, 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.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY date ASC");
            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.readAttachPath(data, getUserConfig().clientUserId);
                    data.reuse();
                    if (messageHashMap.indexOfKey(message.id) < 0) {
                        message.id = cursor.intValue(2);
                        message.date = cursor.intValue(3);
                        if (!cursor.isNull(4)) {
                            message.random_id = cursor.longValue(4);
                        }
                        message.dialog_id = cursor.longValue(5);
                        message.ttl = cursor.intValue(6);
                        scheduledMessages.add(message);
                        messageHashMap.put(message.id, message);
                        if (DialogObject.isEncryptedDialog(message.dialog_id)) {
                            int encryptedChatId = DialogObject.getEncryptedChatId(message.dialog_id);
                            if (!encryptedChatIds.contains(encryptedChatId)) {
                                encryptedChatIds.add(encryptedChatId);
                            }
                        } else if (DialogObject.isUserDialog(message.dialog_id)) {
                            if (!usersToLoad.contains(message.dialog_id)) {
                                usersToLoad.add(message.dialog_id);
                            }
                        } else {
                            if (!chatsToLoad.contains(-message.dialog_id)) {
                                chatsToLoad.add(-message.dialog_id);
                            }
                        }
                        addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
                        if (message.send_state != 3 && (message.peer_id.channel_id == 0 && !MessageObject.isUnread(message) && !DialogObject.isEncryptedDialog(message.dialog_id) || message.id > 0)) {
                            message.send_state = 0;
                        }
                    }
                }
            }
            cursor.dispose();
            if (!encryptedChatIds.isEmpty()) {
                getEncryptedChatsInternal(TextUtils.join(",", encryptedChatIds), encryptedChats, usersToLoad);
            }
            if (!usersToLoad.isEmpty()) {
                getUsersInternal(TextUtils.join(",", usersToLoad), users);
            }
            if (!chatsToLoad.isEmpty()) {
                StringBuilder stringToLoad = new StringBuilder();
                for (int a = 0; a < chatsToLoad.size(); a++) {
                    Long cid = chatsToLoad.get(a);
                    if (stringToLoad.length() != 0) {
                        stringToLoad.append(",");
                    }
                    stringToLoad.append(cid);
                }
                getChatsInternal(stringToLoad.toString(), chats);
            }
            getSendMessagesHelper().processUnsentMessages(messages, scheduledMessages, users, chats, encryptedChats);
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) LongSparseArray(androidx.collection.LongSparseArray) SparseArray(android.util.SparseArray) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 18 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method loadChannelAdmins.

public void loadChannelAdmins(long chatId) {
    storageQueue.postRunnable(() -> {
        try {
            SQLiteCursor cursor = database.queryFinalized("SELECT uid, data FROM channel_admins_v3 WHERE did = " + chatId);
            LongSparseArray<TLRPC.ChannelParticipant> ids = new LongSparseArray<>();
            while (cursor.next()) {
                NativeByteBuffer data = cursor.byteBufferValue(1);
                if (data != null) {
                    TLRPC.ChannelParticipant participant = TLRPC.ChannelParticipant.TLdeserialize(data, data.readInt32(false), false);
                    data.reuse();
                    if (participant != null) {
                        ids.put(cursor.longValue(0), participant);
                    }
                }
            }
            cursor.dispose();
            getMessagesController().processLoadedChannelAdmins(ids, chatId, true);
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 19 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method removeFromDownloadQueue.

public void removeFromDownloadQueue(long id, int type, boolean move) {
    storageQueue.postRunnable(() -> {
        try {
            if (move) {
                int minDate = -1;
                SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(date) FROM download_queue WHERE type = %d", type));
                if (cursor.next()) {
                    minDate = cursor.intValue(0);
                }
                cursor.dispose();
                if (minDate != -1) {
                    database.executeFast(String.format(Locale.US, "UPDATE download_queue SET date = %d WHERE uid = %d AND type = %d", minDate - 1, id, type)).stepThis().dispose();
                }
            } else {
                database.executeFast(String.format(Locale.US, "DELETE FROM download_queue WHERE uid = %d AND type = %d", id, type)).stepThis().dispose();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 20 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method closeHolesInMedia.

public void closeHolesInMedia(long did, int minId, int maxId, int type) {
    try {
        boolean ok = false;
        SQLiteCursor cursor;
        if (type < 0) {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_v2 WHERE uid = %d AND type >= 0 AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", did, minId, maxId, minId, maxId, minId, maxId, minId, maxId));
        } else {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT type, start, end FROM media_holes_v2 WHERE uid = %d AND type = %d AND ((end >= %d AND end <= %d) OR (start >= %d AND start <= %d) OR (start >= %d AND end <= %d) OR (start <= %d AND end >= %d))", did, type, minId, maxId, minId, maxId, minId, maxId, minId, maxId));
        }
        ArrayList<Hole> holes = null;
        while (cursor.next()) {
            if (holes == null) {
                holes = new ArrayList<>();
            }
            int holeType = cursor.intValue(0);
            int start = cursor.intValue(1);
            int end = cursor.intValue(2);
            if (start == end && start == 1) {
                continue;
            }
            holes.add(new Hole(holeType, start, end));
        }
        cursor.dispose();
        if (holes != null) {
            for (int a = 0; a < holes.size(); a++) {
                Hole hole = holes.get(a);
                if (maxId >= hole.end - 1 && minId <= hole.start + 1) {
                    database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d AND start = %d AND end = %d", did, hole.type, hole.start, hole.end)).stepThis().dispose();
                } else if (maxId >= hole.end - 1) {
                    if (hole.end != minId) {
                        try {
                            database.executeFast(String.format(Locale.US, "UPDATE media_holes_v2 SET end = %d WHERE uid = %d AND type = %d AND start = %d AND end = %d", minId, did, hole.type, hole.start, hole.end)).stepThis().dispose();
                        } catch (Exception e) {
                            FileLog.e(e, false);
                        }
                    }
                } else if (minId <= hole.start + 1) {
                    if (hole.start != maxId) {
                        try {
                            database.executeFast(String.format(Locale.US, "UPDATE media_holes_v2 SET start = %d WHERE uid = %d AND type = %d AND start = %d AND end = %d", maxId, did, hole.type, hole.start, hole.end)).stepThis().dispose();
                        } catch (Exception e) {
                            FileLog.e(e, false);
                        }
                    }
                } else {
                    database.executeFast(String.format(Locale.US, "DELETE FROM media_holes_v2 WHERE uid = %d AND type = %d AND start = %d AND end = %d", did, hole.type, hole.start, hole.end)).stepThis().dispose();
                    SQLitePreparedStatement state = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                    state.requery();
                    state.bindLong(1, did);
                    state.bindInteger(2, hole.type);
                    state.bindInteger(3, hole.start);
                    state.bindInteger(4, minId);
                    state.step();
                    state.requery();
                    state.bindLong(1, did);
                    state.bindInteger(2, hole.type);
                    state.bindInteger(3, maxId);
                    state.bindInteger(4, hole.end);
                    state.step();
                    state.dispose();
                }
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Aggregations

SQLiteCursor (org.telegram.SQLite.SQLiteCursor)118 SQLiteException (org.telegram.SQLite.SQLiteException)105 TLRPC (org.telegram.tgnet.TLRPC)77 NativeByteBuffer (org.telegram.tgnet.NativeByteBuffer)66 ArrayList (java.util.ArrayList)58 SQLitePreparedStatement (org.telegram.SQLite.SQLitePreparedStatement)42 LongSparseArray (androidx.collection.LongSparseArray)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 Paint (android.graphics.Paint)18 SparseArray (android.util.SparseArray)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 SpannableStringBuilder (android.text.SpannableStringBuilder)10 LongSparseIntArray (org.telegram.messenger.support.LongSparseIntArray)10 File (java.io.File)8 HashMap (java.util.HashMap)7 Pair (android.util.Pair)6 TLObject (org.telegram.tgnet.TLObject)6 LinkedHashMap (java.util.LinkedHashMap)4 SharedPreferences (android.content.SharedPreferences)3 SpannedString (android.text.SpannedString)3