From 65acadae412c82126fd0384682415ce1577e6d17 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 12 Dec 2018 09:04:23 +0100 Subject: [PATCH] Prevent crash --- .../eu/faircode/email/FragmentMessages.java | 92 +++++++++++-------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index f238340c5b..87c22de2a7 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -736,15 +736,18 @@ public class FragmentMessages extends FragmentEx { DB db = DB.getInstance(context); - for (Long id : ids) { + long account = -1; + for (long id : ids) { EntityMessage message = db.message().getMessage(id); - result[message.ui_seen ? 1 : 0] = true; - result[message.flagged ? 3 : 2] = true; + if (message != null) { + account = message.account; + result[message.ui_seen ? 1 : 0] = true; + result[message.flagged ? 3 : 2] = true; + } } - EntityMessage m0 = db.message().getMessage(ids[0]); - EntityFolder archive = db.folder().getFolderByType(m0.account, EntityFolder.ARCHIVE); - EntityFolder trash = db.folder().getFolderByType(m0.account, EntityFolder.TRASH); + EntityFolder archive = db.folder().getFolderByType(account, EntityFolder.ARCHIVE); + EntityFolder trash = db.folder().getFolderByType(account, EntityFolder.TRASH); result[4] = (archive != null); result[5] = (trash != null); @@ -866,7 +869,7 @@ public class FragmentMessages extends FragmentEx { for (long id : ids) { EntityMessage message = db.message().getMessage(id); - if (message.ui_seen != seen) { + if (message != null && message.ui_seen != seen) { List messages = db.message().getMessageByThread( message.account, message.thread, threading ? null : id, message.folder); for (EntityMessage threaded : messages) @@ -908,7 +911,7 @@ public class FragmentMessages extends FragmentEx { for (long id : ids) { EntityMessage message = db.message().getMessage(id); - if (message.ui_flagged != flagged) { + if (message != null && message.ui_flagged != flagged) { List messages = db.message().getMessageByThread( message.account, message.thread, threading ? null : id, message.folder); for (EntityMessage threaded : messages) @@ -966,13 +969,14 @@ public class FragmentMessages extends FragmentEx { for (long id : ids) { EntityMessage message = db.message().getMessage(id); - List messages = db.message().getMessageByThread( - message.account, message.thread, threading ? null : id, message.folder); - for (EntityMessage threaded : messages) { - if (threaded.uid == null && !TextUtils.isEmpty(threaded.error)) // outbox - db.message().deleteMessage(threaded.id); - else - EntityOperation.queue(db, threaded, EntityOperation.DELETE); + if (message != null) { + List messages = db.message().getMessageByThread( + message.account, message.thread, threading ? null : id, message.folder); + for (EntityMessage threaded : messages) + if (threaded.uid == null && !TextUtils.isEmpty(threaded.error)) // outbox + db.message().deleteMessage(threaded.id); + else + EntityOperation.queue(db, threaded, EntityOperation.DELETE); } } @@ -1016,19 +1020,22 @@ public class FragmentMessages extends FragmentEx { try { db.beginTransaction(); - EntityMessage m0 = db.message().getMessage(ids[0]); - result.target = db.folder().getFolderByType(m0.account, type); - + long account = -1; for (long id : ids) { EntityMessage message = db.message().getMessage(id); - List messages = db.message().getMessageByThread( - message.account, message.thread, threading ? null : id, message.folder); - for (EntityMessage threaded : messages) { - result.ids.add(threaded.id); - db.message().setMessageUiHide(threaded.id, true); + if (message != null) { + account = message.account; + List messages = db.message().getMessageByThread( + message.account, message.thread, threading ? null : id, message.folder); + for (EntityMessage threaded : messages) { + result.ids.add(threaded.id); + db.message().setMessageUiHide(threaded.id, true); + } } } + result.target = db.folder().getFolderByType(account, type); + db.setTransactionSuccessful(); } finally { db.endTransaction(); @@ -1062,8 +1069,16 @@ public class FragmentMessages extends FragmentEx { DB db = DB.getInstance(context); - EntityMessage m0 = db.message().getMessage(ids[0]); - List folders = db.folder().getFolders(m0.account); + long account = -1; + for (long id : ids) { + EntityMessage message = db.message().getMessage(id); + if (message != null) { + account = message.account; + break; + } + } + + List folders = db.folder().getFolders(account); List targets = new ArrayList<>(); for (EntityFolder folder : folders) @@ -1112,11 +1127,13 @@ public class FragmentMessages extends FragmentEx { for (long id : ids) { EntityMessage message = db.message().getMessage(id); - List messages = db.message().getMessageByThread( - message.account, message.thread, threading ? null : id, message.folder); - for (EntityMessage threaded : messages) { - result.ids.add(threaded.id); - db.message().setMessageUiHide(threaded.id, true); + if (message != null) { + List messages = db.message().getMessageByThread( + message.account, message.thread, threading ? null : id, message.folder); + for (EntityMessage threaded : messages) { + result.ids.add(threaded.id); + db.message().setMessageUiHide(threaded.id, true); + } } } @@ -1847,14 +1864,15 @@ public class FragmentMessages extends FragmentEx { try { db.beginTransaction(); - for (long id : result.ids) { - EntityMessage message = db.message().getMessage(id); - if (message != null && message.ui_hide) { - Log.i(Helper.TAG, "Move id=" + id + " target=" + result.target.name); - EntityFolder folder = db.folder().getFolderByName(message.account, result.target.name); - EntityOperation.queue(db, message, EntityOperation.MOVE, folder.id); + if (result.target != null) + for (long id : result.ids) { + EntityMessage message = db.message().getMessage(id); + if (message != null && message.ui_hide) { + Log.i(Helper.TAG, "Move id=" + id + " target=" + result.target.name); + EntityFolder folder = db.folder().getFolderByName(message.account, result.target.name); + EntityOperation.queue(db, message, EntityOperation.MOVE, folder.id); + } } - } db.setTransactionSuccessful(); } finally {