video processing: pick smaller file after transcoding. do not transcode very small files

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/Config.java                                    |  1 
src/main/java/eu/siacs/conversations/persistance/FileBackend.java                   |  9 
src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java | 20 
3 files changed, 24 insertions(+), 6 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/Config.java 🔗

@@ -66,7 +66,6 @@ public final class Config {
 	public static final int IMAGE_SIZE = 1920;
 	public static final Bitmap.CompressFormat IMAGE_FORMAT = Bitmap.CompressFormat.JPEG;
 	public static final int IMAGE_QUALITY = 75;
-	public static final int IMAGE_MAX_SIZE = 524288; //512KiB
 
 	public static final int MESSAGE_MERGE_WINDOW = 20;
 

src/main/java/eu/siacs/conversations/persistance/FileBackend.java 🔗

@@ -137,7 +137,7 @@ public class FileBackend {
 		}
 	}
 
-	private static long getFileSize(Context context, Uri uri) {
+	public static long getFileSize(Context context, Uri uri) {
 		try {
 			final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
 			if (cursor != null && cursor.moveToFirst()) {
@@ -235,14 +235,14 @@ public class FileBackend {
 		}
 		File file = new File(path);
 		long size = file.length();
-		if (size == 0 || size >= Config.IMAGE_MAX_SIZE ) {
+		if (size == 0 || size >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
 			return false;
 		}
 		BitmapFactory.Options options = new BitmapFactory.Options();
 		options.inJustDecodeBounds = true;
 		try {
 			BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
-			if (options == null || options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
+			if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
 				return false;
 			}
 			return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
@@ -348,6 +348,7 @@ public class FileBackend {
 			scaledBitmap = rotate(scaledBitmap, rotation);
 			boolean targetSizeReached = false;
 			int quality = Config.IMAGE_QUALITY;
+			final int imageMaxSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
 			while(!targetSizeReached) {
 				os = new FileOutputStream(file);
 				boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
@@ -355,7 +356,7 @@ public class FileBackend {
 					throw new FileCopyException(R.string.error_compressing_image);
 				}
 				os.flush();
-				targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50;
+				targetSizeReached = file.length() <= imageMaxSize|| quality <= 50;
 				quality -= 5;
 			}
 			scaledBitmap.recycle();

src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java 🔗

@@ -9,6 +9,7 @@ import net.ypresto.androidtranscoder.MediaTranscoder;
 import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
 import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets;
 
+import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
 import java.util.concurrent.ExecutionException;
@@ -31,6 +32,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
 	private final Uri uri;
 	private final UiCallback<Message> callback;
 	private final boolean isVideoMessage;
+	private final long originalFileSize;
 	private int currentProgress = -1;
 
 	public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, Message message, UiCallback<Message> callback) {
@@ -39,7 +41,11 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
 		this.message = message;
 		this.callback = callback;
 		final String mimeType = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri);
-		this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2);
+		final int autoAcceptFileSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
+		this.originalFileSize = FileBackend.getFileSize(mXmppConnectionService,uri);
+		this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/")
+				&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
+				&& originalFileSize > autoAcceptFileSize;
 	}
 
 	public boolean isVideoMessage() {
@@ -111,6 +117,18 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
 	@Override
 	public void onTranscodeCompleted() {
 		mXmppConnectionService.stopForcingForegroundNotification();
+		final File file = mXmppConnectionService.getFileBackend().getFile(message);
+		long convertedFileSize = mXmppConnectionService.getFileBackend().getFile(message).getSize();
+		Log.d(Config.LOGTAG,"originalFileSize="+originalFileSize+" convertedFileSize="+convertedFileSize);
+		if (originalFileSize != 0 && convertedFileSize >= originalFileSize) {
+			if (file.delete()) {
+				Log.d(Config.LOGTAG,"original file size was smaller. deleting and processing as file");
+				processAsFile();
+				return;
+			} else {
+				Log.d(Config.LOGTAG,"unable to delete converted file");
+			}
+		}
 		mXmppConnectionService.getFileBackend().updateFileParams(message);
 		if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 			mXmppConnectionService.getPgpEngine().encrypt(message, callback);