1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.syncadapter
;
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
;
27 import org
.apache
.jackrabbit
.webdav
.DavException
;
28 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
29 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
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
;
46 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
47 * platform ContactOperations provider.
49 * @author Bartek Przybylski
51 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
53 private final static String TAG
= "FileSyncAdapter";
55 private long mCurrentSyncTime
;
57 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
58 super(context
, autoInitialize
);
62 public synchronized void onPerformSync(Account account
, Bundle extras
,
63 String authority
, ContentProviderClient provider
,
64 SyncResult syncResult
) {
66 this.setAccount(account
);
67 this.setContentProvider(provider
);
68 this.setStorageManager(new FileDataStorageManager(account
,
69 getContentProvider()));
71 Log
.d(TAG
, "syncing owncloud account " + account
.name
);
73 sendStickyBroadcast(true
, -1); // starting message to UI
77 mCurrentSyncTime
= System
.currentTimeMillis();
78 query
= new PropFindMethod(getUri().toString() + "/");
79 getClient().executeMethod(query
);
80 MultiStatus resp
= null
;
81 resp
= query
.getResponseBodyAsMultiStatus();
83 if (resp
.getResponses().length
> 0) {
84 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath());
85 OCFile file
= fillOCFile(we
);
87 getStorageManager().saveFile(file
);
88 fetchData(getUri().toString(), syncResult
, file
.getFileId());
90 } catch (OperationCanceledException e
) {
92 } catch (AuthenticatorException e
) {
93 syncResult
.stats
.numAuthExceptions
++;
95 } catch (IOException e
) {
96 syncResult
.stats
.numIoExceptions
++;
98 } catch (DavException e
) {
99 syncResult
.stats
.numIoExceptions
++;
102 sendStickyBroadcast(false
, -1);
105 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
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
);
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());
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
);
134 // synched folder -> notice to IU
135 sendStickyBroadcast(true
, parentId
);
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());
145 } catch (OperationCanceledException e
) {
147 } catch (AuthenticatorException e
) {
148 syncResult
.stats
.numAuthExceptions
++;
150 } catch (IOException e
) {
151 syncResult
.stats
.numIoExceptions
++;
153 } catch (DavException e
) {
154 syncResult
.stats
.numIoExceptions
++;
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
);
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
);
175 i
.putExtra(FileSyncService
.SYNC_FOLDER
, OCDirId
);
177 getContext().sendStickyBroadcast(i
);