Small improvements to the refresh of the files list and memory handling while synchro...
[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 }
104 sendStickyBroadcast(false, -1);
105 }
106
107 private void fetchData(String uri, SyncResult syncResult, long parentId) {
108 try {
109 PropFindMethod query = new PropFindMethod(uri);
110 getClient().executeMethod(query);
111 MultiStatus resp = null;
112 resp = query.getResponseBodyAsMultiStatus();
113 Queue<String> paths = new LinkedList<String>();
114 Queue<Long> fileIds = new LinkedList<Long>();
115 for (int i = 1; i < resp.getResponses().length; ++i) {
116 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());
117 OCFile file = fillOCFile(we);
118 file.setParentId(parentId);
119 getStorageManager().saveFile(file);
120 if (parentId == 0)
121 parentId = file.getFileId();
122 if (we.contentType().equals("DIR")) {
123 // for recursive fetch later
124 paths.add(we.path());
125 fileIds.add(file.getFileId());
126 }
127 }
128
129 Vector<OCFile> files = getStorageManager().getDirectoryContent(
130 getStorageManager().getFileById(parentId));
131 for (OCFile file : files) {
132 if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)
133 getStorageManager().removeFile(file);
134 }
135
136 // synched folder -> notice to IU
137 sendStickyBroadcast(true, parentId);
138
139 // recursive fetch
140 while(!paths.isEmpty()) {
141 fetchData(getUri().toString() + paths.remove(), syncResult, fileIds.remove());
142 }
143 paths = null;
144 fileIds = null;
145
146
147 } catch (OperationCanceledException e) {
148 e.printStackTrace();
149 } catch (AuthenticatorException e) {
150 syncResult.stats.numAuthExceptions++;
151 e.printStackTrace();
152 } catch (IOException e) {
153 syncResult.stats.numIoExceptions++;
154 e.printStackTrace();
155 } catch (DavException e) {
156 syncResult.stats.numIoExceptions++;
157 e.printStackTrace();
158 }
159 }
160
161 private OCFile fillOCFile(WebdavEntry we) {
162 OCFile file = new OCFile(we.path());
163 file.setCreationTimestamp(we.createTimestamp());
164 file.setFileLength(we.contentLength());
165 file.setMimetype(we.contentType());
166 file.setModificationTimestamp(we.modifiedTimesamp());
167 file.setLastSyncDate(mCurrentSyncTime);
168 return file;
169 }
170
171
172 private void sendStickyBroadcast(boolean inProgress, long OCDirId) {
173 Intent i = new Intent(FileSyncService.SYNC_MESSAGE);
174 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
175 i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);
176 if (OCDirId > 0) {
177 i.putExtra(FileSyncService.SYNC_FOLDER, OCDirId);
178 }
179 getContext().sendStickyBroadcast(i);
180 }
181
182 }