Search in sources :

Example 41 with DatabaseAttachment

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

the class PushDecryptJob method handleSynchronizeSentMediaMessage.

private long handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret, @NonNull SentTranscriptMessage message, @NonNull Optional<Long> smsMessageId) throws MmsException {
    MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
    Recipients recipients = getSyncMessageDestination(message);
    OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage(recipients, message.getMessage().getBody().orNull(), PointerAttachment.forPointers(masterSecret, message.getMessage().getAttachments()), message.getTimestamp(), -1, message.getMessage().getExpiresInSeconds() * 1000, ThreadDatabase.DistributionTypes.DEFAULT);
    mediaMessage = new OutgoingSecureMediaMessage(mediaMessage);
    if (recipients.getExpireMessages() != message.getMessage().getExpiresInSeconds()) {
        handleSynchronizeSentExpirationUpdate(masterSecret, message, Optional.<Long>absent());
    }
    long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
    long messageId = database.insertMessageOutbox(masterSecret, mediaMessage, threadId, false);
    database.markAsSent(messageId, true);
    for (DatabaseAttachment attachment : DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(messageId)) {
        ApplicationContext.getInstance(context).getJobManager().add(new AttachmentDownloadJob(context, messageId, attachment.getAttachmentId()));
    }
    if (smsMessageId.isPresent()) {
        DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
    }
    if (message.getMessage().getExpiresInSeconds() > 0) {
        database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
        ApplicationContext.getInstance(context).getExpiringMessageManager().scheduleDeletion(messageId, true, message.getExpirationStartTimestamp(), message.getMessage().getExpiresInSeconds());
    }
    return threadId;
}
Also used : OutgoingSecureMediaMessage(org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage) Recipients(org.thoughtcrime.securesms.recipients.Recipients) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase)

Example 42 with DatabaseAttachment

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

the class MmsDatabase method copyMessageInbox.

public long copyMessageInbox(long messageId) throws MmsException {
    try {
        OutgoingMediaMessage request = getOutgoingMessage(messageId);
        ContentValues contentValues = new ContentValues();
        contentValues.put(ADDRESS, request.getRecipient().getAddress().serialize());
        contentValues.put(DATE_SENT, request.getSentTimeMillis());
        contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE | Types.SECURE_MESSAGE_BIT);
        contentValues.put(THREAD_ID, getThreadIdForMessage(messageId));
        contentValues.put(READ, 1);
        contentValues.put(DATE_RECEIVED, contentValues.getAsLong(DATE_SENT));
        contentValues.put(EXPIRES_IN, request.getExpiresIn());
        List<Attachment> attachments = new LinkedList<>();
        for (Attachment attachment : request.getAttachments()) {
            DatabaseAttachment databaseAttachment = (DatabaseAttachment) attachment;
            attachments.add(new DatabaseAttachment(databaseAttachment.getAttachmentId(), databaseAttachment.getMmsId(), databaseAttachment.hasData(), databaseAttachment.hasThumbnail(), databaseAttachment.getContentType(), AttachmentDatabase.TRANSFER_PROGRESS_DONE, databaseAttachment.getSize(), databaseAttachment.getFileName(), databaseAttachment.getLocation(), databaseAttachment.getKey(), databaseAttachment.getRelay(), databaseAttachment.getDigest(), databaseAttachment.getFastPreflightId(), databaseAttachment.isVoiceNote()));
        }
        return insertMediaMessage(request.getBody(), attachments, contentValues, null);
    } catch (NoSuchMessageException e) {
        throw new MmsException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) MmsException(org.thoughtcrime.securesms.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) MmsNotificationAttachment(org.thoughtcrime.securesms.attachments.MmsNotificationAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) LinkedList(java.util.LinkedList)

Example 43 with DatabaseAttachment

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

the class AttachmentDatabaseTest method testThumbnailGenerationTaskNotRunWhenThumbnailExists.

public void testThumbnailGenerationTaskNotRunWhenThumbnailExists() throws Exception {
    final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
    DatabaseAttachment mockAttachment = getMockAttachment("x/x");
    when(database.getAttachment(null, attachmentId)).thenReturn(mockAttachment);
    InputStream mockInputStream = mock(InputStream.class);
    doReturn(mockInputStream).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
    database.getThumbnailStream(mock(MasterSecret.class), attachmentId);
    // Works as the Future#get() call in AttachmentDatabase#getThumbnailStream() makes updating synchronous
    verify(database, never()).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) InputStream(java.io.InputStream) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId)

Example 44 with DatabaseAttachment

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

the class AttachmentDatabaseTest method testThumbnailGenerationTaskRunWhenThumbnailMissing.

public void testThumbnailGenerationTaskRunWhenThumbnailMissing() throws Exception {
    final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
    DatabaseAttachment mockAttachment = getMockAttachment("image/png");
    when(database.getAttachment(null, attachmentId)).thenReturn(mockAttachment);
    doReturn(null).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
    doNothing().when(database).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
    try {
        database.new ThumbnailFetchCallable(mock(MasterSecret.class), attachmentId).call();
        throw new AssertionError("Didn't try to generate thumbnail");
    } catch (BitmapDecodingException bde) {
        if (!(bde.getCause() instanceof FileNotFoundException)) {
            throw new AssertionError("Thumbnail generation failed for another reason than a FileNotFoundException: " + bde.getMessage());
        }
    // else success
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) InputStream(java.io.InputStream) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) FileNotFoundException(java.io.FileNotFoundException) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Example 45 with DatabaseAttachment

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

the class AttachmentDatabase method getAttachmentsForMessage.

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

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