From 98f98fddb97bb52f8365cd1f238324e7b5e29440 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 16 Dec 2015 01:10:16 +0100 Subject: [PATCH] Fixed possible crash if FolderObserver could not be created. --- .../test/util/FolderObserverTest.java | 26 +++++++++++++++---- .../syncthingandroid/util/FolderObserver.java | 14 +++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java index de70fbcf..259693ae 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java @@ -44,12 +44,13 @@ public class FolderObserverTest extends AndroidTestCase private RestApi.Folder createFolder(String id) { RestApi.Folder r = new RestApi.Folder(); - r.path = mTestFolder.getAbsolutePath(); + r.path = mTestFolder.getPath(); r.id = id; return r; } - public void testRecursion() throws IOException, InterruptedException { + public void testRecursion() throws IOException, InterruptedException, + FolderObserver.FolderNotExistingException { mCurrentTest = "testRecursion"; File subFolder = new File(mTestFolder, "subfolder"); subFolder.mkdir(); @@ -63,7 +64,8 @@ public class FolderObserverTest extends AndroidTestCase fo.stopWatching(); } - public void testRemoveFile() throws IOException, InterruptedException { + public void testRemoveFile() throws IOException, InterruptedException, + FolderObserver.FolderNotExistingException { mCurrentTest = "testRemoveFile"; File test = new File(mTestFolder, "test"); test.createNewFile(); @@ -77,7 +79,8 @@ public class FolderObserverTest extends AndroidTestCase fo.stopWatching(); } - public void testMoveDirectoryOut() throws IOException, InterruptedException { + public void testMoveDirectoryOut() throws IOException, InterruptedException, + FolderObserver.FolderNotExistingException { mCurrentTest = "testMoveDirectory"; File subFolder = new File(mTestFolder, "subfolder"); subFolder.mkdir(); @@ -94,7 +97,8 @@ public class FolderObserverTest extends AndroidTestCase fo.stopWatching(); } - public void testAddDirectory() throws IOException, InterruptedException { + public void testAddDirectory() throws IOException, InterruptedException, + FolderObserver.FolderNotExistingException { mCurrentTest = "testAddDirectory"; File subFolder = new File(mTestFolder, "subfolder"); subFolder.mkdir(); @@ -109,4 +113,16 @@ public class FolderObserverTest extends AndroidTestCase 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)); + } + } + } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java b/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java index ab527fd4..7903e93d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java @@ -28,11 +28,12 @@ public class FolderObserver extends FileObserver { 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, ""); } - public class FolderNotExistingException extends RuntimeException { + public class FolderNotExistingException extends Exception { private String mPath; @@ -53,7 +54,8 @@ 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, RestApi.Folder folder, String path) { + private FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder, String path) + throws FolderNotExistingException { super(folder.path + "/" + path, ATTRIB | CLOSE_WRITE | CREATE | DELETE | DELETE_SELF | MOVED_FROM | MOVED_TO | MOVE_SELF); @@ -115,7 +117,11 @@ public class FolderObserver extends FileObserver { // fall through case CREATE: if (fullPath.isDirectory()) { - mChilds.add(new FolderObserver(mListener, mFolder, path)); + try { + mChilds.add(new FolderObserver(mListener, mFolder, path)); + } catch (FolderNotExistingException e) { + Log.w(TAG, "Failed to add listener for nonexisting folder", e); + } } // fall through default: