Search in sources :

Example 36 with DatabaseAttachment

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

the class MmsDatabase method copyMessageInbox.

public long copyMessageInbox(MasterSecret masterSecret, long messageId) throws MmsException {
    try {
        OutgoingMediaMessage request = getOutgoingMessage(masterSecret, messageId);
        ContentValues contentValues = new ContentValues();
        contentValues.put(ADDRESS, request.getRecipients().getPrimaryRecipient().getNumber());
        contentValues.put(DATE_SENT, request.getSentTimeMillis());
        contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE | Types.SECURE_MESSAGE_BIT | Types.ENCRYPTION_SYMMETRIC_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.getLocation(), databaseAttachment.getKey(), databaseAttachment.getRelay(), databaseAttachment.getDigest()));
        }
        return insertMediaMessage(new MasterSecretUnion(masterSecret), MmsAddresses.forTo(request.getRecipients().toNumberStringList(false)), request.getBody(), attachments, contentValues);
    } catch (NoSuchMessageException e) {
        throw new MmsException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) MasterSecretUnion(org.thoughtcrime.securesms.crypto.MasterSecretUnion) MmsException(ws.com.google.android.mms.MmsException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) Attachment(org.thoughtcrime.securesms.attachments.Attachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) MmsNotificationAttachment(org.thoughtcrime.securesms.attachments.MmsNotificationAttachment) LinkedList(java.util.LinkedList)

Example 37 with DatabaseAttachment

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

the class AttachmentDatabaseTest method getMockAttachment.

private DatabaseAttachment getMockAttachment(String contentType) {
    DatabaseAttachment attachment = mock(DatabaseAttachment.class);
    when(attachment.getContentType()).thenReturn(contentType);
    when(attachment.getDataUri()).thenReturn(Uri.EMPTY);
    when(attachment.hasData()).thenReturn(true);
    return attachment;
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment)

Example 38 with DatabaseAttachment

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

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(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 39 with DatabaseAttachment

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

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(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 40 with DatabaseAttachment

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

the class PushDecryptJob method handleMediaMessage.

private void handleMediaMessage(@NonNull MasterSecretUnion masterSecret, @NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId) throws MmsException {
    MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
    String localNumber = TextSecurePreferences.getLocalNumber(context);
    Recipients recipients = getMessageDestination(envelope, message);
    IncomingMediaMessage mediaMessage = new IncomingMediaMessage(masterSecret, envelope.getSource(), localNumber, message.getTimestamp(), -1, message.getExpiresInSeconds() * 1000, false, Optional.fromNullable(envelope.getRelay()), message.getBody(), message.getGroupInfo(), message.getAttachments());
    if (message.getExpiresInSeconds() != recipients.getExpireMessages()) {
        handleExpirationUpdate(masterSecret, envelope, message, Optional.<Long>absent());
    }
    Optional<InsertResult> insertResult = database.insertSecureDecryptedMessageInbox(masterSecret, mediaMessage, -1);
    if (insertResult.isPresent()) {
        List<DatabaseAttachment> attachments = DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(insertResult.get().getMessageId());
        for (DatabaseAttachment attachment : attachments) {
            ApplicationContext.getInstance(context).getJobManager().add(new AttachmentDownloadJob(context, insertResult.get().getMessageId(), attachment.getAttachmentId()));
        }
        if (smsMessageId.isPresent()) {
            DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
        }
        MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) Recipients(org.thoughtcrime.securesms.recipients.Recipients) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase)

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