1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
.authentication
.AccountUtils
;
32 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
33 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
34 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
36 import android
.accounts
.Account
;
37 import android
.accounts
.AccountManager
;
38 import android
.accounts
.AuthenticatorException
;
39 import android
.accounts
.OperationCanceledException
;
40 import android
.content
.AbstractThreadedSyncAdapter
;
41 import android
.content
.ContentProviderClient
;
42 import android
.content
.Context
;
43 import eu
.alefzero
.webdav
.WebdavClient
;
46 * Base synchronization adapter for ownCloud designed to be subclassed for different
47 * resource types, like FileSync, ConcatsSync, CalendarSync, etc..
49 * Implements the standard {@link AbstractThreadedSyncAdapter}.
52 * @author David A. Velasco
54 public abstract class AbstractOwnCloudSyncAdapter
extends
55 AbstractThreadedSyncAdapter
{
57 private AccountManager accountManager
;
58 private Account account
;
59 private ContentProviderClient contentProvider
;
60 //private Date lastUpdated;
61 private DataStorageManager mStoreManager
;
63 private WebdavClient mClient
= null
;
65 public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) {
66 super(context
, autoInitialize
);
67 this.setAccountManager(AccountManager
.get(context
));
70 public AccountManager
getAccountManager() {
71 return accountManager
;
74 public void setAccountManager(AccountManager accountManager
) {
75 this.accountManager
= accountManager
;
78 public Account
getAccount() {
82 public void setAccount(Account account
) {
83 this.account
= account
;
86 public ContentProviderClient
getContentProvider() {
87 return contentProvider
;
90 public void setContentProvider(ContentProviderClient contentProvider
) {
91 this.contentProvider
= contentProvider
;
94 public void setStorageManager(DataStorageManager storage_manager
) {
95 mStoreManager
= storage_manager
;
98 public DataStorageManager
getStorageManager() {
102 protected void initClientForCurrentAccount() throws OperationCanceledException
, AuthenticatorException
, IOException
, AccountNotFoundException
{
103 AccountUtils
.constructFullURLForAccount(getContext(), account
);
104 mClient
= OwnCloudClientUtils
.createOwnCloudClient(account
, getContext());
107 protected WebdavClient
getClient() {
112 /* method called by ContactSyncAdapter, that is never used */
113 protected HttpResponse
fireRawRequest(HttpRequest query
)
114 throws ClientProtocolException
, OperationCanceledException
,
115 AuthenticatorException
, IOException
{
117 * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme
118 * basicAuth = new BasicScheme();
119 * httpContext.setAttribute("preemptive-auth", basicAuth);
121 * HttpResponse response = getClient().execute(mHost, query,
127 /* methods never used below */
129 public Date getLastUpdated() {
133 public void setLastUpdated(Date lastUpdated) {
134 this.lastUpdated = lastUpdated;
137 protected ConnectionKeepAliveStrategy getKeepAliveStrategy() {
138 return new ConnectionKeepAliveStrategy() {
139 public long getKeepAliveDuration(HttpResponse response,
140 HttpContext context) {
141 // Change keep alive straategy basing on response: ie
142 // forbidden/not found/etc
143 // should have keep alive 0
144 // default return: 5s
145 int statusCode = response.getStatusLine().getStatusCode();
147 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed
149 if ((statusCode >= 400 && statusCode <= 418)
150 || (statusCode >= 421 && statusCode <= 426)
151 || (statusCode >= 500 && statusCode <= 510)
152 || statusCode == 118) {