diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-01-25 22:39:01 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-01-25 22:39:01 +0100 |
commit | 55c83afb676e5d4c5d2fe58132dceeefa7e9ee71 (patch) | |
tree | ed7118ba9243d53df4e20bd4ec7add8423460d18 /src/main/java | |
parent | c9d9e7ea8114b7a23cfa46fcafa1b3e491bc33b4 (diff) |
stop file watching when service has been destroyed
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 14 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java | 24 |
2 files changed, 34 insertions, 4 deletions
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<String> deletedUuids = new ArrayList<>(); final List<DatabaseBackend.FilePath> 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() { diff --git a/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java b/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java index 73d5589e4..5e2948b5b 100644 --- a/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java +++ b/src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java @@ -8,6 +8,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Stack; +import java.util.concurrent.atomic.AtomicBoolean; import de.pixart.messenger.Config; @@ -21,16 +22,26 @@ public abstract class ConversationsFileObserver { private final String path; private final List<SingleFileObserver> mObservers = new ArrayList<>(); + private final AtomicBoolean shouldStop = new AtomicBoolean(true); protected ConversationsFileObserver(String path) { this.path = path; } - public synchronized void startWatching() { + public void startWatching() { + shouldStop.set(false); + startWatchingInternal(); + } + + private synchronized void startWatchingInternal() { Stack<String> stack = new Stack<>(); stack.push(path); while (!stack.empty()) { + if (shouldStop.get()) { + Log.d(Config.LOGTAG, "file observer received command to stop"); + return; + } String parent = stack.pop(); mObservers.add(new SingleFileObserver(parent, FileObserver.DELETE | FileObserver.MOVED_FROM)); final File path = new File(parent); @@ -44,6 +55,10 @@ public abstract class ConversationsFileObserver { continue; } for (File file : files) { + if (shouldStop.get()) { + Log.d(Config.LOGTAG, "file observer received command to stop"); + return; + } if (file.isDirectory() && file.getName().charAt(0) != '.') { final String currentPath = file.getAbsolutePath(); if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(currentPath)) { @@ -74,7 +89,12 @@ public abstract class ConversationsFileObserver { return false; } - public synchronized void stopWatching() { + public void stopWatching() { + shouldStop.set(true); + stopWatchingInternal(); + } + + private synchronized void stopWatchingInternal() { for (FileObserver observer : mObservers) { observer.stopWatching(); } |