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 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 static final String SYNC_MESSAGE
= "eu.alefzero.owncloud.files.ACCOUNT_SYNC";
49 public static final String IN_PROGRESS
= "sync_in_progress";
50 public static final String ACCOUNT_NAME
= "account_name";
52 public FileSyncAdapter(Context context
, boolean autoInitialize
) {
53 super(context
, autoInitialize
);
57 public synchronized void onPerformSync(
61 ContentProviderClient provider
,
62 SyncResult syncResult
) {
64 this.setAccount(account
);
65 this.setContentProvider(provider
);
66 this.setStorageManager(new FileDataStorageManager(account
, getContentProvider()));
68 Intent i
= new Intent(SYNC_MESSAGE
);
69 i
.putExtra(IN_PROGRESS
, true
);
70 i
.putExtra("ACCOUNT_NAME", account
.name
);
71 getContext().sendStickyBroadcast(i
);
75 query
= new PropFindMethod(getUri().toString());
76 getClient().executeMethod(query
);
77 MultiStatus resp
= null
;
78 resp
= query
.getResponseBodyAsMultiStatus();
79 if (resp
.getResponses().length
> 0) {
80 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0]);
81 OCFile file
= fillOCFile(we
);
83 getStorageManager().saveFile(file
);
84 fetchData(getUri().toString(), syncResult
, file
.getFileId());
86 } catch (OperationCanceledException e
) {
88 } catch (AuthenticatorException e
) {
89 syncResult
.stats
.numAuthExceptions
++;
91 } catch (IOException e
) {
92 syncResult
.stats
.numIoExceptions
++;
94 } catch (DavException e
) {
95 syncResult
.stats
.numIoExceptions
++;
98 i
.putExtra(IN_PROGRESS
, false
);
99 getContext().sendStickyBroadcast(i
);
102 private void fetchData(String uri
, SyncResult syncResult
, long parentId
) {
104 PropFindMethod query
= new PropFindMethod(uri
);
105 getClient().executeMethod(query
);
106 MultiStatus resp
= null
;
107 resp
= query
.getResponseBodyAsMultiStatus();
108 for (int i
= 1; i
< resp
.getResponses().length
; ++i
) {
109 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[i
]);
110 OCFile file
= fillOCFile(we
);
111 file
.setParentId(parentId
);
112 getStorageManager().saveFile(file
);
113 if (parentId
== 0) parentId
= file
.getFileId();
114 if (we
.contentType().equals("DIR"))
115 fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId());
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());