Refactoring

This commit is contained in:
M66B 2018-08-21 14:25:42 +00:00
parent 36bdc2cce7
commit 3ef26e5e46
7 changed files with 26 additions and 26 deletions

View File

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

View File

@ -19,6 +19,10 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import java.io.File;
import javax.mail.BodyPart;
import androidx.annotation.NonNull;
@ -60,6 +64,12 @@ public class EntityAttachment {
@Ignore
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
public boolean equals(Object obj) {
if (obj instanceof EntityAttachment) {

View File

@ -108,10 +108,14 @@ public class EntityMessage {
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");
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;
try {
out = new BufferedWriter(new FileWriter(file));
@ -131,8 +135,7 @@ public class EntityMessage {
}
static String read(Context context, Long id) throws IOException {
File dir = new File(context.getFilesDir(), "messages");
File file = new File(dir, id.toString());
File file = getFile(context, id);
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));

View File

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

View File

@ -714,15 +714,11 @@ public class FragmentMessage extends FragmentEx {
try {
db.beginTransaction();
if (debug && BuildConfig.DEBUG)
db.message().deleteMessage(id);
else {
db.message().setMessageUiHide(id, true);
db.message().setMessageUiHide(id, true);
EntityMessage message = db.message().getMessage(id);
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();
} finally {

View File

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

View File

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