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
51 AbstractThreadedSyncAdapter
{
53 private AccountManager accountManager
;
54 private Account account
;
55 private ContentProviderClient contentProvider
;
56 private Date lastUpdated
;
57 private DataStorageManager mStoreManager
;
59 private WebdavClient mClient
= null
;
61 public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) {
62 super(context
, autoInitialize
);
63 this.setAccountManager(AccountManager
.get(context
));
66 public AccountManager
getAccountManager() {
67 return accountManager
;
70 public void setAccountManager(AccountManager accountManager
) {
71 this.accountManager
= accountManager
;
74 public Account
getAccount() {
78 public void setAccount(Account account
) {
79 this.account
= account
;
82 public ContentProviderClient
getContentProvider() {
83 return contentProvider
;
86 public void setContentProvider(ContentProviderClient contentProvider
) {
87 this.contentProvider
= contentProvider
;
90 public Date
getLastUpdated() {
94 public void setLastUpdated(Date lastUpdated
) {
95 this.lastUpdated
= lastUpdated
;
98 public void setStorageManager(DataStorageManager storage_manager
) {
99 mStoreManager
= storage_manager
;
102 public DataStorageManager
getStorageManager() {
103 return mStoreManager
;
106 protected ConnectionKeepAliveStrategy
getKeepAliveStrategy() {
107 return new ConnectionKeepAliveStrategy() {
108 public long getKeepAliveDuration(HttpResponse response
,
109 HttpContext context
) {
110 // Change keep alive straategy basing on response: ie
111 // forbidden/not found/etc
112 // should have keep alive 0
113 // default return: 5s
114 int statusCode
= response
.getStatusLine().getStatusCode();
116 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
118 if ((statusCode
>= 400 && statusCode
<= 418)
119 || (statusCode
>= 421 && statusCode
<= 426)
120 || (statusCode
>= 500 && statusCode
<= 510)
121 || statusCode
== 118) {
130 protected HttpResponse
fireRawRequest(HttpRequest query
)
131 throws ClientProtocolException
, OperationCanceledException
,
132 AuthenticatorException
, IOException
{
134 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
135 * basicAuth = new BasicScheme();
136 * httpContext.setAttribute("preemptive-auth", basicAuth);
138 * HttpResponse response = getClient().execute(mHost, query,
144 protected Uri
getUri() {
145 return Uri
.parse(this.getAccountManager().getUserData(getAccount(),
146 AccountAuthenticator
.KEY_OC_URL
));
149 protected WebdavClient
getClient() throws OperationCanceledException
,
150 AuthenticatorException
, IOException
{
151 if (mClient
== null
) {
152 String username
= getAccount().name
.split("@")[0];
153 String password
= this.getAccountManager().blockingGetAuthToken(
154 getAccount(), AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
);
155 if (this.getAccountManager().getUserData(getAccount(),
156 AccountAuthenticator
.KEY_OC_URL
) == null
) {
157 throw new UnknownHostException();
161 mClient
= new WebdavClient(uri
);
162 mClient
.setCredentials(username
, password
);
163 mClient
.allowUnsignedCertificates();
164 // mHost = mClient.getTargetHost();