From 9b82411c3dcb334ed61e6dbece5588e69a77e763 Mon Sep 17 00:00:00 2001 From: Marek Wehmer Date: Sun, 20 Apr 2014 15:54:16 +0200 Subject: [PATCH] Better share intent handling. 1) Guess mime type from share intent EXTRA_STREAM uri. 2) Always include EXTRA_TEXT (if present) --- .../securesms/RoutingActivity.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RoutingActivity.java b/src/org/thoughtcrime/securesms/RoutingActivity.java index d89fa6c8ae..687665d7fe 100644 --- a/src/org/thoughtcrime/securesms/RoutingActivity.java +++ b/src/org/thoughtcrime/securesms/RoutingActivity.java @@ -2,7 +2,7 @@ package org.thoughtcrime.securesms; import android.content.Intent; import android.net.Uri; -import android.os.Build; +import android.webkit.MimeTypeMap; import org.thoughtcrime.securesms.crypto.MasterSecretUtil; import org.thoughtcrime.securesms.database.DatabaseFactory; @@ -228,24 +228,39 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity { private ConversationParameters getConversationParametersForShareAction() { String type = getIntent().getType(); - String draftText = null; + String draftText = getIntent().getStringExtra(Intent.EXTRA_TEXT); Uri draftImage = null; Uri draftAudio = null; Uri draftVideo = null; - if ("text/plain".equals(type)) { - draftText = getIntent().getStringExtra(Intent.EXTRA_TEXT); - } else if (type != null && type.startsWith("image/")) { - draftImage = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + + if (streamExtra != null) { + type = getMimeType(streamExtra); + } + + if (type != null && type.startsWith("image/")) { + draftImage = streamExtra; } else if (type != null && type.startsWith("audio/")) { - draftAudio = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + draftAudio = streamExtra; } else if (type != null && type.startsWith("video/")) { - draftVideo = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + draftVideo = streamExtra; } return new ConversationParameters(-1, null, draftText, draftImage, draftAudio, draftVideo); } + private String getMimeType(Uri uri) { + String type = getContentResolver().getType(uri); + + if (type == null) { + String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); + type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); + } + + return type; + } + private ConversationParameters getConversationParametersForInternalAction() { long threadId = getIntent().getLongExtra("thread_id", -1); Recipients recipients = getIntent().getParcelableExtra("recipients");