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/de/pixart/messenger/utils | |
parent | c9d9e7ea8114b7a23cfa46fcafa1b3e491bc33b4 (diff) |
stop file watching when service has been destroyed
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/ConversationsFileObserver.java | 24 |
1 files changed, 22 insertions, 2 deletions
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(); } |