Search in sources :

Example 6 with Attachment

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

the class AttachmentDownloadJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
    final Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(attachmentId);
    if (attachment == null) {
        Log.w(TAG, "attachment no longer exists.");
        return;
    }
    if (!attachment.isInProgress()) {
        Log.w(TAG, "Attachment was already downloaded.");
        return;
    }
    Log.w(TAG, "Downloading push part " + attachmentId);
    retrieveAttachment(masterSecret, messageId, attachmentId, attachment);
    MessageNotifier.updateNotification(context, masterSecret);
}
Also used : Attachment(org.thoughtcrime.securesms.attachments.Attachment) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId)

Example 7 with Attachment

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

the class MmsSendJob method constructSendPdu.

private SendReq constructSendPdu(MasterSecret masterSecret, OutgoingMediaMessage message) throws UndeliverableMessageException {
    SendReq sendReq = new SendReq();
    PduBody body = new PduBody();
    List<String> numbers = message.getRecipients().toNumberStringList(true);
    for (String number : numbers) {
        if (message.getDistributionType() == DistributionTypes.CONVERSATION) {
            sendReq.addTo(new EncodedStringValue(Util.toIsoBytes(number)));
        } else {
            sendReq.addBcc(new EncodedStringValue(Util.toIsoBytes(number)));
        }
    }
    sendReq.setDate(message.getSentTimeMillis() / 1000L);
    if (!TextUtils.isEmpty(message.getBody())) {
        PduPart part = new PduPart();
        part.setData(Util.toUtf8Bytes(message.getBody()));
        part.setCharset(CharacterSets.UTF_8);
        part.setContentType(ContentType.TEXT_PLAIN.getBytes());
        part.setContentId((System.currentTimeMillis() + "").getBytes());
        part.setName(("Text" + System.currentTimeMillis()).getBytes());
        body.addPart(part);
    }
    List<Attachment> scaledAttachments = scaleAttachments(masterSecret, MediaConstraints.MMS_CONSTRAINTS, message.getAttachments());
    for (Attachment attachment : scaledAttachments) {
        try {
            if (attachment.getDataUri() == null)
                throw new IOException("Assertion failed, attachment for outgoing MMS has no data!");
            PduPart part = new PduPart();
            part.setData(Util.readFully(PartAuthority.getAttachmentStream(context, masterSecret, attachment.getDataUri())));
            part.setContentType(Util.toIsoBytes(attachment.getContentType()));
            part.setContentId((System.currentTimeMillis() + "").getBytes());
            part.setName((System.currentTimeMillis() + "").getBytes());
            body.addPart(part);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    sendReq.setBody(body);
    return sendReq;
}
Also used : EncodedStringValue(ws.com.google.android.mms.pdu.EncodedStringValue) PduBody(ws.com.google.android.mms.pdu.PduBody) PduPart(ws.com.google.android.mms.pdu.PduPart) Attachment(org.thoughtcrime.securesms.attachments.Attachment) IOException(java.io.IOException) SendReq(ws.com.google.android.mms.pdu.SendReq)

Example 8 with Attachment

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

the class SendJob method scaleAttachments.

protected List<Attachment> scaleAttachments(@NonNull MasterSecret masterSecret, @NonNull MediaConstraints constraints, @NonNull List<Attachment> attachments) throws UndeliverableMessageException {
    AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
    List<Attachment> results = new LinkedList<>();
    for (Attachment attachment : attachments) {
        try {
            if (constraints.isSatisfied(context, masterSecret, attachment)) {
                results.add(attachment);
            } else if (constraints.canResize(attachment)) {
                MediaStream resized = constraints.getResizedMedia(context, masterSecret, attachment);
                results.add(attachmentDatabase.updateAttachmentData(masterSecret, attachment, resized));
            } else {
                throw new UndeliverableMessageException("Size constraints could not be met!");
            }
        } catch (IOException | MmsException e) {
            throw new UndeliverableMessageException(e);
        }
    }
    return results;
}
Also used : MmsException(ws.com.google.android.mms.MmsException) MediaStream(org.thoughtcrime.securesms.mms.MediaStream) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) Attachment(org.thoughtcrime.securesms.attachments.Attachment) IOException(java.io.IOException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) LinkedList(java.util.LinkedList)

Example 9 with Attachment

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

the class MmsDownloadJob method storeRetrievedMms.

private void storeRetrievedMms(MasterSecret masterSecret, String contentLocation, long messageId, long threadId, RetrieveConf retrieved, int subscriptionId) throws MmsException, NoSessionException, DuplicateMessageException, InvalidMessageException, LegacyMessageException {
    MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
    SingleUseBlobProvider provider = SingleUseBlobProvider.getInstance();
    String from = null;
    List<String> to = new LinkedList<>();
    List<String> cc = new LinkedList<>();
    String body = null;
    List<Attachment> attachments = new LinkedList<>();
    if (retrieved.getFrom() != null) {
        from = Util.toIsoString(retrieved.getFrom().getTextString());
    }
    if (retrieved.getTo() != null) {
        for (EncodedStringValue toValue : retrieved.getTo()) {
            to.add(Util.toIsoString(toValue.getTextString()));
        }
    }
    if (retrieved.getCc() != null) {
        for (EncodedStringValue ccValue : retrieved.getCc()) {
            cc.add(Util.toIsoString(ccValue.getTextString()));
        }
    }
    if (retrieved.getBody() != null) {
        body = PartParser.getMessageText(retrieved.getBody());
        PduBody media = PartParser.getSupportedMediaParts(retrieved.getBody());
        for (int i = 0; i < media.getPartsNum(); i++) {
            PduPart part = media.getPart(i);
            if (part.getData() != null) {
                Uri uri = provider.createUri(part.getData());
                attachments.add(new UriAttachment(uri, Util.toIsoString(part.getContentType()), AttachmentDatabase.TRANSFER_PROGRESS_DONE, part.getData().length));
            }
        }
    }
    IncomingMediaMessage message = new IncomingMediaMessage(from, to, cc, body, retrieved.getDate() * 1000L, attachments, subscriptionId, 0, false);
    Optional<InsertResult> insertResult = database.insertMessageInbox(new MasterSecretUnion(masterSecret), message, contentLocation, threadId);
    if (insertResult.isPresent()) {
        database.delete(messageId);
        MessageNotifier.updateNotification(context, masterSecret, insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) EncodedStringValue(ws.com.google.android.mms.pdu.EncodedStringValue) MasterSecretUnion(org.thoughtcrime.securesms.crypto.MasterSecretUnion) PduBody(ws.com.google.android.mms.pdu.PduBody) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Uri(android.net.Uri) LinkedList(java.util.LinkedList) SingleUseBlobProvider(org.thoughtcrime.securesms.providers.SingleUseBlobProvider) PduPart(ws.com.google.android.mms.pdu.PduPart) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase)

Example 10 with Attachment

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

the class GroupManager method sendGroupUpdate.

private static GroupActionResult sendGroupUpdate(@NonNull Context context, @NonNull MasterSecret masterSecret, @NonNull byte[] groupId, @NonNull Set<String> e164numbers, @Nullable String groupName, @Nullable byte[] avatar) {
    Attachment avatarAttachment = null;
    String groupRecipientId = GroupUtil.getEncodedId(groupId);
    Recipients groupRecipient = RecipientFactory.getRecipientsFromString(context, groupRecipientId, false);
    GroupContext.Builder groupContextBuilder = GroupContext.newBuilder().setId(ByteString.copyFrom(groupId)).setType(GroupContext.Type.UPDATE).addAllMembers(e164numbers);
    if (groupName != null)
        groupContextBuilder.setName(groupName);
    GroupContext groupContext = groupContextBuilder.build();
    if (avatar != null) {
        Uri avatarUri = SingleUseBlobProvider.getInstance().createUri(avatar);
        avatarAttachment = new UriAttachment(avatarUri, ContentType.IMAGE_PNG, AttachmentDatabase.TRANSFER_PROGRESS_DONE, avatar.length);
    }
    OutgoingGroupMediaMessage outgoingMessage = new OutgoingGroupMediaMessage(groupRecipient, groupContext, avatarAttachment, System.currentTimeMillis(), 0);
    long threadId = MessageSender.send(context, masterSecret, outgoingMessage, -1, false);
    return new GroupActionResult(groupRecipient, threadId);
}
Also used : OutgoingGroupMediaMessage(org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage) Recipients(org.thoughtcrime.securesms.recipients.Recipients) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) ByteString(com.google.protobuf.ByteString) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Uri(android.net.Uri) GroupContext(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)

Aggregations

Attachment (org.thoughtcrime.securesms.attachments.Attachment)14 IOException (java.io.IOException)5 LinkedList (java.util.LinkedList)5 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)3 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)3 OutgoingGroupMediaMessage (org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage)3 Recipients (org.thoughtcrime.securesms.recipients.Recipients)3 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)3 MmsException (ws.com.google.android.mms.MmsException)3 Uri (android.net.Uri)2 Test (org.junit.Test)2 BaseUnitTest (org.thoughtcrime.securesms.BaseUnitTest)2 MmsNotificationAttachment (org.thoughtcrime.securesms.attachments.MmsNotificationAttachment)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 MasterSecretUnion (org.thoughtcrime.securesms.crypto.MasterSecretUnion)2 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)2 OutgoingMediaMessage (org.thoughtcrime.securesms.mms.OutgoingMediaMessage)2 UndeliverableMessageException (org.thoughtcrime.securesms.transport.UndeliverableMessageException)2 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)2 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)2