mirror of
https://github.com/nextcloud/android.git
synced 2024-11-23 13:45:35 +03:00
parent
c1f896a07a
commit
deb35e5b9f
2 changed files with 37 additions and 49 deletions
|
@ -19,6 +19,8 @@
|
||||||
*/
|
*/
|
||||||
package com.owncloud.android.datamodel;
|
package com.owncloud.android.datamodel;
|
||||||
|
|
||||||
|
import android.support.v4.util.Pair;
|
||||||
|
|
||||||
import org.apache.commons.io.monitor.FileEntry;
|
import org.apache.commons.io.monitor.FileEntry;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -27,40 +29,23 @@ import java.io.Serializable;
|
||||||
* Pair that we can serialize
|
* Pair that we can serialize
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class RealPair implements Serializable {
|
public class SerializablePair<S, F> implements Serializable {
|
||||||
private static final long serialVersionUID = 3823582869520000090L;
|
private transient Pair pair = null;
|
||||||
|
|
||||||
private SyncedFolder syncedFolder;
|
|
||||||
private FileEntry fileEntry;
|
|
||||||
|
|
||||||
public RealPair() {
|
|
||||||
|
|
||||||
|
public SerializablePair(SyncedFolder key, FileEntry value) {
|
||||||
|
this.pair = new Pair(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RealPair(SyncedFolder syncedFolder, FileEntry fileEntry) {
|
@Override
|
||||||
|
public String toString() {
|
||||||
this.syncedFolder = syncedFolder;
|
return pair.toString();
|
||||||
this.fileEntry = fileEntry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long getSerialVersionUID() {
|
public SyncedFolder getKey() {
|
||||||
return serialVersionUID;
|
return (SyncedFolder)this.pair.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SyncedFolder getSyncedFolder() {
|
public FileEntry getValue() {
|
||||||
return syncedFolder;
|
return (FileEntry)this.pair.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSyncedFolder(SyncedFolder syncedFolder) {
|
|
||||||
this.syncedFolder = syncedFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FileEntry getFileEntry() {
|
|
||||||
return fileEntry;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFileEntry(FileEntry fileEntry) {
|
|
||||||
this.fileEntry = fileEntry;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -28,7 +28,7 @@ import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import com.owncloud.android.MainApp;
|
import com.owncloud.android.MainApp;
|
||||||
import com.owncloud.android.datamodel.RealPair;
|
import com.owncloud.android.datamodel.SerializablePair;
|
||||||
import com.owncloud.android.datamodel.SyncedFolder;
|
import com.owncloud.android.datamodel.SyncedFolder;
|
||||||
import com.owncloud.android.datamodel.SyncedFolderProvider;
|
import com.owncloud.android.datamodel.SyncedFolderProvider;
|
||||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||||
|
@ -36,6 +36,7 @@ import com.owncloud.android.services.FileAlterationMagicListener;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
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.FileEntry;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileFilter;
|
import java.io.FileFilter;
|
||||||
|
@ -55,7 +56,7 @@ public class SyncedFolderObserverService extends Service {
|
||||||
private final IBinder mBinder = new SyncedFolderObserverBinder();
|
private final IBinder mBinder = new SyncedFolderObserverBinder();
|
||||||
private FileAlterationMonitor monitor;
|
private FileAlterationMonitor monitor;
|
||||||
private FileFilter fileFilter;
|
private FileFilter fileFilter;
|
||||||
private CopyOnWriteArrayList<RealPair> pairArrayList = new CopyOnWriteArrayList<>();
|
private CopyOnWriteArrayList<SerializablePair<SyncedFolder, FileEntry>> pairArrayList = new CopyOnWriteArrayList<>();
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -84,7 +85,7 @@ public class SyncedFolderObserverService extends Service {
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(file);
|
fis = new FileInputStream(file);
|
||||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||||
pairArrayList = (CopyOnWriteArrayList<RealPair>)ois.readObject();
|
pairArrayList = (CopyOnWriteArrayList<SerializablePair<SyncedFolder, FileEntry>>)ois.readObject();
|
||||||
readPerstistanceEntries = true;
|
readPerstistanceEntries = true;
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log_OC.d(TAG, "Failed with FileNotFound while reading persistence file");
|
Log_OC.d(TAG, "Failed with FileNotFound while reading persistence file");
|
||||||
|
@ -114,7 +115,8 @@ public class SyncedFolderObserverService extends Service {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
observer.init();
|
observer.init();
|
||||||
RealPair pair = new RealPair(syncedFolder, observer.getRootEntry());
|
SerializablePair<SyncedFolder, FileEntry> pair = new SerializablePair<>(syncedFolder,
|
||||||
|
observer.getRootEntry());
|
||||||
pairArrayList.add(pair);
|
pairArrayList.add(pair);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.d(TAG, "Failed getting an observer to intialize");
|
Log_OC.d(TAG, "Failed getting an observer to intialize");
|
||||||
|
@ -127,10 +129,10 @@ public class SyncedFolderObserverService extends Service {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(int i = 0; i < pairArrayList.size(); i++) {
|
for(int i = 0; i < pairArrayList.size(); i++) {
|
||||||
SyncedFolder syncFolder = pairArrayList.get(i).getSyncedFolder();
|
SyncedFolder syncFolder = pairArrayList.get(i).getKey();
|
||||||
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(new File(
|
FileAlterationMagicObserver observer = new FileAlterationMagicObserver(new File(
|
||||||
syncFolder.getLocalPath()), fileFilter);
|
syncFolder.getLocalPath()), fileFilter);
|
||||||
observer.setRootEntry(pairArrayList.get(i).getFileEntry());
|
observer.setRootEntry(pairArrayList.get(i).getValue());
|
||||||
|
|
||||||
observer.addListener(new FileAlterationMagicListener(syncFolder));
|
observer.addListener(new FileAlterationMagicListener(syncFolder));
|
||||||
monitor.addObserver(observer);
|
monitor.addObserver(observer);
|
||||||
|
@ -153,15 +155,19 @@ public class SyncedFolderObserverService extends Service {
|
||||||
|
|
||||||
private void writePersistenceEntries(boolean readPerstistanceEntries, File file) {
|
private void writePersistenceEntries(boolean readPerstistanceEntries, File file) {
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
ObjectOutputStream os = null;
|
|
||||||
try {
|
try {
|
||||||
if (pairArrayList.size() > 0 && !readPerstistanceEntries) {
|
if (pairArrayList.size() > 0 && !readPerstistanceEntries) {
|
||||||
if (!file.exists()) {
|
File newFile = new File(file.getAbsolutePath());
|
||||||
file.createNewFile();
|
if (!newFile.exists()) {
|
||||||
|
newFile.createNewFile();
|
||||||
}
|
}
|
||||||
fos = FileUtils.openOutputStream(file, false);
|
fos = new FileOutputStream (new File(file.getAbsolutePath()), false);
|
||||||
os = new ObjectOutputStream(fos);
|
ObjectOutputStream os = new ObjectOutputStream(fos);
|
||||||
os.writeObject(pairArrayList);
|
for (int i = 0; i < pairArrayList.size(); i++) {
|
||||||
|
os.writeObject(pairArrayList.get(i));
|
||||||
|
}
|
||||||
|
os.close();
|
||||||
} else if (file.exists() && pairArrayList.size() == 0) {
|
} else if (file.exists() && pairArrayList.size() == 0) {
|
||||||
FileUtils.deleteQuietly(file);
|
FileUtils.deleteQuietly(file);
|
||||||
}
|
}
|
||||||
|
@ -170,11 +176,6 @@ public class SyncedFolderObserverService extends Service {
|
||||||
fos.close();
|
fos.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (os != null) {
|
|
||||||
os.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) {
|
||||||
|
@ -189,9 +190,10 @@ public class SyncedFolderObserverService extends Service {
|
||||||
for (SyncedFolder syncedFolder : syncedFolderMap.keySet()) {
|
for (SyncedFolder syncedFolder : syncedFolderMap.keySet()) {
|
||||||
FileAlterationMagicObserver obs = syncedFolderMap.get(syncedFolder);
|
FileAlterationMagicObserver obs = syncedFolderMap.get(syncedFolder);
|
||||||
for (int i = 0; i < pairArrayList.size(); i++) {
|
for (int i = 0; i < pairArrayList.size(); i++) {
|
||||||
SyncedFolder pairSyncedFolder = pairArrayList.get(i).getSyncedFolder();
|
SyncedFolder pairSyncedFolder = pairArrayList.get(i).getKey();
|
||||||
if (pairSyncedFolder.equals(syncedFolder)) {
|
if (pairSyncedFolder.equals(syncedFolder)) {
|
||||||
RealPair newPairEntry = new RealPair(syncedFolder, obs.getRootEntry());
|
SerializablePair<SyncedFolder, FileEntry> newPairEntry = new SerializablePair<>(syncedFolder,
|
||||||
|
obs.getRootEntry());
|
||||||
pairArrayList.set(i, newPairEntry);
|
pairArrayList.set(i, newPairEntry);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -230,7 +232,7 @@ public class SyncedFolderObserverService extends Service {
|
||||||
|
|
||||||
// remove it from the paired array list
|
// remove it from the paired array list
|
||||||
for (int i = 0; i < pairArrayList.size(); i++) {
|
for (int i = 0; i < pairArrayList.size(); i++) {
|
||||||
if (syncedFolder.equals(pairArrayList.get(i).getSyncedFolder())) {
|
if (syncedFolder.equals(pairArrayList.get(i).getKey())) {
|
||||||
pairArrayList.remove(i);
|
pairArrayList.remove(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -252,7 +254,8 @@ public class SyncedFolderObserverService extends Service {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fileAlterationObserver.init();
|
fileAlterationObserver.init();
|
||||||
RealPair pair = new RealPair(syncedFolder, fileAlterationObserver.getRootEntry());
|
SerializablePair<SyncedFolder, FileEntry> pair = new SerializablePair<>(syncedFolder,
|
||||||
|
fileAlterationObserver.getRootEntry());
|
||||||
pairArrayList.add(pair);
|
pairArrayList.add(pair);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.d(TAG, "Failed getting an observer to intialize");
|
Log_OC.d(TAG, "Failed getting an observer to intialize");
|
||||||
|
|
Loading…
Reference in a new issue