From c1c314f47a5623d3d22c2c879a65ac952f67f869 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 3 Mar 2018 16:58:04 +0100 Subject: [PATCH] delegate mime type from intent --- .../eu/siacs/conversations/persistance/FileBackend.java | 6 +++--- .../services/AttachFileToConversationRunnable.java | 8 +++++--- .../conversations/services/XmppConnectionService.java | 8 +++----- .../eu/siacs/conversations/ui/ConversationFragment.java | 7 ++++--- .../java/eu/siacs/conversations/ui/ShareWithActivity.java | 4 +++- src/main/java/eu/siacs/conversations/utils/MimeUtils.java | 5 +++-- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 740b49ef1..df64e0600 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -311,10 +311,10 @@ public class FileBackend { } } - public void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException { - String mime = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri); + public void copyFileToPrivateStorage(Message message, Uri uri, String type) throws FileCopyException { + String mime = type != null ? type : MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri); Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")"); - String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime); + String extension = MimeUtils.guessExtensionFromMimeType(mime); if (extension == null) { extension = getExtensionFromUri(uri); } diff --git a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java index e5a3a4dd3..3edf19e8f 100644 --- a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java +++ b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java @@ -30,17 +30,19 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod private final XmppConnectionService mXmppConnectionService; private final Message message; private final Uri uri; + private final String type; private final UiCallback callback; private final boolean isVideoMessage; private final long originalFileSize; private int currentProgress = -1; - public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, Message message, UiCallback callback) { + public AttachFileToConversationRunnable(XmppConnectionService xmppConnectionService, Uri uri, String type, Message message, UiCallback callback) { this.uri = uri; + this.type = type; this.mXmppConnectionService = xmppConnectionService; this.message = message; this.callback = callback; - final String mimeType = MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri); + final String mimeType = type != null ? type : MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri); final int autoAcceptFileSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize); this.originalFileSize = FileBackend.getFileSize(mXmppConnectionService,uri); this.isVideoMessage = (mimeType != null && mimeType.startsWith("video/") @@ -65,7 +67,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod } } else { try { - mXmppConnectionService.getFileBackend().copyFileToPrivateStorage(message, uri); + mXmppConnectionService.getFileBackend().copyFileToPrivateStorage(message, uri, type); mXmppConnectionService.getFileBackend().updateFileParams(message); if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { final PgpEngine pgpEngine = mXmppConnectionService.getPgpEngine(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index f672d495d..01ad6bc20 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -464,9 +464,7 @@ public class XmppConnectionService extends Service { } } - public void attachFileToConversation(final Conversation conversation, - final Uri uri, - final UiCallback callback) { + public void attachFileToConversation(final Conversation conversation, final Uri uri, final String type, final UiCallback callback) { if (FileBackend.weOwnFile(this, uri)) { Log.d(Config.LOGTAG, "trying to attach file that belonged to us"); callback.error(R.string.security_error_invalid_file_access, null); @@ -480,7 +478,7 @@ public class XmppConnectionService extends Service { } message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_FILE); - final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, message, callback); + final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback); if (runnable.isVideoMessage()) { mVideoCompressionExecutor.execute(runnable); } else { @@ -502,7 +500,7 @@ public class XmppConnectionService extends Service { || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri)) || (mimeType != null && mimeType.endsWith("/gif"))) { Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": not compressing picture. sending as file"); - attachFileToConversation(conversation, uri, callback); + attachFileToConversation(conversation, uri, mimeType, callback); return; } final Message message; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 3b565ef51..1b4a2efc3 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -508,14 +508,14 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke }); } - private void attachFileToConversation(Conversation conversation, Uri uri) { + private void attachFileToConversation(Conversation conversation, Uri uri, String type) { if (conversation == null) { return; } final Toast prepareFileToast = Toast.makeText(getActivity(), getText(R.string.preparing_file), Toast.LENGTH_LONG); prepareFileToast.show(); activity.delegateUriPermissionsToService(uri); - activity.xmppConnectionService.attachFileToConversation(conversation, uri, new UiInformableCallback() { + activity.xmppConnectionService.attachFileToConversation(conversation, uri, type, new UiInformableCallback() { @Override public void inform(final String text) { hidePrepareFileToast(prepareFileToast); @@ -705,10 +705,11 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke case ATTACHMENT_CHOICE_RECORD_VIDEO: case ATTACHMENT_CHOICE_RECORD_VOICE: final List fileUris = AttachmentTool.extractUriFromIntent(data); + String type = data.getType(); final PresenceSelector.OnPresenceSelected callback = () -> { for (Iterator i = fileUris.iterator(); i.hasNext(); i.remove()) { Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO"); - attachFileToConversation(conversation, i.next()); + attachFileToConversation(conversation, i.next(), type); } }; if (conversation == null || conversation.getMode() == Conversation.MODE_MULTI || FileBackend.allFilesUnderSize(getActivity(), fileUris, getMaxHttpUploadSize(conversation))) { diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index 5be76124c..472d4950a 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -53,6 +53,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer public String text; public String uuid; public boolean multiple = false; + public String type; } private Share share; @@ -215,6 +216,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer this.share.uris.clear(); this.share.uris.add(uri); this.share.image = type.startsWith("image/") || isImage(uri); + this.share.type = type; } else { this.share.text = text; } @@ -315,7 +317,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer replaceToast(getString(R.string.preparing_file)); final Uri uri = share.uris.get(0); delegateUriPermissionsToService(uri); - xmppConnectionService.attachFileToConversation(conversation, uri, attachFileCallback); + xmppConnectionService.attachFileToConversation(conversation, uri, share.type, attachFileCallback); } }; if (account.httpUploadAvailable() diff --git a/src/main/java/eu/siacs/conversations/utils/MimeUtils.java b/src/main/java/eu/siacs/conversations/utils/MimeUtils.java index 78b5dd6a9..8d764cf90 100644 --- a/src/main/java/eu/siacs/conversations/utils/MimeUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/MimeUtils.java @@ -55,7 +55,6 @@ public final class MimeUtils { add("application/msaccess", "mdb"); add("application/oda", "oda"); add("application/ogg", "ogg"); - add("application/ogg", "oga"); add("application/pdf", "pdf"); add("application/pgp-keys", "key"); add("application/pgp-signature", "pgp"); @@ -234,6 +233,7 @@ public final class MimeUtils { add("audio/mpeg", "mp2"); add("audio/mpeg", "m4a"); add("audio/mpegurl", "m3u"); + add("audio/ogg","oga"); add("audio/prs.sid", "sid"); add("audio/x-aiff", "aif"); add("audio/x-aiff", "aiff"); @@ -355,6 +355,7 @@ public final class MimeUtils { add("video/fli", "fli"); add("video/m4v", "m4v"); add("video/mp2ts", "ts"); + add("video/ogg","ogv"); add("video/mpeg", "mpeg"); add("video/mpeg", "mpg"); add("video/mpeg", "mpe"); @@ -489,7 +490,7 @@ public final class MimeUtils { if (mimeType == null || mimeType.isEmpty()) { return null; } - return mimeTypeToExtensionMap.get(mimeType); + return mimeTypeToExtensionMap.get(mimeType.split(";")[0]); } public static String guessMimeTypeFromUri(Context context, Uri uri) {