Search in sources :

Example 1 with NonSuccessfulResponseCodeException

use of org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException 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 NonSuccessfulResponseCodeException

use of org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException in project Signal-Android by WhisperSystems.

the class AvatarDownloadJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
    GroupDatabase.GroupRecord record = database.getGroup(groupId);
    File attachment = null;
    try {
        if (record != null) {
            long avatarId = record.getAvatarId();
            String contentType = record.getAvatarContentType();
            byte[] key = record.getAvatarKey();
            String relay = record.getRelay();
            Optional<byte[]> digest = Optional.fromNullable(record.getAvatarDigest());
            if (avatarId == -1 || key == null) {
                return;
            }
            if (digest.isPresent()) {
                Log.w(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
            }
            attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
            attachment.deleteOnExit();
            SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(avatarId, contentType, key, relay, digest);
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
            Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key), 500, 500);
            database.updateAvatar(groupId, avatar);
            inputStream.close();
        }
    } catch (BitmapDecodingException | NonSuccessfulResponseCodeException | InvalidMessageException e) {
        Log.w(TAG, e);
    } finally {
        if (attachment != null)
            attachment.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) Bitmap(android.graphics.Bitmap) AttachmentModel(org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Aggregations

File (java.io.File)2 InputStream (java.io.InputStream)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)2 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)2 Bitmap (android.graphics.Bitmap)1 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)1 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)1 PartProgressEvent (org.thoughtcrime.securesms.events.PartProgressEvent)1 AttachmentModel (org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel)1 BitmapDecodingException (org.thoughtcrime.securesms.util.BitmapDecodingException)1 ProgressListener (org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener)1 MmsException (ws.com.google.android.mms.MmsException)1