From c195e8b3d280123a43430f083615fcf4ccbb40c8 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 10 Sep 2021 19:07:57 +0200 Subject: [PATCH] run file observer on its own thread. fixes #4164 --- .../services/XmppConnectionService.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 1f3b666ac..1c91c1ee8 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -75,6 +75,8 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -184,8 +186,9 @@ public class XmppConnectionService extends Service { private static final String SETTING_LAST_ACTIVITY_TS = "last_activity_timestamp"; public final CountDownLatch restoredFromDatabaseLatch = new CountDownLatch(1); - private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor("FileAdding"); - private final SerialSingleThreadExecutor mVideoCompressionExecutor = new SerialSingleThreadExecutor("VideoCompression"); + private final static Executor FILE_OBSERVER_EXECUTOR = Executors.newSingleThreadExecutor(); + private final static Executor FILE_ATTACHMENT_EXECUTOR = Executors.newSingleThreadExecutor(); + private final static SerialSingleThreadExecutor VIDEO_COMPRESSION_EXECUTOR = new SerialSingleThreadExecutor("VideoCompression"); private final SerialSingleThreadExecutor mDatabaseWriterExecutor = new SerialSingleThreadExecutor("DatabaseWriter"); private final SerialSingleThreadExecutor mDatabaseReaderExecutor = new SerialSingleThreadExecutor("DatabaseReader"); private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor"); @@ -563,9 +566,9 @@ public class XmppConnectionService extends Service { Log.d(Config.LOGTAG, "counterpart=" + message.getCounterpart()); final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback); if (runnable.isVideoMessage()) { - mVideoCompressionExecutor.execute(runnable); + VIDEO_COMPRESSION_EXECUTOR.execute(runnable); } else { - mFileAddingExecutor.execute(runnable); + FILE_ATTACHMENT_EXECUTOR.execute(runnable); } } @@ -592,7 +595,7 @@ public class XmppConnectionService extends Service { message.setType(Message.TYPE_IMAGE); } Log.d(Config.LOGTAG, "attachImage: type=" + message.getType()); - mFileAddingExecutor.execute(() -> { + FILE_ATTACHMENT_EXECUTOR.execute(() -> { try { getFileBackend().copyImageToPrivateStorage(message, uri); } catch (FileBackend.ImageCompressionException e) { @@ -1146,11 +1149,11 @@ public class XmppConnectionService extends Service { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { startContactObserver(); } - mFileAddingExecutor.execute(fileBackend::deleteHistoricAvatarPath); + FILE_OBSERVER_EXECUTOR.execute(fileBackend::deleteHistoricAvatarPath); if (Compatibility.hasStoragePermission(this)) { Log.d(Config.LOGTAG, "starting file observer"); - mFileAddingExecutor.execute(this.fileObserver::startWatching); - mFileAddingExecutor.execute(this::checkForDeletedFiles); + FILE_OBSERVER_EXECUTOR.execute(this.fileObserver::startWatching); + FILE_OBSERVER_EXECUTOR.execute(this::checkForDeletedFiles); } if (Config.supportOpenPgp()) { this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() { @@ -1266,8 +1269,8 @@ public class XmppConnectionService extends Service { public void restartFileObserver() { Log.d(Config.LOGTAG, "restarting file observer"); - mFileAddingExecutor.execute(this.fileObserver::restartWatching); - mFileAddingExecutor.execute(this::checkForDeletedFiles); + FILE_OBSERVER_EXECUTOR.execute(this.fileObserver::restartWatching); + FILE_OBSERVER_EXECUTOR.execute(this::checkForDeletedFiles); } public void toggleScreenEventReceiver() { @@ -1927,7 +1930,7 @@ public class XmppConnectionService extends Service { private void restoreMessages(Conversation conversation) { conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING)); - conversation.findUnreadMessages(message -> mNotificationService.pushFromBacklog(message)); + conversation.findUnreadMessages(mNotificationService::pushFromBacklog); } public void loadPhoneContacts() {