mirror of
https://github.com/deltachat/deltachat-android.git
synced 2025-10-03 17:59:39 +02:00
parent
85427d3c01
commit
0ea7181b58
4 changed files with 97 additions and 79 deletions
|
@ -614,8 +614,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
|||
}
|
||||
|
||||
private void handleSharing() {
|
||||
ArrayList uriList = RelayUtil.getSharedUris(this);
|
||||
if (uriList != null && uriList.size() > 0) {
|
||||
ArrayList<Uri> uriList = RelayUtil.getSharedUris(this);
|
||||
if (uriList == null || uriList.size() == 0) return;
|
||||
if (uriList.size() > 1) {
|
||||
String message = String.format(getString(R.string.share_multiple_attachments), uriList.size());
|
||||
new AlertDialog.Builder(this)
|
||||
.setMessage(message)
|
||||
|
@ -625,6 +626,10 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
|||
}))
|
||||
.setPositiveButton(R.string.menu_send, (dialog, which) -> new RelayingTask(this, chatId).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR))
|
||||
.show();
|
||||
} else {
|
||||
DcMsg message = createMessage(this, uriList.get(0));
|
||||
dcContext.setDraft(chatId, message);
|
||||
initializeDraft();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1099,7 +1104,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
|||
ArrayList<Uri> uris = getSharedUris(activity);
|
||||
try {
|
||||
for(Uri uri : uris) {
|
||||
DcMsg message = createMessage(uri);
|
||||
DcMsg message = createMessage(activityRef.get(), uri);
|
||||
dcContext.sendMsg(chatId, message);
|
||||
}
|
||||
|
||||
|
@ -1120,55 +1125,53 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
|||
}
|
||||
}
|
||||
|
||||
private DcMsg createMessage(Uri uri) throws NullPointerException {
|
||||
Context context = activityRef.get();
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
DcMsg message;
|
||||
String mimeType = MediaUtil.getMimeType(context, uri);
|
||||
if (MediaUtil.isImageType(mimeType)) {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_IMAGE);
|
||||
}
|
||||
else if (MediaUtil.isAudioType(mimeType)) {
|
||||
message = new DcMsg(dcContext,DcMsg.DC_MSG_AUDIO);
|
||||
}
|
||||
else if (MediaUtil.isVideoType(mimeType)) {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_VIDEO);
|
||||
}
|
||||
else {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
|
||||
}
|
||||
message.setFile(getRealPathFromUri(uri), mimeType);
|
||||
return message;
|
||||
}
|
||||
|
||||
private static DcMsg createMessage(Context context, Uri uri) throws NullPointerException {
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
DcMsg message;
|
||||
String mimeType = MediaUtil.getMimeType(context, uri);
|
||||
if (MediaUtil.isImageType(mimeType)) {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_IMAGE);
|
||||
}
|
||||
|
||||
private String getRealPathFromUri(Uri uri) throws NullPointerException {
|
||||
Context context = activityRef.get();
|
||||
ApplicationDcContext dcContext = DcHelper.getContext(context);
|
||||
try {
|
||||
String filename = uri.getPathSegments().get(2); // Get real file name from Uri
|
||||
String ext = "";
|
||||
int i = filename.lastIndexOf(".");
|
||||
if(i>=0) {
|
||||
ext = filename.substring(i);
|
||||
filename = filename.substring(0, i);
|
||||
}
|
||||
String path = dcContext.getBlobdirFile(filename, ext);
|
||||
|
||||
// copy content to this file
|
||||
if(path != null) {
|
||||
InputStream inputStream = PartAuthority.getAttachmentStream(context, uri);
|
||||
OutputStream outputStream = new FileOutputStream(path);
|
||||
Util.copy(inputStream, outputStream);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
else if (MediaUtil.isAudioType(mimeType)) {
|
||||
message = new DcMsg(dcContext,DcMsg.DC_MSG_AUDIO);
|
||||
}
|
||||
else if (MediaUtil.isVideoType(mimeType)) {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_VIDEO);
|
||||
}
|
||||
else {
|
||||
message = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
|
||||
}
|
||||
message.setFile(getRealPathFromUri(context, uri), mimeType);
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String getRealPathFromUri(Context context, Uri uri) throws NullPointerException {
|
||||
ApplicationDcContext dcContext = DcHelper.getContext(context);
|
||||
try {
|
||||
String filename = uri.getPathSegments().get(2); // Get real file name from Uri
|
||||
String ext = "";
|
||||
int i = filename.lastIndexOf(".");
|
||||
if(i>=0) {
|
||||
ext = filename.substring(i);
|
||||
filename = filename.substring(0, i);
|
||||
}
|
||||
String path = dcContext.getBlobdirFile(filename, ext);
|
||||
|
||||
// copy content to this file
|
||||
if(path != null) {
|
||||
InputStream inputStream = PartAuthority.getAttachmentStream(context, uri);
|
||||
OutputStream outputStream = new FileOutputStream(path);
|
||||
Util.copy(inputStream, outputStream);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import android.os.Process;
|
|||
import android.provider.OpenableColumns;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
||||
import org.thoughtcrime.securesms.util.FileUtils;
|
||||
|
@ -20,6 +21,8 @@ import java.io.InputStream;
|
|||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.thoughtcrime.securesms.util.MediaUtil.getMimeType;
|
||||
|
||||
public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
||||
|
||||
private static final String TAG = ResolveMediaTask.class.getSimpleName();
|
||||
|
@ -135,12 +138,4 @@ public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
|||
return "file".equals(uri.getScheme());
|
||||
}
|
||||
|
||||
private String getMimeType(Activity activityContext, @Nullable Uri uri) {
|
||||
if (uri != null) {
|
||||
final String mimeType = MediaUtil.getMimeType(activityContext.getApplicationContext(), uri);
|
||||
if (mimeType != null) return mimeType;
|
||||
}
|
||||
return MediaUtil.getCorrectedMimeType(activityContext.getIntent().getType());
|
||||
}
|
||||
|
||||
}
|
|
@ -54,6 +54,8 @@ import java.io.OutputStream;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.thoughtcrime.securesms.util.RelayUtil.setSharedText;
|
||||
|
||||
/**
|
||||
* An activity to quickly share content with chats
|
||||
*
|
||||
|
@ -126,14 +128,12 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity implement
|
|||
resolvedExtras = new ArrayList<>();
|
||||
|
||||
List<Uri> streamExtras = new ArrayList<>();
|
||||
if (Intent.ACTION_SEND.equals(getIntent().getAction())) {
|
||||
streamExtras.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
|
||||
} else {
|
||||
if (Intent.ACTION_SEND.equals(getIntent().getAction()) &&
|
||||
getIntent().getParcelableExtra(Intent.EXTRA_STREAM) != null) {
|
||||
Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
|
||||
streamExtras.add(uri);
|
||||
} else if (getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM) != null) {
|
||||
streamExtras = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
||||
if (streamExtras == null || streamExtras.isEmpty()) {
|
||||
streamExtras = new ArrayList<>();
|
||||
streamExtras.add(null); // force checking at least EXTRA_TEXT & Co.
|
||||
}
|
||||
}
|
||||
|
||||
if (needsFilePermission(streamExtras)) {
|
||||
|
@ -232,7 +232,7 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity implement
|
|||
if (hasResolvedDestination) {
|
||||
createConversation(chatId);
|
||||
} else {
|
||||
Intent composeIntent = new Intent(this, ConversationListActivity.class);
|
||||
Intent composeIntent = getBaseShareIntent(ConversationListActivity.class);
|
||||
RelayUtil.setSharedUris(composeIntent, resolvedExtras);
|
||||
startActivity(composeIntent);
|
||||
finish();
|
||||
|
@ -316,18 +316,16 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity implement
|
|||
}
|
||||
|
||||
private Intent getBaseShareIntent(final @NonNull Class<?> target) {
|
||||
if (resolvedExtras.size() == 1) {
|
||||
final Intent intent = new Intent(this, target);
|
||||
final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT);
|
||||
intent.putExtra(ConversationActivity.TEXT_EXTRA, textExtra);
|
||||
final Intent intent = new Intent(this, target);
|
||||
setSharedText(intent, getIntent().getStringExtra(Intent.EXTRA_TEXT));
|
||||
if (resolvedExtras.size() > 0) {
|
||||
Uri data = resolvedExtras.get(0);
|
||||
if (data != null) {
|
||||
String mimeType = getMimeType(data);
|
||||
intent.setDataAndType(data, mimeType);
|
||||
}
|
||||
return intent;
|
||||
}
|
||||
return new Intent(this, target);
|
||||
return intent;
|
||||
}
|
||||
|
||||
private String getMimeType(@Nullable Uri uri) {
|
||||
|
|
|
@ -5,20 +5,20 @@ import android.content.Intent;
|
|||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.ConversationActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.thoughtcrime.securesms.ConversationActivity.TEXT_EXTRA;
|
||||
|
||||
public class RelayUtil {
|
||||
private static final String FORWARDED_MESSAGE_IDS = "forwarded_message_ids";
|
||||
private static final String SHARED_URIS = "shared_uris";
|
||||
private static final String IS_SHARING = "is_sharing";
|
||||
public static final int REQUEST_RELAY = 100;
|
||||
|
||||
public static boolean isRelayingMessageContent(Activity activity) {
|
||||
try {
|
||||
return activity.getIntent().getIntArrayExtra(FORWARDED_MESSAGE_IDS) != null ||
|
||||
activity.getIntent().getParcelableArrayListExtra(SHARED_URIS) != null;
|
||||
} catch (NullPointerException npe) {
|
||||
return false;
|
||||
}
|
||||
return isForwarding(activity) || isSharing(activity);
|
||||
}
|
||||
|
||||
public static boolean isForwarding(Activity activity) {
|
||||
|
@ -31,7 +31,7 @@ public class RelayUtil {
|
|||
|
||||
public static boolean isSharing(Activity activity) {
|
||||
try {
|
||||
return activity.getIntent().getParcelableArrayListExtra(SHARED_URIS) != null;
|
||||
return activity.getIntent().getBooleanExtra(IS_SHARING, false);
|
||||
} catch (NullPointerException npe) {
|
||||
return false;
|
||||
}
|
||||
|
@ -53,10 +53,20 @@ public class RelayUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static String getSharedText(Activity activity) {
|
||||
try {
|
||||
return activity.getIntent().getStringExtra(TEXT_EXTRA);
|
||||
} catch (NullPointerException npe) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void resetRelayingMessageContent(Activity activity) {
|
||||
try {
|
||||
activity.getIntent().removeExtra(FORWARDED_MESSAGE_IDS);
|
||||
activity.getIntent().removeExtra(SHARED_URIS);
|
||||
activity.getIntent().removeExtra(IS_SHARING);
|
||||
activity.getIntent().removeExtra(TEXT_EXTRA);
|
||||
} catch (NullPointerException npe) {
|
||||
npe.printStackTrace();
|
||||
}
|
||||
|
@ -66,7 +76,13 @@ public class RelayUtil {
|
|||
if (isForwarding(currentActivity)) {
|
||||
newActivityIntent.putExtra(FORWARDED_MESSAGE_IDS, getForwardedMessageIDs(currentActivity));
|
||||
} else if (isSharing(currentActivity)) {
|
||||
newActivityIntent.putParcelableArrayListExtra(SHARED_URIS, getSharedUris(currentActivity));
|
||||
newActivityIntent.putExtra(IS_SHARING, true);
|
||||
if (getSharedUris(currentActivity) != null) {
|
||||
newActivityIntent.putParcelableArrayListExtra(SHARED_URIS, getSharedUris(currentActivity));
|
||||
}
|
||||
if (getSharedText(currentActivity) != null) {
|
||||
newActivityIntent.putExtra(TEXT_EXTRA, getSharedText(currentActivity));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,6 +92,12 @@ public class RelayUtil {
|
|||
|
||||
public static void setSharedUris(Intent composeIntent, ArrayList<Uri> uris) {
|
||||
composeIntent.putParcelableArrayListExtra(SHARED_URIS, uris);
|
||||
composeIntent.putExtra(IS_SHARING, true);
|
||||
}
|
||||
|
||||
public static void setSharedText(Intent composeIntent, String text) {
|
||||
composeIntent.putExtra(TEXT_EXTRA, text);
|
||||
composeIntent.putExtra(IS_SHARING, true);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue