Search in sources :

Example 11 with SQLiteCursor

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

the class MediaDataController method loadBotKeyboard.

public void loadBotKeyboard(long dialogId) {
    TLRPC.Message keyboard = botKeyboards.get(dialogId);
    if (keyboard != null) {
        getNotificationCenter().postNotificationName(NotificationCenter.botKeyboardDidLoad, keyboard, dialogId);
        return;
    }
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        try {
            TLRPC.Message botKeyboard = null;
            SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT info FROM bot_keyboard WHERE uid = %d", dialogId));
            if (cursor.next()) {
                NativeByteBuffer data;
                if (!cursor.isNull(0)) {
                    data = cursor.byteBufferValue(0);
                    if (data != null) {
                        botKeyboard = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                        data.reuse();
                    }
                }
            }
            cursor.dispose();
            if (botKeyboard != null) {
                TLRPC.Message botKeyboardFinal = botKeyboard;
                AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.botKeyboardDidLoad, botKeyboardFinal, dialogId));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 12 with SQLiteCursor

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

the class MediaDataController method loadMusic.

public void loadMusic(long dialogId, long maxId, long minId) {
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        ArrayList<MessageObject> arrayListBegin = new ArrayList<>();
        ArrayList<MessageObject> arrayListEnd = new ArrayList<>();
        try {
            for (int a = 0; a < 2; a++) {
                ArrayList<MessageObject> arrayList = a == 0 ? arrayListBegin : arrayListEnd;
                SQLiteCursor cursor;
                if (a == 0) {
                    if (!DialogObject.isEncryptedDialog(dialogId)) {
                        cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid < %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, maxId, MEDIA_MUSIC));
                    } else {
                        cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, maxId, MEDIA_MUSIC));
                    }
                } else {
                    if (!DialogObject.isEncryptedDialog(dialogId)) {
                        cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, minId, MEDIA_MUSIC));
                    } else {
                        cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid < %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, minId, MEDIA_MUSIC));
                    }
                }
                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();
                        if (MessageObject.isMusicMessage(message)) {
                            message.id = cursor.intValue(1);
                            message.dialog_id = dialogId;
                            arrayList.add(0, new MessageObject(currentAccount, message, false, true));
                        }
                    }
                }
                cursor.dispose();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.musicDidLoad, dialogId, arrayListBegin, arrayListEnd));
    });
}
Also used : ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) Paint(android.graphics.Paint) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 13 with SQLiteCursor

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

the class MessagesStorage method getUnreadMention.

public void getUnreadMention(long dialog_id, IntCallback callback) {
    storageQueue.postRunnable(() -> {
        try {
            int result;
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT MIN(mid) FROM messages_v2 WHERE uid = %d AND mention = 1 AND read_state IN(0, 1)", dialog_id));
            if (cursor.next()) {
                result = cursor.intValue(0);
            } else {
                result = 0;
            }
            cursor.dispose();
            AndroidUtilities.runOnUIThread(() -> callback.run(result));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 14 with SQLiteCursor

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

the class MessagesStorage method loadUserInfo.

public void loadUserInfo(TLRPC.User user, boolean force, int classGuid, int fromMessageId) {
    if (user == null) {
        return;
    }
    storageQueue.postRunnable(() -> {
        HashMap<Integer, MessageObject> pinnedMessagesMap = new HashMap<>();
        ArrayList<Integer> pinnedMessages = new ArrayList<>();
        int totalPinnedCount = 0;
        boolean pinnedEndReached = false;
        TLRPC.UserFull info = null;
        try {
            SQLiteCursor cursor = database.queryFinalized("SELECT info, pinned FROM user_settings WHERE uid = " + user.id);
            if (cursor.next()) {
                NativeByteBuffer data = cursor.byteBufferValue(0);
                if (data != null) {
                    info = TLRPC.UserFull.TLdeserialize(data, data.readInt32(false), false);
                    info.pinned_msg_id = cursor.intValue(1);
                    data.reuse();
                }
            }
            cursor.dispose();
            cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", user.id));
            while (cursor.next()) {
                int id = cursor.intValue(0);
                pinnedMessages.add(id);
                pinnedMessagesMap.put(id, null);
            }
            cursor.dispose();
            cursor = database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + user.id);
            if (cursor.next()) {
                totalPinnedCount = cursor.intValue(0);
                pinnedEndReached = cursor.intValue(1) != 0;
            }
            cursor.dispose();
            if (info != null && info.pinned_msg_id != 0) {
                if (pinnedMessages.isEmpty() || info.pinned_msg_id > pinnedMessages.get(0)) {
                    pinnedMessages.clear();
                    pinnedMessages.add(info.pinned_msg_id);
                    pinnedMessagesMap.put(info.pinned_msg_id, null);
                }
            }
            if (!pinnedMessages.isEmpty()) {
                ArrayList<MessageObject> messageObjects = getMediaDataController().loadPinnedMessages(user.id, 0, pinnedMessages, false);
                if (messageObjects != null) {
                    for (int a = 0, N = messageObjects.size(); a < N; a++) {
                        MessageObject messageObject = messageObjects.get(a);
                        pinnedMessagesMap.put(messageObject.getId(), messageObject);
                    }
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            getMessagesController().processUserInfo(user, info, true, force, classGuid, pinnedMessages, pinnedMessagesMap, totalPinnedCount, pinnedEndReached);
        }
    });
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 15 with SQLiteCursor

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

the class MessagesStorage method markMessagesContentAsRead.

public void markMessagesContentAsRead(long dialogId, ArrayList<Integer> mids, int date) {
    if (isEmpty(mids)) {
        return;
    }
    storageQueue.postRunnable(() -> {
        if (dialogId == 0) {
            try {
                LongSparseArray<ArrayList<Integer>> sparseArray = new LongSparseArray<>();
                SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, mid FROM messages_v2 WHERE mid IN (%s) AND is_channel = 0", TextUtils.join(",", mids)));
                while (cursor.next()) {
                    long did = cursor.longValue(0);
                    ArrayList<Integer> arrayList = sparseArray.get(did);
                    if (arrayList == null) {
                        arrayList = new ArrayList<>();
                        sparseArray.put(did, arrayList);
                    }
                    arrayList.add(cursor.intValue(1));
                }
                cursor.dispose();
                for (int a = 0, N = sparseArray.size(); a < N; a++) {
                    markMessagesContentAsReadInternal(sparseArray.keyAt(a), sparseArray.valueAt(a), date);
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        } else {
            markMessagesContentAsReadInternal(dialogId, mids, date);
        }
    });
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) ArrayList(java.util.ArrayList) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

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