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
.io
.ObjectInputStream
.GetField
;
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
.owncloud
.files
.services
.FileDownloader
;
41 import eu
.alefzero
.webdav
.WebdavEntry
;
44 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
45 * platform ContactOperations provider.
47 * @author Bartek Przybylski
49 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
51 private final static String TAG
= "FileSyncAdapter";
53 private long mCurrentSyncTime
;
55 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
56 super(context
, autoInitialize
);
60 public synchronized void onPerformSync(Account account
, Bundle extras
,
61 String authority
, ContentProviderClient provider
,
62 SyncResult syncResult
) {
64 this.setAccount(account
);
65 this.setContentProvider(provider
);
66 this.setStorageManager(new FileDataStorageManager(account
,
67 getContentProvider()));
69 Log
.d(TAG
, "syncing owncloud account " + account
.name
);
71 sendStickyBroadcast(true
, null
); // message to signal the start to the UI
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], getUri().getPath());
83 OCFile file
= fillOCFile(we
);
85 getStorageManager().saveFile(file
);
86 fetchData(getUri().toString(), syncResult
, file
.getFileId(), account
);
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
++;
99 } catch (Throwable t
) {
100 // TODO update syncResult
101 Log
.e(TAG
, "problem while synchronizing owncloud account " + account
.name
, t
);
104 sendStickyBroadcast(false
, null
);
107 private void fetchData(String uri
, SyncResult syncResult
, long parentId
, Account account
) {
109 Log
.v(TAG
, "syncing: fetching " + uri
);
112 PropFindMethod query
= new PropFindMethod(uri
);
113 getClient().executeMethod(query
);
114 MultiStatus resp
= null
;
116 resp
= query
.getResponseBodyAsMultiStatus();
118 // insertion of updated files
119 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
120 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath());
121 OCFile file
= fillOCFile(we
);
122 file
.setParentId(parentId
);
123 if (getStorageManager().getFileByPath(file
.getRemotePath()) != null
&&
124 getStorageManager().getFileByPath(file
.getRemotePath()).keepInSync() &&
125 file
.getModificationTimestamp() > getStorageManager().getFileByPath(file
.getRemotePath())
126 .getModificationTimestamp()) {
127 Intent intent
= new Intent(this.getContext(), FileDownloader
.class);
128 intent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
129 intent
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, file
.getURLDecodedRemotePath());
130 intent
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
131 intent
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, file
.getFileLength());
132 file
.setKeepInSync(true
);
133 getContext().startService(intent
);
135 if (getStorageManager().getFileByPath(file
.getRemotePath()) != null
)
136 file
.setKeepInSync(getStorageManager().getFileByPath(file
.getRemotePath()).keepInSync());
137 getStorageManager().saveFile(file
);
139 parentId
= file
.getFileId();
142 // removal of old files
143 Vector
<OCFile
> files
= getStorageManager().getDirectoryContent(
144 getStorageManager().getFileById(parentId
));
146 for (int i
=0; i
< files
.size(); ) {
148 if (file
.getLastSyncDate() != mCurrentSyncTime
&& file
.getLastSyncDate() != 0) {
149 getStorageManager().removeFile(file
);
156 // synchronized folder -> notice to UI
157 sendStickyBroadcast(true
, getStorageManager().getFileById(parentId
).getRemotePath());
160 for (OCFile newFile
: files
) {
161 if (newFile
.getMimetype().equals("DIR")) {
162 fetchData(getUri().toString() + newFile
.getRemotePath(), syncResult
, newFile
.getFileId(), account
);
167 } catch (OperationCanceledException e
) {
169 } catch (AuthenticatorException e
) {
170 syncResult
.stats
.numAuthExceptions
++;
172 } catch (IOException e
) {
173 syncResult
.stats
.numIoExceptions
++;
175 } catch (DavException e
) {
176 syncResult
.stats
.numIoExceptions
++;
178 } catch (Throwable t
) {
179 // TODO update syncResult
180 Log
.e(TAG
, "problem while synchronizing owncloud account " + account
.name
, t
);
185 private OCFile
fillOCFile(WebdavEntry we
) {
186 OCFile file
= new OCFile(we
.path());
187 file
.setCreationTimestamp(we
.createTimestamp());
188 file
.setFileLength(we
.contentLength());
189 file
.setMimetype(we
.contentType());
190 file
.setModificationTimestamp(we
.modifiedTimesamp());
191 file
.setLastSyncDate(mCurrentSyncTime
);
196 private void sendStickyBroadcast(boolean inProgress
, String dirRemotePath
) {
197 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
198 i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
);
199 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
);
200 if (dirRemotePath
!= null
) {
201 i
.putExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
, dirRemotePath
);
203 getContext().sendStickyBroadcast(i
);