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
;
22 import java
.net
.UnknownHostException
;
23 import java
.util
.Arrays
;
24 import java
.util
.Date
;
25 import java
.util
.LinkedList
;
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
;
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
;
62 * SyncAdapter implementation for syncing sample SyncAdapter contacts to the
63 * platform ContactOperations provider.
65 public class SyncAdapter
extends AbstractThreadedSyncAdapter
{
66 private static final String TAG
= "SyncAdapter";
68 private final AccountManager mAccountManager
;
69 private Account mAccount
;
70 private ContentProviderClient mContentProvider
;
71 private final Context mContext
;
73 private Date mLastUpdated
;
75 public SyncAdapter(Context context
, boolean autoInitialize
) {
76 super(context
, autoInitialize
);
78 mAccountManager
= AccountManager
.get(context
);
82 public synchronized void onPerformSync(Account account
, Bundle extras
, String authority
,
83 ContentProviderClient provider
, SyncResult syncResult
) {
85 mContentProvider
= provider
;
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();
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());
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
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)
113 : uri
.getPort(), (uri
.getScheme() == "https") ?
"https" : "http");
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
);
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) {
129 TreeNode root
= new TreeNode();
130 root
.setProperty(TreeNode
.NodeProperty
.NAME
, "/");
131 parseResponse(response
, uri
, client
, targetHost
, httpContext
, root
.getChildList());
133 commitToDatabase(root
, null
);
135 } catch (OperationCanceledException e
) {
137 } catch (AuthenticatorException e
) {
138 syncResult
.stats
.numAuthExceptions
++;
140 } catch (IOException e
) {
141 syncResult
.stats
.numIoExceptions
++;
143 } catch (RemoteException e
) {
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
);
157 String name
= n
.getProperty(NodeProperty
.NAME
),
158 path
= n
.getProperty(NodeProperty
.PATH
);
159 Cursor c
= mContentProvider
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
161 ProviderTableMeta
.FILE_NAME
+"=? AND " + ProviderTableMeta
.FILE_PATH
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
162 new String
[]{name
, path
, mAccount
.name
},
164 if (c
.moveToFirst()) {
165 mContentProvider
.update(ProviderTableMeta
.CONTENT_URI
,
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
)));
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
);
179 if (n
.getProperty(NodeProperty
.RESOURCE_TYPE
).equals("DIR")) {
180 commitToDatabase(n
, c
.getString(c
.getColumnIndex(ProviderTableMeta
._ID
)));
183 // clean removed files
184 String
[] selection
= new String
[root
.getChildList().size()+2];
185 selection
[0] = mAccount
.name
;
186 selection
[1] = parentId
;
188 for (int i
= 2; i
< selection
.length
-1; ++i
) {
190 selection
[i
] = root
.getChildList().get(i
-2).getProperty(NodeProperty
.NAME
);
192 if (selection
.length
>= 3) {
193 selection
[selection
.length
-1] = root
.getChildrenNames()[selection
.length
-3];
196 for (int i
= 0; i
< selection
.length
; ++i
) {
197 Log
.d(TAG
,selection
[i
]+"");
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
+")",
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());
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());