run file observer on its own thread. fixes #4164

This commit is contained in:
Daniel Gultsch 2021-09-10 19:07:57 +02:00
parent 25f137441b
commit c195e8b3d2
1 changed files with 14 additions and 11 deletions

View File

@ -75,6 +75,8 @@ import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; 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.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; 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"; private static final String SETTING_LAST_ACTIVITY_TS = "last_activity_timestamp";
public final CountDownLatch restoredFromDatabaseLatch = new CountDownLatch(1); public final CountDownLatch restoredFromDatabaseLatch = new CountDownLatch(1);
private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor("FileAdding"); private final static Executor FILE_OBSERVER_EXECUTOR = Executors.newSingleThreadExecutor();
private final SerialSingleThreadExecutor mVideoCompressionExecutor = new SerialSingleThreadExecutor("VideoCompression"); 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 mDatabaseWriterExecutor = new SerialSingleThreadExecutor("DatabaseWriter");
private final SerialSingleThreadExecutor mDatabaseReaderExecutor = new SerialSingleThreadExecutor("DatabaseReader"); private final SerialSingleThreadExecutor mDatabaseReaderExecutor = new SerialSingleThreadExecutor("DatabaseReader");
private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor"); private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor");
@ -563,9 +566,9 @@ public class XmppConnectionService extends Service {
Log.d(Config.LOGTAG, "counterpart=" + message.getCounterpart()); Log.d(Config.LOGTAG, "counterpart=" + message.getCounterpart());
final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback); final AttachFileToConversationRunnable runnable = new AttachFileToConversationRunnable(this, uri, type, message, callback);
if (runnable.isVideoMessage()) { if (runnable.isVideoMessage()) {
mVideoCompressionExecutor.execute(runnable); VIDEO_COMPRESSION_EXECUTOR.execute(runnable);
} else { } else {
mFileAddingExecutor.execute(runnable); FILE_ATTACHMENT_EXECUTOR.execute(runnable);
} }
} }
@ -592,7 +595,7 @@ public class XmppConnectionService extends Service {
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
} }
Log.d(Config.LOGTAG, "attachImage: type=" + message.getType()); Log.d(Config.LOGTAG, "attachImage: type=" + message.getType());
mFileAddingExecutor.execute(() -> { FILE_ATTACHMENT_EXECUTOR.execute(() -> {
try { try {
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
} catch (FileBackend.ImageCompressionException e) { } 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) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
startContactObserver(); startContactObserver();
} }
mFileAddingExecutor.execute(fileBackend::deleteHistoricAvatarPath); FILE_OBSERVER_EXECUTOR.execute(fileBackend::deleteHistoricAvatarPath);
if (Compatibility.hasStoragePermission(this)) { if (Compatibility.hasStoragePermission(this)) {
Log.d(Config.LOGTAG, "starting file observer"); Log.d(Config.LOGTAG, "starting file observer");
mFileAddingExecutor.execute(this.fileObserver::startWatching); FILE_OBSERVER_EXECUTOR.execute(this.fileObserver::startWatching);
mFileAddingExecutor.execute(this::checkForDeletedFiles); FILE_OBSERVER_EXECUTOR.execute(this::checkForDeletedFiles);
} }
if (Config.supportOpenPgp()) { if (Config.supportOpenPgp()) {
this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() { this.pgpServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {
@ -1266,8 +1269,8 @@ public class XmppConnectionService extends Service {
public void restartFileObserver() { public void restartFileObserver() {
Log.d(Config.LOGTAG, "restarting file observer"); Log.d(Config.LOGTAG, "restarting file observer");
mFileAddingExecutor.execute(this.fileObserver::restartWatching); FILE_OBSERVER_EXECUTOR.execute(this.fileObserver::restartWatching);
mFileAddingExecutor.execute(this::checkForDeletedFiles); FILE_OBSERVER_EXECUTOR.execute(this::checkForDeletedFiles);
} }
public void toggleScreenEventReceiver() { public void toggleScreenEventReceiver() {
@ -1927,7 +1930,7 @@ public class XmppConnectionService extends Service {
private void restoreMessages(Conversation conversation) { private void restoreMessages(Conversation conversation) {
conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE)); conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING)); conversation.findUnsentTextMessages(message -> markMessage(message, Message.STATUS_WAITING));
conversation.findUnreadMessages(message -> mNotificationService.pushFromBacklog(message)); conversation.findUnreadMessages(mNotificationService::pushFromBacklog);
} }
public void loadPhoneContacts() { public void loadPhoneContacts() {