Add persistence layer on service start

This commit is contained in:
Mario Danic 2017-02-05 10:19:55 +01:00 committed by AndyScherzinger
parent 8a978a28ce
commit 2c92eafdda
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
3 changed files with 77 additions and 20 deletions

View file

@ -59,6 +59,7 @@
<application <application
android:name=".MainApp" android:name=".MainApp"
android:fullBackupContent="@xml/my_backup_rules"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/Theme.ownCloud.Toolbar" android:theme="@style/Theme.ownCloud.Toolbar"

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="external" path="nc_persistence" />
</full-backup-content>

View file

@ -35,15 +35,18 @@ import com.owncloud.android.datamodel.SyncedFolderProvider;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.services.FileAlterationMagicListener; import com.owncloud.android.services.FileAlterationMagicListener;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor; import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver; import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.io.monitor.FileEntry; import org.apache.commons.io.monitor.FileEntry;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -72,39 +75,88 @@ public class SyncedFolderObserverService extends Service {
} }
}; };
File file = new File(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath() + "/nc_persistence");
FileOutputStream fos = null; FileOutputStream fos = null;
boolean readPerstistanceEntries = false;
ArrayList<Pair<SyncedFolder, FileEntry>> pairArrayList = new ArrayList<>(); ArrayList<Pair<SyncedFolder, FileEntry>> pairArrayList = new ArrayList<>();
Log_OC.d(TAG, "start"); if (file.exists() ) {
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) { FileInputStream fis = null;
if (syncedFolder.isEnabled() && !syncedFolderMap.containsKey(syncedFolder.getLocalPath())) { try {
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath()); fis = new FileInputStream(file);
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(new File( ObjectInputStream ois = new ObjectInputStream(fis);
syncedFolder.getLocalPath()), fileFilter); pairArrayList = (ArrayList<Pair<SyncedFolder, FileEntry>>)ois.readObject();
readPerstistanceEntries = true;
} catch (FileNotFoundException e) {
Log_OC.d(TAG, "Failed with FileNotFound while reading persistence file");
} catch (IOException e) {
Log_OC.d(TAG, "Failed with IOException while reading persistence file");
} catch (ClassNotFoundException e) {
Log_OC.d(TAG, "Failed with ClassNotFound while reading persistence file");
} finally {
try { try {
observer.init(); if (fis != null) {
Pair<SyncedFolder, FileEntry> pair = new Pair<>(syncedFolder, observer.getRootEntry()); fis.close();
pairArrayList.add(pair); }
} catch (Exception e) { } catch (IOException e) {
Log_OC.d(TAG, "Failed getting an observer to intialize"); Log_OC.d(TAG, "Failed with closing FIS");
} }
}
observer.addListener(new FileAlterationMagicListener(syncedFolder)); }
Log_OC.d(TAG, "start");
if (pairArrayList.size() == 0) {
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
if (syncedFolder.isEnabled() && !syncedFolderMap.containsKey(syncedFolder.getLocalPath())) {
Log_OC.d(TAG, "start observer: " + syncedFolder.getLocalPath());
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(new File(
syncedFolder.getLocalPath()), fileFilter);
try {
observer.init();
Pair<SyncedFolder, FileEntry> pair = new Pair<>(syncedFolder, observer.getRootEntry());
pairArrayList.add(pair);
} catch (Exception e) {
Log_OC.d(TAG, "Failed getting an observer to intialize");
}
observer.addListener(new FileAlterationMagicListener(syncedFolder));
monitor.addObserver(observer);
syncedFolderMap.put(syncedFolder.getLocalPath(), observer);
}
}
} else {
for(int i = 0; i < pairArrayList.size(); i++) {
SyncedFolder syncFolder = pairArrayList.get(i).first;
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(new File(
syncFolder.getLocalPath()), fileFilter);
observer.setRootEntry(pairArrayList.get(i).second);
observer.addListener(new FileAlterationMagicListener(syncFolder));
monitor.addObserver(observer); monitor.addObserver(observer);
syncedFolderMap.put(syncedFolder.getLocalPath(), observer); syncedFolderMap.put(syncFolder.getLocalPath(), observer);
} }
} }
try { try {
fos = MainApp.getAppContext().openFileOutput("nc_sync_persistance.persist", Context.MODE_PRIVATE); if (pairArrayList.size() > 0 && !readPerstistanceEntries) {
ObjectOutputStream os = new ObjectOutputStream(fos); fos = MainApp.getAppContext().openFileOutput(file.getAbsolutePath(), Context.MODE_PRIVATE);
for (int i = 0; i < pairArrayList.size(); i++) { ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(pairArrayList.get(i)); for (int i = 0; i < pairArrayList.size(); i++) {
os.writeObject(pairArrayList.get(i));
}
os.close();
} else if (file.exists() && pairArrayList.size() == 0) {
FileUtils.deleteQuietly(file);
} }
os.close();
fos.close(); if (fos != null) {
fos.close();
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log_OC.d(TAG, "Failed writing to nc_sync_persistance file via FileNotFound"); Log_OC.d(TAG, "Failed writing to nc_sync_persistance file via FileNotFound");
} catch (IOException e) { } catch (IOException e) {