diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils')
4 files changed, 78 insertions, 34 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ConversationUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ConversationUtil.java index 77c2c728..55fd1b8e 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/ConversationUtil.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ConversationUtil.java @@ -6,6 +6,7 @@ import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.crypto.PgpEngine; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.exceptions.FileCopyException; import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.ui.UiCallback; @@ -43,10 +44,12 @@ public class ConversationUtil { message = new Message(conversation, "", conversation.getNextEncryption()); } message.setCounterpart(conversation.getNextCounterpart()); - message.setType(Message.TYPE_FILE); + //message.setType(Message.TYPE_FILE); + message.getFileParams().setFileStatus(FileStatus.NEEDS_UPLOAD); String path = FileUtils.getPath(uri); if (path != null) { - message.setRelativeFilePath(path); + message.getFileParams().setPath(path); + message.setRelativeFilePath(path); // TODO: Remove when everything is moved to fileparams MessageUtil.updateFileParams(message); if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { PgpEngine.getInstance().encrypt(message, callback); diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java index 3ee15f70..ce289968 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java @@ -16,6 +16,7 @@ import java.io.File; import java.util.List; import de.thedevstack.conversationsplus.ConversationsPlusApplication; +import de.thedevstack.conversationsplus.entities.Transferable; public final class FileUtils { @@ -160,6 +161,27 @@ public final class FileUtils { return "com.android.providers.media.documents".equals(uri.getAuthority()); } + public static String getRelevantExtension(String path) { + if (path == null || path.isEmpty()) { + return null; + } + + String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase(); + + final String lastPart = FileUtils.getLastExtension(filename); + + if (!lastPart.isEmpty()) { + // we want the real file extension, not the crypto one + final String secondToLastPart = FileUtils.getSecondToLastExtension(filename); + if (!secondToLastPart.isEmpty() && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { + return secondToLastPart; + } else { + return lastPart; + } + } + return null; + } + /** * @param filename The filename to extract extension from * @return last extension or empty string diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java index bf79d7e0..0b8ace95 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java @@ -337,20 +337,28 @@ public final class ImageUtil { public static int calcSampleSize(Uri image, int size) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeStream(StreamUtil.openInputStreamFromContentResolver(image), null, options); - return calcSampleSize(options, size); + Bitmap bmp = BitmapFactory.decodeStream(StreamUtil.openInputStreamFromContentResolver(image), null, options); + int height = options.outHeight; + int width = options.outWidth; + if (null != bmp) { + bmp.recycle(); + } + return calcSampleSize(width, height, size); } public static int calcSampleSize(File image, int size) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(image.getAbsolutePath(), options); - return calcSampleSize(options, size); - } - - public static int calcSampleSize(BitmapFactory.Options options, int size) { + Bitmap bmp = BitmapFactory.decodeFile(image.getAbsolutePath(), options); int height = options.outHeight; int width = options.outWidth; + if (null != bmp) { + bmp.recycle(); + } + return calcSampleSize(width, height, size); + } + + private static int calcSampleSize(int width, int height, int size) { int inSampleSize = 1; if (height > size || width > size) { diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java index 626b1bf3..dc9607f0 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java @@ -1,5 +1,6 @@ package de.thedevstack.conversationsplus.utils; +import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.net.URL; @@ -9,7 +10,9 @@ import java.util.regex.Pattern; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.DownloadableFile; +import de.thedevstack.conversationsplus.entities.FileParams; import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.persistance.DatabaseBackend; import de.thedevstack.conversationsplus.persistance.FileBackend; @@ -18,6 +21,13 @@ import de.thedevstack.conversationsplus.persistance.FileBackend; */ public final class MessageUtil { + public static boolean needsDownload(Message message) { + FileStatus fileStatus = (null != message.getFileParams()) ? message.getFileParams().getFileStatus() : null; + return (null != fileStatus && (fileStatus == FileStatus.NEEDS_DOWNLOAD + || fileStatus == FileStatus.UNDEFINED)) + && message.treatAsDownloadable() != Message.Decision.NEVER; + } + public static boolean isMessageSent(Message message) { switch (message.getStatus()) { case Message.STATUS_SEND: @@ -29,6 +39,12 @@ public final class MessageUtil { } } + public static void setAndSaveFileStatus(Message message, FileStatus fileStatus) { + message.getFileParams().setFileStatus(fileStatus); + DatabaseBackend.getInstance().updateMessage(message); + UiUpdateHelper.updateConversationUi(); + } + public static boolean markMessage(Conversation conversation, String uuid, int status) { if (uuid == null) { return false; @@ -76,7 +92,7 @@ public final class MessageUtil { public static void updateMessageWithImageDetails(Message message, String filePath, long size, int imageWidth, int imageHeight) { message.setRelativeFilePath(filePath); - MessageUtil.updateMessageBodyWithImageParams(message, size, imageWidth, imageHeight); + MessageUtil.updateMessageWithFileParams(message, null, size, imageWidth, imageHeight); } public static void updateFileParams(Message message) { @@ -90,42 +106,37 @@ public final class MessageUtil { if (message.getType() == Message.TYPE_IMAGE || file.getMimeType().startsWith("image/")) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(file.getAbsolutePath(), options); + Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options); imageHeight = options.outHeight; imageWidth = options.outWidth; + if (null != bmp) { + bmp.recycle(); + } } - MessageUtil.updateMessageBodyWithFileParams(message, url, file.getSize(), imageWidth, imageHeight); - } - - private static void updateMessageBodyWithFileParams(Message message, URL url, long fileSize, int imageWidth, int imageHeight) { - message.setBody(MessageUtil.getMessageBodyWithImageParams(url, fileSize, imageWidth, imageHeight)); - } - - private static void updateMessageBodyWithImageParams(Message message, long size, int imageWidth, int imageHeight) { - MessageUtil.updateMessageBodyWithImageParams(message, null, size, imageWidth, imageHeight); + MessageUtil.updateMessageWithFileParams(message, url, file.getSize(), imageWidth, imageHeight); } - private static void updateMessageBodyWithImageParams(Message message, URL url, long size, int imageWidth, int imageHeight) { - message.setBody(MessageUtil.getMessageBodyWithImageParams(url, size, imageWidth, imageHeight)); - } - - private static String getMessageBodyWithImageParams(URL url, long size, int imageWidth, int imageHeight) { - StringBuilder sb = new StringBuilder(); + private static void updateMessageWithFileParams(Message message, URL url, long size, int imageWidth, int imageHeight) { + FileParams fileParams = message.getFileParams(); + if (null == fileParams) { + fileParams = new FileParams(); + } + fileParams.setSize(size); if (null != url) { - sb.append(url.toString()); - sb.append('|'); + fileParams.setUrl(url.toString()); } - sb.append(size); if (-1 < imageWidth) { - sb.append('|'); - sb.append(imageWidth); + fileParams.setWidth(imageWidth); } if (-1 < imageHeight) { - sb.append('|'); - sb.append(imageHeight); + fileParams.setHeight(imageHeight); + } + String relativeFilePathFromMessage = message.getRelativeFilePath(); + if (null != relativeFilePathFromMessage && relativeFilePathFromMessage.startsWith("/")) { + fileParams.setPath(relativeFilePathFromMessage); } - return sb.toString(); + message.setFileParams(fileParams); } private MessageUtil() { |