FileObserver: start monitoring new directories when they are created
This commit is contained in:
parent
e10b182d6b
commit
f5f9075da2
|
@ -239,8 +239,8 @@ public class XmppConnectionService extends Service {
|
|||
Environment.getExternalStorageDirectory().getAbsolutePath()
|
||||
) {
|
||||
@Override
|
||||
public void onEvent(int event, String path) {
|
||||
markFileDeleted(path);
|
||||
public void onEvent(final int event, final File file) {
|
||||
markFileDeleted(file);
|
||||
}
|
||||
};
|
||||
private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
|
||||
|
@ -1859,17 +1859,16 @@ public class XmppConnectionService extends Service {
|
|||
return this.conversations;
|
||||
}
|
||||
|
||||
private void markFileDeleted(final String path) {
|
||||
private void markFileDeleted(final File file) {
|
||||
synchronized (FILENAMES_TO_IGNORE_DELETION) {
|
||||
if (FILENAMES_TO_IGNORE_DELETION.remove(path)) {
|
||||
Log.d(Config.LOGTAG, "ignored deletion of " + path);
|
||||
if (FILENAMES_TO_IGNORE_DELETION.remove(file.getAbsolutePath())) {
|
||||
Log.d(Config.LOGTAG, "ignored deletion of " + file.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
}
|
||||
final File file = new File(path);
|
||||
final boolean isInternalFile = fileBackend.isInternalFile(file);
|
||||
final List<String> uuids = databaseBackend.markFileAsDeleted(file, isInternalFile);
|
||||
Log.d(Config.LOGTAG, "deleted file " + path + " internal=" + isInternalFile + ", database hits=" + uuids.size());
|
||||
Log.d(Config.LOGTAG, "deleted file " + file.getAbsolutePath() + " internal=" + isInternalFile + ", database hits=" + uuids.size());
|
||||
markUuidsAsDeletedFiles(uuids);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,11 @@ import android.util.Log;
|
|||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
|
@ -19,6 +22,9 @@ import eu.siacs.conversations.Config;
|
|||
*/
|
||||
|
||||
public abstract class ConversationsFileObserver {
|
||||
private static final Executor EVENT_EXECUTOR = Executors.newSingleThreadExecutor();
|
||||
private static final int MASK = FileObserver.DELETE | FileObserver.MOVED_FROM | FileObserver.CREATE;
|
||||
|
||||
|
||||
private final String path;
|
||||
private final List<SingleFileObserver> mObservers = new ArrayList<>();
|
||||
|
@ -34,7 +40,7 @@ public abstract class ConversationsFileObserver {
|
|||
}
|
||||
|
||||
private synchronized void startWatchingInternal() {
|
||||
Stack<String> stack = new Stack<>();
|
||||
final Stack<String> stack = new Stack<>();
|
||||
stack.push(path);
|
||||
|
||||
while (!stack.empty()) {
|
||||
|
@ -42,21 +48,18 @@ public abstract class ConversationsFileObserver {
|
|||
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 String parent = stack.pop();
|
||||
final File path = new File(parent);
|
||||
mObservers.add(new SingleFileObserver(path, MASK));
|
||||
final File[] files = path.listFiles();
|
||||
if (files == null) {
|
||||
continue;
|
||||
}
|
||||
for(File file : files) {
|
||||
for (final File file : (files == null ? new File[0] : 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)) {
|
||||
if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(file)) {
|
||||
stack.push(currentPath);
|
||||
}
|
||||
}
|
||||
|
@ -75,8 +78,8 @@ public abstract class ConversationsFileObserver {
|
|||
return depth;
|
||||
}
|
||||
|
||||
private boolean observing(String path) {
|
||||
for(SingleFileObserver observer : mObservers) {
|
||||
private boolean observing(final File path) {
|
||||
for (final SingleFileObserver observer : mObservers) {
|
||||
if (path.equals(observer.path)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -96,7 +99,7 @@ public abstract class ConversationsFileObserver {
|
|||
mObservers.clear();
|
||||
}
|
||||
|
||||
abstract public void onEvent(int event, String path);
|
||||
abstract public void onEvent(final int event, File path);
|
||||
|
||||
public void restartWatching() {
|
||||
stopWatching();
|
||||
|
@ -104,21 +107,33 @@ public abstract class ConversationsFileObserver {
|
|||
}
|
||||
|
||||
private class SingleFileObserver extends FileObserver {
|
||||
private final String path;
|
||||
private final File path;
|
||||
|
||||
SingleFileObserver(String path, int mask) {
|
||||
super(path, mask);
|
||||
SingleFileObserver(final File path, final int mask) {
|
||||
super(path.getAbsolutePath(), mask);
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(int event, String filename) {
|
||||
public void onEvent(final int event, final String filename) {
|
||||
if (filename == null) {
|
||||
Log.d(Config.LOGTAG, "ignored file event with NULL filename (event=" + event + ")");
|
||||
return;
|
||||
}
|
||||
ConversationsFileObserver.this.onEvent(event, path+'/'+filename);
|
||||
}
|
||||
|
||||
EVENT_EXECUTOR.execute(() -> {
|
||||
final File file = new File(this.path, filename);
|
||||
if ((event & FileObserver.ALL_EVENTS) == FileObserver.CREATE) {
|
||||
if (file.isDirectory()) {
|
||||
Log.d(Config.LOGTAG, "file observer observed new directory creation " + file);
|
||||
if (!observing(file)) {
|
||||
final SingleFileObserver observer = new SingleFileObserver(file, MASK);
|
||||
observer.startWatching();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
ConversationsFileObserver.this.onEvent(event, file);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue