Search in sources :

Example 1 with PartProgressEvent

use of org.thoughtcrime.securesms.events.PartProgressEvent in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method retrieveAttachment.

private void retrieveAttachment(MasterSecret masterSecret, long messageId, final AttachmentId attachmentId, final Attachment attachment) throws IOException {
    AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
    File attachmentFile = null;
    try {
        attachmentFile = createTempFile();
        SignalServiceAttachmentPointer pointer = createAttachmentPointer(masterSecret, attachment);
        InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, new ProgressListener() {

            @Override
            public void onAttachmentProgress(long total, long progress) {
                EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
            }
        });
        database.insertAttachmentsForPlaceholder(masterSecret, messageId, attachmentId, stream);
    } catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException e) {
        Log.w(TAG, e);
        markFailed(messageId, attachmentId);
    } finally {
        if (attachmentFile != null)
            attachmentFile.delete();
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) MmsException(ws.com.google.android.mms.MmsException) ProgressListener(org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener) File(java.io.File)

Example 2 with PartProgressEvent

use of org.thoughtcrime.securesms.events.PartProgressEvent in project Signal-Android by WhisperSystems.

the class PushSendJob method getAttachmentsFor.

protected List<SignalServiceAttachment> getAttachmentsFor(MasterSecret masterSecret, List<Attachment> parts) {
    List<SignalServiceAttachment> attachments = new LinkedList<>();
    for (final Attachment attachment : parts) {
        if (ContentType.isImageType(attachment.getContentType()) || ContentType.isAudioType(attachment.getContentType()) || ContentType.isVideoType(attachment.getContentType())) {
            try {
                if (attachment.getDataUri() == null || attachment.getSize() == 0)
                    throw new IOException("Assertion failed, outgoing attachment has no data!");
                InputStream is = PartAuthority.getAttachmentStream(context, masterSecret, attachment.getDataUri());
                attachments.add(SignalServiceAttachment.newStreamBuilder().withStream(is).withContentType(attachment.getContentType()).withLength(attachment.getSize()).withListener(new ProgressListener() {

                    @Override
                    public void onAttachmentProgress(long total, long progress) {
                        EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
                    }
                }).build());
            } catch (IOException ioe) {
                Log.w(TAG, "Couldn't open attachment", ioe);
            }
        }
    }
    return attachments;
}
Also used : SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) ProgressListener(org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener) InputStream(java.io.InputStream) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Aggregations

InputStream (java.io.InputStream)2 PartProgressEvent (org.thoughtcrime.securesms.events.PartProgressEvent)2 ProgressListener (org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener)2 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Attachment (org.thoughtcrime.securesms.attachments.Attachment)1 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)1 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)1 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)1 MmsException (ws.com.google.android.mms.MmsException)1