1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-25 22:01:16 +00:00

Delay file scan events (fixes #857)

This commit is contained in:
Felix Ableitner 2017-12-10 17:17:56 +09:00
parent 9d1d954807
commit 531f1c4041
3 changed files with 19 additions and 11 deletions

View file

@ -92,6 +92,7 @@ public abstract class ApiRequest {
*/
void connect(int requestMethod, Uri uri, @Nullable String requestBody,
@Nullable OnSuccessListener listener, @Nullable OnErrorListener errorListener) {
Log.v(TAG, "Performing request to " + uri.toString());
StringRequest request = new StringRequest(requestMethod, uri.toString(), reply -> {
if (listener != null)
listener.onSuccess(reply);

View file

@ -4,6 +4,7 @@ import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
@ -215,10 +216,11 @@ public class SyncthingService extends Service {
private void onSyncthingStarted() {
onApiChange(State.ACTIVE);
Handler handler = new Handler();
new Thread(() -> {
for (Folder r : mApi.getFolders()) {
try {
mObservers.add(new FolderObserver(mApi, r));
mObservers.add(new FolderObserver(mApi, r, handler));
} catch (FolderObserver.FolderNotExistingException e) {
Log.w(TAG, "Failed to add observer for folder", e);
} catch (StackOverflowError e) {

View file

@ -1,6 +1,7 @@
package com.nutomic.syncthingandroid.util;
import android.os.FileObserver;
import android.os.Handler;
import android.util.Log;
import com.annimon.stream.Stream;
@ -8,6 +9,7 @@ import com.nutomic.syncthingandroid.model.Folder;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
* Recursively watches a directory and all subfolders.
@ -16,21 +18,21 @@ public class FolderObserver extends FileObserver {
private static final String TAG = "FolderObserver";
private static final long SCAN_DELAY_MILLIS = TimeUnit.SECONDS.toMillis(10);
private final OnFolderFileChangeListener mListener;
private final Folder mFolder;
private final String mPath;
private final ArrayList<FolderObserver> mChilds = new ArrayList<>();
private final Handler mHandler;
public interface OnFolderFileChangeListener {
void onFolderFileChange(String folderId, String relativePath);
}
public FolderObserver(OnFolderFileChangeListener listener, Folder folder)
public FolderObserver(OnFolderFileChangeListener listener, Folder folder, Handler mainHandler)
throws FolderNotExistingException {
this(listener, folder, "");
this(listener, folder, "", mainHandler);
Log.i(TAG, "Observer created for (folder " + folder.id + ")");
}
@ -55,7 +57,7 @@ public class FolderObserver extends FileObserver {
* @param folder The folder where this folder belongs to.
* @param path path to the monitored folder, relative to folder root.
*/
private FolderObserver(OnFolderFileChangeListener listener, Folder folder, String path)
private FolderObserver(OnFolderFileChangeListener listener, Folder folder, String path, Handler handler)
throws FolderNotExistingException {
super(folder.path + "/" + path,
ATTRIB | CLOSE_WRITE | CREATE | DELETE | DELETE_SELF | MOVED_FROM |
@ -63,6 +65,7 @@ public class FolderObserver extends FileObserver {
mListener = listener;
mFolder = folder;
mPath = path;
mHandler = handler;
Log.v(TAG, "Observer created for " + new File(mFolder.path, mPath).toString() + " (folder " + folder.id + ")");
startWatching();
@ -74,7 +77,7 @@ public class FolderObserver extends FileObserver {
if (directories != null) {
for (File f : directories) {
mChilds.add(new FolderObserver(mListener, mFolder, path + "/" + f.getName()));
mChilds.add(new FolderObserver(mListener, mFolder, path + "/" + f.getName(), mHandler));
}
}
}
@ -107,21 +110,23 @@ public class FolderObserver extends FileObserver {
break;
}
}
mListener.onFolderFileChange(mFolder.id, fullPath.getPath());
mHandler.postDelayed(() -> mListener.onFolderFileChange(mFolder.id, fullPath.getPath()),
SCAN_DELAY_MILLIS);
break;
case MOVED_TO:
// fall through
case CREATE:
if (fullPath.isDirectory()) {
try {
mChilds.add(new FolderObserver(mListener, mFolder, path));
mChilds.add(new FolderObserver(mListener, mFolder, path, mHandler));
} catch (FolderNotExistingException e) {
Log.w(TAG, "Failed to add listener for nonexisting folder", e);
}
}
// fall through
default:
mListener.onFolderFileChange(mFolder.id, fullPath.getPath());
mHandler.postDelayed(() -> mListener.onFolderFileChange(mFolder.id, fullPath.getPath()),
SCAN_DELAY_MILLIS);
}
}