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 de
.mobilcom
.debitel
.cloud
.android
.syncadapter
; 
  21 import java
.io
.IOException
; 
  22 import java
.util
.Date
; 
  24 import org
.apache
.http
.HttpRequest
; 
  25 import org
.apache
.http
.HttpResponse
; 
  26 import org
.apache
.http
.client
.ClientProtocolException
; 
  27 import org
.apache
.http
.conn
.ConnectionKeepAliveStrategy
; 
  28 import org
.apache
.http
.protocol
.HttpContext
; 
  30 import de
.mobilcom
.debitel
.cloud
.android
.authentication
.AccountUtils
; 
  31 import de
.mobilcom
.debitel
.cloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
; 
  32 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.DataStorageManager
; 
  33 import de
.mobilcom
.debitel
.cloud
.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 eu
.alefzero
.webdav
.WebdavClient
; 
  45  * Base SyncAdapter for OwnCloud Designed to be subclassed for the concrete 
  46  * SyncAdapter, like ConcatsSync, CalendarSync, FileSync etc.. 
  51 public abstract class AbstractOwnCloudSyncAdapter 
extends 
  52         AbstractThreadedSyncAdapter 
{ 
  54     private AccountManager accountManager
; 
  55     private Account account
; 
  56     private ContentProviderClient contentProvider
; 
  57     private Date lastUpdated
; 
  58     private DataStorageManager mStoreManager
; 
  60     private WebdavClient mClient 
= null
; 
  62     public AbstractOwnCloudSyncAdapter(Context context
, boolean autoInitialize
) { 
  63         super(context
, autoInitialize
); 
  64         this.setAccountManager(AccountManager
.get(context
)); 
  67     public AccountManager 
getAccountManager() { 
  68         return accountManager
; 
  71     public void setAccountManager(AccountManager accountManager
) { 
  72         this.accountManager 
= accountManager
; 
  75     public Account 
getAccount() { 
  79     public void setAccount(Account account
) { 
  80         this.account 
= account
; 
  83     public ContentProviderClient 
getContentProvider() { 
  84         return contentProvider
; 
  87     public void setContentProvider(ContentProviderClient contentProvider
) { 
  88         this.contentProvider 
= contentProvider
; 
  91     public Date 
getLastUpdated() { 
  95     public void setLastUpdated(Date lastUpdated
) { 
  96         this.lastUpdated 
= lastUpdated
; 
  99     public void setStorageManager(DataStorageManager storage_manager
) { 
 100         mStoreManager 
= storage_manager
; 
 103     public DataStorageManager 
getStorageManager() { 
 104         return mStoreManager
; 
 107     protected ConnectionKeepAliveStrategy 
getKeepAliveStrategy() { 
 108         return new ConnectionKeepAliveStrategy() { 
 109             public long getKeepAliveDuration(HttpResponse response
, 
 110                     HttpContext context
) { 
 111                 // Change keep alive straategy basing on response: ie 
 112                 // forbidden/not found/etc 
 113                 // should have keep alive 0 
 114                 // default return: 5s 
 115                 int statusCode 
= response
.getStatusLine().getStatusCode(); 
 117                 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed 
 119                 if ((statusCode 
>= 400 && statusCode 
<= 418) 
 120                         || (statusCode 
>= 421 && statusCode 
<= 426) 
 121                         || (statusCode 
>= 500 && statusCode 
<= 510) 
 122                         || statusCode 
== 118) { 
 131     protected HttpResponse 
fireRawRequest(HttpRequest query
) 
 132             throws ClientProtocolException
, OperationCanceledException
, 
 133             AuthenticatorException
, IOException 
{ 
 135          * BasicHttpContext httpContext = new BasicHttpContext(); BasicScheme 
 136          * basicAuth = new BasicScheme(); 
 137          * httpContext.setAttribute("preemptive-auth", basicAuth); 
 139          * HttpResponse response = getClient().execute(mHost, query, 
 145     protected void initClientForCurrentAccount() throws OperationCanceledException
, AuthenticatorException
, IOException
, AccountNotFoundException 
{ 
 146         AccountUtils
.constructFullURLForAccount(getContext(), account
); 
 147         mClient 
= OwnCloudClientUtils
.createOwnCloudClient(account
, getContext()); 
 150     protected WebdavClient 
getClient() {