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 sendStickyBroadcast(true
, null
); // message to signal the start to the UI
73 mCurrentSyncTime
= System
.currentTimeMillis();
74 query
= new PropFindMethod(getUri().toString() + "/");
75 getClient().executeMethod(query
);
76 MultiStatus resp
= null
;
77 resp
= query
.getResponseBodyAsMultiStatus();
79 if (resp
.getResponses().length
> 0) {
80 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath());
81 OCFile file
= fillOCFile(we
);
83 getStorageManager().saveFile(file
);
84 fetchData(getUri().toString(), syncResult
, file
.getFileId(), account
);
86 } catch (OperationCanceledException e
) {
88 } catch (AuthenticatorException e
) {
89 syncResult
.stats
.numAuthExceptions
++;
91 } catch (IOException e
) {
92 syncResult
.stats
.numIoExceptions
++;
94 } catch (DavException e
) {
95 syncResult
.stats
.numIoExceptions
++;
97 } catch (Throwable t
) {
98 // TODO update syncResult
99 Log
.e(TAG
, "problem while synchronizing owncloud account " + account
.name
, t
);
102 sendStickyBroadcast(false
, null
);
105 private void fetchData(String uri
, SyncResult syncResult
, long parentId
, Account account
) {
107 Log
.v(TAG
, "syncing: fetching " + uri
);
110 PropFindMethod query
= new PropFindMethod(uri
);
111 getClient().executeMethod(query
);
112 MultiStatus resp
= null
;
114 resp
= query
.getResponseBodyAsMultiStatus();
116 // insertion of updated files
117 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
118 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath());
119 OCFile file
= fillOCFile(we
);
120 file
.setParentId(parentId
);
121 getStorageManager().saveFile(file
);
123 parentId
= file
.getFileId();
126 // removal of old files
127 Vector
<OCFile
> files
= getStorageManager().getDirectoryContent(
128 getStorageManager().getFileById(parentId
));
130 for (int i
=0; i
< files
.size(); ) {
132 if (file
.getLastSyncDate() != mCurrentSyncTime
&& file
.getLastSyncDate() != 0) {
133 getStorageManager().removeFile(file
);
140 // synchronized folder -> notice to UI
141 sendStickyBroadcast(true
, getStorageManager().getFileById(parentId
).getRemotePath());
144 for (OCFile newFile
: files
) {
145 if (newFile
.getMimetype().equals("DIR")) {
146 fetchData(getUri().toString() + newFile
.getRemotePath(), syncResult
, newFile
.getFileId(), account
);
151 } catch (OperationCanceledException e
) {
153 } catch (AuthenticatorException e
) {
154 syncResult
.stats
.numAuthExceptions
++;
156 } catch (IOException e
) {
157 syncResult
.stats
.numIoExceptions
++;
159 } catch (DavException e
) {
160 syncResult
.stats
.numIoExceptions
++;
162 } catch (Throwable t
) {
163 // TODO update syncResult
164 Log
.e(TAG
, "problem while synchronizing owncloud account " + account
.name
, t
);
169 private OCFile
fillOCFile(WebdavEntry we
) {
170 OCFile file
= new OCFile(we
.path());
171 file
.setCreationTimestamp(we
.createTimestamp());
172 file
.setFileLength(we
.contentLength());
173 file
.setMimetype(we
.contentType());
174 file
.setModificationTimestamp(we
.modifiedTimesamp());
175 file
.setLastSyncDate(mCurrentSyncTime
);
180 private void sendStickyBroadcast(boolean inProgress
, String dirRemotePath
) {
181 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
182 i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
);
183 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
);
184 if (dirRemotePath
!= null
) {
185 i
.putExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
, dirRemotePath
);
187 getContext().sendStickyBroadcast(i
);