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
.util
.Vector
;
24 import org
.apache
.jackrabbit
.webdav
.DavException
;
25 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
26 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
28 import android
.accounts
.Account
;
29 import android
.accounts
.AuthenticatorException
;
30 import android
.accounts
.OperationCanceledException
;
31 import android
.content
.ContentProviderClient
;
32 import android
.content
.Context
;
33 import android
.content
.Intent
;
34 import android
.content
.SyncResult
;
35 import android
.os
.Bundle
;
36 import android
.util
.Log
;
37 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
38 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
39 import eu
.alefzero
.webdav
.WebdavEntry
;
42 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
43 * platform ContactOperations provider.
45 * @author Bartek Przybylski
47 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
49 private final static String TAG
= "FileSyncAdapter";
51 private long mCurrentSyncTime
;
53 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
54 super(context
, autoInitialize
);
58 public synchronized void onPerformSync(Account account
, Bundle extras
,
59 String authority
, ContentProviderClient provider
,
60 SyncResult syncResult
) {
62 this.setAccount(account
);
63 this.setContentProvider(provider
);
64 this.setStorageManager(new FileDataStorageManager(account
,
65 getContentProvider()));
67 Log
.d(TAG
, "syncing owncloud account " + account
.name
);
69 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
70 i
.putExtra(FileSyncService
.IN_PROGRESS
, true
);
71 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, account
.name
);
72 getContext().sendStickyBroadcast(i
);
76 mCurrentSyncTime
= System
.currentTimeMillis();
77 query
= new PropFindMethod(getUri().toString() + "/");
78 getClient().executeMethod(query
);
79 MultiStatus resp
= null
;
80 resp
= query
.getResponseBodyAsMultiStatus();
82 if (resp
.getResponses().length
> 0) {
83 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath());
84 OCFile file
= fillOCFile(we
);
86 getStorageManager().saveFile(file
);
87 fetchData(getUri().toString(), syncResult
, file
.getFileId());
89 } catch (OperationCanceledException e
) {
91 } catch (AuthenticatorException e
) {
92 syncResult
.stats
.numAuthExceptions
++;
94 } catch (IOException e
) {
95 syncResult
.stats
.numIoExceptions
++;
97 } catch (DavException e
) {
98 syncResult
.stats
.numIoExceptions
++;
101 i
.putExtra(FileSyncService
.IN_PROGRESS
, false
);
102 getContext().sendStickyBroadcast(i
);
105 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
107 PropFindMethod query
= new PropFindMethod(uri
);
108 getClient().executeMethod(query
);
109 MultiStatus resp
= null
;
110 resp
= query
.getResponseBodyAsMultiStatus();
111 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
112 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath());
113 OCFile file
= fillOCFile(we
);
114 file
.setParentId(parentId
);
115 getStorageManager().saveFile(file
);
117 parentId
= file
.getFileId();
118 if (we
.contentType().equals("DIR"))
119 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
121 Vector
<OCFile
> files
= getStorageManager().getDirectoryContent(
122 getStorageManager().getFileById(parentId
));
123 for (OCFile file
: files
) {
124 if (file
.getLastSyncDate() != mCurrentSyncTime
&& file
.getLastSyncDate() != 0)
125 getStorageManager().removeFile(file
);
127 } catch (OperationCanceledException e
) {
129 } catch (AuthenticatorException e
) {
130 syncResult
.stats
.numAuthExceptions
++;
132 } catch (IOException e
) {
133 syncResult
.stats
.numIoExceptions
++;
135 } catch (DavException e
) {
136 syncResult
.stats
.numIoExceptions
++;
141 private OCFile
fillOCFile(WebdavEntry we
) {
142 OCFile file
= new OCFile(we
.path());
143 file
.setCreationTimestamp(we
.createTimestamp());
144 file
.setFileLength(we
.contentLength());
145 file
.setMimetype(we
.contentType());
146 file
.setModificationTimestamp(we
.modifiedTimesamp());
147 file
.setLastSyncDate(mCurrentSyncTime
);