X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/a2a8dc1ee6681d641a4316dccaacca6751c32a6a..5f61cb15552eb2fda01716078a9191b88cebf764:/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 816e79ee..85f9cbb3 100644 --- a/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java @@ -19,6 +19,8 @@ package eu.alefzero.owncloud.syncadapter; import java.io.IOException; +import java.net.URLDecoder; +import java.util.Vector; import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.MultiStatus; @@ -31,8 +33,8 @@ import android.content.ContentProviderClient; import android.content.Context; import android.content.Intent; import android.content.SyncResult; -import android.content.IntentSender.SendIntentException; import android.os.Bundle; +import android.util.Log; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; import eu.alefzero.webdav.WebdavEntry; @@ -45,96 +47,106 @@ import eu.alefzero.webdav.WebdavEntry; */ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { - public static final String SYNC_MESSAGE = "eu.alefzero.owncloud.files.ACCOUNT_SYNC"; - public static final String IN_PROGRESS = "sync_in_progress"; - public static final String ACCOUNT_NAME = "account_name"; - - public FileSyncAdapter(Context context, boolean autoInitialize) { - super(context, autoInitialize); - } - - @Override - public synchronized void onPerformSync( - Account account, - Bundle extras, - String authority, - ContentProviderClient provider, - SyncResult syncResult) { + private final static String TAG = "FileSyncAdapter"; + + private long mCurrentSyncTime; + + public FileSyncAdapter(Context context, boolean autoInitialize) { + super(context, autoInitialize); + } + + @Override + public synchronized void onPerformSync(Account account, Bundle extras, + String authority, ContentProviderClient provider, + SyncResult syncResult) { + + this.setAccount(account); + this.setContentProvider(provider); + this.setStorageManager(new FileDataStorageManager(account, + getContentProvider())); + + Log.d(TAG, "syncing owncloud account " + account.name); + + Intent i = new Intent(FileSyncService.SYNC_MESSAGE); + i.putExtra(FileSyncService.IN_PROGRESS, true); + i.putExtra(FileSyncService.ACCOUNT_NAME, account.name); + getContext().sendStickyBroadcast(i); - this.setAccount(account); - this.setContentProvider(provider); - this.setStorageManager(new FileDataStorageManager(account, getContentProvider())); + PropFindMethod query; + try { + mCurrentSyncTime = System.currentTimeMillis(); + query = new PropFindMethod(getUri().toString() + "/"); + getClient().executeMethod(query); + MultiStatus resp = null; + resp = query.getResponseBodyAsMultiStatus(); - Intent i = new Intent(SYNC_MESSAGE); - i.putExtra(IN_PROGRESS, true); - i.putExtra("ACCOUNT_NAME", account.name); - getContext().sendStickyBroadcast(i); - - PropFindMethod query; - try { - query = new PropFindMethod(getUri().toString()); - getClient().executeMethod(query); - MultiStatus resp = null; - resp = query.getResponseBodyAsMultiStatus(); - if (resp.getResponses().length > 0) { - WebdavEntry we = new WebdavEntry(resp.getResponses()[0]); - OCFile file = fillOCFile(we); - file.setParentId(0); - getStorageManager().saveFile(file); - fetchData(getUri().toString(), syncResult, file.getFileId()); + if (resp.getResponses().length > 0) { + WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath()); + OCFile file = fillOCFile(we); + file.setParentId(0); + getStorageManager().saveFile(file); + fetchData(getUri().toString(), syncResult, file.getFileId()); + } + } catch (OperationCanceledException e) { + e.printStackTrace(); + } catch (AuthenticatorException e) { + syncResult.stats.numAuthExceptions++; + e.printStackTrace(); + } catch (IOException e) { + syncResult.stats.numIoExceptions++; + e.printStackTrace(); + } catch (DavException e) { + syncResult.stats.numIoExceptions++; + e.printStackTrace(); } - } catch (OperationCanceledException e) { - e.printStackTrace(); - } catch (AuthenticatorException e) { - syncResult.stats.numAuthExceptions++; - e.printStackTrace(); - } catch (IOException e) { - syncResult.stats.numIoExceptions++; - e.printStackTrace(); - } catch (DavException e) { - syncResult.stats.numIoExceptions++; - e.printStackTrace(); - } - i.putExtra(IN_PROGRESS, false); - getContext().sendStickyBroadcast(i); - } + i.putExtra(FileSyncService.IN_PROGRESS, false); + getContext().sendStickyBroadcast(i); + } - private void fetchData(String uri, SyncResult syncResult, long parentId) { - try { - PropFindMethod query = new PropFindMethod(uri); - getClient().executeMethod(query); - MultiStatus resp = null; - resp = query.getResponseBodyAsMultiStatus(); - for (int i = 1; i < resp.getResponses().length; ++i) { - WebdavEntry we = new WebdavEntry(resp.getResponses()[i]); - OCFile file = fillOCFile(we); - file.setParentId(parentId); - getStorageManager().saveFile(file); - if (parentId == 0) parentId = file.getFileId(); - if (we.contentType().equals("DIR")) - fetchData(getUri().toString() + we.path(), syncResult, file.getFileId()); - } - } catch (OperationCanceledException e) { - e.printStackTrace(); - } catch (AuthenticatorException e) { - syncResult.stats.numAuthExceptions++; - e.printStackTrace(); - } catch (IOException e) { - syncResult.stats.numIoExceptions++; - e.printStackTrace(); - } catch (DavException e) { - syncResult.stats.numIoExceptions++; - e.printStackTrace(); + private void fetchData(String uri, SyncResult syncResult, long parentId) { + try { + PropFindMethod query = new PropFindMethod(uri); + getClient().executeMethod(query); + MultiStatus resp = null; + resp = query.getResponseBodyAsMultiStatus(); + 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 (parentId == 0) + parentId = file.getFileId(); + if (we.contentType().equals("DIR")) + fetchData(getUri().toString() + we.path(), syncResult, file.getFileId()); + } + Vector files = getStorageManager().getDirectoryContent( + getStorageManager().getFileById(parentId)); + for (OCFile file : files) { + if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) + getStorageManager().removeFile(file); + } + } catch (OperationCanceledException e) { + e.printStackTrace(); + } catch (AuthenticatorException e) { + syncResult.stats.numAuthExceptions++; + e.printStackTrace(); + } catch (IOException e) { + syncResult.stats.numIoExceptions++; + e.printStackTrace(); + } catch (DavException e) { + syncResult.stats.numIoExceptions++; + e.printStackTrace(); + } } - } - - private OCFile fillOCFile(WebdavEntry we) { - OCFile file = new OCFile(we.path()); - file.setCreationTimestamp(we.createTimestamp()); - file.setFileLength(we.contentLength()); - file.setMimetype(we.contentType()); - file.setModificationTimestamp(we.modifiedTimesamp()); - return file; - } - + + private OCFile fillOCFile(WebdavEntry we) { + OCFile file = new OCFile(URLDecoder.decode(we.path())); + file.setCreationTimestamp(we.createTimestamp()); + file.setFileLength(we.contentLength()); + file.setMimetype(we.contentType()); + file.setModificationTimestamp(we.modifiedTimesamp()); + file.setLastSyncDate(mCurrentSyncTime); + return file; + } + }