5bede732a1ed66800737106f6144207dd5021ec4
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / syncadapter / SyncAdapter.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 import java.net.UnknownHostException;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.LinkedList;
26
27 import org.apache.http.HttpEntity;
28 import org.apache.http.HttpHost;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.auth.AuthScope;
31 import org.apache.http.auth.UsernamePasswordCredentials;
32 import org.apache.http.conn.ConnectionKeepAliveStrategy;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.auth.BasicScheme;
35 import org.apache.http.impl.client.DefaultHttpClient;
36 import org.apache.http.protocol.BasicHttpContext;
37 import org.apache.http.protocol.HttpContext;
38
39 import android.accounts.Account;
40 import android.accounts.AccountManager;
41 import android.accounts.AuthenticatorException;
42 import android.accounts.OperationCanceledException;
43 import android.content.AbstractThreadedSyncAdapter;
44 import android.content.ContentProviderClient;
45 import android.content.ContentValues;
46 import android.content.Context;
47 import android.content.SyncResult;
48 import android.database.Cursor;
49 import android.net.Uri;
50 import android.os.Bundle;
51 import android.os.RemoteException;
52 import android.text.TextUtils;
53 import android.util.Log;
54 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
55 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
56 import eu.alefzero.webdav.HttpPropFind;
57 import eu.alefzero.webdav.TreeNode;
58 import eu.alefzero.webdav.WebdavUtils;
59 import eu.alefzero.webdav.TreeNode.NodeProperty;
60
61 /**
62 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
63 * platform ContactOperations provider.
64 */
65 public class SyncAdapter extends AbstractThreadedSyncAdapter {
66 private static final String TAG = "SyncAdapter";
67
68 private final AccountManager mAccountManager;
69 private Account mAccount;
70 private ContentProviderClient mContentProvider;
71 private final Context mContext;
72
73 private Date mLastUpdated;
74
75 public SyncAdapter(Context context, boolean autoInitialize) {
76 super(context, autoInitialize);
77 mContext = context;
78 mAccountManager = AccountManager.get(context);
79 }
80
81 @Override
82 public synchronized void onPerformSync(Account account, Bundle extras, String authority,
83 ContentProviderClient provider, SyncResult syncResult) {
84 mAccount = account;
85 mContentProvider = provider;
86 try {
87 String username = account.name.split("@")[0];
88 String password = mAccountManager.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
89 if (mAccountManager.getUserData(account, AccountAuthenticator.KEY_OC_URL) == null) {
90 throw new UnknownHostException();
91 }
92 Uri uri = Uri.parse(mAccountManager.getUserData(account, AccountAuthenticator.KEY_OC_URL));
93 Log.i(TAG, "Syncing owncloud account: " + account.name + " on url: " + uri.toString());
94
95 DefaultHttpClient client = new DefaultHttpClient();
96 client.getCredentialsProvider().setCredentials(
97 new AuthScope(uri.getHost(), (uri.getPort() == -1)?80:uri.getPort()),
98 new UsernamePasswordCredentials(username, password));
99 client.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
100 public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
101 // TODO: change keep alive straategy basing on response: ie forbidden/not found/etc
102 // should have keep alive 0
103 // default return: 5s
104 return 5 * 1000;
105 }
106 });
107
108 BasicHttpContext httpContext = new BasicHttpContext();
109 BasicScheme basicAuth = new BasicScheme();
110 httpContext.setAttribute("preemptive-auth", basicAuth);
111 HttpHost targetHost = new HttpHost(uri.getHost(), (uri.getPort() == -1)
112 ? 80
113 : uri.getPort(), (uri.getScheme() == "https") ? "https" : "http");
114
115 HttpPropFind query = new HttpPropFind(uri.toString());
116 query.setHeader("Content-type", "text/xml");
117 query.setHeader("User-Agent", "Android-ownCloud");
118 HttpEntity entity = new StringEntity(WebdavUtils.prepareXmlForPropFind());
119 query.setEntity(entity);
120 HttpResponse response = client.execute(targetHost, query, httpContext);
121 /*try {
122 mContentProvider.delete(ProviderTableMeta.CONTENT_URI,
123 ProviderTableMeta.FILE_NAME + " LIKE '%' AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER +"=?"
124 , new String[]{account.name});
125 } catch (RemoteException e) {
126 e.printStackTrace();
127 return;
128 }*/
129 TreeNode root = new TreeNode();
130 root.setProperty(TreeNode.NodeProperty.NAME, "/");
131 parseResponse(response, uri, client, targetHost, httpContext, root.getChildList());
132
133 commitToDatabase(root, null);
134
135 } catch (OperationCanceledException e) {
136 e.printStackTrace();
137 } catch (AuthenticatorException e) {
138 syncResult.stats.numAuthExceptions++;
139 e.printStackTrace();
140 } catch (IOException e) {
141 syncResult.stats.numIoExceptions++;
142 e.printStackTrace();
143 } catch (RemoteException e) {
144 e.printStackTrace();
145 }
146 }
147
148 private void commitToDatabase(TreeNode root, String parentId) throws RemoteException {
149 for (TreeNode n : root.getChildList()) {
150 Log.d(TAG, n.toString());
151 ContentValues cv = new ContentValues();
152 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, n.getProperty(NodeProperty.CONTENT_LENGTH));
153 cv.put(ProviderTableMeta.FILE_MODIFIED, n.getProperty(NodeProperty.LAST_MODIFIED_DATE));
154 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, n.getProperty(NodeProperty.RESOURCE_TYPE));
155 cv.put(ProviderTableMeta.FILE_PARENT, parentId);
156
157 String name = n.getProperty(NodeProperty.NAME),
158 path = n.getProperty(NodeProperty.PATH);
159 Cursor c = mContentProvider.query(ProviderTableMeta.CONTENT_URI_FILE,
160 null,
161 ProviderTableMeta.FILE_NAME+"=? AND " + ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
162 new String[]{name, path, mAccount.name},
163 null);
164 if (c.moveToFirst()) {
165 mContentProvider.update(ProviderTableMeta.CONTENT_URI,
166 cv,
167 ProviderTableMeta._ID+"=?",
168 new String[]{c.getString(c.getColumnIndex(ProviderTableMeta._ID))});
169 Log.d(TAG, "ID of: "+name+":"+c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
170 } else {
171 cv.put(ProviderTableMeta.FILE_NAME, n.getProperty(NodeProperty.NAME));
172 cv.put(ProviderTableMeta.FILE_PATH, n.getProperty(NodeProperty.PATH));
173 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
174 Uri entry = mContentProvider.insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
175 Log.d(TAG, "Inserting new entry " + path + name);
176 c = mContentProvider.query(entry, null, null, null, null);
177 c.moveToFirst();
178 }
179 if (n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
180 commitToDatabase(n, c.getString(c.getColumnIndex(ProviderTableMeta._ID)));
181 }
182 }
183 // clean removed files
184 String[] selection = new String[root.getChildList().size()+2];
185 selection[0] = mAccount.name;
186 selection[1] = parentId;
187 String qm = "";
188 for (int i = 2; i < selection.length-1; ++i) {
189 qm += "?,";
190 selection[i] = root.getChildList().get(i-2).getProperty(NodeProperty.NAME);
191 }
192 if (selection.length >= 3) {
193 selection[selection.length-1] = root.getChildrenNames()[selection.length-3];
194 qm += "?";
195 }
196 for (int i = 0; i < selection.length; ++i) {
197 Log.d(TAG,selection[i]+"");
198 }
199 Log.d(TAG,"Removing files "+ parentId);
200 mContentProvider.delete(ProviderTableMeta.CONTENT_URI,
201 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=? AND " + ProviderTableMeta.FILE_PARENT + (parentId==null?" IS ":"=")+"? AND " + ProviderTableMeta.FILE_NAME + " NOT IN ("+qm+")",
202 selection);
203 }
204
205 private void parseResponse(HttpResponse resp, Uri uri, DefaultHttpClient client, HttpHost targetHost, BasicHttpContext httpContext, LinkedList<TreeNode> insertList) throws IOException {
206 boolean skipFirst = true;
207 for (TreeNode n :WebdavUtils.parseResponseToNodes(resp.getEntity().getContent())) {
208 String path = n.stripPathFromFilename(uri.getPath());
209 if (skipFirst) {
210 skipFirst = false;
211 continue;
212 }
213 insertList.add(n);
214
215 if (!TextUtils.isEmpty(n.getProperty(NodeProperty.NAME)) &&
216 n.getProperty(NodeProperty.RESOURCE_TYPE).equals("DIR")) {
217 HttpPropFind method = new HttpPropFind(uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
218 Log.i(TAG, uri.getPath() + path + n.getProperty(NodeProperty.NAME).replace(" ", "%20") + "/");
219 Log.i(TAG, method.getRequestLine().toString());
220 HttpResponse response = client.execute(targetHost, method, httpContext);
221 parseResponse(response, uri, client, targetHost, httpContext, n.getChildList());
222 }
223 }
224 }
225 }