Refactoring

pull/50/head
M66B 7 years ago
parent 36bdc2cce7
commit 3ef26e5e46

@ -132,10 +132,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
DB.getInstance(context).attachment().deleteAttachment(attachment.id); DB.getInstance(context).attachment().deleteAttachment(attachment.id);
File dir = new File(context.getFilesDir(), "attachments"); EntityAttachment.getFile(context, attachment.id).delete();
File file = new File(dir, attachment.id.toString());
file.delete();
return null; return null;
} }
}.load(context, owner, args); }.load(context, owner, args);
@ -143,8 +140,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
} else { } else {
if (attachment.available) { if (attachment.available) {
// Build file name // Build file name
File dir = new File(context.getFilesDir(), "attachments"); File file = EntityAttachment.getFile(context, attachment.id);
File file = new File(dir, attachment.id.toString());
// https://developer.android.com/reference/android/support/v4/content/FileProvider // https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file); Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);

@ -19,6 +19,10 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import java.io.File;
import javax.mail.BodyPart; import javax.mail.BodyPart;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -60,6 +64,12 @@ public class EntityAttachment {
@Ignore @Ignore
BodyPart part; BodyPart part;
static File getFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "attachments");
dir.mkdir();
return new File(dir, Long.toString(id));
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof EntityAttachment) { if (obj instanceof EntityAttachment) {

@ -108,10 +108,14 @@ public class EntityMessage {
return sb.toString(); return sb.toString();
} }
void write(Context context, String body) throws IOException { static File getFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "messages"); File dir = new File(context.getFilesDir(), "messages");
dir.mkdir(); dir.mkdir();
File file = new File(dir, id.toString()); return new File(dir, id.toString());
}
void write(Context context, String body) throws IOException {
File file = getFile(context, id);
BufferedWriter out = null; BufferedWriter out = null;
try { try {
out = new BufferedWriter(new FileWriter(file)); out = new BufferedWriter(new FileWriter(file));
@ -131,8 +135,7 @@ public class EntityMessage {
} }
static String read(Context context, Long id) throws IOException { static String read(Context context, Long id) throws IOException {
File dir = new File(context.getFilesDir(), "messages"); File file = getFile(context, id);
File file = new File(dir, id.toString());
BufferedReader in = null; BufferedReader in = null;
try { try {
in = new BufferedReader(new FileReader(file)); in = new BufferedReader(new FileReader(file));

@ -495,9 +495,7 @@ public class FragmentCompose extends FragmentEx {
} }
try { try {
File dir = new File(context.getFilesDir(), "attachments"); File file = EntityAttachment.getFile(context, attachment.id);
dir.mkdir();
File file = new File(dir, Long.toString(attachment.id));
InputStream is = null; InputStream is = null;
OutputStream os = null; OutputStream os = null;

@ -714,15 +714,11 @@ public class FragmentMessage extends FragmentEx {
try { try {
db.beginTransaction(); db.beginTransaction();
if (debug && BuildConfig.DEBUG) db.message().setMessageUiHide(id, true);
db.message().deleteMessage(id);
else { EntityMessage message = db.message().getMessage(id);
db.message().setMessageUiHide(id, true); EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
EntityOperation.queue(db, message, EntityOperation.MOVE, trash.id);
EntityMessage message = db.message().getMessage(id);
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
EntityOperation.queue(db, message, EntityOperation.MOVE, trash.id);
}
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {

@ -129,8 +129,7 @@ public class MessageHelper {
BodyPart bpAttachment = new MimeBodyPart(); BodyPart bpAttachment = new MimeBodyPart();
bpAttachment.setFileName(attachment.name); bpAttachment.setFileName(attachment.name);
File dir = new File(context.getFilesDir(), "attachments"); File file = EntityAttachment.getFile(context, attachment.id);
File file = new File(dir, attachment.id.toString());
FileDataSource dataSource = new FileDataSource(file); FileDataSource dataSource = new FileDataSource(file);
dataSource.setFileTypeMap(new FileTypeMap() { dataSource.setFileTypeMap(new FileTypeMap() {
@Override @Override

@ -1010,9 +1010,7 @@ public class ServiceSynchronize extends LifecycleService {
EntityAttachment a = helper.getAttachments().get(sequence - 1); EntityAttachment a = helper.getAttachments().get(sequence - 1);
// Build filename // Build filename
File dir = new File(getFilesDir(), "attachments"); File file = EntityAttachment.getFile(this, attachment.id);
dir.mkdir();
File file = new File(dir, Long.toString(attachment.id));
// Download attachment // Download attachment
InputStream is = null; InputStream is = null;

Loading…
Cancel
Save