Search in sources :

Example 1 with UiInformableCallback

use of eu.siacs.conversations.ui.UiInformableCallback in project Conversations by siacs.

the class XmppConnectionService method attachFileToConversation.

public void attachFileToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
    if (FileBackend.weOwnFile(this, uri)) {
        Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
        callback.error(R.string.security_error_invalid_file_access, null);
        return;
    }
    final Message message;
    if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
        message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
    } else {
        message = new Message(conversation, "", conversation.getNextEncryption());
    }
    message.setCounterpart(conversation.getNextCounterpart());
    message.setType(Message.TYPE_FILE);
    mFileAddingExecutor.execute(new Runnable() {

        private void processAsFile() {
            final String path = getFileBackend().getOriginalPath(uri);
            if (path != null) {
                message.setRelativeFilePath(path);
                getFileBackend().updateFileParams(message);
                if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                    getPgpEngine().encrypt(message, callback);
                } else {
                    callback.success(message);
                }
            } else {
                try {
                    getFileBackend().copyFileToPrivateStorage(message, uri);
                    getFileBackend().updateFileParams(message);
                    if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        final PgpEngine pgpEngine = getPgpEngine();
                        if (pgpEngine != null) {
                            pgpEngine.encrypt(message, callback);
                        } else if (callback != null) {
                            callback.error(R.string.unable_to_connect_to_keychain, null);
                        }
                    } else {
                        callback.success(message);
                    }
                } catch (FileBackend.FileCopyException e) {
                    callback.error(e.getResId(), message);
                }
            }
        }

        private void processAsVideo() throws FileNotFoundException {
            Log.d(Config.LOGTAG, "processing file as video");
            message.setRelativeFilePath(message.getUuid() + ".mp4");
            final DownloadableFile file = getFileBackend().getFile(message);
            file.getParentFile().mkdirs();
            ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            final ArrayList<Integer> progressTracker = new ArrayList<>();
            final UiInformableCallback<Message> informableCallback;
            if (callback instanceof UiInformableCallback) {
                informableCallback = (UiInformableCallback<Message>) callback;
            } else {
                informableCallback = null;
            }
            MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {

                @Override
                public void onTranscodeProgress(double progress) {
                    int p = ((int) Math.round(progress * 100) / 20) * 20;
                    if (!progressTracker.contains(p) && p != 100 && p != 0) {
                        progressTracker.add(p);
                        if (informableCallback != null) {
                            informableCallback.inform(getString(R.string.transcoding_video_progress, p));
                        }
                    }
                }

                @Override
                public void onTranscodeCompleted() {
                    if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        getPgpEngine().encrypt(message, callback);
                    } else {
                        callback.success(message);
                    }
                }

                @Override
                public void onTranscodeCanceled() {
                    processAsFile();
                }

                @Override
                public void onTranscodeFailed(Exception e) {
                    Log.d(Config.LOGTAG, "video transcoding failed " + e.getMessage());
                    processAsFile();
                }
            };
            MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), MediaFormatStrategyPresets.createAndroid720pStrategy(), listener);
        }

        @Override
        public void run() {
            final String mimeType = MimeUtils.guessMimeTypeFromUri(XmppConnectionService.this, uri);
            if (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                try {
                    processAsVideo();
                } catch (Throwable e) {
                    processAsFile();
                }
            } else {
                processAsFile();
            }
        }
    });
}
Also used : OnPhoneContactsLoadedListener(eu.siacs.conversations.utils.OnPhoneContactsLoadedListener) OnBindListener(eu.siacs.conversations.xmpp.OnBindListener) OnRenameListener(eu.siacs.conversations.entities.MucOptions.OnRenameListener) XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message) FileNotFoundException(java.io.FileNotFoundException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) MediaTranscoder(net.ypresto.androidtranscoder.MediaTranscoder) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) UiInformableCallback(eu.siacs.conversations.ui.UiInformableCallback) OtrException(net.java.otr4j.OtrException) FileNotFoundException(java.io.FileNotFoundException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) CertificateException(java.security.cert.CertificateException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 PgpEngine (eu.siacs.conversations.crypto.PgpEngine)1 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)1 DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)1 Message (eu.siacs.conversations.entities.Message)1 OnRenameListener (eu.siacs.conversations.entities.MucOptions.OnRenameListener)1 UiInformableCallback (eu.siacs.conversations.ui.UiInformableCallback)1 OnPhoneContactsLoadedListener (eu.siacs.conversations.utils.OnPhoneContactsLoadedListener)1 OnBindListener (eu.siacs.conversations.xmpp.OnBindListener)1 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)1 FileDescriptor (java.io.FileDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 OtrException (net.java.otr4j.OtrException)1 MediaTranscoder (net.ypresto.androidtranscoder.MediaTranscoder)1