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
;
22 import java
.net
.URLDecoder
;
23 import java
.util
.Vector
;
25 import org
.apache
.jackrabbit
.webdav
.DavException
;
26 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
27 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
29 import android
.accounts
.Account
;
30 import android
.accounts
.AuthenticatorException
;
31 import android
.accounts
.OperationCanceledException
;
32 import android
.content
.ContentProviderClient
;
33 import android
.content
.Context
;
34 import android
.content
.Intent
;
35 import android
.content
.SyncResult
;
36 import android
.os
.Bundle
;
37 import android
.util
.Log
;
38 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
39 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
40 import eu
.alefzero
.webdav
.WebdavEntry
;
43 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
44 * platform ContactOperations provider.
46 * @author Bartek Przybylski
48 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
50 private long mCurrentSyncTime
;
52 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
53 super(context
, autoInitialize
);
57 public synchronized void onPerformSync(Account account
, Bundle extras
,
58 String authority
, ContentProviderClient provider
,
59 SyncResult syncResult
) {
61 this.setAccount(account
);
62 this.setContentProvider(provider
);
63 this.setStorageManager(new FileDataStorageManager(account
,
64 getContentProvider()));
66 Log
.d("ASD", "syncing owncloud account " + account
.name
);
68 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
69 i
.putExtra(FileSyncService
.IN_PROGRESS
, true
);
70 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, account
.name
);
71 getContext().sendStickyBroadcast(i
);
75 mCurrentSyncTime
= System
.currentTimeMillis();
76 query
= new PropFindMethod(getUri().toString() + "/");
77 getClient().executeMethod(query
);
78 MultiStatus resp
= null
;
79 resp
= query
.getResponseBodyAsMultiStatus();
81 if (resp
.getResponses().length
> 0) {
82 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
83 OCFile file
= fillOCFile(we
);
85 getStorageManager().saveFile(file
);
86 fetchData(getUri().toString(), syncResult
, file
.getFileId());
88 } catch (OperationCanceledException e
) {
90 } catch (AuthenticatorException e
) {
91 syncResult
.stats
.numAuthExceptions
++;
93 } catch (IOException e
) {
94 syncResult
.stats
.numIoExceptions
++;
96 } catch (DavException e
) {
97 syncResult
.stats
.numIoExceptions
++;
100 i
.putExtra(FileSyncService
.IN_PROGRESS
, false
);
101 getContext().sendStickyBroadcast(i
);
104 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
106 PropFindMethod query
= new PropFindMethod(uri
);
107 getClient().executeMethod(query
);
108 MultiStatus resp
= null
;
109 resp
= query
.getResponseBodyAsMultiStatus();
110 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
111 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
112 OCFile file
= fillOCFile(we
);
113 file
.setParentId(parentId
);
114 getStorageManager().saveFile(file
);
116 parentId
= file
.getFileId();
117 if (we
.contentType().equals("DIR"))
118 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
120 Vector
<OCFile
> files
= getStorageManager().getDirectoryContent(
121 getStorageManager().getFileById(parentId
));
122 for (OCFile file
: files
) {
123 if (file
.getLastSyncDate() != mCurrentSyncTime
&& file
.getLastSyncDate() != 0)
124 getStorageManager().removeFile(file
);
126 } catch (OperationCanceledException e
) {
128 } catch (AuthenticatorException e
) {
129 syncResult
.stats
.numAuthExceptions
++;
131 } catch (IOException e
) {
132 syncResult
.stats
.numIoExceptions
++;
134 } catch (DavException e
) {
135 syncResult
.stats
.numIoExceptions
++;
140 private OCFile
fillOCFile(WebdavEntry we
) {
141 OCFile file
= new OCFile(URLDecoder
.decode(we
.path()));
142 file
.setCreationTimestamp(we
.createTimestamp());
143 file
.setFileLength(we
.contentLength());
144 file
.setMimetype(we
.contentType());
145 file
.setModificationTimestamp(we
.modifiedTimesamp());
146 file
.setLastSyncDate(mCurrentSyncTime
);