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
;
23 import org
.apache
.jackrabbit
.webdav
.DavException
;
24 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
25 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
27 import android
.accounts
.Account
;
28 import android
.accounts
.AuthenticatorException
;
29 import android
.accounts
.OperationCanceledException
;
30 import android
.content
.ContentProviderClient
;
31 import android
.content
.Context
;
32 import android
.content
.Intent
;
33 import android
.content
.SyncResult
;
34 import android
.os
.Bundle
;
35 import android
.util
.Log
;
36 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
37 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
38 import eu
.alefzero
.webdav
.WebdavEntry
;
41 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
42 * platform ContactOperations provider.
44 * @author Bartek Przybylski
46 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
48 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
49 super(context
, autoInitialize
);
53 public synchronized void onPerformSync(Account account
, Bundle extras
,
54 String authority
, ContentProviderClient provider
,
55 SyncResult syncResult
) {
57 this.setAccount(account
);
58 this.setContentProvider(provider
);
59 this.setStorageManager(new FileDataStorageManager(account
,
60 getContentProvider()));
62 Log
.d("ASD", "syncing owncloud account " + account
.name
);
64 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
65 i
.putExtra(FileSyncService
.IN_PROGRESS
, true
);
66 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, account
.name
);
67 getContext().sendStickyBroadcast(i
);
71 Log
.e("ASD", getUri().toString());
72 query
= new PropFindMethod(getUri().toString() + "/");
73 getClient().executeMethod(query
);
74 MultiStatus resp
= null
;
75 resp
= query
.getResponseBodyAsMultiStatus();
77 if (resp
.getResponses().length
> 0) {
78 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
79 OCFile file
= fillOCFile(we
);
81 getStorageManager().saveFile(file
);
82 fetchData(getUri().toString(), syncResult
, file
.getFileId());
84 } catch (OperationCanceledException e
) {
86 } catch (AuthenticatorException e
) {
87 syncResult
.stats
.numAuthExceptions
++;
89 } catch (IOException e
) {
90 syncResult
.stats
.numIoExceptions
++;
92 } catch (DavException e
) {
93 syncResult
.stats
.numIoExceptions
++;
96 i
.putExtra(FileSyncService
.IN_PROGRESS
, false
);
97 getContext().sendStickyBroadcast(i
);
100 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
102 PropFindMethod query
= new PropFindMethod(uri
);
103 getClient().executeMethod(query
);
104 MultiStatus resp
= null
;
105 resp
= query
.getResponseBodyAsMultiStatus();
106 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
107 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
108 OCFile file
= fillOCFile(we
);
109 file
.setParentId(parentId
);
110 getStorageManager().saveFile(file
);
112 parentId
= file
.getFileId();
113 if (we
.contentType().equals("DIR"))
114 fetchData(getUri().toString() + we
.path(), syncResult
,
117 } catch (OperationCanceledException e
) {
119 } catch (AuthenticatorException e
) {
120 syncResult
.stats
.numAuthExceptions
++;
122 } catch (IOException e
) {
123 syncResult
.stats
.numIoExceptions
++;
125 } catch (DavException e
) {
126 syncResult
.stats
.numIoExceptions
++;
131 private OCFile
fillOCFile(WebdavEntry we
) {
132 OCFile file
= new OCFile(we
.path());
133 file
.setCreationTimestamp(we
.createTimestamp());
134 file
.setFileLength(we
.contentLength());
135 file
.setMimetype(we
.contentType());
136 file
.setModificationTimestamp(we
.modifiedTimesamp());