X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/d4f8391f509b4b211d6742db6db546b80e8277c7..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 ef7a3d5b..d14151a2 100644 --- a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java @@ -19,7 +19,6 @@ package eu.alefzero.owncloud.syncadapter; import java.io.IOException; -import java.io.ObjectInputStream.GetField; import java.util.List; import java.util.Vector; @@ -40,6 +39,7 @@ 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 @@ -60,6 +60,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { */ private long mCurrentSyncTime; + private boolean mCancellation; + private Account mAccount; public FileSyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); @@ -70,9 +72,12 @@ 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())); /* Commented code for ugly performance tests @@ -81,7 +86,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { */ - Log.d(TAG, "syncing owncloud account " + account.name); + Log.d(TAG, "syncing owncloud account " + mAccount.name); sendStickyBroadcast(true, null); // message to signal the start to the UI @@ -98,7 +103,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { OCFile file = fillOCFile(we); file.setParentId(0); getStorageManager().saveFile(file); - fetchData(getUri().toString(), syncResult, file.getFileId(), account); + if (!mCancellation) { + fetchData(getUri().toString(), syncResult, file.getFileId()); + } } } catch (OperationCanceledException e) { e.printStackTrace(); @@ -141,7 +148,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { sendStickyBroadcast(false, null); } - private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) { + private void fetchData(String uri, SyncResult syncResult, long parentId) { try { //Log.v(TAG, "syncing: fetching " + uri); @@ -170,7 +177,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { .getModificationTimestamp()) { Intent intent = new Intent(this.getContext(), FileDownloader.class); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); - intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getURLDecodedRemotePath()); + 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); @@ -178,6 +185,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } if (getStorageManager().getFileByPath(file.getRemotePath()) != null) file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync()); + //getStorageManager().saveFile(file); updatedFiles.add(file); if (parentId == 0) @@ -210,12 +218,14 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath()); // recursive fetch - for (OCFile newFile : files) { + for (int i=0; i < files.size() && !mCancellation; i++) { + OCFile newFile = files.get(i); if (newFile.getMimetype().equals("DIR")) { - fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account); + 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; @@ -240,13 +250,13 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { e.printStackTrace(); } catch (Throwable t) { // TODO update syncResult - Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t); + 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()); @@ -265,5 +275,18 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { } 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(); + } }