diff --git a/src/androidTest/java/com/b44t/messenger/uitests/offline/SharingTest.java b/src/androidTest/java/com/b44t/messenger/uitests/offline/SharingTest.java index bd92116b5..6759343c0 100644 --- a/src/androidTest/java/com/b44t/messenger/uitests/offline/SharingTest.java +++ b/src/androidTest/java/com/b44t/messenger/uitests/offline/SharingTest.java @@ -93,7 +93,7 @@ public class SharingTest { } } Uri uri = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".attachments/" + Uri.encode(pngImage)); - DcHelper.sharedFiles.put(pngImage, 1); + DcHelper.sharedFiles.put(pngImage, "image/png"); Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/png"); diff --git a/src/main/java/org/thoughtcrime/securesms/connect/AttachmentsContentProvider.java b/src/main/java/org/thoughtcrime/securesms/connect/AttachmentsContentProvider.java index 6471c0ea0..e03d805e5 100644 --- a/src/main/java/org/thoughtcrime/securesms/connect/AttachmentsContentProvider.java +++ b/src/main/java/org/thoughtcrime/securesms/connect/AttachmentsContentProvider.java @@ -5,9 +5,15 @@ import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.ParcelFileDescriptor; +import android.webkit.MimeTypeMap; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.b44t.messenger.DcContext; +import org.thoughtcrime.securesms.util.MediaUtil; + import java.io.File; import java.io.FileNotFoundException; @@ -34,12 +40,12 @@ public class AttachmentsContentProvider extends ContentProvider { // where ef39a39 is the file in the blob directory // and text.txt is the original name of the file, as returned by `msg.getFilename()`. // `uri.getPathSegments()` returns ["ef39a39", "text.txt"] in this example. - String path = uri.getPathSegments().get(0); - if (!DcHelper.sharedFiles.containsKey(path)) { + String file = uri.getPathSegments().get(0); + if (!DcHelper.sharedFiles.containsKey(file)) { throw new FileNotFoundException("File was not shared before."); } - File privateFile = new File(dcContext.getBlobdir(), path); + File privateFile = new File(dcContext.getBlobdir(), file); return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY); } @@ -49,8 +55,17 @@ public class AttachmentsContentProvider extends ContentProvider { } @Override - public String getType(Uri arg0) { - return null; + public String getType(Uri uri) { + String file = uri.getPathSegments().get(0); + String mimeType = DcHelper.sharedFiles.get(file); + + return DcHelper.checkMime(uri.toString(), mimeType); + } + + @Override + public String getTypeAnonymous(Uri uri) { + String ext = MediaUtil.getFileExtensionFromUrl(uri.toString()); + return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext); } @Override diff --git a/src/main/java/org/thoughtcrime/securesms/connect/DcHelper.java b/src/main/java/org/thoughtcrime/securesms/connect/DcHelper.java index 5f1b37686..d09a5dfc6 100644 --- a/src/main/java/org/thoughtcrime/securesms/connect/DcHelper.java +++ b/src/main/java/org/thoughtcrime/securesms/connect/DcHelper.java @@ -266,7 +266,9 @@ public class DcHelper { return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } - public static final HashMap sharedFiles = new HashMap<>(); + // When the user shares a file to another app or opens a file in another app, it is added here. + // `HashMap` where `file` is the name of the file in the blobdir (not the user-visible filename). + public static final HashMap sharedFiles = new HashMap<>(); public static void openForViewOrShare(Context activity, int msg_id, String cmd) { DcContext dcContext = getContext(activity); @@ -291,12 +293,17 @@ public class DcHelper { } Uri uri; + mimeType = checkMime(filename, mimeType); if (path.startsWith(dcContext.getBlobdir())) { // Build a Uri that will later be passed to AttachmentsContentProvider.openFile(). // The last part needs to be `filename`, i.e. the original, user-visible name of the file, // so that the external apps show the name of the file correctly. uri = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".attachments/" + Uri.encode(file.getName()) + "/" + Uri.encode(filename)); - sharedFiles.put(file.getName(), 1); // as different Android version handle uris in putExtra differently, we also check them on our own + + // As different Android version handle uris in putExtra differently, + // we also check on our own that the file was actually shared. + // The check happens in AttachmentsContentProvider.openFile(). + sharedFiles.put(file.getName(), mimeType); } else { if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".fileprovider", file); @@ -306,7 +313,6 @@ public class DcHelper { } if (cmd.equals(Intent.ACTION_VIEW)) { - mimeType = checkMime(filename, mimeType); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, mimeType); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); @@ -357,7 +363,7 @@ public class DcHelper { activity.startActivity(intent); } - private static String checkMime(String path, String mimeType) { + public static String checkMime(String path, String mimeType) { if(mimeType == null || mimeType.equals("application/octet-stream")) { path = path.replaceAll(" ", ""); String extension = MediaUtil.getFileExtensionFromUrl(path); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 93ed533fb..084d19c21 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -503,6 +503,8 @@ Encryption Shared Chats Related Chats + + Past Members Contact Group Gallery