From 55c83afb676e5d4c5d2fe58132dceeefa7e9ee71 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 25 Jan 2019 22:39:01 +0100 Subject: stop file watching when service has been destroyed --- .../pixart/messenger/services/XmppConnectionService.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/main/java/de/pixart/messenger/services/XmppConnectionService.java') diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 43fd2f875..c332bfae4 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -336,6 +336,7 @@ public class XmppConnectionService extends Service { syncDirtyContacts(account); } }; + private boolean destroyed = false; private int unreadCount = -1; private AtomicLong mLastExpiryRun = new AtomicLong(0); private SecureRandom mRandom; @@ -1116,6 +1117,7 @@ public class XmppConnectionService extends Service { @SuppressLint("TrulyRandom") @Override public void onCreate() { + this.destroyed = false; OmemoSetting.load(this); ExceptionHelper.init(getApplicationContext()); try { @@ -1160,7 +1162,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.fileObserver::startWatching); mFileAddingExecutor.execute(this::checkForDeletedFiles); } if (Config.supportOpenPgp()) { @@ -1203,9 +1205,17 @@ public class XmppConnectionService extends Service { } private void checkForDeletedFiles() { + if (destroyed) { + Log.d(Config.LOGTAG, "Do not check for deleted files because service has been destroyed"); + return; + } final List deletedUuids = new ArrayList<>(); final List relativeFilePaths = databaseBackend.getAllNonDeletedFilePath(); for (final DatabaseBackend.FilePath filePath : relativeFilePaths) { + if (destroyed) { + Log.d(Config.LOGTAG, "Stop checking for deleted files because service has been destroyed"); + return; + } final File file = fileBackend.getFileForPath(filePath.path); if (!file.exists()) { deletedUuids.add(filePath.uuid.toString()); @@ -1254,8 +1264,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); - new Thread(fileObserver::restartWatching).start(); } public void toggleScreenEventReceiver() { -- cgit v1.2.3