From 531f1c40414bf7f89240cfe5554f5ae0f40fd95e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 10 Dec 2017 17:17:56 +0900 Subject: [PATCH] Delay file scan events (fixes #857) --- .../syncthingandroid/http/ApiRequest.java | 1 + .../service/SyncthingService.java | 4 ++- .../syncthingandroid/util/FolderObserver.java | 25 +++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java b/app/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java index e7cad0a3..fdce9cf7 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java @@ -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); diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 828b25af..bd778ca4 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -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) { diff --git a/app/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java b/app/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java index 2034603d..e62406c0 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java @@ -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 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); } }