Search in sources :

Example 16 with DatabaseAttachment

use of org.thoughtcrime.securesms.attachments.DatabaseAttachment in project Signal-Android by WhisperSystems.

the class AttachmentDatabase method getAttachmentsForMessage.

@NonNull
public List<DatabaseAttachment> getAttachmentsForMessage(long mmsId) {
    SQLiteDatabase database = databaseHelper.getSignalReadableDatabase();
    List<DatabaseAttachment> results = new LinkedList<>();
    Cursor cursor = null;
    try {
        cursor = database.query(TABLE_NAME, PROJECTION, MMS_ID + " = ?", new String[] { mmsId + "" }, null, null, UNIQUE_ID + " ASC, " + ROW_ID + " ASC");
        while (cursor != null && cursor.moveToNext()) {
            results.addAll(getAttachments(cursor));
        }
        return results;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) NonNull(androidx.annotation.NonNull)

Example 17 with DatabaseAttachment

use of org.thoughtcrime.securesms.attachments.DatabaseAttachment in project Signal-Android by WhisperSystems.

the class AttachmentDatabase method getAttachments.

public List<DatabaseAttachment> getAttachments(@NonNull Cursor cursor) {
    try {
        if (cursor.getColumnIndex(AttachmentDatabase.ATTACHMENT_JSON_ALIAS) != -1) {
            if (cursor.isNull(cursor.getColumnIndexOrThrow(ATTACHMENT_JSON_ALIAS))) {
                return new LinkedList<>();
            }
            List<DatabaseAttachment> result = new LinkedList<>();
            JSONArray array = new JSONArray(cursor.getString(cursor.getColumnIndexOrThrow(ATTACHMENT_JSON_ALIAS)));
            for (int i = 0; i < array.length(); i++) {
                JsonUtils.SaneJSONObject object = new JsonUtils.SaneJSONObject(array.getJSONObject(i));
                if (!object.isNull(ROW_ID)) {
                    String contentType = object.getString(CONTENT_TYPE);
                    result.add(new DatabaseAttachment(new AttachmentId(object.getLong(ROW_ID), object.getLong(UNIQUE_ID)), object.getLong(MMS_ID), !TextUtils.isEmpty(object.getString(DATA)), MediaUtil.isImageType(contentType) || MediaUtil.isVideoType(contentType), contentType, object.getInt(TRANSFER_STATE), object.getLong(SIZE), object.getString(FILE_NAME), object.getInt(CDN_NUMBER), object.getString(CONTENT_LOCATION), object.getString(CONTENT_DISPOSITION), object.getString(NAME), null, object.getString(FAST_PREFLIGHT_ID), object.getInt(VOICE_NOTE) == 1, object.getInt(BORDERLESS) == 1, object.getInt(VIDEO_GIF) == 1, object.getInt(WIDTH), object.getInt(HEIGHT), object.getInt(QUOTE) == 1, object.getString(CAPTION), object.getInt(STICKER_ID) >= 0 ? new StickerLocator(object.getString(STICKER_PACK_ID), object.getString(STICKER_PACK_KEY), object.getInt(STICKER_ID), object.getString(STICKER_EMOJI)) : null, MediaUtil.isAudioType(contentType) ? null : BlurHash.parseOrNull(object.getString(VISUAL_HASH)), MediaUtil.isAudioType(contentType) ? AudioHash.parseOrNull(object.getString(VISUAL_HASH)) : null, TransformProperties.parse(object.getString(TRANSFORM_PROPERTIES)), object.getInt(DISPLAY_ORDER), object.getLong(UPLOAD_TIMESTAMP)));
                }
            }
            return result;
        } else {
            return Collections.singletonList(getAttachment(cursor));
        }
    } catch (JSONException e) {
        throw new AssertionError(e);
    }
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) JSONArray(org.json.JSONArray) StickerLocator(org.thoughtcrime.securesms.stickers.StickerLocator) JSONException(org.json.JSONException) LinkedList(java.util.LinkedList) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId) JsonUtils(org.thoughtcrime.securesms.util.JsonUtils)

Example 18 with DatabaseAttachment

use of org.thoughtcrime.securesms.attachments.DatabaseAttachment in project Signal-Android by WhisperSystems.

the class AttachmentDatabase method markAttachmentUploaded.

public void markAttachmentUploaded(long messageId, Attachment attachment) {
    ContentValues values = new ContentValues(1);
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    values.put(TRANSFER_STATE, TRANSFER_PROGRESS_DONE);
    database.update(TABLE_NAME, values, PART_ID_WHERE, ((DatabaseAttachment) attachment).getAttachmentId().toStrings());
    notifyConversationListeners(SignalDatabase.mms().getThreadIdForMessage(messageId));
}
Also used : ContentValues(android.content.ContentValues) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment)

Example 19 with DatabaseAttachment

use of org.thoughtcrime.securesms.attachments.DatabaseAttachment in project Signal-Android by WhisperSystems.

the class AttachmentDatabase method copyAttachmentData.

public void copyAttachmentData(@NonNull AttachmentId sourceId, @NonNull AttachmentId destinationId) throws MmsException {
    DatabaseAttachment sourceAttachment = getAttachment(sourceId);
    if (sourceAttachment == null) {
        throw new MmsException("Cannot find attachment for source!");
    }
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    DataInfo sourceDataInfo = getAttachmentDataFileInfo(sourceId, DATA);
    if (sourceDataInfo == null) {
        throw new MmsException("No attachment data found for source!");
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(DATA, sourceDataInfo.file.getAbsolutePath());
    contentValues.put(DATA_HASH, sourceDataInfo.hash);
    contentValues.put(SIZE, sourceDataInfo.length);
    contentValues.put(DATA_RANDOM, sourceDataInfo.random);
    contentValues.put(TRANSFER_STATE, sourceAttachment.getTransferState());
    contentValues.put(CDN_NUMBER, sourceAttachment.getCdnNumber());
    contentValues.put(CONTENT_LOCATION, sourceAttachment.getLocation());
    contentValues.put(DIGEST, sourceAttachment.getDigest());
    contentValues.put(CONTENT_DISPOSITION, sourceAttachment.getKey());
    contentValues.put(NAME, sourceAttachment.getRelay());
    contentValues.put(SIZE, sourceAttachment.getSize());
    contentValues.put(FAST_PREFLIGHT_ID, sourceAttachment.getFastPreflightId());
    contentValues.put(WIDTH, sourceAttachment.getWidth());
    contentValues.put(HEIGHT, sourceAttachment.getHeight());
    contentValues.put(CONTENT_TYPE, sourceAttachment.getContentType());
    contentValues.put(VISUAL_HASH, getVisualHashStringOrNull(sourceAttachment));
    database.update(TABLE_NAME, contentValues, PART_ID_WHERE, destinationId.toStrings());
}
Also used : ContentValues(android.content.ContentValues) MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment)

Example 20 with DatabaseAttachment

use of org.thoughtcrime.securesms.attachments.DatabaseAttachment in project Signal-Android by WhisperSystems.

the class AttachmentDatabase method getAttachment.

@Nullable
public DatabaseAttachment getAttachment(@NonNull AttachmentId attachmentId) {
    SQLiteDatabase database = databaseHelper.getSignalReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = database.query(TABLE_NAME, PROJECTION, PART_ID_WHERE, attachmentId.toStrings(), null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            List<DatabaseAttachment> list = getAttachments(cursor);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Cursor(android.database.Cursor) Nullable(androidx.annotation.Nullable)

Aggregations

DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)51 LinkedList (java.util.LinkedList)15 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)14 Attachment (org.thoughtcrime.securesms.attachments.Attachment)12 MmsException (org.thoughtcrime.securesms.mms.MmsException)11 NonNull (androidx.annotation.NonNull)10 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)10 ContentValues (android.content.ContentValues)9 OutgoingMediaMessage (org.thoughtcrime.securesms.mms.OutgoingMediaMessage)9 Nullable (androidx.annotation.Nullable)8 IOException (java.io.IOException)8 List (java.util.List)8 Recipient (org.thoughtcrime.securesms.recipients.Recipient)8 Cursor (android.database.Cursor)7 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)6 OutgoingSecureMediaMessage (org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage)6 Context (android.content.Context)5 HashMap (java.util.HashMap)5 Contact (org.thoughtcrime.securesms.contactshare.Contact)5 InputStream (java.io.InputStream)4