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
.HttpHost
; 
  26 import org
.apache
.http
.HttpRequest
; 
  27 import org
.apache
.http
.HttpResponse
; 
  28 import org
.apache
.http
.client
.ClientProtocolException
; 
  29 import org
.apache
.http
.conn
.ConnectionKeepAliveStrategy
; 
  30 import org
.apache
.http
.protocol
.HttpContext
; 
  32 import android
.accounts
.Account
; 
  33 import android
.accounts
.AccountManager
; 
  34 import android
.accounts
.AuthenticatorException
; 
  35 import android
.accounts
.OperationCanceledException
; 
  36 import android
.content
.AbstractThreadedSyncAdapter
; 
  37 import android
.content
.ContentProviderClient
; 
  38 import android
.content
.Context
; 
  39 import android
.net
.Uri
; 
  40 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
; 
  41 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
; 
  42 import eu
.alefzero
.webdav
.HttpPropFind
; 
  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 HttpHost mHost
; 
  62         private WebdavClient mClient 
= null
; 
  63         private static String TAG 
= "AbstractOwnCloudSyncAdapter"; 
  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 Date 
getLastUpdated() { 
  98         public void setLastUpdated(Date lastUpdated
) { 
  99                 this.lastUpdated 
= lastUpdated
; 
 102         public void setStorageManager(DataStorageManager storage_manager
) { 
 103           mStoreManager 
= storage_manager
; 
 106         public DataStorageManager 
getStorageManager() { 
 107           return mStoreManager
; 
 110         protected ConnectionKeepAliveStrategy 
getKeepAliveStrategy() { 
 111                 return new ConnectionKeepAliveStrategy() { 
 112                         public long getKeepAliveDuration(HttpResponse response
, 
 113                                         HttpContext context
) { 
 114                                 // Change keep alive straategy basing on response: ie 
 115                                 // forbidden/not found/etc 
 116                                 // should have keep alive 0 
 117                                 // default return: 5s 
 118                                 int statusCode 
= response
.getStatusLine().getStatusCode(); 
 120                                 // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed 
 122                                 if ((statusCode 
>= 400 && statusCode 
<= 418) 
 123                                                 || (statusCode 
>= 421 && statusCode 
<= 426) 
 124                                                 || (statusCode 
>= 500 && statusCode 
<= 510) 
 125                                                 || statusCode 
== 118) { 
 134         protected HttpPropFind 
getPropFindQuery() 
 135                         throws OperationCanceledException
, AuthenticatorException
, 
 137                 HttpPropFind query 
= new HttpPropFind(getUri().toString()); 
 138                 query
.setHeader("Content-type", "text/xml"); 
 139                 query
.setHeader("User-Agent", "Android-ownCloud"); 
 143         protected HttpResponse 
fireRawRequest(HttpRequest query
) 
 144                         throws ClientProtocolException
, OperationCanceledException
, 
 145                         AuthenticatorException
, IOException 
{ 
 146                 /*BasicHttpContext httpContext = new BasicHttpContext(); 
 147                 BasicScheme basicAuth = new BasicScheme(); 
 148                 httpContext.setAttribute("preemptive-auth", basicAuth); 
 150                 HttpResponse response = getClient().execute(mHost, query, httpContext);*/ 
 154         protected Uri 
getUri() { 
 155                 return Uri
.parse(this.getAccountManager().getUserData(getAccount(), 
 156                                 AccountAuthenticator
.KEY_OC_URL
)); 
 159         protected WebdavClient 
getClient() throws OperationCanceledException
, 
 160                         AuthenticatorException
, IOException 
{ 
 161                 if (mClient 
== null
) { 
 162                         String username 
= getAccount().name
.split("@")[0]; 
 163                         String password 
= this.getAccountManager().blockingGetAuthToken( 
 164                                         getAccount(), AccountAuthenticator
.AUTH_TOKEN_TYPE
, true
); 
 165                         if (this.getAccountManager().getUserData(getAccount(), 
 166                                         AccountAuthenticator
.KEY_OC_URL
) == null
) { 
 167                                 throw new UnknownHostException(); 
 171                         mClient 
= new WebdavClient(uri
); 
 172                         mClient
.setCredentials(username
, password
); 
 173                         mClient
.allowUnsignedCertificates(); 
 174                         mHost 
= mClient
.getTargetHost();