|
@@ -1,3 +1,24 @@
|
|
|
+/**
|
|
|
+ * Nextcloud Android client application
|
|
|
+ *
|
|
|
+ * @author Mario Danic
|
|
|
+ * At least some parts are Copyright (C) 2017 Mario Danic
|
|
|
+ * Those parts are under the following licence:
|
|
|
+ * <p>
|
|
|
+ * This program is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU Affero General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * at your option) any later version.
|
|
|
+ * <p>
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU Affero General Public License for more details.
|
|
|
+ * <p>
|
|
|
+ * You should have received a copy of the GNU Affero General Public License
|
|
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
+ */
|
|
|
+
|
|
|
package com.owncloud.android.services.observer;
|
|
|
|
|
|
import android.app.Service;
|
|
@@ -9,14 +30,22 @@ import com.owncloud.android.MainApp;
|
|
|
import com.owncloud.android.datamodel.SyncedFolder;
|
|
|
import com.owncloud.android.datamodel.SyncedFolderProvider;
|
|
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
+import com.owncloud.android.services.FileAlterationMagicListener;
|
|
|
+
|
|
|
+import org.apache.commons.io.monitor.FileAlterationMonitor;
|
|
|
+import org.apache.commons.io.monitor.FileAlterationObserver;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileFilter;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
public class SyncedFolderObserverService extends Service {
|
|
|
private static final String TAG = "SyncedFolderObserverService";
|
|
|
private SyncedFolderProvider mProvider;
|
|
|
- private HashMap<String, SyncedFolderObserver> syncedFolderMap = new HashMap<>();
|
|
|
+ private HashMap<String, FileAlterationObserver> syncedFolderMap = new HashMap<>();
|
|
|
private final IBinder mBinder = new SyncedFolderObserverBinder();
|
|
|
+ private FileAlterationMonitor monitor;
|
|
|
+ private FileFilter fileFilter;
|
|
|
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
@@ -25,13 +54,32 @@ public class SyncedFolderObserverService extends Service {
|
|
|
|
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ monitor = new FileAlterationMonitor();
|
|
|
+ fileFilter = new FileFilter() {
|
|
|
+ @Override
|
|
|
+ public boolean accept(File pathname) {
|
|
|
+ if (pathname.getName().startsWith(".")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ };
|
|
|
Log_OC.d(TAG, "start");
|
|
|
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
|
|
|
- if (syncedFolder.isEnabled()) {
|
|
|
+ if (syncedFolder.isEnabled() && !syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
|
|
|
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath());
|
|
|
- SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder);
|
|
|
- observer.startWatching();
|
|
|
- syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
|
|
|
+
|
|
|
+ FileAlterationObserver observer = new FileAlterationObserver(syncedFolder.getLocalPath(), fileFilter);
|
|
|
+ observer.addListener(new FileAlterationMagicListener(syncedFolder));
|
|
|
+ monitor.addObserver(observer);
|
|
|
+ try {
|
|
|
+ monitor.start();
|
|
|
+ syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log_OC.d(TAG, "Something went very wrong at onStartCommand");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -40,8 +88,13 @@ public class SyncedFolderObserverService extends Service {
|
|
|
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
- for (SyncedFolderObserver observer : syncedFolderMap.values()) {
|
|
|
- observer.stopWatching();
|
|
|
+ for (FileAlterationObserver observer : syncedFolderMap.values()) {
|
|
|
+ monitor.removeObserver(observer);
|
|
|
+ try {
|
|
|
+ observer.destroy();
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log_OC.d(TAG, "Something went very wrong at onDestroy");
|
|
|
+ }
|
|
|
syncedFolderMap.remove(observer);
|
|
|
}
|
|
|
}
|
|
@@ -49,23 +102,42 @@ public class SyncedFolderObserverService extends Service {
|
|
|
/**
|
|
|
* Restart oberver if it is enabled
|
|
|
* If syncedFolder exists already, use it, otherwise create new observer
|
|
|
+ *
|
|
|
* @param syncedFolder
|
|
|
*/
|
|
|
- public void restartObserver(SyncedFolder syncedFolder){
|
|
|
+
|
|
|
+ public void restartObserver(SyncedFolder syncedFolder) {
|
|
|
+ FileAlterationObserver fileAlterationObserver;
|
|
|
if (syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
|
|
|
Log_OC.d(TAG, "stop observer: " + syncedFolder.getLocalPath());
|
|
|
- syncedFolderMap.get(syncedFolder.getLocalPath()).stopWatching();
|
|
|
+ fileAlterationObserver = syncedFolderMap.get(syncedFolder.getLocalPath());
|
|
|
+ monitor.removeObserver(fileAlterationObserver);
|
|
|
+ try {
|
|
|
+ fileAlterationObserver.destroy();
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log_OC.d(TAG, "Something went very wrong at onDestroy");
|
|
|
+ }
|
|
|
syncedFolderMap.remove(syncedFolder.getLocalPath());
|
|
|
}
|
|
|
|
|
|
if (syncedFolder.isEnabled()) {
|
|
|
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath());
|
|
|
if (syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
|
|
|
- syncedFolderMap.get(syncedFolder.getLocalPath()).startWatching();
|
|
|
+ fileAlterationObserver = syncedFolderMap.get(syncedFolder.getLocalPath());
|
|
|
+ if (fileAlterationObserver.getListeners() == null) {
|
|
|
+ fileAlterationObserver.addListener(new FileAlterationMagicListener(syncedFolder));
|
|
|
+ }
|
|
|
+ monitor.addObserver(fileAlterationObserver);
|
|
|
} else {
|
|
|
- SyncedFolderObserver observer = new SyncedFolderObserver(syncedFolder);
|
|
|
- observer.startWatching();
|
|
|
- syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
|
|
|
+ fileAlterationObserver = new FileAlterationObserver(syncedFolder.getLocalPath(), fileFilter);
|
|
|
+ fileAlterationObserver.addListener(new FileAlterationMagicListener(syncedFolder));
|
|
|
+ monitor.addObserver(fileAlterationObserver);
|
|
|
+ try {
|
|
|
+ syncedFolderMap.put(syncedFolder.getLocalPath(), fileAlterationObserver);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log_OC.d(TAG, "Something went very wrong on RestartObserver");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|