Search in sources :

Example 6 with DownloadableFile

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

the class ConversationFragment method resendMessage.

public void resendMessage(final Message message) {
    if (message.isFileOrImage()) {
        DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
        if (file.exists()) {
            final Conversation conversation = message.getConversation();
            final XmppConnection xmppConnection = conversation.getAccount().getXmppConnection();
            if (!message.hasFileOnRemoteHost() && xmppConnection != null && !xmppConnection.getFeatures().httpUpload(message.getFileParams().size)) {
                activity.selectPresence(conversation, () -> {
                    message.setCounterpart(conversation.getNextCounterpart());
                    activity.xmppConnectionService.resendFailedMessages(message);
                    new Handler().post(() -> {
                        int size = messageList.size();
                        this.binding.messagesView.setSelection(size - 1);
                    });
                });
                return;
            }
        } else {
            Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
            message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
            activity.onConversationsListItemUpdated();
            refresh();
            return;
        }
    }
    activity.xmppConnectionService.resendFailedMessages(message);
    new Handler().post(() -> {
        int size = messageList.size();
        this.binding.messagesView.setSelection(size - 1);
    });
}
Also used : XmppConnection(de.pixart.messenger.xmpp.XmppConnection) TransferablePlaceholder(de.pixart.messenger.entities.TransferablePlaceholder) Handler(android.os.Handler) Conversation(de.pixart.messenger.entities.Conversation) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) SuppressLint(android.annotation.SuppressLint)

Example 7 with DownloadableFile

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

the class PgpDecryptionService method executeApi.

private void executeApi(Message message) {
    synchronized (message) {
        Intent params = userInteractionResult != null ? userInteractionResult : new Intent();
        params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
        if (message.getType() == Message.TYPE_TEXT) {
            InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
            final OutputStream os = new ByteArrayOutputStream();
            Intent result = getOpenPgpApi().executeApi(params, is, os);
            switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                case OpenPgpApi.RESULT_CODE_SUCCESS:
                    try {
                        os.flush();
                        final String body = os.toString();
                        if (body == null) {
                            throw new IOException("body was null");
                        }
                        message.setBody(body);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
                        if (message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) {
                            manager.createNewDownloadConnection(message);
                        }
                    } catch (IOException e) {
                        message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                    }
                    mXmppConnectionService.updateMessage(message);
                    break;
                case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                    synchronized (PgpDecryptionService.this) {
                        PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
                        messages.addFirst(message);
                        currentMessage = null;
                        storePendingIntent(pendingIntent);
                    }
                    break;
                case OpenPgpApi.RESULT_CODE_ERROR:
                    message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                    mXmppConnectionService.updateMessage(message);
                    break;
            }
        } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
            try {
                final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
                final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
                if (outputFile.getParentFile().mkdirs()) {
                    Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
                }
                outputFile.createNewFile();
                InputStream is = new FileInputStream(inputFile);
                OutputStream os = new FileOutputStream(outputFile);
                Intent result = getOpenPgpApi().executeApi(params, is, os);
                switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
                    case OpenPgpApi.RESULT_CODE_SUCCESS:
                        OpenPgpMetadata openPgpMetadata = result.getParcelableExtra(OpenPgpApi.RESULT_METADATA);
                        String originalFilename = openPgpMetadata.getFilename();
                        String originalExtension = originalFilename == null ? null : MimeUtils.extractRelevantExtension(originalFilename);
                        if (originalExtension != null && MimeUtils.extractRelevantExtension(outputFile.getName()) == null) {
                            Log.d(Config.LOGTAG, "detected original filename during pgp decryption");
                            String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension);
                            String path = outputFile.getName() + "." + originalExtension;
                            DownloadableFile fixedFile = mXmppConnectionService.getFileBackend().getFileForPath(path, mime);
                            if (fixedFile.getParentFile().mkdirs()) {
                                Log.d(Config.LOGTAG, "created parent directories for " + fixedFile.getAbsolutePath());
                            }
                            if (outputFile.renameTo(fixedFile)) {
                                Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath());
                                message.setRelativeFilePath(path);
                            }
                        }
                        URL url = message.getFileParams().url;
                        mXmppConnectionService.getFileBackend().updateFileParams(message, url);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        inputFile.delete();
                        mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
                        mXmppConnectionService.updateMessage(message);
                        break;
                    case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
                        synchronized (PgpDecryptionService.this) {
                            PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
                            messages.addFirst(message);
                            currentMessage = null;
                            storePendingIntent(pendingIntent);
                        }
                        break;
                    case OpenPgpApi.RESULT_CODE_ERROR:
                        message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                        mXmppConnectionService.updateMessage(message);
                        break;
                }
            } catch (final IOException e) {
                message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
                mXmppConnectionService.updateMessage(message);
            }
        }
    }
    notifyIfPending(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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) HttpConnectionManager(de.pixart.messenger.http.HttpConnectionManager) PendingIntent(android.app.PendingIntent) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) OpenPgpMetadata(org.openintents.openpgp.OpenPgpMetadata)

Example 8 with DownloadableFile

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

the class AttachFileToConversationRunnable method processAsVideo.

private void processAsVideo() throws FileNotFoundException {
    Log.d(Config.LOGTAG, "processing file as video");
    mXmppConnectionService.startForcingForegroundNotification();
    SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
    message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "_komp.mp4");
    final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
    file.getParentFile().mkdirs();
    final ParcelFileDescriptor parcelFileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
    if (parcelFileDescriptor == null) {
        throw new FileNotFoundException("Parcel File Descriptor was null");
    }
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Future<Void> future = MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), MediaFormatStrategyPresets.createAndroidStandardStrategy(mXmppConnectionService.getCompressVideoBitratePreference(), mXmppConnectionService.getCompressVideoResolutionPreference()), this);
    try {
        future.get();
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    } catch (ExecutionException e) {
        Log.d(Config.LOGTAG, "ignoring execution exception. Should get handled by onTranscodeFiled() instead", e);
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) ExecutionException(java.util.concurrent.ExecutionException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 9 with DownloadableFile

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

the class MessageAdapter method openDownloadable.

public void openDownloadable(Message message) {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ConversationFragment.registerPendingMessage(activity, message);
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, ConversationActivity.REQUEST_OPEN_MESSAGE);
        return;
    }
    DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
    if (!file.exists()) {
        Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
        return;
    }
    String mime = file.getMimeType();
    if (mime.startsWith("image/")) {
        Intent intent = new Intent(getContext(), ShowFullscreenMessageActivity.class);
        intent.putExtra("image", Uri.fromFile(file));
        try {
            activity.startActivity(intent);
            return;
        } catch (ActivityNotFoundException e) {
        // ignored
        }
    } else if (mime.startsWith("video/")) {
        Intent intent = new Intent(getContext(), ShowFullscreenMessageActivity.class);
        intent.putExtra("video", Uri.fromFile(file));
        try {
            activity.startActivity(intent);
            return;
        } catch (ActivityNotFoundException e) {
        // ignored
        }
    }
    Intent openIntent = new Intent(Intent.ACTION_VIEW);
    if (mime == null) {
        mime = "*/*";
    }
    Uri uri;
    try {
        uri = FileBackend.getUriForFile(activity, file);
    } catch (SecurityException e) {
        Log.d(Config.LOGTAG, "No permission to access " + file.getAbsolutePath(), e);
        Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
        return;
    }
    openIntent.setDataAndType(uri, mime);
    openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    PackageManager manager = activity.getPackageManager();
    List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
    if (info.size() == 0) {
        openIntent.setDataAndType(uri, "*/*");
    }
    try {
        getContext().startActivity(openIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
    }
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ShowFullscreenMessageActivity(de.pixart.messenger.ui.ShowFullscreenMessageActivity) PackageManager(android.content.pm.PackageManager) ActivityNotFoundException(android.content.ActivityNotFoundException) Intent(android.content.Intent) SpannableString(android.text.SpannableString) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) Uri(android.net.Uri) XmppUri(de.pixart.messenger.utils.XmppUri)

Example 10 with DownloadableFile

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

the class FileBackend method updateFileParams.

public void updateFileParams(Message message, URL url) {
    DownloadableFile file = getFile(message);
    final String mime = file.getMimeType();
    boolean image = message.getType() == Message.TYPE_IMAGE || (mime != null && mime.startsWith("image/"));
    boolean video = mime != null && mime.startsWith("video/");
    boolean audio = mime != null && mime.startsWith("audio/");
    boolean vcard = mime != null && mime.contains("vcard");
    boolean apk = mime != null && mime.equals("application/vnd.android.package-archive");
    final StringBuilder body = new StringBuilder();
    if (url != null) {
        body.append(url.toString());
    }
    body.append('|').append(file.getSize());
    if (image || video) {
        try {
            Dimensions dimensions = image ? getImageDimensions(file) : getVideoDimensions(file);
            body.append('|').append(dimensions.width).append('|').append(dimensions.height);
        } catch (NotAVideoFile notAVideoFile) {
            Log.d(Config.LOGTAG, "file with mime type " + file.getMimeType() + " was not a video file");
        // fall threw
        }
    } else if (audio) {
        body.append("|0|0|").append(getMediaRuntime(file));
    } else if (vcard) {
        body.append("|0|0|0|").append(getVCard(file));
    } else if (apk) {
        body.append("|0|0|0|").append(getAPK(file, mXmppConnectionService.getApplicationContext()));
    }
    message.setBody(body.toString());
}
Also used : 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