14237e478f18896fdafd0c20ab1d7978a2789fbc
[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.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;
28
29 import org.apache.jackrabbit.webdav.DavException;
30 import org.apache.jackrabbit.webdav.MultiStatus;
31 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
32
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;
46
47 /**
48 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
49 * platform ContactOperations provider.
50 *
51 * @author Bartek Przybylski
52 */
53 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
54
55 private final static String TAG = "FileSyncAdapter";
56
57 private long mCurrentSyncTime;
58
59 public FileSyncAdapter(Context context, boolean autoInitialize) {
60 super(context, autoInitialize);
61 }
62
63 @Override
64 public synchronized void onPerformSync(Account account, Bundle extras,
65 String authority, ContentProviderClient provider,
66 SyncResult syncResult) {
67
68 this.setAccount(account);
69 this.setContentProvider(provider);
70 this.setStorageManager(new FileDataStorageManager(account,
71 getContentProvider()));
72
73 Log.d(TAG, "syncing owncloud account " + account.name);
74
75 sendStickyBroadcast(true, -1); // message to signal the start to the UI
76
77 PropFindMethod query;
78 try {
79 mCurrentSyncTime = System.currentTimeMillis();
80 query = new PropFindMethod(getUri().toString() + "/");
81 getClient().executeMethod(query);
82 MultiStatus resp = null;
83 resp = query.getResponseBodyAsMultiStatus();
84
85 if (resp.getResponses().length > 0) {
86 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
87 OCFile file = fillOCFile(we);
88 file.setParentId(0);
89 getStorageManager().saveFile(file);
90 fetchData(getUri().toString(), syncResult, file.getFileId());
91 }
92 } catch (OperationCanceledException e) {
93 e.printStackTrace();
94 } catch (AuthenticatorException e) {
95 syncResult.stats.numAuthExceptions++;
96 e.printStackTrace();
97 } catch (IOException e) {
98 syncResult.stats.numIoExceptions++;
99 e.printStackTrace();
100 } catch (DavException e) {
101 syncResult.stats.numIoExceptions++;
102 e.printStackTrace();
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
106 r.printStackTrace();
107 throw r;
108 }
109 sendStickyBroadcast(false, -1);
110 }
111
112 private void fetchData(String uri, SyncResult syncResult, long parentId) {
113 try {
114 Log.v(TAG, "syncing: fetching " + uri);
115
116 boolean logmore = (uri.contains("many-files"));
117
118 // remote request
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;
123
124 if (logmore) Log.v(TAG, "syncing: fetching many-files, TO PREPARE THE RESPONSE");
125 resp = query.getResponseBodyAsMultiStatus();
126
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);
135 if (parentId == 0)
136 parentId = file.getFileId();
137 }
138
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);
148 }
149
150 // synched folder -> notice to IU
151 if (logmore) Log.v(TAG, "syncing: fetching many-files, NOTIFYING THE UI");
152 sendStickyBroadcast(true, parentId);
153
154 // recursive fetch
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());
160 }
161 }
162
163
164 } catch (OperationCanceledException e) {
165 e.printStackTrace();
166 } catch (AuthenticatorException e) {
167 syncResult.stats.numAuthExceptions++;
168 e.printStackTrace();
169 } catch (IOException e) {
170 syncResult.stats.numIoExceptions++;
171 e.printStackTrace();
172 } catch (DavException e) {
173 syncResult.stats.numIoExceptions++;
174 e.printStackTrace();
175 }
176 }
177
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);
185 return file;
186 }
187
188
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);
193 if (OCDirId > 0) {
194 i.putExtra(FileSyncService.SYNC_FOLDER, OCDirId);
195 }
196 getContext().sendStickyBroadcast(i);
197 }
198
199 }