Fixed shared URI types

master
M66B 3 months ago
parent 614bbc578c
commit b724113798

@ -259,7 +259,7 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
Uri stream = (item == null ? null : item.getUri()); Uri stream = (item == null ? null : item.getUri());
if (stream != null) if (stream != null)
uris.add(new UriType(stream, uris.add(new UriType(stream,
description != null && i < description.getMimeTypeCount() ? description.getMimeType(i) : null)); description != null && i < description.getMimeTypeCount() ? description.getMimeType(i) : null, this));
} }
if (intent.hasExtra(Intent.EXTRA_STREAM)) { if (intent.hasExtra(Intent.EXTRA_STREAM)) {
@ -277,7 +277,7 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
break; break;
} }
if (!found) if (!found)
uris.add(new UriType(stream, streams.size() == 1 ? intent.getType() : null)); uris.add(new UriType(stream, streams.size() == 1 ? intent.getType() : null, this));
} }
} }
} }

@ -580,7 +580,7 @@ public class EntityAnswer implements Serializable {
for (Uri file : attachments) for (Uri file : attachments)
try { try {
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
Helper.UriInfo info = Helper.getInfo(new UriType(file, null), context); Helper.UriInfo info = Helper.getInfo(new UriType(file, null, null), context);
attachment.message = id; attachment.message = id;
attachment.sequence = db.attachment().getAttachmentSequence(id) + 1; attachment.sequence = db.attachment().getAttachmentSequence(id) + 1;

@ -251,15 +251,18 @@ public class EntityAttachment {
} }
String getMimeType() { String getMimeType() {
if (encryption != null)
return type;
return getMimeType(type, name);
}
static String getMimeType(String type, String name) {
// Try to guess a better content type // Try to guess a better content type
// For example, sometimes PDF files are sent as application/octet-stream // For example, sometimes PDF files are sent as application/octet-stream
// https://android.googlesource.com/platform/libcore/+/refs/tags/android-9.0.0_r49/luni/src/main/java/libcore/net/MimeUtils.java // https://android.googlesource.com/platform/libcore/+/refs/tags/android-9.0.0_r49/luni/src/main/java/libcore/net/MimeUtils.java
// https://docs.microsoft.com/en-us/archive/blogs/vsofficedeveloper/office-2007-file-format-mime-types-for-http-content-streaming-2 // https://docs.microsoft.com/en-us/archive/blogs/vsofficedeveloper/office-2007-file-format-mime-types-for-http-content-streaming-2
if (encryption != null)
return type;
if ("audio/mid".equals(type)) if ("audio/mid".equals(type))
return "audio/midi"; return "audio/midi";

@ -393,7 +393,7 @@ public class FragmentCompose extends FragmentBase {
pickImages = pickImages =
registerForActivityResult(new ActivityResultContracts.PickMultipleVisualMedia(max), uris -> { registerForActivityResult(new ActivityResultContracts.PickMultipleVisualMedia(max), uris -> {
if (!uris.isEmpty()) if (!uris.isEmpty())
onAddImageFile(UriType.getList(uris), false); onAddImageFile(UriType.getList(uris, getContext()), false);
}); });
} }
@ -655,7 +655,7 @@ public class FragmentCompose extends FragmentBase {
int resize = prefs.getInt("resize", FragmentCompose.REDUCED_IMAGE_SIZE); int resize = prefs.getInt("resize", FragmentCompose.REDUCED_IMAGE_SIZE);
boolean resize_width_only = prefs.getBoolean("resize_width_only", false); boolean resize_width_only = prefs.getBoolean("resize_width_only", false);
onAddAttachment( onAddAttachment(
Arrays.asList(new UriType(uri, type)), Arrays.asList(new UriType(uri, type, etBody.getContext())),
true, true,
resize_paste ? resize : 0, resize_paste ? resize : 0,
resize_width_only, resize_width_only,
@ -3382,7 +3382,7 @@ public class FragmentCompose extends FragmentBase {
case REQUEST_TAKE_PHOTO: case REQUEST_TAKE_PHOTO:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (photoURI != null) if (photoURI != null)
onAddImageFile(Arrays.asList(new UriType(photoURI, null)), false); onAddImageFile(Arrays.asList(new UriType(photoURI, null, null)), false);
} }
break; break;
case REQUEST_ATTACHMENT: case REQUEST_ATTACHMENT:
@ -3944,7 +3944,7 @@ public class FragmentCompose extends FragmentBase {
if (clipData == null) { if (clipData == null) {
Uri uri = data.getData(); Uri uri = data.getData();
if (uri != null) if (uri != null)
result.add(new UriType(uri, data.getType())); result.add(new UriType(uri, data.getType(), getContext()));
} else { } else {
ClipDescription description = clipData.getDescription(); ClipDescription description = clipData.getDescription();
for (int i = 0; i < clipData.getItemCount(); i++) { for (int i = 0; i < clipData.getItemCount(); i++) {
@ -3952,7 +3952,8 @@ public class FragmentCompose extends FragmentBase {
Uri uri = item.getUri(); Uri uri = item.getUri();
if (uri != null) if (uri != null)
result.add(new UriType(uri, result.add(new UriType(uri,
description != null && i < description.getMimeTypeCount() ? description.getMimeType(i) : null)); description != null && i < description.getMimeTypeCount() ? description.getMimeType(i) : null,
getContext()));
} }
} }
@ -3962,7 +3963,7 @@ public class FragmentCompose extends FragmentBase {
if (result.size() == 0 && data.hasExtra("media-uri-list")) if (result.size() == 0 && data.hasExtra("media-uri-list"))
try { try {
List<Uri> uris = data.getParcelableArrayListExtra("media-uri-list"); List<Uri> uris = data.getParcelableArrayListExtra("media-uri-list");
result.addAll(UriType.getList(uris)); result.addAll(UriType.getList(uris, getContext()));
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2025 by Marcel Bokhorst (M66B) Copyright 2018-2025 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@ -38,10 +39,15 @@ public class UriType implements Parcelable {
this.type = in.readString(); this.type = in.readString();
} }
public UriType(Uri uri, String type) { public UriType(Uri uri, String type, Context context) {
this.uri = uri; this.uri = uri;
if (!TextUtils.isEmpty(type)) if (!TextUtils.isEmpty(type))
this.type = type; this.type = type;
if (context != null) {
Helper.UriInfo info = Helper.getInfo(this, context);
this.type = EntityAttachment.getMimeType(type, info.name);
}
} }
public Uri getUri() { public Uri getUri() {
@ -75,10 +81,11 @@ public class UriType implements Parcelable {
} }
}; };
public static List<UriType> getList(List<Uri> uris) { public static List<UriType> getList(List<Uri> uris, Context context) {
List<UriType> result = new ArrayList<>(); List<UriType> result = new ArrayList<>();
for (Uri uri : uris) if (uris != null)
result.add(new UriType(uri, null)); for (Uri uri : uris)
result.add(new UriType(uri, null, context));
return result; return result;
} }

Loading…
Cancel
Save