Search in sources :

Example 1 with AttachmentModel

use of org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel in project Signal-Android by signalapp.

the class AvatarDownloadJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    String encodeId = GroupUtil.getEncodedId(groupId, false);
    GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
    Optional<GroupRecord> record = database.getGroup(encodeId);
    File attachment = null;
    try {
        if (record.isPresent()) {
            long avatarId = record.get().getAvatarId();
            String contentType = record.get().getAvatarContentType();
            byte[] key = record.get().getAvatarKey();
            String relay = record.get().getRelay();
            Optional<byte[]> digest = Optional.fromNullable(record.get().getAvatarDigest());
            Optional<String> fileName = Optional.absent();
            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, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false);
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
            Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key, 0, digest), 500, 500);
            database.updateAvatar(encodeId, 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) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) 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 2 with AttachmentModel

use of org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel 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

Bitmap (android.graphics.Bitmap)2 File (java.io.File)2 InputStream (java.io.InputStream)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 AttachmentModel (org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel)2 BitmapDecodingException (org.thoughtcrime.securesms.util.BitmapDecodingException)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)2 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)2 GroupRecord (org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord)1