Search in sources :

Example 1 with BitmapDecodingException

use of org.thoughtcrime.securesms.util.BitmapDecodingException in project Signal-Android by WhisperSystems.

the class MediaConstraints method isWithinBounds.

private boolean isWithinBounds(Context context, MasterSecret masterSecret, Uri uri) throws IOException {
    try {
        InputStream is = PartAuthority.getAttachmentStream(context, masterSecret, uri);
        Pair<Integer, Integer> dimensions = BitmapUtil.getDimensions(is);
        return dimensions.first > 0 && dimensions.first <= getImageMaxWidth(context) && dimensions.second > 0 && dimensions.second <= getImageMaxHeight(context);
    } catch (BitmapDecodingException e) {
        throw new IOException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Example 2 with BitmapDecodingException

use of org.thoughtcrime.securesms.util.BitmapDecodingException 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)

Example 3 with BitmapDecodingException

use of org.thoughtcrime.securesms.util.BitmapDecodingException 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 4 with BitmapDecodingException

use of org.thoughtcrime.securesms.util.BitmapDecodingException in project Signal-Android by WhisperSystems.

the class EmojiPageBitmap method loadPage.

private Bitmap loadPage() throws IOException {
    if (bitmapReference != null && bitmapReference.get() != null)
        return bitmapReference.get();
    try {
        final Bitmap bitmap = BitmapUtil.createScaledBitmap(context, "file:///android_asset/" + model.getSprite(), decodeScale);
        bitmapReference = new SoftReference<>(bitmap);
        Log.w(TAG, "onPageLoaded(" + model.getSprite() + ")");
        return bitmap;
    } catch (BitmapDecodingException e) {
        Log.w(TAG, e);
        throw new IOException(e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) IOException(java.io.IOException) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Aggregations

BitmapDecodingException (org.thoughtcrime.securesms.util.BitmapDecodingException)4 InputStream (java.io.InputStream)3 Bitmap (android.graphics.Bitmap)2 IOException (java.io.IOException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)1 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)1 MasterSecret (org.thoughtcrime.securesms.crypto.MasterSecret)1 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)1 AttachmentModel (org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)1 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)1