Do not crash if folder does not exist (fixes #148).

This commit is contained in:
Felix Ableitner 2014-10-10 14:00:13 +03:00
parent 48188b845c
commit 47cc76a9fd
2 changed files with 24 additions and 2 deletions

View File

@ -270,7 +270,11 @@ public class SyncthingService extends Service {
public void onApiAvailable() {
onApiChange();
for (RestApi.Folder r : mApi.getFolders()) {
mObservers.add(new FolderObserver(mApi, r));
try {
mObservers.add(new FolderObserver(mApi, r));
} catch (FolderObserver.FolderNotExistingException e) {
Log.w(TAG, e.getMessage());
}
}
}
});

View File

@ -32,6 +32,20 @@ public class FolderObserver extends FileObserver {
this(listener, folder, "");
}
public class FolderNotExistingException extends RuntimeException {
private String mPath;
public FolderNotExistingException(String path) {
mPath = path;
}
@Override
public String getMessage() {
return "Path " + mPath + " does not exist, aborting file observer";
}
}
/**
* Constructs watcher and starts watching the given directory recursively.
*
@ -49,7 +63,11 @@ public class FolderObserver extends FileObserver {
Log.v(TAG, "observer created for " + path + " in " + folder.ID);
startWatching();
File[] directories = new File(folder.Path, path).listFiles(new FilenameFilter() {
File currentFolder = new File(folder.Path, path);
if (!currentFolder.exists()) {
throw new FolderNotExistingException(currentFolder.getAbsolutePath());
}
File[] directories = currentFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();