mirror of
https://github.com/nextcloud/android.git
synced 2024-11-23 13:45:35 +03:00
Preparation for persistance
This commit is contained in:
parent
9e44de9896
commit
a24d14f57c
2 changed files with 30 additions and 64 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue