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 com
.owncloud
.android
.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 com
.owncloud
.android
.AccountUtils
;
32 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
33 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
35 import android
.accounts
.Account
;
36 import android
.accounts
.AccountManager
;
37 import android
.accounts
.AuthenticatorException
;
38 import android
.accounts
.OperationCanceledException
;
39 import android
.content
.AbstractThreadedSyncAdapter
;
40 import android
.content
.ContentProviderClient
;
41 import android
.content
.Context
;
42 import android
.net
.Uri
;
43 import eu
.alefzero
.webdav
.WebdavClient
;
46 * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete
47 * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc..
52 public abstract class AbstractOwnCloudSyncAdapter
extends
53 AbstractThreadedSyncAdapter
{
55 private AccountManager accountManager
;
56 private Account account
;
57 private ContentProviderClient contentProvider
;
58 private Date lastUpdated
;
59 private DataStorageManager mStoreManager
;
61 private WebdavClient mClient
= null
;
63 public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) {
64 super(context
, autoInitialize
);
65 this.setAccountManager(AccountManager
.get(context
));
68 public AccountManager
getAccountManager() {
69 return accountManager
;
72 public void setAccountManager(AccountManager accountManager
) {
73 this.accountManager
= accountManager
;
76 public Account
getAccount() {
80 public void setAccount(Account account
) {
81 this.account
= account
;
84 public ContentProviderClient
getContentProvider() {
85 return contentProvider
;
88 public void setContentProvider(ContentProviderClient contentProvider
) {
89 this.contentProvider
= contentProvider
;
92 public Date
getLastUpdated() {
96 public void setLastUpdated(Date lastUpdated
) {
97 this.lastUpdated
= lastUpdated
;
100 public void setStorageManager(DataStorageManager storage_manager
) {
101 mStoreManager
= storage_manager
;
104 public DataStorageManager
getStorageManager() {
105 return mStoreManager
;
108 protected ConnectionKeepAliveStrategy
getKeepAliveStrategy() {
109 return new ConnectionKeepAliveStrategy() {
110 public long getKeepAliveDuration(HttpResponse response
,
111 HttpContext context
) {
112 // Change keep alive straategy basing on response: ie
113 // forbidden/not found/etc
114 // should have keep alive 0
115 // default return: 5s
116 int statusCode
= response
.getStatusLine().getStatusCode();
118 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
120 if ((statusCode
>= 400 && statusCode
<= 418)
121 || (statusCode
>= 421 && statusCode
<= 426)
122 || (statusCode
>= 500 && statusCode
<= 510)
123 || statusCode
== 118) {
132 protected HttpResponse
fireRawRequest(HttpRequest query
)
133 throws ClientProtocolException
, OperationCanceledException
,
134 AuthenticatorException
, IOException
{
136 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
137 * basicAuth = new BasicScheme();
138 * httpContext.setAttribute("preemptive-auth", basicAuth);
140 * HttpResponse response = getClient().execute(mHost, query,
146 protected Uri
getUri() {
147 return Uri
.parse(AccountUtils
.constructFullURLForAccount(getContext(), getAccount()));
150 protected WebdavClient
getClient() throws /*OperationCanceledException,
151 AuthenticatorException,*/ IOException
{
152 if (mClient
== null
) {
153 if (AccountUtils
.constructFullURLForAccount(getContext(), getAccount()) == null
) {
154 throw new UnknownHostException();
156 mClient
= OwnCloudClientUtils
.createOwnCloudClient(account
, getContext());