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
.content
.IntentSender
.SendIntentException
;
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
;
42 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
43 * platform ContactOperations provider.
45 * @author Bartek Przybylski
47 public class FileSyncAdapter
extends AbstractOwnCloudSyncAdapter
{
49 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
50 super(context
, autoInitialize
);
54 public synchronized void onPerformSync(
58 ContentProviderClient provider
,
59 SyncResult syncResult
) {
61 this.setAccount(account
);
62 this.setContentProvider(provider
);
63 this.setStorageManager(new FileDataStorageManager(account
, getContentProvider()));
65 Intent i
= new Intent(FileSyncService
.SYNC_MESSAGE
);
66 i
.putExtra(FileSyncService
.IN_PROGRESS
, true
);
67 i
.putExtra(FileSyncService
.ACCOUNT_NAME
, account
.name
);
68 getContext().sendStickyBroadcast(i
);
72 Log
.e("ASD", getUri().toString());
73 query
= new PropFindMethod(getUri().toString()+"/");
74 getClient().executeMethod(query
);
75 MultiStatus resp
= null
;
76 resp
= query
.getResponseBodyAsMultiStatus();
78 if (resp
.getResponses().length
> 0) {
79 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
80 OCFile file
= fillOCFile(we
);
82 getStorageManager().saveFile(file
);
83 fetchData(getUri().toString(), syncResult
, file
.getFileId());
85 } catch (OperationCanceledException e
) {
87 } catch (AuthenticatorException e
) {
88 syncResult
.stats
.numAuthExceptions
++;
90 } catch (IOException e
) {
91 syncResult
.stats
.numIoExceptions
++;
93 } catch (DavException e
) {
94 syncResult
.stats
.numIoExceptions
++;
97 i
.putExtra(FileSyncService
.IN_PROGRESS
, false
);
98 getContext().sendStickyBroadcast(i
);
101 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
103 PropFindMethod query
= new PropFindMethod(uri
);
104 getClient().executeMethod(query
);
105 MultiStatus resp
= null
;
106 resp
= query
.getResponseBodyAsMultiStatus();
107 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
108 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
109 OCFile file
= fillOCFile(we
);
110 file
.setParentId(parentId
);
111 getStorageManager().saveFile(file
);
112 if (parentId
== 0) parentId
= file
.getFileId();
113 if (we
.contentType().equals("DIR"))
114 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
116 } catch (OperationCanceledException e
) {
118 } catch (AuthenticatorException e
) {
119 syncResult
.stats
.numAuthExceptions
++;
121 } catch (IOException e
) {
122 syncResult
.stats
.numIoExceptions
++;
124 } catch (DavException e
) {
125 syncResult
.stats
.numIoExceptions
++;
130 private OCFile
fillOCFile(WebdavEntry we
) {
131 OCFile file
= new OCFile(we
.path());
132 file
.setCreationTimestamp(we
.createTimestamp());
133 file
.setFileLength(we
.contentLength());
134 file
.setMimetype(we
.contentType());
135 file
.setModificationTimestamp(we
.modifiedTimesamp());