aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2017-01-05 21:05:24 +0100
committersteckbrief <steckbrief@chefmail.de>2017-01-05 21:05:24 +0100
commitc09e7f0a21f563359c69f0e4198c7563cd2d4809 (patch)
treea52009fa04da58a10bad7de19814e15e010fde7a /src/main/java/de/thedevstack/conversationsplus/utils
parent867ee316619f814e6311668d0f2b910cecdd6393 (diff)
parent2586fe2b9d745c0666656acb6beb4020805dd220 (diff)
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java9
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java67
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java33
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/UrlUtil.java25
4 files changed, 126 insertions, 8 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
index ce289968..77c313f7 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/FileUtils.java
@@ -13,6 +13,7 @@ import android.provider.MediaStore;
import android.provider.OpenableColumns;
import java.io.File;
+import java.net.URL;
import java.util.List;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
@@ -161,6 +162,14 @@ public final class FileUtils {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
+ public static String getRelevantExtension(URL url) {
+ if (url == null) {
+ return null;
+ }
+ String path = url.getPath();
+ return getRelevantExtension(path);
+ }
+
public static String getRelevantExtension(String path) {
if (path == null || path.isEmpty()) {
return null;
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
index dc9607f0..acd73575 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
@@ -3,6 +3,7 @@ package de.thedevstack.conversationsplus.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -12,6 +13,7 @@ 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.entities.Transferable;
import de.thedevstack.conversationsplus.enums.FileStatus;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.persistance.FileBackend;
@@ -21,10 +23,69 @@ import de.thedevstack.conversationsplus.persistance.FileBackend;
*/
public final class MessageUtil {
+ public static void extractFileParamsFromBody(Message message) {
+ if (null == message) {
+ return;
+ }
+ String body = message.getBody();
+ /**
+ * there are a few cases where spaces result in an unwanted behavior, e.g.
+ * "http://example.com/image.jpg" text that will not be shown /abc.png"
+ * or more than one image link in one message.
+ */
+ if (null == body || body.isEmpty() || body.contains(" ")) {
+ return;
+ }
+
+ FileParams fileParams = message.getFileParams();
+ if (null == fileParams) {
+ fileParams = new FileParams();
+ message.setFileParams(fileParams);
+ }
+
+ try {
+ URL url = new URL(body);
+ if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ return;
+ }
+ if (message.isHttpUploaded()) {
+ fileParams.setUrl(url.toString());
+ message.setTreatAsDownloadable(Message.Decision.MUST);
+ return;
+ }
+ String extension = FileUtils.getRelevantExtension(url);
+ if (extension == null) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ return;
+ }
+ byte[] ivAndKey = UrlUtil.getIvAndKeyFromURL(url);
+
+ if (null != ivAndKey) {
+ if (MimeUtils.guessMimeTypeFromExtension(extension) != null) {
+ message.setTreatAsDownloadable(Message.Decision.MUST);
+ fileParams.setKeyAndIv(ivAndKey);
+ fileParams.setUrl(url.toString());
+ } else {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+ } else if (Transferable.VALID_IMAGE_EXTENSIONS.contains(extension)
+ || Transferable.WELL_KNOWN_EXTENSIONS.contains(extension)) {
+ message.setTreatAsDownloadable(Message.Decision.SHOULD);
+ fileParams.setUrl(url.toString());
+ } else {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+
+ } catch (MalformedURLException e) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+ }
+
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))
+ return (null == fileStatus
+ || (null != fileStatus && (fileStatus == FileStatus.NEEDS_DOWNLOAD || fileStatus == FileStatus.UNDEFINED)))
&& message.treatAsDownloadable() != Message.Decision.NEVER;
}
@@ -121,6 +182,7 @@ public final class MessageUtil {
FileParams fileParams = message.getFileParams();
if (null == fileParams) {
fileParams = new FileParams();
+ message.setFileParams(fileParams);
}
fileParams.setSize(size);
if (null != url) {
@@ -136,7 +198,6 @@ public final class MessageUtil {
if (null != relativeFilePathFromMessage && relativeFilePathFromMessage.startsWith("/")) {
fileParams.setPath(relativeFilePathFromMessage);
}
- message.setFileParams(fileParams);
}
private MessageUtil() {
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java b/src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java
index f27aabb6..a2ed6d81 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java
@@ -210,16 +210,26 @@ public class UIHelper {
} else if (mime.startsWith("image/")) {
return context.getString(R.string.image);
} else if (mime.contains("pdf")) {
- return context.getString(R.string.pdf_document) ;
+ return context.getString(R.string.pdf_document);
} else if (mime.contains("application/vnd.android.package-archive")) {
- return context.getString(R.string.apk) ;
+ return context.getString(R.string.apk);
} else if (mime.contains("vcard")) {
- return context.getString(R.string.vcard) ;
+ return context.getString(R.string.vcard);
} else {
- return message.getRelativeFilePath();
+ String filename = getDisplayFilename(message);
+ if (null == filename) {
+ return context.getString(R.string.file);
+ } else {
+ return filename;
+ }
}
}
+ public static String getDisplayFilename(final Message message) {
+ String originalFilename = (null != message.getFileParams()) ? message.getFileParams().getOriginalFilename() : null;
+ return (null != originalFilename) ? originalFilename : message.getRelativeFilePath();
+ }
+
public static String getMessageDisplayName(final Message message) {
final Conversation conversation = message.getConversation();
if (message.getStatus() == Message.STATUS_RECEIVED) {
@@ -280,7 +290,20 @@ public class UIHelper {
return LOCATION_QUESTIONS.contains(body);
}
- public static String getHumanReadableFileSize(long filesize) {
+ public static String getHumanReadableFileSize(long size) {
+ String filesize;
+ if (size > (1.5 * 1024 * 1024)) {
+ filesize = size / (1024 * 1024)+ " MiB";
+ } else if (size > 0) {
+ filesize = size / 1024 + " KiB";
+ } else {
+ filesize = null;
+ }
+
+ return filesize;
+ }
+
+ public static String getHumanReadableDetailedFileSize(long filesize) {
if (0 > filesize) {
return "?";
}
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/UrlUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/UrlUtil.java
new file mode 100644
index 00000000..53bc24d6
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/UrlUtil.java
@@ -0,0 +1,25 @@
+package de.thedevstack.conversationsplus.utils;
+
+import java.net.URL;
+
+import de.thedevstack.conversationsplus.utils.CryptoHelper;
+
+/**
+ * This utility class provides helper methods to handle URLs.
+ */
+
+public final class UrlUtil {
+ public static byte[] getIvAndKeyFromURL(URL url) {
+ if (null == url) {
+ return null;
+ }
+ String reference = url.getRef();
+ boolean linkHasIvAndKey = reference != null && reference.matches("([A-Fa-f0-9]{2}){48}");
+
+ return linkHasIvAndKey ? CryptoHelper.hexToBytes(reference) : null;
+ }
+
+ private UrlUtil() {
+ // Helper Class
+ }
+}