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
.ArrayList
;
23 import java
.util
.Iterator
;
24 import java
.util
.LinkedList
;
25 import java
.util
.List
;
26 import java
.util
.Queue
;
27 import java
.util
.Vector
;
29 import org
.apache
.jackrabbit
.webdav
.DavException
;
30 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
31 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
33 import android
.accounts
.Account
;
34 import android
.accounts
.AuthenticatorException
;
35 import android
.accounts
.OperationCanceledException
;
36 import android
.content
.ContentProviderClient
;
37 import android
.content
.Context
;
38 import android
.content
.Intent
;
39 import android
.content
.SyncResult
;
40 import android
.os
.Bundle
;
41 import android
.util
.Log
;
42 import android
.webkit
.MimeTypeMap
;
43 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
44 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
45 import eu
.alefzero
.webdav
.WebdavEntry
;
48 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
49 * platform ContactOperations provider.
51 * @author Bartek Przybylski
53 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
55 private final static String TAG
= "FileSyncAdapter";
57 private long mCurrentSyncTime
;
59 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
60 super(context
, autoInitialize
);
64 public synchronized void onPerformSync(Account account
, Bundle extras
,
65 String authority
, ContentProviderClient provider
,
66 SyncResult syncResult
) {
68 this.setAccount(account
);
69 this.setContentProvider(provider
);
70 this.setStorageManager(new FileDataStorageManager(account
,
71 getContentProvider()));
73 Log
.d(TAG
, "syncing owncloud account " + account
.name
);
75 sendStickyBroadcast(true
, -1); // message to signal the start to the UI
79 mCurrentSyncTime
= System
.currentTimeMillis();
80 query
= new PropFindMethod(getUri().toString() + "/");
81 getClient().executeMethod(query
);
82 MultiStatus resp
= null
;
83 resp
= query
.getResponseBodyAsMultiStatus();
85 if (resp
.getResponses().length
> 0) {
86 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath());
87 OCFile file
= fillOCFile(we
);
89 getStorageManager().saveFile(file
);
90 fetchData(getUri().toString(), syncResult
, file
.getFileId());
92 } catch (OperationCanceledException e
) {
94 } catch (AuthenticatorException e
) {
95 syncResult
.stats
.numAuthExceptions
++;
97 } catch (IOException e
) {
98 syncResult
.stats
.numIoExceptions
++;
100 } catch (DavException e
) {
101 syncResult
.stats
.numIoExceptions
++;
103 } catch (RuntimeException r
) {
104 //TODO count ; any type of exception should be treated, and the progress indicator finished;
105 // reporting the user about bad synchronizations should be discussed
109 sendStickyBroadcast(false
, -1);
112 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
114 Log
.v(TAG
, "syncing: fetching " + uri
);
116 boolean logmore
= (uri
.contains("many-files"));
119 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, TO REQUEST");
120 PropFindMethod query
= new PropFindMethod(uri
);
121 getClient().executeMethod(query
);
122 MultiStatus resp
= null
;
124 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, TO PREPARE THE RESPONSE");
125 resp
= query
.getResponseBodyAsMultiStatus();
127 // insertion of updated files
128 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, TO PARSE REPONSES");
129 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
130 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, PARSING REPONSE " + i
+ "-esima");
131 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath());
132 OCFile file
= fillOCFile(we
);
133 file
.setParentId(parentId
);
134 getStorageManager().saveFile(file
);
136 parentId
= file
.getFileId();
139 // removal of old files
140 // TODO - getDirectoryContent is crashing the app by lack of memory when a lot of files are in the same directory!!!!
141 // tested with the path /
142 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, RETRIEVING VECTOR OF FILES");
143 Vector
<OCFile
> files
= getStorageManager().getDirectoryContent(
144 getStorageManager().getFileById(parentId
));
145 for (OCFile file
: files
) {
146 if (file
.getLastSyncDate() != mCurrentSyncTime
&& file
.getLastSyncDate() != 0)
147 getStorageManager().removeFile(file
);
150 // synched folder -> notice to IU
151 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, NOTIFYING THE UI");
152 sendStickyBroadcast(true
, parentId
);
155 if (logmore
) Log
.v(TAG
, "syncing: fetching many-files, TRYING TO RECURSE");
156 files
= getStorageManager().getDirectoryContent(getStorageManager().getFileById(parentId
));
157 for (OCFile file
: files
) {
158 if (file
.getMimetype().equals("DIR")) {
159 fetchData(getUri().toString() + file
.getRemotePath(), syncResult
, file
.getFileId());
164 } catch (OperationCanceledException e
) {
166 } catch (AuthenticatorException e
) {
167 syncResult
.stats
.numAuthExceptions
++;
169 } catch (IOException e
) {
170 syncResult
.stats
.numIoExceptions
++;
172 } catch (DavException e
) {
173 syncResult
.stats
.numIoExceptions
++;
178 private OCFile
fillOCFile(WebdavEntry we
) {
179 OCFile file
= new OCFile(we
.path());
180 file
.setCreationTimestamp(we
.createTimestamp());
181 file
.setFileLength(we
.contentLength());
182 file
.setMimetype(we
.contentType());
183 file
.setModificationTimestamp(we
.modifiedTimesamp());
184 file
.setLastSyncDate(mCurrentSyncTime
);
189 private void sendStickyBroadcast(boolean inProgress
, long OCDirId
) {
190 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
191 i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
);
192 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
);
194 i
.putExtra(FileSyncService
.SYNC_FOLDER
, OCDirId
);
196 getContext().sendStickyBroadcast(i
);