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