1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 07:41:17 +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, void connect(int requestMethod, Uri uri, @Nullable String requestBody,
@Nullable OnSuccessListener listener, @Nullable OnErrorListener errorListener) { @Nullable OnSuccessListener listener, @Nullable OnErrorListener errorListener) {
Log.v(TAG, "Performing request to " + uri.toString());
StringRequest request = new StringRequest(requestMethod, uri.toString(), reply -> { StringRequest request = new StringRequest(requestMethod, uri.toString(), reply -> {
if (listener != null) if (listener != null)
listener.onSuccess(reply); listener.onSuccess(reply);

View file

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

View file

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