X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/16b5c6b9ad29bee58dec8aa301b21a2992d63d00..7f75f13f0c47b5cc4f5fe6a743a6fb8a9281fef7:/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java diff --git a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java index 230d3a75..d14151a2 100644 --- a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java @@ -19,8 +19,6 @@ package eu.alefzero.owncloud.syncadapter; import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Vector; @@ -37,10 +35,11 @@ import android.content.Intent; import android.content.SyncResult; import android.os.Bundle; import android.util.Log; -import android.webkit.MimeTypeMap; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.services.FileDownloader; import eu.alefzero.webdav.WebdavEntry; +import eu.alefzero.webdav.WebdavUtils; /** * SyncAdapter implementation for syncing sample SyncAdapter contacts to the @@ -52,7 +51,17 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { private final static String TAG = "FileSyncAdapter"; + /* Commented code for ugly performance tests + private final static int MAX_DELAYS = 100; + private static long[] mResponseDelays = new long[MAX_DELAYS]; + private static long[] mSaveDelays = new long[MAX_DELAYS]; + private int mDelaysIndex = 0; + private int mDelaysCount = 0; + */ + private long mCurrentSyncTime; + private boolean mCancellation; + private Account mAccount; public FileSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); @@ -63,14 +72,23 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { String authority, ContentProviderClient provider, SyncResult syncResult) { - this.setAccount(account); + mCancellation = false; + mAccount = account; + + this.setAccount(mAccount); this.setContentProvider(provider); - this.setStorageManager(new FileDataStorageManager(account, + this.setStorageManager(new FileDataStorageManager(mAccount, getContentProvider())); - Log.d(TAG, "syncing owncloud account " + account.name); + /* Commented code for ugly performance tests + mDelaysIndex = 0; + mDelaysCount = 0; + */ + + + Log.d(TAG, "syncing owncloud account " + mAccount.name); - sendStickyBroadcast(true, -1); // starting message to UI + sendStickyBroadcast(true, null); // message to signal the start to the UI PropFindMethod query; try { @@ -85,7 +103,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { OCFile file = fillOCFile(we); file.setParentId(0); getStorageManager().saveFile(file); - fetchData(getUri().toString(), syncResult, file.getFileId()); + if (!mCancellation) { + fetchData(getUri().toString(), syncResult, file.getFileId()); + } } } catch (OperationCanceledException e) { e.printStackTrace(); @@ -98,48 +118,123 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } catch (DavException e) { syncResult.stats.numIoExceptions++; e.printStackTrace(); + } catch (Throwable t) { + // TODO update syncResult + Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t); + t.printStackTrace(); } - sendStickyBroadcast(false, -1); + + /* Commented code for ugly performance tests + long sum = 0, mean = 0, max = 0, min = Long.MAX_VALUE; + for (int i=0; i paths = new ArrayList(); - List fileIds = new ArrayList(); + + // insertion or update of files + List updatedFiles = new Vector(resp.getResponses().length - 1); for (int i = 1; i < resp.getResponses().length; ++i) { WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath()); OCFile file = fillOCFile(we); file.setParentId(parentId); - getStorageManager().saveFile(file); + if (getStorageManager().getFileByPath(file.getRemotePath()) != null && + getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() && + file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath()) + .getModificationTimestamp()) { + Intent intent = new Intent(this.getContext(), FileDownloader.class); + intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); + intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getRemotePath()); + intent.putExtra(FileDownloader.EXTRA_REMOTE_PATH, file.getRemotePath()); + intent.putExtra(FileDownloader.EXTRA_FILE_SIZE, file.getFileLength()); + file.setKeepInSync(true); + getContext().startService(intent); + } + if (getStorageManager().getFileByPath(file.getRemotePath()) != null) + file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync()); + + //getStorageManager().saveFile(file); + updatedFiles.add(file); if (parentId == 0) parentId = file.getFileId(); - if (we.contentType().equals("DIR")) { - // for recursive fetch later - paths.add(we.path()); - fileIds.add(file.getFileId()); - } } + /* Commented code for ugly performance tests + long saveDelay = System.currentTimeMillis(); + */ + getStorageManager().saveFiles(updatedFiles); // all "at once" ; trying to get a best performance in database update + /* Commented code for ugly performance tests + saveDelay = System.currentTimeMillis() - saveDelay; + Log.e(TAG, "syncing: SAVE TIME for " + uri + " contents, " + mSaveDelays[mDelaysIndex] + "ms"); + */ + // removal of obsolete files Vector files = getStorageManager().getDirectoryContent( getStorageManager().getFileById(parentId)); - for (OCFile file : files) { - if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) + OCFile file; + for (int i=0; i < files.size(); ) { + file = files.get(i); + if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) { getStorageManager().removeFile(file); + files.remove(i); + } else { + i++; + } } - // synched folder -> notice to IU - sendStickyBroadcast(true, parentId); + // synchronized folder -> notice to UI + sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath()); // recursive fetch - Iterator pathsIt = paths.iterator(); - Iterator fileIdsIt = fileIds.iterator(); - while (pathsIt.hasNext()) { - fetchData(getUri().toString() + pathsIt.next(), syncResult, fileIdsIt.next()); + for (int i=0; i < files.size() && !mCancellation; i++) { + OCFile newFile = files.get(i); + if (newFile.getMimetype().equals("DIR")) { + fetchData(getUri().toString() + WebdavUtils.encodePath(newFile.getRemotePath()), syncResult, newFile.getFileId()); + } } + if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancellation request"); + + /* Commented code for ugly performance tests + mResponseDelays[mDelaysIndex] = responseDelay; + mSaveDelays[mDelaysIndex] = saveDelay; + mDelaysCount++; + mDelaysIndex++; + if (mDelaysIndex >= MAX_DELAYS) + mDelaysIndex = 0; + */ + } catch (OperationCanceledException e) { @@ -153,11 +248,15 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } catch (DavException e) { syncResult.stats.numIoExceptions++; e.printStackTrace(); + } catch (Throwable t) { + // TODO update syncResult + Log.e(TAG, "problem while synchronizing owncloud account " + mAccount.name, t); + t.printStackTrace(); } } private OCFile fillOCFile(WebdavEntry we) { - OCFile file = new OCFile(we.path()); + OCFile file = new OCFile(we.decodedPath()); file.setCreationTimestamp(we.createTimestamp()); file.setFileLength(we.contentLength()); file.setMimetype(we.contentType()); @@ -167,14 +266,27 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } - private void sendStickyBroadcast(boolean inProgress, long OCDirId) { + private void sendStickyBroadcast(boolean inProgress, String dirRemotePath) { Intent i = new Intent(FileSyncService.SYNC_MESSAGE); i.putExtra(FileSyncService.IN_PROGRESS, inProgress); i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name); - if (OCDirId > 0) { - i.putExtra(FileSyncService.SYNC_FOLDER, OCDirId); + if (dirRemotePath != null) { + i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath); } getContext().sendStickyBroadcast(i); } + + /** + * Called by system SyncManager when a synchronization is required to be cancelled. + * + * Sets the mCancellation flag to 'true'. THe synchronization will be stopped when before a new folder is fetched. Data of the last folder + * fetched will be still saved in the database. See onPerformSync implementation. + */ + @Override + public void onSyncCanceled() { + Log.d(TAG, "Synchronization of " + mAccount.name + " has been requested to cancell"); + mCancellation = true; + super.onSyncCanceled(); + } }