Refactoring

pull/153/head
M66B 6 years ago
parent fbab61a0b9
commit 6d1438f31f

@ -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<EntityAttachment> attachments = db.attachment().getAttachments(id);
for (EntityAttachment attachment : attachments) {
File file = attachment.getFile(context);
String name = attachment.name;
if (TextUtils.isEmpty(name))

@ -171,8 +171,16 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
new SimpleTask<Void>() {
@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<AdapterAttachment.Vi
private void onShare(EntityAttachment attachment) {
// 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);

@ -78,8 +78,7 @@ public class AdapterImage extends RecyclerView.Adapter<AdapterImage.ViewHolder>
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<AdapterImage.ViewHolder>
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);

@ -2882,14 +2882,16 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private String getHtmlEmbedded(long id) throws IOException {
String html = Helper.readText(EntityMessage.getFile(context, id));
DB db = DB.getInstance(context);
Document doc = Jsoup.parse(html);
for (Element img : doc.select("img")) {
String src = img.attr("src");
if (src.startsWith("cid:")) {
String cid = '<' + src.substring(4) + '>';
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)

@ -48,6 +48,10 @@ public interface DaoAttachment {
" ORDER BY sequence")
List<EntityAttachment> getAttachments(long message);
@Query("SELECT * FROM attachment" +
" WHERE id = :id")
EntityAttachment getAttachment(long id);
@Query("SELECT * FROM attachment" +
" WHERE message = :message" +
" AND sequence = :sequence")

@ -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<EntityAttachment> 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);
}
}
}
}

@ -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

@ -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());
}

@ -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",

@ -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());

@ -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;

Loading…
Cancel
Save