From afcbc3ccf36a44ebb1ff2b3b471078aa13e2aeb2 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 25 Jan 2019 21:15:09 +0100 Subject: mark deleted files in database and not query them when querying for media --- .../messenger/services/NotificationService.java | 1 + .../messenger/services/XmppConnectionService.java | 65 +++++++++++----------- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'src/main/java/de/pixart/messenger/services') diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index 270005dc9..7608976a5 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -737,6 +737,7 @@ public class NotificationService { private static boolean isImageMessage(Message message) { return message.getType() != Message.TYPE_TEXT && message.getTransferable() == null + && !message.isDeleted() && message.getEncryption() != Message.ENCRYPTION_PGP && message.getFileParams().height > 0; } diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index defc6244d..e123e8cff 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -52,6 +52,7 @@ import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; +import java.io.File; import java.net.URL; import java.security.SecureRandom; import java.security.Security; @@ -238,7 +239,6 @@ public class XmppConnectionService extends Service { ) { @Override public void onEvent(int event, String path) { - Log.d(Config.LOGTAG, "event " + event + " path=" + path); markFileDeleted(path); } }; @@ -375,7 +375,6 @@ public class XmppConnectionService extends Service { if (!conversation.startOtrIfNeeded()) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": couldn't start OTR with " + conversation.getContact().getJid() + " when needed"); } - checkDeletedFiles(conversation); sendUnsentMessages(conversation); resendFailedFileMessages(conversation); } @@ -1162,6 +1161,7 @@ public class XmppConnectionService extends Service { if (Compatibility.hasStoragePermission(this)) { Log.d(Config.LOGTAG, "starting file observer"); new Thread(fileObserver::startWatching).start(); + mFileAddingExecutor.execute(this::checkForDeletedFiles); } if (Config.supportOpenPgp()) { this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() { @@ -1202,6 +1202,22 @@ public class XmppConnectionService extends Service { CancelAutomaticExport(false); } + private void checkForDeletedFiles() { + final List deletedUuids = new ArrayList<>(); + final List relativeFilePaths = databaseBackend.getAllNonDeletedFilePath(); + for (final DatabaseBackend.FilePath filePath : relativeFilePaths) { + final File file = fileBackend.getFileForPath(filePath.path); + if (!file.exists()) { + deletedUuids.add(filePath.uuid.toString()); + } + } + Log.d(Config.LOGTAG, "found " + deletedUuids.size() + " deleted files on start up. total=" + relativeFilePaths.size()); + if (deletedUuids.size() > 0) { + databaseBackend.markFileAsDeleted(deletedUuids); + markUuidsAsDeletedFiles(deletedUuids); + } + } + public void startContactObserver() { getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new ContentObserver(null) { @Override @@ -1238,6 +1254,7 @@ public class XmppConnectionService extends Service { public void restartFileObserver() { Log.d(Config.LOGTAG, "restarting file observer"); + mFileAddingExecutor.execute(this::checkForDeletedFiles); new Thread(fileObserver::restartWatching).start(); } @@ -1776,7 +1793,6 @@ public class XmppConnectionService extends Service { private void restoreMessages(Conversation conversation) { conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); - checkDeletedFiles(conversation); conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING)); conversation.findUnreadMessages(message -> mNotificationService.pushFromBacklog(message)); } @@ -1817,37 +1833,21 @@ public class XmppConnectionService extends Service { return this.conversations; } - private void checkDeletedFiles(Conversation conversation) { - conversation.findMessagesWithFiles(message -> { - if (!getFileBackend().isFileAvailable(message)) { - message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED)); - final int s = message.getStatus(); - if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) { - markMessage(message, Message.STATUS_SEND_FAILED); - } - } - }); + private void markFileDeleted(final String path) { + final File file = new File(path); + final boolean isInternalFile = fileBackend.isInternalFile(file); + final List uuids = databaseBackend.markFileAsDeleted(file, isInternalFile); + Log.d(Config.LOGTAG, "deleted file " + path + " internal=" + isInternalFile + ", database hits=" + uuids.size()); + markUuidsAsDeletedFiles(uuids); } - private void markFileDeleted(final String path) { - Log.d(Config.LOGTAG, "deleted file " + path); + private void markUuidsAsDeletedFiles(List uuids) { + boolean deleted = false; for (Conversation conversation : getConversations()) { - conversation.findMessagesWithFiles(message -> { - DownloadableFile file = fileBackend.getFile(message); - if (file.getAbsolutePath().equals(path)) { - if (!file.exists()) { - message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED)); - final int s = message.getStatus(); - if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) { - markMessage(message, Message.STATUS_SEND_FAILED); - } else { - updateConversationUi(); - } - } else { - Log.d(Config.LOGTAG, "found matching message for file " + path + " but file still exists"); - } - } - }); + deleted |= conversation.markAsDeleted(uuids); + } + if (deleted) { + updateConversationUi(); } } @@ -1886,7 +1886,6 @@ public class XmppConnectionService extends Service { List messages = databaseBackend.getMessages(conversation, 50, timestamp); if (messages.size() > 0) { conversation.addAll(0, messages); - checkDeletedFiles(conversation); callback.onMoreMessagesLoaded(messages.size(), conversation); } else if (conversation.hasMessagesLeftOnServer() && account.isOnlineAndConnected() @@ -2033,7 +2032,6 @@ public class XmppConnectionService extends Service { } } } - checkDeletedFiles(c); if (joinAfterCreate) { joinMuc(c); @@ -2097,7 +2095,6 @@ public class XmppConnectionService extends Service { updateConversationUi(); c.messagesLoaded.set(true); } - checkDeletedFiles(c); } }); updateConversationUi(); -- cgit v1.2.3