1dcc74570d51e3fc6befb6211f18d3151035f7c2
[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.io.ObjectInputStream.GetField;
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 private long mCurrentSyncTime;
54
55 public FileSyncAdapter(Context context, boolean autoInitialize) {
56 super(context, autoInitialize);
57 }
58
59 @Override
60 public synchronized void onPerformSync(Account account, Bundle extras,
61 String authority, ContentProviderClient provider,
62 SyncResult syncResult) {
63
64 this.setAccount(account);
65 this.setContentProvider(provider);
66 this.setStorageManager(new FileDataStorageManager(account,
67 getContentProvider()));
68
69 Log.d(TAG, "syncing owncloud account " + account.name);
70
71 sendStickyBroadcast(true, null); // message to signal the start to the UI
72
73 PropFindMethod query;
74 try {
75 mCurrentSyncTime = System.currentTimeMillis();
76 query = new PropFindMethod(getUri().toString() + "/");
77 getClient().executeMethod(query);
78 MultiStatus resp = null;
79 resp = query.getResponseBodyAsMultiStatus();
80
81 if (resp.getResponses().length > 0) {
82 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
83 OCFile file = fillOCFile(we);
84 file.setParentId(0);
85 getStorageManager().saveFile(file);
86 fetchData(getUri().toString(), syncResult, file.getFileId(), account);
87 }
88 } catch (OperationCanceledException e) {
89 e.printStackTrace();
90 } catch (AuthenticatorException e) {
91 syncResult.stats.numAuthExceptions++;
92 e.printStackTrace();
93 } catch (IOException e) {
94 syncResult.stats.numIoExceptions++;
95 e.printStackTrace();
96 } catch (DavException e) {
97 syncResult.stats.numIoExceptions++;
98 e.printStackTrace();
99 } catch (Throwable t) {
100 // TODO update syncResult
101 Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);
102 t.printStackTrace();
103 }
104 sendStickyBroadcast(false, null);
105 }
106
107 private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) {
108 try {
109 Log.v(TAG, "syncing: fetching " + uri);
110
111 // remote request
112 PropFindMethod query = new PropFindMethod(uri);
113 getClient().executeMethod(query);
114 MultiStatus resp = null;
115
116 resp = query.getResponseBodyAsMultiStatus();
117
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);
134 }
135 if (getStorageManager().getFileByPath(file.getRemotePath()) != null)
136 file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync());
137 getStorageManager().saveFile(file);
138 if (parentId == 0)
139 parentId = file.getFileId();
140 }
141
142 // removal of old files
143 Vector<OCFile> files = getStorageManager().getDirectoryContent(
144 getStorageManager().getFileById(parentId));
145 OCFile file;
146 for (int i=0; i < files.size(); ) {
147 file = files.get(i);
148 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) {
149 getStorageManager().removeFile(file);
150 files.remove(i);
151 } else {
152 i++;
153 }
154 }
155
156 // synchronized folder -> notice to UI
157 sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath());
158
159 // recursive fetch
160 for (OCFile newFile : files) {
161 if (newFile.getMimetype().equals("DIR")) {
162 fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account);
163 }
164 }
165
166
167 } catch (OperationCanceledException e) {
168 e.printStackTrace();
169 } catch (AuthenticatorException e) {
170 syncResult.stats.numAuthExceptions++;
171 e.printStackTrace();
172 } catch (IOException e) {
173 syncResult.stats.numIoExceptions++;
174 e.printStackTrace();
175 } catch (DavException e) {
176 syncResult.stats.numIoExceptions++;
177 e.printStackTrace();
178 } catch (Throwable t) {
179 // TODO update syncResult
180 Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);
181 t.printStackTrace();
182 }
183 }
184
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);
192 return file;
193 }
194
195
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);
202 }
203 getContext().sendStickyBroadcast(i);
204 }
205
206 }