Simplify code

This commit is contained in:
Mario Danic 2017-02-14 12:21:11 +01:00 committed by AndyScherzinger
parent 79fb4e884b
commit 623a48328d
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
2 changed files with 1 additions and 126 deletions

View file

@ -186,9 +186,6 @@ public class MainApp extends Application {
public void onActivityStopped(Activity activity) {
Log_OC.d(activity.getClass().getSimpleName(), "onStop() ending");
PassCodeManager.getPassCodeManager().onActivityStopped(activity);
if (MainApp.getSyncedFolderObserverService() != null) {
MainApp.getSyncedFolderObserverService().syncToDisk(false);
}
}
@Override

View file

@ -34,21 +34,13 @@ 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.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.io.monitor.FileEntry;
import java.io.EOFException;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.CopyOnWriteArrayList;
public class SyncedFolderObserverService extends Service {
@ -72,64 +64,7 @@ public class SyncedFolderObserverService extends Service {
}
};
file = new File(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath() + File.separator +
"nc_persistence");
boolean readPerstistanceEntries = false;
if (file.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
boolean cont = true;
while (cont) {
Object obj = ois.readObject();
if (obj != null)
pairArrayList.add((SerializablePair<SyncedFolder, FileEntry>) obj);
else
cont = false;
}
readPerstistanceEntries = true;
} catch (FileNotFoundException e) {
Log_OC.d(TAG, "Failed with FileNotFound while reading persistence file");
} catch (EOFException e) {
Log_OC.d(TAG, "Failed with EOFException while reading persistence file");
readPerstistanceEntries = true;
} 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 {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
Log_OC.d(TAG, "Failed with closing FIS");
}
}
}
if (readPerstistanceEntries && pairArrayList.size() > 0) {
for (int i = 0; i < pairArrayList.size(); i++) {
SyncedFolder syncFolder = pairArrayList.get(i).getKey();
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
if (syncedFolder.getId() == pairArrayList.get(i).getKey().getId()) {
syncFolder = syncedFolder;
break;
}
}
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(syncFolder, fileFilter);
observer.setRootEntry(pairArrayList.get(i).getValue());
observer.addListener(new FileAlterationMagicListener(syncFolder));
monitor.addObserver(observer);
}
} else {
for (SyncedFolder syncedFolder : mProvider.getSyncedFolders()) {
if (syncedFolder.isEnabled()) {
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(syncedFolder, fileFilter);
@ -144,9 +79,8 @@ public class SyncedFolderObserverService extends Service {
monitor.addObserver(observer);
}
}
}
//}
syncToDisk(false);
try {
monitor.start();
@ -161,60 +95,6 @@ public class SyncedFolderObserverService extends Service {
return Service.START_NOT_STICKY;
}
public void syncToDisk(boolean destructive) {
pairArrayList = new CopyOnWriteArrayList<>();
for (FileAlterationObserver fileAlterationObserver : monitor.getObservers()) {
FileAlterationMagicObserver fileAlterationMagicObserver =
(FileAlterationMagicObserver) fileAlterationObserver;
pairArrayList.add(new SerializablePair<SyncedFolder, FileEntry>(fileAlterationMagicObserver.getSyncedFolder(),
fileAlterationMagicObserver.getRootEntry()));
}
if (pairArrayList.size() > 0) {
try {
File newFile = new File(file.getAbsolutePath());
if (!newFile.exists()) {
newFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(new File(file.getAbsolutePath()), false);
ObjectOutputStream os = new ObjectOutputStream(fos);
for (int i = 0; i < pairArrayList.size(); i++) {
os.writeObject(pairArrayList.get(i));
}
os.close();
if (fos != null) {
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_persisten file via IOException");
}
} else {
if (file.exists()) {
FileUtils.deleteQuietly(file);
}
}
if (destructive) {
monitor.removeObserver(null);
try {
monitor.stop();
} catch (Exception e) {
Log_OC.d(TAG, "Failed in stopping monitor");
}
}
}
@Override
public void onDestroy() {
syncToDisk(true);
}
/**
* Restart oberver if it is enabled
* If syncedFolder exists already, use it, otherwise create new observer
@ -254,8 +134,6 @@ public class SyncedFolderObserverService extends Service {
monitor.addObserver(fileAlterationMagicObserver);
}
syncToDisk(false);
}
@Override