Search in sources :

Example 6 with DownloadableFile

use of eu.siacs.conversations.entities.DownloadableFile in project Conversations by siacs.

the class FileBackend method getThumbnail.

public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws FileNotFoundException {
    final String uuid = message.getUuid();
    final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
    Bitmap thumbnail = cache.get(uuid);
    if ((thumbnail == null) && (!cacheOnly)) {
        synchronized (THUMBNAIL_LOCK) {
            thumbnail = cache.get(uuid);
            if (thumbnail != null) {
                return thumbnail;
            }
            DownloadableFile file = getFile(message);
            final String mime = file.getMimeType();
            if (mime.startsWith("video/")) {
                thumbnail = getVideoPreview(file, size);
            } else {
                Bitmap fullsize = getFullsizeImagePreview(file, size);
                if (fullsize == null) {
                    throw new FileNotFoundException();
                }
                thumbnail = resize(fullsize, size);
                thumbnail = rotate(thumbnail, getRotation(file));
                if (mime.equals("image/gif")) {
                    Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
                    drawOverlay(withGifOverlay, R.drawable.play_gif, 1.0f);
                    thumbnail.recycle();
                    thumbnail = withGifOverlay;
                }
            }
            this.mXmppConnectionService.getBitmapCache().put(uuid, thumbnail);
        }
    }
    return thumbnail;
}
Also used : Bitmap(android.graphics.Bitmap) FileNotFoundException(java.io.FileNotFoundException) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 7 with DownloadableFile

use of eu.siacs.conversations.entities.DownloadableFile in project Conversations by siacs.

the class FileBackend method getFile.

public DownloadableFile getFile(Message message, boolean decrypted) {
    final boolean encrypted = !decrypted && (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
    final DownloadableFile file;
    String path = message.getRelativeFilePath();
    if (path == null) {
        path = message.getUuid();
    }
    if (path.startsWith("/")) {
        file = new DownloadableFile(path);
    } else {
        String mime = message.getMimeType();
        if (mime != null && mime.startsWith("image/")) {
            file = new DownloadableFile(getConversationsDirectory("Images") + path);
        } else if (mime != null && mime.startsWith("video/")) {
            file = new DownloadableFile(getConversationsDirectory("Videos") + path);
        } else {
            file = new DownloadableFile(getConversationsDirectory("Files") + path);
        }
    }
    if (encrypted) {
        return new DownloadableFile(getConversationsDirectory("Files") + file.getName() + ".pgp");
    } else {
        return file;
    }
}
Also used : DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 8 with DownloadableFile

use of eu.siacs.conversations.entities.DownloadableFile in project Conversations by siacs.

the class ConversationFragment method resendMessage.

private void resendMessage(Message message) {
    if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
        DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
        if (!file.exists()) {
            Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
            message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
            activity.updateConversationList();
            updateMessages();
            return;
        }
    }
    activity.xmppConnectionService.resendFailedMessages(message);
}
Also used : TransferablePlaceholder(eu.siacs.conversations.entities.TransferablePlaceholder) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 9 with DownloadableFile

use of eu.siacs.conversations.entities.DownloadableFile in project Conversations by siacs.

the class PgpEngine method encrypt.

public void encrypt(final Message message, final UiCallback<Message> callback) {
    Intent params = new Intent();
    params.setAction(OpenPgpApi.ACTION_ENCRYPT);
    final Conversation conversation = message.getConversation();
    if (conversation.getMode() == Conversation.MODE_SINGLE) {
        long[] keys = { conversation.getContact().getPgpKeyId(), conversation.getAccount().getPgpId() };
        params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, keys);
    } else {
        params.putExtra(OpenPgpApi.EXTRA_KEY_IDS, conversation.getMucOptions().getPgpKeyIds());
    }
    if (!message.needsUploading()) {
        params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
        String body;
        if (message.hasFileOnRemoteHost()) {
            body = message.getFileParams().url.toString();
        } else {
            body = message.getBody();
        }
        InputStream is = new ByteArrayInputStream(body.getBytes());
        final OutputStream os = new ByteArrayOutputStream();
        api.executeApiAsync(params, is, os, new IOpenPgpCallback() {

            @Override
            public void onReturn(Intent result) {
                switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                    case OpenPgpApi.RESULT_CODE_SUCCESS:
                        try {
                            os.flush();
                            StringBuilder encryptedMessageBody = new StringBuilder();
                            String[] lines = os.toString().split("\n");
                            for (int i = 2; i < lines.length - 1; ++i) {
                                if (!lines[i].contains("Version")) {
                                    encryptedMessageBody.append(lines[i].trim());
                                }
                            }
                            message.setEncryptedBody(encryptedMessageBody.toString());
                            callback.success(message);
                        } catch (IOException e) {
                            callback.error(R.string.openpgp_error, message);
                        }
                        break;
                    case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                        callback.userInputRequried((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
                        break;
                    case OpenPgpApi.RESULT_CODE_ERROR:
                        logError(conversation.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
                        callback.error(R.string.openpgp_error, message);
                        break;
                }
            }
        });
    } else {
        try {
            DownloadableFile inputFile = this.mXmppConnectionService.getFileBackend().getFile(message, true);
            DownloadableFile outputFile = this.mXmppConnectionService.getFileBackend().getFile(message, false);
            outputFile.getParentFile().mkdirs();
            outputFile.createNewFile();
            final InputStream is = new FileInputStream(inputFile);
            final OutputStream os = new FileOutputStream(outputFile);
            api.executeApiAsync(params, is, os, new IOpenPgpCallback() {

                @Override
                public void onReturn(Intent result) {
                    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                        case OpenPgpApi.RESULT_CODE_SUCCESS:
                            try {
                                os.flush();
                            } catch (IOException ignored) {
                            //ignored
                            }
                            FileBackend.close(os);
                            callback.success(message);
                            break;
                        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                            callback.userInputRequried((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
                            break;
                        case OpenPgpApi.RESULT_CODE_ERROR:
                            logError(conversation.getAccount(), (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
                            callback.error(R.string.openpgp_error, message);
                            break;
                    }
                }
            });
        } catch (final IOException e) {
            callback.error(R.string.openpgp_error, message);
        }
    }
}
Also used : IOpenPgpCallback(org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Conversation(eu.siacs.conversations.entities.Conversation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Aggregations

DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)9 Intent (android.content.Intent)4 PendingIntent (android.app.PendingIntent)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 SpannableString (android.text.SpannableString)1 PgpEngine (eu.siacs.conversations.crypto.PgpEngine)1 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)1