diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index ee1d262b0b..764b999908 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -1252,7 +1252,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB if (!attachment.available) throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing)); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); encrypted = new BufferedInputStream(new FileInputStream(file)); break; } @@ -1491,7 +1491,11 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB throw new IllegalArgumentException(context.getString(R.string.title_no_stream)); } - File file = EntityAttachment.getFile(context, id); + DB db = DB.getInstance(context); + EntityAttachment attachment = db.attachment().getAttachment(id); + if (attachment == null) + return null; + File file = attachment.getFile(context); ParcelFileDescriptor pfd = null; OutputStream os = null; @@ -1557,8 +1561,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB DB db = DB.getInstance(context); DocumentFile tree = DocumentFile.fromTreeUri(context, uri); - for (EntityAttachment attachment : db.attachment().getAttachments(id)) { - File file = EntityAttachment.getFile(context, attachment.id); + List attachments = db.attachment().getAttachments(id); + for (EntityAttachment attachment : attachments) { + File file = attachment.getFile(context); String name = attachment.name; if (TextUtils.isEmpty(name)) diff --git a/app/src/main/java/eu/faircode/email/AdapterAttachment.java b/app/src/main/java/eu/faircode/email/AdapterAttachment.java index d7b2ec1ef2..8483236316 100644 --- a/app/src/main/java/eu/faircode/email/AdapterAttachment.java +++ b/app/src/main/java/eu/faircode/email/AdapterAttachment.java @@ -171,8 +171,16 @@ public class AdapterAttachment extends RecyclerView.Adapter() { @Override protected Void onExecute(Context context, Bundle args) { - DB.getInstance(context).attachment().deleteAttachment(attachment.id); - EntityAttachment.getFile(context, attachment.id).delete(); + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + EntityAttachment attachment = db.attachment().getAttachment(id); + if (attachment == null) + return null; + db.attachment().setDownloaded(id, null); + attachment.getFile(context).delete(); + db.attachment().deleteAttachment(id); + return null; } @@ -194,7 +202,7 @@ public class AdapterAttachment extends RecyclerView.Adapter private void bindTo(EntityAttachment attachment) { if (attachment.available) { - Bitmap bm = Helper.decodeImage( - EntityAttachment.getFile(context, attachment.id), + Bitmap bm = Helper.decodeImage(attachment.getFile(context), context.getResources().getDisplayMetrics().widthPixels / 2); if (bm == null) image.setImageResource(R.drawable.baseline_broken_image_24); @@ -101,7 +100,7 @@ public class AdapterImage extends RecyclerView.Adapter EntityAttachment attachment = filtered.get(pos); if (attachment.available) { // Build file name - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); // https://developer.android.com/reference/android/support/v4/content/FileProvider final Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file); diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index e64635d4de..5fe483e79b 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -2882,14 +2882,16 @@ public class AdapterMessage extends RecyclerView.Adapter'; - EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); + EntityAttachment attachment = db.attachment().getAttachment(id, cid); if (attachment != null && attachment.available) { - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { byte[] bytes = new byte[(int) file.length()]; if (is.read(bytes) != bytes.length) diff --git a/app/src/main/java/eu/faircode/email/DaoAttachment.java b/app/src/main/java/eu/faircode/email/DaoAttachment.java index 60a25d6343..badfe22526 100644 --- a/app/src/main/java/eu/faircode/email/DaoAttachment.java +++ b/app/src/main/java/eu/faircode/email/DaoAttachment.java @@ -48,6 +48,10 @@ public interface DaoAttachment { " ORDER BY sequence") List getAttachments(long message); + @Query("SELECT * FROM attachment" + + " WHERE id = :id") + EntityAttachment getAttachment(long id); + @Query("SELECT * FROM attachment" + " WHERE message = :message" + " AND sequence = :sequence") diff --git a/app/src/main/java/eu/faircode/email/EntityAttachment.java b/app/src/main/java/eu/faircode/email/EntityAttachment.java index 687c57bab0..3e732db391 100644 --- a/app/src/main/java/eu/faircode/email/EntityAttachment.java +++ b/app/src/main/java/eu/faircode/email/EntityAttachment.java @@ -75,30 +75,33 @@ public class EntityAttachment { return (disposition != null && disposition.equalsIgnoreCase(Part.INLINE)); } - static File getFile(Context context, Long id) { + File getFile(Context context) { File dir = new File(context.getFilesDir(), "attachments"); if (!dir.exists()) dir.mkdir(); return new File(dir, Long.toString(id)); } - static void copy(Context context, DB db, long oldid, long newid) { + static void copy(Context context, long oldid, long newid) { + DB db = DB.getInstance(context); List attachments = db.attachment().getAttachments(oldid); for (EntityAttachment attachment : attachments) { - long aid = attachment.id; + File source = attachment.getFile(context); + attachment.id = null; attachment.message = newid; attachment.progress = null; attachment.id = db.attachment().insertAttachment(attachment); - if (attachment.available) + + if (attachment.available) { + File target = attachment.getFile(context); try { - Helper.copy( - EntityAttachment.getFile(context, aid), - EntityAttachment.getFile(context, attachment.id)); + Helper.copy(source, target); } catch (IOException ex) { Log.e(ex); db.attachment().setProgress(attachment.id, null); } + } } } diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index d787ee4abe..6bb37f82b6 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -172,7 +172,7 @@ public class EntityOperation { db.message().setMessageContent(tmpid, false, null, null); } - EntityAttachment.copy(context, db, message.id, tmpid); + EntityAttachment.copy(context, message.id, tmpid); } // Cross account move diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index c9e137b448..4085d88f1d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1156,7 +1156,7 @@ public class FragmentCompose extends FragmentBase { attachment1.encryption = EntityAttachment.PGP_MESSAGE; attachment1.id = db.attachment().insertAttachment(attachment1); - File file1 = EntityAttachment.getFile(context, attachment1.id); + File file1 = attachment1.getFile(context); byte[] bytes1 = encrypted.toByteArray(); try (OutputStream os1 = new BufferedOutputStream(new FileOutputStream(file1))) { @@ -1173,7 +1173,7 @@ public class FragmentCompose extends FragmentBase { attachment2.encryption = EntityAttachment.PGP_SIGNATURE; attachment2.id = db.attachment().insertAttachment(attachment2); - File file2 = EntityAttachment.getFile(context, attachment2.id); + File file2 = attachment2.getFile(context); byte[] bytes2 = key.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE); try (OutputStream os2 = new BufferedOutputStream(new FileOutputStream(file2))) { @@ -1323,7 +1323,7 @@ public class FragmentCompose extends FragmentBase { @Override protected void onExecuted(Bundle args, final EntityAttachment attachment) { if (image) { - File file = EntityAttachment.getFile(getContext(), attachment.id); + File file = attachment.getFile(getContext()); Drawable d = Drawable.createFromPath(file.getAbsolutePath()); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); @@ -1459,7 +1459,7 @@ public class FragmentCompose extends FragmentBase { long size = 0; try { - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); InputStream is = null; OutputStream os = null; @@ -1769,14 +1769,14 @@ public class FragmentCompose extends FragmentBase { for (EntityAttachment attachment : attachments) if (attachment.available && ("forward".equals(action) || attachment.isInline())) { - long orig = attachment.id; + File source = attachment.getFile(context); + attachment.id = null; attachment.message = draft.id; attachment.sequence = ++sequence; attachment.id = db.attachment().insertAttachment(attachment); - File source = EntityAttachment.getFile(context, orig); - File target = EntityAttachment.getFile(context, attachment.id); + File target = attachment.getFile(context); Helper.copy(source, target); } } @@ -2448,7 +2448,7 @@ public class FragmentCompose extends FragmentBase { if (attachment == null) return null; - File file = EntityAttachment.getFile(getContext(), attachment.id); + File file = attachment.getFile(context); return Drawable.createFromPath(file.getAbsolutePath()); } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index c07e4573f8..7cc8d07734 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -495,7 +495,7 @@ public class Helper { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { long size = 0; @@ -521,7 +521,7 @@ public class Helper { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { long size = 0; @@ -543,16 +543,16 @@ public class Helper { private static void attachLog(Context context, long id, int sequence) throws IOException { DB db = DB.getInstance(context); - EntityAttachment log = new EntityAttachment(); - log.message = id; - log.sequence = sequence; - log.name = "log.txt"; - log.type = "text/plain"; - log.size = null; - log.progress = 0; - log.id = db.attachment().insertAttachment(log); + EntityAttachment attachment = new EntityAttachment(); + attachment.message = id; + attachment.sequence = sequence; + attachment.name = "log.txt"; + attachment.type = "text/plain"; + attachment.size = null; + attachment.progress = 0; + attachment.id = db.attachment().insertAttachment(attachment); - File file = EntityAttachment.getFile(context, log.id); + File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { long size = 0; @@ -562,7 +562,7 @@ public class Helper { for (EntityLog entry : db.log().getLogs(from)) size += write(os, String.format("%s %s\r\n", DF.format(entry.time), entry.data)); - db.attachment().setDownloaded(log.id, size); + db.attachment().setDownloaded(attachment.id, size); } } @@ -578,7 +578,7 @@ public class Helper { attachment.progress = 0; attachment.id = db.attachment().insertAttachment(attachment); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { long size = 0; @@ -609,7 +609,7 @@ public class Helper { attachment.id = db.attachment().insertAttachment(attachment); Process proc = null; - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { String[] cmd = new String[]{"logcat", diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 0071346df3..ecb79ea037 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -337,8 +337,9 @@ public class HtmlHelper { // Embedded images if (embedded) { + DB db = DB.getInstance(context); String cid = "<" + source.substring(4) + ">"; - EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); + EntityAttachment attachment = db.attachment().getAttachment(id, cid); if (attachment == null) { Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme()); d.setBounds(0, 0, px, px); @@ -348,8 +349,7 @@ public class HtmlHelper { d.setBounds(0, 0, px, px); return d; } else { - Bitmap bm = Helper.decodeImage( - EntityAttachment.getFile(context, attachment.id), + Bitmap bm = Helper.decodeImage(attachment.getFile(context), context.getResources().getDisplayMetrics().widthPixels); if (bm == null) { Drawable d = context.getResources().getDrawable(R.drawable.baseline_broken_image_24, context.getTheme()); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index f33da55a76..98e862c275 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -234,7 +234,7 @@ public class MessageHelper { for (EntityAttachment attachment : attachments) if (attachment.available && EntityAttachment.PGP_SIGNATURE.equals(attachment.encryption)) { InternetAddress from = (InternetAddress) message.from[0]; - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; @@ -257,7 +257,7 @@ public class MessageHelper { BodyPart bpAttachment = new MimeBodyPart(); bpAttachment.setFileName(attachment.name); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); FileDataSource dataSource = new FileDataSource(file); dataSource.setFileTypeMap(new FileTypeMap() { @Override @@ -351,7 +351,7 @@ public class MessageHelper { BodyPart bpAttachment = new MimeBodyPart(); bpAttachment.setFileName(attachment.name); - File file = EntityAttachment.getFile(context, attachment.id); + File file = attachment.getFile(context); FileDataSource dataSource = new FileDataSource(file); dataSource.setFileTypeMap(new FileTypeMap() { @Override @@ -705,12 +705,16 @@ public class MessageHelper { return false; } + DB db = DB.getInstance(context); + // Get data AttachmentPart apart = attachments.get(index); - File file = EntityAttachment.getFile(context, id); + EntityAttachment attachment = db.attachment().getAttachment(id); + if (attachment == null) + return false; + File file = attachment.getFile(context); // Download attachment - DB db = DB.getInstance(context); db.attachment().setProgress(id, null); try (InputStream is = apart.part.getInputStream()) { long size = 0;