9f0c0dc583fe893f4f0bb7166de445615f11d010
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / FileSyncAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.syncadapter;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.Vector;
24
25 import org.apache.jackrabbit.webdav.DavException;
26 import org.apache.jackrabbit.webdav.MultiStatus;
27 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
28
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;
42
43 /**
44 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
45 * platform ContactOperations provider.
46 *
47 * @author Bartek Przybylski
48 */
49 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
50
51 private final static String TAG = "FileSyncAdapter";
52
53 /* Commented code for ugly performance tests
54 private final static int MAX_DELAYS = 100;
55 private static long[] mResponseDelays = new long[MAX_DELAYS];
56 private static long[] mSaveDelays = new long[MAX_DELAYS];
57 private int mDelaysIndex = 0;
58 private int mDelaysCount = 0;
59 */
60
61 private long mCurrentSyncTime;
62 private boolean mCancellation;
63 private Account mAccount;
64
65 public FileSyncAdapter(Context context, boolean autoInitialize) {
66 super(context, autoInitialize);
67 }
68
69 @Override
70 public synchronized void onPerformSync(Account account, Bundle extras,
71 String authority, ContentProviderClient provider,
72 SyncResult syncResult) {
73
74 mCancellation = false;
75 mAccount = account;
76
77 this.setAccount(mAccount);
78 this.setContentProvider(provider);
79 this.setStorageManager(new FileDataStorageManager(mAccount,
80 getContentProvider()));
81
82 /* Commented code for ugly performance tests
83 mDelaysIndex = 0;
84 mDelaysCount = 0;
85 */
86
87
88 Log.d(TAG, "syncing owncloud account " + mAccount.name);
89
90 sendStickyBroadcast(true, null); // message to signal the start to the UI
91
92 PropFindMethod query;
93 try {
94 mCurrentSyncTime = System.currentTimeMillis();
95 query = new PropFindMethod(getUri().toString() + "/");
96 getClient().executeMethod(query);
97 MultiStatus resp = null;
98 resp = query.getResponseBodyAsMultiStatus();
99
100 if (resp.getResponses().length > 0) {
101 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
102 OCFile file = fillOCFile(we);
103 file.setParentId(0);
104 getStorageManager().saveFile(file);
105 if (!mCancellation) {
106 fetchData(getUri().toString(), syncResult, file.getFileId());
107 }
108 }
109 } catch (OperationCanceledException e) {
110 e.printStackTrace();
111 } catch (AuthenticatorException e) {
112 syncResult.stats.numAuthExceptions++;
113 e.printStackTrace();
114 } catch (IOException e) {
115 syncResult.stats.numIoExceptions++;
116 e.printStackTrace();
117 } catch (DavException e) {
118 syncResult.stats.numIoExceptions++;
119 e.printStackTrace();
120 } catch (Throwable t) {
121 // TODO update syncResult
122 Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);
123 t.printStackTrace();
124 }
125
126 /* Commented code for ugly performance tests
127 long sum = 0, mean = 0, max = 0, min = Long.MAX_VALUE;
128 for (int i=0; i<MAX_DELAYS && i<mDelaysCount; i++) {
129 sum += mResponseDelays[i];
130 max = Math.max(max, mResponseDelays[i]);
131 min = Math.min(min, mResponseDelays[i]);
132 }
133 mean = sum / mDelaysCount;
134 Log.e(TAG, "SYNC STATS - response: mean time = " + mean + " ; max time = " + max + " ; min time = " + min);
135
136 sum = 0; max = 0; min = Long.MAX_VALUE;
137 for (int i=0; i<MAX_DELAYS && i<mDelaysCount; i++) {
138 sum += mSaveDelays[i];
139 max = Math.max(max, mSaveDelays[i]);
140 min = Math.min(min, mSaveDelays[i]);
141 }
142 mean = sum / mDelaysCount;
143 Log.e(TAG, "SYNC STATS - save: mean time = " + mean + " ; max time = " + max + " ; min time = " + min);
144 Log.e(TAG, "SYNC STATS - folders measured: " + mDelaysCount);
145 */
146
147 sendStickyBroadcast(false, null);
148 }
149
150 private void fetchData(String uri, SyncResult syncResult, long parentId) {
151 try {
152 //Log.v(TAG, "syncing: fetching " + uri);
153
154 // remote request
155 PropFindMethod query = new PropFindMethod(uri);
156 /* Commented code for ugly performance tests
157 long responseDelay = System.currentTimeMillis();
158 */
159 getClient().executeMethod(query);
160 /* Commented code for ugly performance tests
161 responseDelay = System.currentTimeMillis() - responseDelay;
162 Log.e(TAG, "syncing: RESPONSE TIME for " + uri + " contents, " + responseDelay + "ms");
163 */
164 MultiStatus resp = null;
165 resp = query.getResponseBodyAsMultiStatus();
166
167 // insertion or update of files
168 List<OCFile> updatedFiles = new Vector<OCFile>(resp.getResponses().length - 1);
169 for (int i = 1; i < resp.getResponses().length; ++i) {
170 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());
171 OCFile file = fillOCFile(we);
172 file.setParentId(parentId);
173 if (getStorageManager().getFileByPath(file.getRemotePath()) != null &&
174 getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() &&
175 file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath())
176 .getModificationTimestamp()) {
177 Intent intent = new Intent(this.getContext(), FileDownloader.class);
178 intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
179 intent.putExtra(FileDownloader.EXTRA_FILE_PATH, file.getURLDecodedRemotePath());
180 intent.putExtra(FileDownloader.EXTRA_REMOTE_PATH, file.getRemotePath());
181 intent.putExtra(FileDownloader.EXTRA_FILE_SIZE, file.getFileLength());
182 file.setKeepInSync(true);
183 getContext().startService(intent);
184 }
185 if (getStorageManager().getFileByPath(file.getRemotePath()) != null)
186 file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync());
187 //getStorageManager().saveFile(file);
188 updatedFiles.add(file);
189 if (parentId == 0)
190 parentId = file.getFileId();
191 }
192 /* Commented code for ugly performance tests
193 long saveDelay = System.currentTimeMillis();
194 */
195 getStorageManager().saveFiles(updatedFiles); // all "at once" ; trying to get a best performance in database update
196 /* Commented code for ugly performance tests
197 saveDelay = System.currentTimeMillis() - saveDelay;
198 Log.e(TAG, "syncing: SAVE TIME for " + uri + " contents, " + mSaveDelays[mDelaysIndex] + "ms");
199 */
200
201 // removal of obsolete files
202 Vector<OCFile> files = getStorageManager().getDirectoryContent(
203 getStorageManager().getFileById(parentId));
204 OCFile file;
205 for (int i=0; i < files.size(); ) {
206 file = files.get(i);
207 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) {
208 getStorageManager().removeFile(file);
209 files.remove(i);
210 } else {
211 i++;
212 }
213 }
214
215 // synchronized folder -> notice to UI
216 sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath());
217
218 // recursive fetch
219 for (int i=0; i < files.size() && !mCancellation; i++) {
220 OCFile newFile = files.get(i);
221 if (newFile.getMimetype().equals("DIR")) {
222 fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId());
223 }
224 }
225 if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancellation request");
226
227 /* Commented code for ugly performance tests
228 mResponseDelays[mDelaysIndex] = responseDelay;
229 mSaveDelays[mDelaysIndex] = saveDelay;
230 mDelaysCount++;
231 mDelaysIndex++;
232 if (mDelaysIndex >= MAX_DELAYS)
233 mDelaysIndex = 0;
234 */
235
236
237
238 } catch (OperationCanceledException e) {
239 e.printStackTrace();
240 } catch (AuthenticatorException e) {
241 syncResult.stats.numAuthExceptions++;
242 e.printStackTrace();
243 } catch (IOException e) {
244 syncResult.stats.numIoExceptions++;
245 e.printStackTrace();
246 } catch (DavException e) {
247 syncResult.stats.numIoExceptions++;
248 e.printStackTrace();
249 } catch (Throwable t) {
250 // TODO update syncResult
251 Log.e(TAG, "problem while synchronizing owncloud account " + mAccount.name, t);
252 t.printStackTrace();
253 }
254 }
255
256 private OCFile fillOCFile(WebdavEntry we) {
257 OCFile file = new OCFile(we.path());
258 file.setCreationTimestamp(we.createTimestamp());
259 file.setFileLength(we.contentLength());
260 file.setMimetype(we.contentType());
261 file.setModificationTimestamp(we.modifiedTimesamp());
262 file.setLastSyncDate(mCurrentSyncTime);
263 return file;
264 }
265
266
267 private void sendStickyBroadcast(boolean inProgress, String dirRemotePath) {
268 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
269 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
270 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
271 if (dirRemotePath != null) {
272 i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
273 }
274 getContext().sendStickyBroadcast(i);
275 }
276
277 /**
278 * Called by system SyncManager when a synchronization is required to be cancelled.
279 *
280 * Sets the mCancellation flag to 'true'. THe synchronization will be stopped when before a new folder is fetched. Data of the last folder
281 * fetched will be still saved in the database. See onPerformSync implementation.
282 */
283 @Override
284 public void onSyncCanceled() {
285 Log.d(TAG, "Synchronization of " + mAccount.name + " has been requested to cancell");
286 mCancellation = true;
287 super.onSyncCanceled();
288 }
289
290 }