2016-07-23 14:12:45 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.FileObserver;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.util.Log;
|
2016-07-23 14:12:45 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
2020-07-30 10:55:19 +00:00
|
|
|
import java.util.Collections;
|
2016-07-23 14:12:45 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Stack;
|
2020-07-30 10:55:19 +00:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
2019-01-12 09:21:21 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2016-07-23 14:12:45 +00:00
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
|
2016-07-23 14:12:45 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2012 Bartek Przybylski
|
|
|
|
* Copyright (C) 2015 ownCloud Inc.
|
|
|
|
* Copyright (C) 2016 Daniel Gultsch
|
|
|
|
*/
|
|
|
|
|
|
|
|
public abstract class ConversationsFileObserver {
|
2020-07-30 10:55:19 +00:00
|
|
|
private static final Executor EVENT_EXECUTOR = Executors.newSingleThreadExecutor();
|
|
|
|
private static final int MASK = FileObserver.DELETE | FileObserver.MOVED_FROM | FileObserver.CREATE;
|
|
|
|
|
2016-07-23 14:12:45 +00:00
|
|
|
|
|
|
|
private final String path;
|
|
|
|
private final List<SingleFileObserver> mObservers = new ArrayList<>();
|
2019-01-12 09:21:21 +00:00
|
|
|
private final AtomicBoolean shouldStop = new AtomicBoolean(true);
|
2016-07-23 14:12:45 +00:00
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
protected ConversationsFileObserver(String path) {
|
2016-07-23 14:12:45 +00:00
|
|
|
this.path = path;
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:21:21 +00:00
|
|
|
public void startWatching() {
|
|
|
|
shouldStop.set(false);
|
|
|
|
startWatchingInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void startWatchingInternal() {
|
2020-07-30 10:55:19 +00:00
|
|
|
final Stack<String> stack = new Stack<>();
|
2016-07-23 14:12:45 +00:00
|
|
|
stack.push(path);
|
|
|
|
|
|
|
|
while (!stack.empty()) {
|
2019-01-12 09:21:21 +00:00
|
|
|
if (shouldStop.get()) {
|
2020-07-30 10:55:19 +00:00
|
|
|
Log.d(Config.LOGTAG, "file observer received command to stop");
|
2019-01-12 09:21:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 10:55:19 +00:00
|
|
|
final String parent = stack.pop();
|
2016-07-23 14:12:45 +00:00
|
|
|
final File path = new File(parent);
|
2020-07-30 10:55:19 +00:00
|
|
|
mObservers.add(new SingleFileObserver(path, MASK));
|
2016-07-23 14:12:45 +00:00
|
|
|
final File[] files = path.listFiles();
|
2020-07-30 10:55:19 +00:00
|
|
|
for (final File file : (files == null ? new File[0] : files)) {
|
2019-01-12 09:21:21 +00:00
|
|
|
if (shouldStop.get()) {
|
2020-07-30 10:55:19 +00:00
|
|
|
Log.d(Config.LOGTAG, "file observer received command to stop");
|
2019-01-12 09:21:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-05 17:12:44 +00:00
|
|
|
if (file.isDirectory() && file.getName().charAt(0) != '.') {
|
2016-08-13 10:35:10 +00:00
|
|
|
final String currentPath = file.getAbsolutePath();
|
2020-07-30 10:55:19 +00:00
|
|
|
if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(file)) {
|
2016-08-13 10:35:10 +00:00
|
|
|
stack.push(currentPath);
|
|
|
|
}
|
2016-07-23 14:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 10:55:19 +00:00
|
|
|
for (FileObserver observer : mObservers) {
|
2016-07-23 14:12:45 +00:00
|
|
|
observer.startWatching();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 10:35:10 +00:00
|
|
|
private static int depth(File file) {
|
|
|
|
int depth = 0;
|
2020-07-30 10:55:19 +00:00
|
|
|
while ((file = file.getParentFile()) != null) {
|
2016-08-13 10:35:10 +00:00
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2020-07-30 10:55:19 +00:00
|
|
|
private boolean observing(final File path) {
|
|
|
|
for (final SingleFileObserver observer : mObservers) {
|
|
|
|
if (path.equals(observer.path)) {
|
2016-08-13 10:35:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:21:21 +00:00
|
|
|
public void stopWatching() {
|
|
|
|
shouldStop.set(true);
|
|
|
|
stopWatchingInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void stopWatchingInternal() {
|
2020-07-30 10:55:19 +00:00
|
|
|
for (FileObserver observer : mObservers) {
|
2016-07-23 14:12:45 +00:00
|
|
|
observer.stopWatching();
|
|
|
|
}
|
|
|
|
mObservers.clear();
|
|
|
|
}
|
|
|
|
|
2020-07-30 10:55:19 +00:00
|
|
|
abstract public void onEvent(final int event, File path);
|
2016-07-23 14:12:45 +00:00
|
|
|
|
2018-06-24 18:54:01 +00:00
|
|
|
public void restartWatching() {
|
|
|
|
stopWatching();
|
|
|
|
startWatching();
|
|
|
|
}
|
|
|
|
|
2016-07-23 14:12:45 +00:00
|
|
|
private class SingleFileObserver extends FileObserver {
|
2020-07-30 10:55:19 +00:00
|
|
|
private final File path;
|
2016-07-23 14:12:45 +00:00
|
|
|
|
2020-07-30 10:55:19 +00:00
|
|
|
SingleFileObserver(final File path, final int mask) {
|
|
|
|
super(path.getAbsolutePath(), mask);
|
2016-07-23 14:12:45 +00:00
|
|
|
this.path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-30 10:55:19 +00:00
|
|
|
public void onEvent(final int event, final String filename) {
|
2018-09-05 19:37:05 +00:00
|
|
|
if (filename == null) {
|
2020-07-30 10:55:19 +00:00
|
|
|
Log.d(Config.LOGTAG, "ignored file event with NULL filename (event=" + event + ")");
|
2018-09-05 19:37:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 10:55:19 +00:00
|
|
|
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);
|
|
|
|
});
|
2016-07-23 14:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|