1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package eu
.alefzero
.owncloud
.syncadapter
;
21 import java
.io
.IOException
;
23 import org
.apache
.http
.entity
.StringEntity
;
24 import org
.apache
.jackrabbit
.webdav
.DavException
;
25 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
26 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
27 import org
.apache
.jackrabbit
.webdav
.property.DavProperty
;
28 import org
.apache
.jackrabbit
.webdav
.property.DavPropertyName
;
30 import android
.accounts
.Account
;
31 import android
.accounts
.AuthenticatorException
;
32 import android
.accounts
.OperationCanceledException
;
33 import android
.content
.ContentProviderClient
;
34 import android
.content
.ContentValues
;
35 import android
.content
.Context
;
36 import android
.content
.SyncResult
;
37 import android
.database
.Cursor
;
38 import android
.net
.Uri
;
39 import android
.os
.Bundle
;
40 import android
.os
.RemoteException
;
41 import android
.util
.Log
;
42 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
43 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
44 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
45 import eu
.alefzero
.webdav
.HttpPropFind
;
46 import eu
.alefzero
.webdav
.TreeNode
;
47 import eu
.alefzero
.webdav
.TreeNode
.NodeProperty
;
48 import eu
.alefzero
.webdav
.WebdavEntry
;
49 import eu
.alefzero
.webdav
.WebdavUtils
;
52 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
53 * platform ContactOperations provider.
55 * @author Bartek Przybylski
57 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
58 private static final String TAG
= "FileSyncAdapter";
60 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
61 super(context
, autoInitialize
);
65 public synchronized void onPerformSync(
69 ContentProviderClient provider
,
70 SyncResult syncResult
) {
72 this.setAccount(account
);
73 this.setContentProvider(provider
);
74 this.setStorageManager(new FileDataStorageManager(account
, getContentProvider()));
76 fetchData(getUri().toString(), syncResult
, 0);
79 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
81 PropFindMethod query
= new PropFindMethod(uri
);
82 getClient().executeMethod(query
);
83 MultiStatus resp
= null
;
84 resp
= query
.getResponseBodyAsMultiStatus();
85 for (int i
= (parentId
==0?
0:1); i
< resp
.getResponses().length
; ++i
) {
86 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
87 OCFile file
= new OCFile(we
.path());
88 file
.setCreationTimestamp(we
.createTimestamp());
89 file
.setFileLength(we
.contentLength());
90 file
.setMimetype(we
.contentType());
91 file
.setModificationTimestamp(we
.modifiedTimesamp());
92 file
.setParentId(parentId
);
93 if (we
.contentType().equals("DIR"))
94 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
97 } catch (OperationCanceledException e
) {
99 } catch (AuthenticatorException e
) {
100 syncResult
.stats
.numAuthExceptions
++;
102 } catch (IOException e
) {
103 syncResult
.stats
.numIoExceptions
++;
105 } catch (DavException e
) {
106 syncResult
.stats
.numIoExceptions
++;