1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-27 06:41:15 +00:00

Fixed possible crash if FolderObserver could not be created.

This commit is contained in:
Felix Ableitner 2015-12-16 01:10:16 +01:00
parent 0350778db9
commit 98f98fddb9
2 changed files with 31 additions and 9 deletions

View file

@ -44,12 +44,13 @@ public class FolderObserverTest extends AndroidTestCase
private RestApi.Folder createFolder(String id) { private RestApi.Folder createFolder(String id) {
RestApi.Folder r = new RestApi.Folder(); RestApi.Folder r = new RestApi.Folder();
r.path = mTestFolder.getAbsolutePath(); r.path = mTestFolder.getPath();
r.id = id; r.id = id;
return r; return r;
} }
public void testRecursion() throws IOException, InterruptedException { public void testRecursion() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRecursion"; mCurrentTest = "testRecursion";
File subFolder = new File(mTestFolder, "subfolder"); File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir(); subFolder.mkdir();
@ -63,7 +64,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching(); fo.stopWatching();
} }
public void testRemoveFile() throws IOException, InterruptedException { public void testRemoveFile() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRemoveFile"; mCurrentTest = "testRemoveFile";
File test = new File(mTestFolder, "test"); File test = new File(mTestFolder, "test");
test.createNewFile(); test.createNewFile();
@ -77,7 +79,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching(); fo.stopWatching();
} }
public void testMoveDirectoryOut() throws IOException, InterruptedException { public void testMoveDirectoryOut() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testMoveDirectory"; mCurrentTest = "testMoveDirectory";
File subFolder = new File(mTestFolder, "subfolder"); File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir(); subFolder.mkdir();
@ -94,7 +97,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching(); fo.stopWatching();
} }
public void testAddDirectory() throws IOException, InterruptedException { public void testAddDirectory() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testAddDirectory"; mCurrentTest = "testAddDirectory";
File subFolder = new File(mTestFolder, "subfolder"); File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir(); subFolder.mkdir();
@ -109,4 +113,16 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching(); fo.stopWatching();
} }
public void testNotExisting() throws IOException, InterruptedException {
RestApi.Folder r = new RestApi.Folder();
r.path = new File(new MockContext(getContext()).getFilesDir(), "not-existing").getPath();
r.id = "testNotExisting";
try {
new FolderObserver(this, r);
fail("Expected FolderNotExistingException");
} catch (FolderObserver.FolderNotExistingException e) {
assertTrue(e.getMessage().contains(r.path));
}
}
} }

View file

@ -28,11 +28,12 @@ public class FolderObserver extends FileObserver {
public void onFolderFileChange(String folderId, String relativePath); public void onFolderFileChange(String folderId, String relativePath);
} }
public FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder) { public FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder)
throws FolderNotExistingException {
this(listener, folder, ""); this(listener, folder, "");
} }
public class FolderNotExistingException extends RuntimeException { public class FolderNotExistingException extends Exception {
private String mPath; private String mPath;
@ -53,7 +54,8 @@ 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, RestApi.Folder folder, String path) { private FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder, String path)
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 |
MOVED_TO | MOVE_SELF); MOVED_TO | MOVE_SELF);
@ -115,7 +117,11 @@ public class FolderObserver extends FileObserver {
// fall through // fall through
case CREATE: case CREATE:
if (fullPath.isDirectory()) { if (fullPath.isDirectory()) {
try {
mChilds.add(new FolderObserver(mListener, mFolder, path)); mChilds.add(new FolderObserver(mListener, mFolder, path));
} catch (FolderNotExistingException e) {
Log.w(TAG, "Failed to add listener for nonexisting folder", e);
}
} }
// fall through // fall through
default: default: