-/* ownCloud Android client application\r
- * Copyright (C) 2011 Bartek Przybylski\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-\r
-package eu.alefzero.owncloud.syncadapter;\r
-\r
-import java.io.IOException;\r
-import java.util.Vector;\r
-\r
-import org.apache.jackrabbit.webdav.DavException;\r
-import org.apache.jackrabbit.webdav.MultiStatus;\r
-import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
-\r
-import android.accounts.Account;\r
-import android.accounts.AuthenticatorException;\r
-import android.accounts.OperationCanceledException;\r
-import android.content.ContentProviderClient;\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.content.SyncResult;\r
-import android.os.Bundle;\r
-import android.util.Log;\r
-import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
-import eu.alefzero.owncloud.datamodel.OCFile;\r
-import eu.alefzero.webdav.WebdavEntry;\r
-\r
-/**\r
- * SyncAdapter implementation for syncing sample SyncAdapter contacts to the\r
- * platform ContactOperations provider.\r
- * \r
- * @author Bartek Przybylski\r
- */\r
-public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
-\r
- private final static String TAG = "FileSyncAdapter"; \r
- \r
- private long mCurrentSyncTime;\r
- \r
- public FileSyncAdapter(Context context, boolean autoInitialize) {\r
- super(context, autoInitialize);\r
- }\r
-\r
- @Override\r
- public synchronized void onPerformSync(Account account, Bundle extras,\r
- String authority, ContentProviderClient provider,\r
- SyncResult syncResult) {\r
-\r
- this.setAccount(account);\r
- this.setContentProvider(provider);\r
- this.setStorageManager(new FileDataStorageManager(account,\r
- getContentProvider()));\r
-\r
- Log.d(TAG, "syncing owncloud account " + account.name);\r
-\r
- Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
- i.putExtra(FileSyncService.IN_PROGRESS, true);\r
- i.putExtra(FileSyncService.ACCOUNT_NAME, account.name);\r
- getContext().sendStickyBroadcast(i);\r
-\r
- PropFindMethod query;\r
- try {\r
- mCurrentSyncTime = System.currentTimeMillis();\r
- query = new PropFindMethod(getUri().toString() + "/");\r
- getClient().executeMethod(query);\r
- MultiStatus resp = null;\r
- resp = query.getResponseBodyAsMultiStatus();\r
-\r
- if (resp.getResponses().length > 0) {\r
- WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());\r
- OCFile file = fillOCFile(we);\r
- file.setParentId(0);\r
- getStorageManager().saveFile(file);\r
- fetchData(getUri().toString(), syncResult, file.getFileId());\r
- }\r
- } catch (OperationCanceledException e) {\r
- e.printStackTrace();\r
- } catch (AuthenticatorException e) {\r
- syncResult.stats.numAuthExceptions++;\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- syncResult.stats.numIoExceptions++;\r
- e.printStackTrace();\r
- } catch (DavException e) {\r
- syncResult.stats.numIoExceptions++;\r
- e.printStackTrace();\r
- }\r
- i.putExtra(FileSyncService.IN_PROGRESS, false);\r
- getContext().sendStickyBroadcast(i);\r
- }\r
-\r
- private void fetchData(String uri, SyncResult syncResult, long parentId) {\r
- try {\r
- PropFindMethod query = new PropFindMethod(uri);\r
- getClient().executeMethod(query);\r
- MultiStatus resp = null;\r
- resp = query.getResponseBodyAsMultiStatus();\r
- for (int i = 1; i < resp.getResponses().length; ++i) {\r
- WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());\r
- OCFile file = fillOCFile(we);\r
- file.setParentId(parentId);\r
- getStorageManager().saveFile(file);\r
- if (parentId == 0)\r
- parentId = file.getFileId();\r
- if (we.contentType().equals("DIR"))\r
- fetchData(getUri().toString() + we.path(), syncResult, file.getFileId());\r
- }\r
- Vector<OCFile> files = getStorageManager().getDirectoryContent(\r
- getStorageManager().getFileById(parentId));\r
- for (OCFile file : files) {\r
- if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)\r
- getStorageManager().removeFile(file);\r
- }\r
- } catch (OperationCanceledException e) {\r
- e.printStackTrace();\r
- } catch (AuthenticatorException e) {\r
- syncResult.stats.numAuthExceptions++;\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- syncResult.stats.numIoExceptions++;\r
- e.printStackTrace();\r
- } catch (DavException e) {\r
- syncResult.stats.numIoExceptions++;\r
- e.printStackTrace();\r
- }\r
- }\r
-\r
- private OCFile fillOCFile(WebdavEntry we) {\r
- OCFile file = new OCFile(we.path());\r
- file.setCreationTimestamp(we.createTimestamp());\r
- file.setFileLength(we.contentLength());\r
- file.setMimetype(we.contentType());\r
- file.setModificationTimestamp(we.modifiedTimesamp());\r
- file.setLastSyncDate(mCurrentSyncTime);\r
- return file;\r
- }\r
-\r
-}\r