Preparation for persistance

This commit is contained in:
Mario Danic 2017-02-05 03:22:48 +01:00 committed by AndyScherzinger
parent 9e44de9896
commit a24d14f57c
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
2 changed files with 30 additions and 64 deletions

View file

@ -1,62 +0,0 @@
/**
* Nextcloud Android client application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* 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.
*
* 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.
*
* 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 org.apache.commons.io.IOCase;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.io.monitor.FileEntry;
import java.io.File;
import java.io.FileFilter;
/**
* Magical file observer
*/
public class FileAlterationMagicObserver extends FileAlterationObserver {
public FileAlterationMagicObserver(String directoryName) {
super(directoryName);
}
public FileAlterationMagicObserver(String directoryName, FileFilter fileFilter) {
super(directoryName, fileFilter);
}
public FileAlterationMagicObserver(String directoryName, FileFilter fileFilter, IOCase caseSensitivity) {
super(directoryName, fileFilter, caseSensitivity);
}
public FileAlterationMagicObserver(File directory) {
super(directory);
}
public FileAlterationMagicObserver(File directory, FileFilter fileFilter) {
super(directory, fileFilter);
}
public FileAlterationMagicObserver(File directory, FileFilter fileFilter, IOCase caseSensitivity) {
super(directory, fileFilter, caseSensitivity);
}
public FileAlterationMagicObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase caseSensitivity) {
super(rootEntry, fileFilter, caseSensitivity);
}
}

View file

@ -23,9 +23,11 @@
package com.owncloud.android.services.observer;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.util.Pair;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.SyncedFolder;
@ -39,7 +41,12 @@ import org.apache.commons.io.monitor.FileEntry;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
public class SyncedFolderObserverService extends Service {
@ -58,6 +65,7 @@ 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) {
@ -67,18 +75,22 @@ public class SyncedFolderObserverService extends Service {
FileEntry rootEntry;
FileOutputStream fos = null;
ArrayList<Pair<SyncedFolder, FileEntry>> pairArrayList = new ArrayList<>();
Log_OC.d(TAG, "start");
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
if (syncedFolder.isEnabled() && !syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath());
FileAlterationObserver observer = new FileAlterationObserver(syncedFolder.getLocalPath(), fileFilter);
Field f = null;
Field f;
try {
observer.initialize();
f = observer.getClass().getDeclaredField("rootEntry");
f.setAccessible(true);
rootEntry = (FileEntry) f.get(observer);
observer = new FileAlterationMagicObserver(rootEntry, fileFilter, null);
Pair<SyncedFolder, FileEntry> pair = new Pair<>(syncedFolder, rootEntry);
pairArrayList.add(pair);
} catch (NoSuchFieldException e) {
Log_OC.d(TAG, "Failed getting private field rootEntry via NoSuchFieldException");
} catch (IllegalAccessException e) {
@ -93,6 +105,22 @@ public class SyncedFolderObserverService extends Service {
}
}
try {
fos = MainApp.getAppContext().openFileOutput("nc_sync_persistance.persist", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
for (int i = 0; i < pairArrayList.size(); i++) {
os.writeObject(pairArrayList.get(i));
}
os.close();
fos.close();
} catch (FileNotFoundException e) {
Log_OC.d(TAG, "Failed writing to nc_sync_persistance file via FileNotFound");
} catch (IOException e) {
Log_OC.d(TAG, "Failed writing to nc_sync_persistance file via IOException");
}
try {
monitor.start();
} catch (Exception e) {