AttachFileToConversationRunnable.java

  1package eu.siacs.conversations.services;
  2
  3import android.net.Uri;
  4import android.os.Build;
  5import android.os.ParcelFileDescriptor;
  6import android.util.Log;
  7
  8import net.ypresto.androidtranscoder.MediaTranscoder;
  9import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
 10import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets;
 11
 12import java.io.FileDescriptor;
 13import java.io.FileNotFoundException;
 14import java.util.concurrent.ExecutionException;
 15import java.util.concurrent.Future;
 16import java.util.concurrent.atomic.AtomicInteger;
 17
 18import eu.siacs.conversations.Config;
 19import eu.siacs.conversations.R;
 20import eu.siacs.conversations.crypto.PgpEngine;
 21import eu.siacs.conversations.entities.DownloadableFile;
 22import eu.siacs.conversations.entities.Message;
 23import eu.siacs.conversations.persistance.FileBackend;
 24import eu.siacs.conversations.ui.UiCallback;
 25import eu.siacs.conversations.utils.MimeUtils;
 26
 27public class AttachFileToConversationRunnable implements Runnable, MediaTranscoder.Listener {
 28
 29	private final XmppConnectionService mXmppConnectionService;
 30	private final Message message;
 31	private final Uri uri;
 32	private final UiCallback<Message> callback;
 33	private final boolean isVideoMessage;
 34	private int currentProgress = -1;
 35
 36	public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, Message message, UiCallback<Message> callback) {
 37		this.uri = uri;
 38		this.mXmppConnectionService = xmppConnectionService;
 39		this.message = message;
 40		this.callback = callback;
 41		final String mimeType = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri);
 42		this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2);
 43	}
 44
 45
 46	private void processAsFile() {
 47		final String path = mXmppConnectionService.getFileBackend().getOriginalPath(uri);
 48		if (path != null) {
 49			message.setRelativeFilePath(path);
 50			mXmppConnectionService.getFileBackend().updateFileParams(message);
 51			if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 52				mXmppConnectionService.getPgpEngine().encrypt(message, callback);
 53			} else {
 54				callback.success(message);
 55			}
 56		} else {
 57			try {
 58				mXmppConnectionService.getFileBackend().copyFileToPrivateStorage(message, uri);
 59				mXmppConnectionService.getFileBackend().updateFileParams(message);
 60				if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 61					final PgpEngine pgpEngine = mXmppConnectionService.getPgpEngine();
 62					if (pgpEngine != null) {
 63						pgpEngine.encrypt(message, callback);
 64					} else if (callback != null) {
 65						callback.error(R.string.unable_to_connect_to_keychain, null);
 66					}
 67				} else {
 68					callback.success(message);
 69				}
 70			} catch (FileBackend.FileCopyException e) {
 71				callback.error(e.getResId(), message);
 72			}
 73		}
 74	}
 75
 76	private void processAsVideo() throws FileNotFoundException {
 77		Log.d(Config.LOGTAG,"processing file as video");
 78		mXmppConnectionService.startForcingForegroundNotification();
 79		message.setRelativeFilePath(message.getUuid() + ".mp4");
 80		final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
 81		final int runtime = mXmppConnectionService.getFileBackend().getMediaRuntime(uri);
 82		MediaFormatStrategy formatStrategy = runtime >= 8000 ? MediaFormatStrategyPresets.createExportPreset960x540Strategy() : MediaFormatStrategyPresets.createAndroid720pStrategy();
 83		file.getParentFile().mkdirs();
 84		ParcelFileDescriptor parcelFileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
 85		FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
 86		Future<Void> future = MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), formatStrategy, this);
 87		try {
 88			future.get();
 89		} catch (Exception e) {
 90			throw new AssertionError(e);
 91		}
 92	}
 93
 94	@Override
 95	public void onTranscodeProgress(double progress) {
 96		final int p = (int) Math.round(progress * 100);
 97		if (p > currentProgress) {
 98			currentProgress = p;
 99			mXmppConnectionService.getNotificationService().updateFileAddingNotification(p,message);
100		}
101	}
102
103	@Override
104	public void onTranscodeCompleted() {
105		mXmppConnectionService.stopForcingForegroundNotification();
106		mXmppConnectionService.getFileBackend().updateFileParams(message);
107		if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
108			mXmppConnectionService.getPgpEngine().encrypt(message, callback);
109		} else {
110			callback.success(message);
111		}
112	}
113
114	@Override
115	public void onTranscodeCanceled() {
116		mXmppConnectionService.stopForcingForegroundNotification();
117		processAsFile();
118	}
119
120	@Override
121	public void onTranscodeFailed(Exception e) {
122		mXmppConnectionService.stopForcingForegroundNotification();
123		Log.d(Config.LOGTAG,"video transcoding failed",e);
124		processAsFile();
125	}
126
127	@Override
128	public void run() {
129		if (isVideoMessage) {
130			try {
131				processAsVideo();
132			} catch (Throwable e) {
133				processAsFile();
134			}
135		} else {
136			processAsFile();
137		}
138	}
139
140}