Merge branch 'main' into r10s/improve-saved-messages

This commit is contained in:
adb
2025-02-07 18:15:37 +01:00
committed by GitHub
4 changed files with 33 additions and 10 deletions
@@ -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
@@ -266,7 +266,9 @@ public class DcHelper {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
public static final HashMap<String, Integer> sharedFiles = new HashMap<>();
// When the user shares a file to another app or opens a file in another app, it is added here.
// `HashMap<file, mimetype>` where `file` is the name of the file in the blobdir (not the user-visible filename).
public static final HashMap<String, String> 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);