Some improvements in synchronization and refresh of directory list
[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.Vector;
23
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.MultiStatus;
26 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
27
28 import android.accounts.Account;
29 import android.accounts.AuthenticatorException;
30 import android.accounts.OperationCanceledException;
31 import android.content.ContentProviderClient;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.SyncResult;
35 import android.os.Bundle;
36 import android.util.Log;
37 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.datamodel.OCFile;
39 import eu.alefzero.webdav.WebdavEntry;
40
41 /**
42 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
43 * platform ContactOperations provider.
44 *
45 * @author Bartek Przybylski
46 */
47 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
48
49 private final static String TAG = "FileSyncAdapter";
50
51 private long mCurrentSyncTime;
52
53 public FileSyncAdapter(Context context, boolean autoInitialize) {
54 super(context, autoInitialize);
55 }
56
57 @Override
58 public synchronized void onPerformSync(Account account, Bundle extras,
59 String authority, ContentProviderClient provider,
60 SyncResult syncResult) {
61
62 this.setAccount(account);
63 this.setContentProvider(provider);
64 this.setStorageManager(new FileDataStorageManager(account,
65 getContentProvider()));
66
67 Log.d(TAG, "syncing owncloud account " + account.name);
68
69 sendStickyBroadcast(true, null); // message to signal the start to the UI
70
71 PropFindMethod query;
72 try {
73 mCurrentSyncTime = System.currentTimeMillis();
74 query = new PropFindMethod(getUri().toString() + "/");
75 getClient().executeMethod(query);
76 MultiStatus resp = null;
77 resp = query.getResponseBodyAsMultiStatus();
78
79 if (resp.getResponses().length > 0) {
80 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath());
81 OCFile file = fillOCFile(we);
82 file.setParentId(0);
83 getStorageManager().saveFile(file);
84 fetchData(getUri().toString(), syncResult, file.getFileId(), account);
85 }
86 } catch (OperationCanceledException e) {
87 e.printStackTrace();
88 } catch (AuthenticatorException e) {
89 syncResult.stats.numAuthExceptions++;
90 e.printStackTrace();
91 } catch (IOException e) {
92 syncResult.stats.numIoExceptions++;
93 e.printStackTrace();
94 } catch (DavException e) {
95 syncResult.stats.numIoExceptions++;
96 e.printStackTrace();
97 } catch (Throwable t) {
98 // TODO update syncResult
99 Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);
100 t.printStackTrace();
101 }
102 sendStickyBroadcast(false, null);
103 }
104
105 private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) {
106 try {
107 Log.v(TAG, "syncing: fetching " + uri);
108
109 // remote request
110 PropFindMethod query = new PropFindMethod(uri);
111 getClient().executeMethod(query);
112 MultiStatus resp = null;
113
114 resp = query.getResponseBodyAsMultiStatus();
115
116 // insertion of updated files
117 for (int i = 1; i < resp.getResponses().length; ++i) {
118 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());
119 OCFile file = fillOCFile(we);
120 file.setParentId(parentId);
121 getStorageManager().saveFile(file);
122 if (parentId == 0)
123 parentId = file.getFileId();
124 }
125
126 // removal of old files
127 Vector<OCFile> files = getStorageManager().getDirectoryContent(
128 getStorageManager().getFileById(parentId));
129 OCFile file;
130 for (int i=0; i < files.size(); ) {
131 file = files.get(i);
132 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) {
133 getStorageManager().removeFile(file);
134 files.remove(i);
135 } else {
136 i++;
137 }
138 }
139
140 // synchronized folder -> notice to UI
141 sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath());
142
143 // recursive fetch
144 for (OCFile newFile : files) {
145 if (newFile.getMimetype().equals("DIR")) {
146 fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account);
147 }
148 }
149
150
151 } catch (OperationCanceledException e) {
152 e.printStackTrace();
153 } catch (AuthenticatorException e) {
154 syncResult.stats.numAuthExceptions++;
155 e.printStackTrace();
156 } catch (IOException e) {
157 syncResult.stats.numIoExceptions++;
158 e.printStackTrace();
159 } catch (DavException e) {
160 syncResult.stats.numIoExceptions++;
161 e.printStackTrace();
162 } catch (Throwable t) {
163 // TODO update syncResult
164 Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);
165 t.printStackTrace();
166 }
167 }
168
169 private OCFile fillOCFile(WebdavEntry we) {
170 OCFile file = new OCFile(we.path());
171 file.setCreationTimestamp(we.createTimestamp());
172 file.setFileLength(we.contentLength());
173 file.setMimetype(we.contentType());
174 file.setModificationTimestamp(we.modifiedTimesamp());
175 file.setLastSyncDate(mCurrentSyncTime);
176 return file;
177 }
178
179
180 private void sendStickyBroadcast(boolean inProgress, String dirRemotePath) {
181 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
182 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
183 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
184 if (dirRemotePath != null) {
185 i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
186 }
187 getContext().sendStickyBroadcast(i);
188 }
189
190 }