Search in sources :

Example 1 with DownloadableFile

use of de.pixart.messenger.entities.DownloadableFile in project Pix-Art-Messenger by kriztan.

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(de.pixart.messenger.entities.DownloadableFile)

Example 2 with DownloadableFile

use of de.pixart.messenger.entities.DownloadableFile in project Pix-Art-Messenger by kriztan.

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);
    String path = message.getRelativeFilePath();
    if (path == null) {
        path = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
    }
    final DownloadableFile file = getFileForPath(path, message.getMimeType());
    if (encrypted) {
        return new DownloadableFile(getConversationsDirectory("Files", true) + file.getName() + ".pgp");
    } else {
        return file;
    }
}
Also used : DownloadableFile(de.pixart.messenger.entities.DownloadableFile) Date(java.util.Date)

Example 3 with DownloadableFile

use of de.pixart.messenger.entities.DownloadableFile in project Pix-Art-Messenger by kriztan.

the class ConversationFragment method shareWith.

private void shareWith(Message message) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    if (message.isGeoUri() || message.isXmppUri()) {
        shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
        shareIntent.setType("text/plain");
    } else if (!message.isFileOrImage()) {
        shareIntent.putExtra(Intent.EXTRA_TEXT, message.getMergedBody().toString());
        shareIntent.setType("text/plain");
    } else {
        final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
        try {
            shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(getActivity(), file));
        } catch (SecurityException e) {
            Toast.makeText(getActivity(), activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
            return;
        }
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String mime = message.getMimeType();
        if (mime == null) {
            mime = "*/*";
        }
        shareIntent.setType(mime);
    }
    try {
        startActivity(Intent.createChooser(shareIntent, getText(R.string.share_with)));
    } catch (ActivityNotFoundException e) {
        // This should happen only on faulty androids because normally chooser is always available
        Toast.makeText(getActivity(), R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
    }
}
Also used : ActivityNotFoundException(android.content.ActivityNotFoundException) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) DownloadableFile(de.pixart.messenger.entities.DownloadableFile)

Example 4 with DownloadableFile

use of de.pixart.messenger.entities.DownloadableFile in project Pix-Art-Messenger by kriztan.

the class MessageAdapter method displayStatus.

private void displayStatus(ViewHolder viewHolder, final Message message, int type, boolean darkBackground) {
    String filesize = null;
    String info = null;
    boolean error = false;
    if (viewHolder.indicatorReceived != null) {
        viewHolder.indicatorReceived.setVisibility(View.GONE);
        viewHolder.indicatorRead.setVisibility(View.GONE);
    }
    if (viewHolder.edit_indicator != null) {
        if (message.edited()) {
            viewHolder.edit_indicator.setVisibility(View.VISIBLE);
            viewHolder.edit_indicator.setAlpha(0.7f);
        } else {
            viewHolder.edit_indicator.setVisibility(View.GONE);
        }
    }
    boolean multiReceived = message.getConversation().getMode() == Conversation.MODE_MULTI && message.getMergedStatus() <= Message.STATUS_RECEIVED;
    if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.getTransferable() != null) {
        FileParams params = message.getFileParams();
        if (params.size > (1 * 1024 * 1024)) {
            filesize = Math.round(params.size * 1f / (1024 * 1024)) + " MiB";
        } else if (params.size >= (1 * 1024)) {
            filesize = Math.round(params.size * 1f / 1024) + " KiB";
        } else if (params.size > 0) {
            filesize = params.size + " B";
        }
        if (message.getTransferable() != null && message.getTransferable().getStatus() == Transferable.STATUS_FAILED) {
            error = true;
        }
    }
    switch(message.getMergedStatus()) {
        case Message.STATUS_WAITING:
            info = getContext().getString(R.string.waiting);
            break;
        case Message.STATUS_UNSEND:
            Transferable d = message.getTransferable();
            if (d != null) {
                info = getContext().getString(R.string.sending_file, d.getProgress());
            } else {
                info = getContext().getString(R.string.sending);
            }
            break;
        case Message.STATUS_OFFERED:
            info = getContext().getString(R.string.offering);
            break;
        case Message.STATUS_SEND_RECEIVED:
            if (mIndicateReceived) {
                viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
            }
            break;
        case Message.STATUS_SEND_DISPLAYED:
            if (mIndicateReceived) {
                viewHolder.indicatorReceived.setVisibility(View.VISIBLE);
                viewHolder.indicatorRead.setVisibility(View.VISIBLE);
            }
            break;
        case Message.STATUS_SEND_FAILED:
            if (isResendable) {
                info = getContext().getString(R.string.send_failed_resend);
            } else {
                info = getContext().getString(R.string.send_failed);
            }
            error = true;
            break;
        default:
            if (multiReceived) {
                info = UIHelper.getMessageDisplayName(message);
            }
            break;
    }
    if (error && type == SENT) {
        viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_Waring);
        DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
        if (file.exists()) {
            if (activity.xmppConnectionService.mHttpConnectionManager.getAutoAcceptFileSize() >= message.getFileParams().size) {
                isResendable = true;
                viewHolder.resend_button.setVisibility(View.GONE);
            } else {
                isResendable = false;
                viewHolder.resend_button.setVisibility(View.VISIBLE);
            }
        }
        viewHolder.resend_button.setText(R.string.send_again);
        viewHolder.resend_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_resend_grey600_48dp, 0, 0, 0);
        viewHolder.resend_button.setOnClickListener(new OnClickListener() {

            final Message mMessage = message;

            @Override
            public void onClick(View v) {
                mConversationFragment.resendMessage(mMessage);
            }
        });
    } else if (!error && type == SENT) {
        viewHolder.resend_button.setVisibility(View.GONE);
        viewHolder.time.setTextColor(this.getMessageTextColor(darkBackground, false));
    } else {
        if (darkBackground) {
            viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption_OnDark);
        } else {
            viewHolder.time.setTextAppearance(getContext(), R.style.TextAppearance_Conversations_Caption);
        }
        viewHolder.time.setTextColor(this.getMessageTextColor(darkBackground, false));
    }
    if (message.getEncryption() == Message.ENCRYPTION_NONE) {
        viewHolder.indicator.setVisibility(View.GONE);
    } else {
        boolean verified = false;
        if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
            final FingerprintStatus status = message.getConversation().getAccount().getAxolotlService().getFingerprintTrust(message.getFingerprint());
            if (status != null && status.isVerified()) {
                verified = true;
            }
        }
        if (verified) {
            viewHolder.indicator.setImageResource(R.drawable.ic_verified_user_blue_18dp);
        } else {
            viewHolder.indicator.setImageResource(R.drawable.ic_lock_blue_18dp);
        }
        viewHolder.indicator.setAlpha(0.7f);
        viewHolder.indicator.setVisibility(View.VISIBLE);
    }
    String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent());
    if (message.getStatus() <= Message.STATUS_RECEIVED) {
        if ((filesize != null) && (info != null)) {
            viewHolder.time.setText(formatedTime + " \u00B7 " + filesize + " \u00B7 " + info);
        } else if ((filesize == null) && (info != null)) {
            viewHolder.time.setText(formatedTime + " \u00B7 " + info);
        } else if ((filesize != null) && (info == null)) {
            viewHolder.time.setText(formatedTime + " \u00B7 " + filesize);
        } else {
            viewHolder.time.setText(formatedTime);
        }
    } else {
        if ((filesize != null) && (info != null)) {
            viewHolder.time.setText(filesize + " \u00B7 " + info);
        } else if ((filesize == null) && (info != null)) {
            if (error) {
                viewHolder.time.setText(info + " \u00B7 " + formatedTime);
            } else {
                viewHolder.time.setText(info);
            }
        } else if ((filesize != null) && (info == null)) {
            viewHolder.time.setText(filesize + " \u00B7 " + formatedTime);
        } else {
            viewHolder.time.setText(formatedTime);
        }
    }
}
Also used : Message(de.pixart.messenger.entities.Message) FingerprintStatus(de.pixart.messenger.crypto.axolotl.FingerprintStatus) FileParams(de.pixart.messenger.entities.Message.FileParams) Transferable(de.pixart.messenger.entities.Transferable) OnClickListener(android.view.View.OnClickListener) SpannableString(android.text.SpannableString) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CopyTextView(de.pixart.messenger.ui.widget.CopyTextView)

Example 5 with DownloadableFile

use of de.pixart.messenger.entities.DownloadableFile in project Pix-Art-Messenger by kriztan.

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, 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());
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        mXmppConnectionService.sendMessage(message);
                        callback.success(message);
                    } catch (IOException e) {
                        callback.error(R.string.openpgp_error, message);
                    }
                    break;
                case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                    callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
                    break;
                case OpenPgpApi.RESULT_CODE_ERROR:
                    logError(conversation.getAccount(), 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, 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);
                        mXmppConnectionService.sendMessage(message);
                        callback.success(message);
                        break;
                    case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                        callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
                        break;
                    case OpenPgpApi.RESULT_CODE_ERROR:
                        logError(conversation.getAccount(), 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 : 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(de.pixart.messenger.entities.Conversation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) DownloadableFile(de.pixart.messenger.entities.DownloadableFile)

Aggregations

DownloadableFile (de.pixart.messenger.entities.DownloadableFile)10 Intent (android.content.Intent)4 PendingIntent (android.app.PendingIntent)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 SpannableString (android.text.SpannableString)2 Conversation (de.pixart.messenger.entities.Conversation)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 Date (java.util.Date)2 SuppressLint (android.annotation.SuppressLint)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1