diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java | 71 |
1 files changed, 46 insertions, 25 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java index ca24bd1d..37e39285 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; @@ -7,6 +8,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import de.thedevstack.conversationsplus.ConversationsPlusApplication; +import de.thedevstack.conversationsplus.entities.FileParams; +import de.thedevstack.conversationsplus.enums.FileStatus; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.DownloadableFile; @@ -19,6 +22,29 @@ import eu.siacs.conversations.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: + case Message.STATUS_SEND_DISPLAYED: + case Message.STATUS_SEND_RECEIVED: + return true; + default: + return false; + } + } + + 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) { @@ -67,7 +93,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) { @@ -81,42 +107,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() { |