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