sync progress indicator
[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
23 import org.apache.jackrabbit.webdav.DavException;
24 import org.apache.jackrabbit.webdav.MultiStatus;
25 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
26
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;
39
40 /**
41 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
42 * platform ContactOperations provider.
43 *
44 * @author Bartek Przybylski
45 */
46 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
47
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";
51
52 public FileSyncAdapter(Context context, boolean autoInitialize) {
53 super(context, autoInitialize);
54 }
55
56 @Override
57 public synchronized void onPerformSync(
58 Account account,
59 Bundle extras,
60 String authority,
61 ContentProviderClient provider,
62 SyncResult syncResult) {
63
64 this.setAccount(account);
65 this.setContentProvider(provider);
66 this.setStorageManager(new FileDataStorageManager(account, getContentProvider()));
67
68 Intent i = new Intent(SYNC_MESSAGE);
69 i.putExtra(IN_PROGRESS, true);
70 i.putExtra("ACCOUNT_NAME", account.name);
71 getContext().sendStickyBroadcast(i);
72
73 PropFindMethod query;
74 try {
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);
82 file.setParentId(0);
83 getStorageManager().saveFile(file);
84 fetchData(getUri().toString(), syncResult, file.getFileId());
85 }
86 } catch (OperationCanceledException e) {
87 e.printStackTrace();
88 } catch (AuthenticatorException e) {
89 syncResult.stats.numAuthExceptions++;
90 e.printStackTrace();
91 } catch (IOException e) {
92 syncResult.stats.numIoExceptions++;
93 e.printStackTrace();
94 } catch (DavException e) {
95 syncResult.stats.numIoExceptions++;
96 e.printStackTrace();
97 }
98 i.putExtra(IN_PROGRESS, false);
99 getContext().sendStickyBroadcast(i);
100 }
101
102 private void fetchData(String uri, SyncResult syncResult, long parentId) {
103 try {
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());
116 }
117 } catch (OperationCanceledException e) {
118 e.printStackTrace();
119 } catch (AuthenticatorException e) {
120 syncResult.stats.numAuthExceptions++;
121 e.printStackTrace();
122 } catch (IOException e) {
123 syncResult.stats.numIoExceptions++;
124 e.printStackTrace();
125 } catch (DavException e) {
126 syncResult.stats.numIoExceptions++;
127 e.printStackTrace();
128 }
129 }
130
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());
137 return file;
138 }
139
140 }