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
.Date
;
25 import org
.apache
.http
.HttpRequest
;
26 import org
.apache
.http
.HttpResponse
;
27 import org
.apache
.http
.client
.ClientProtocolException
;
28 import org
.apache
.http
.conn
.ConnectionKeepAliveStrategy
;
29 import org
.apache
.http
.protocol
.HttpContext
;
31 import android
.accounts
.Account
;
32 import android
.accounts
.AccountManager
;
33 import android
.accounts
.AuthenticatorException
;
34 import android
.accounts
.OperationCanceledException
;
35 import android
.content
.AbstractThreadedSyncAdapter
;
36 import android
.content
.ContentProviderClient
;
37 import android
.content
.Context
;
38 import android
.net
.Uri
;
39 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
40 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
41 import eu
.alefzero
.webdav
.WebdavClient
;
44 * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete
45 * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
50 public abstract class AbstractOwnCloudSyncAdapter
extends AbstractThreadedSyncAdapter
{
52 private AccountManager accountManager
;
53 private Account account
;
54 private ContentProviderClient contentProvider
;
55 private Date lastUpdated
;
56 private DataStorageManager mStoreManager
;
58 private WebdavClient mClient
= null
;
60 public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) {
61 super(context
, autoInitialize
);
62 this.setAccountManager(AccountManager
.get(context
));
65 public AccountManager
getAccountManager() {
66 return accountManager
;
69 public void setAccountManager(AccountManager accountManager
) {
70 this.accountManager
= accountManager
;
73 public Account
getAccount() {
77 public void setAccount(Account account
) {
78 this.account
= account
;
81 public ContentProviderClient
getContentProvider() {
82 return contentProvider
;
85 public void setContentProvider(ContentProviderClient contentProvider
) {
86 this.contentProvider
= contentProvider
;
89 public Date
getLastUpdated() {
93 public void setLastUpdated(Date lastUpdated
) {
94 this.lastUpdated
= lastUpdated
;
97 public void setStorageManager(DataStorageManager storage_manager
) {
98 mStoreManager
= storage_manager
;
101 public DataStorageManager
getStorageManager() {
102 return mStoreManager
;
105 protected ConnectionKeepAliveStrategy
getKeepAliveStrategy() {
106 return new ConnectionKeepAliveStrategy() {
107 public long getKeepAliveDuration(HttpResponse response
,
108 HttpContext context
) {
109 // Change keep alive straategy basing on response: ie
110 // forbidden/not found/etc
111 // should have keep alive 0
112 // default return: 5s
113 int statusCode
= response
.getStatusLine().getStatusCode();
115 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
117 if ((statusCode
>= 400 && statusCode
<= 418)
118 || (statusCode
>= 421 && statusCode
<= 426)
119 || (statusCode
>= 500 && statusCode
<= 510)
120 || statusCode
== 118) {
129 protected HttpResponse
fireRawRequest(HttpRequest query
)
130 throws ClientProtocolException
, OperationCanceledException
,
131 AuthenticatorException
, IOException
{
132 /*BasicHttpContext httpContext = new BasicHttpContext();
133 BasicScheme basicAuth = new BasicScheme();
134 httpContext.setAttribute("preemptive-auth", basicAuth);
136 HttpResponse response = getClient().execute(mHost, query, httpContext);*/
140 protected Uri
getUri() {
141 return Uri
.parse(this.getAccountManager().getUserData(getAccount(),
142 AccountAuthenticator
.KEY_OC_URL
));
145 protected WebdavClient
getClient() throws OperationCanceledException
,
146 AuthenticatorException
, IOException
{
147 if (mClient
== null
) {
148 String username
= getAccount().name
.split("@")[0];
149 String password
= this.getAccountManager().blockingGetAuthToken(
150 getAccount(), AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
151 if (this.getAccountManager().getUserData(getAccount(),
152 AccountAuthenticator
.KEY_OC_URL
) == null
) {
153 throw new UnknownHostException();
157 mClient
= new WebdavClient(uri
);
158 mClient
.setCredentials(username
, password
);
159 mClient
.allowUnsignedCertificates();
160 //mHost = mClient.getTargetHost();