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
.URLDecoder
; 
  23 import java
.util
.Vector
; 
  25 import org
.apache
.jackrabbit
.webdav
.DavException
; 
  26 import org
.apache
.jackrabbit
.webdav
.MultiStatus
; 
  27 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
; 
  29 import android
.accounts
.Account
; 
  30 import android
.accounts
.AuthenticatorException
; 
  31 import android
.accounts
.OperationCanceledException
; 
  32 import android
.content
.ContentProviderClient
; 
  33 import android
.content
.Context
; 
  34 import android
.content
.Intent
; 
  35 import android
.content
.SyncResult
; 
  36 import android
.os
.Bundle
; 
  37 import android
.util
.Log
; 
  38 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
; 
  39 import eu
.alefzero
.owncloud
.datamodel
.OCFile
; 
  40 import eu
.alefzero
.webdav
.WebdavEntry
; 
  43  * SyncAdapter implementation for syncing sample SyncAdapter contacts to the 
  44  * platform ContactOperations provider. 
  46  * @author Bartek Przybylski 
  48 public class FileSyncAdapter 
extends AbstractOwnCloudSyncAdapter 
{ 
  50     private final static String TAG 
= "FileSyncAdapter";  
  52     private long mCurrentSyncTime
; 
  54     public FileSyncAdapter(Context context
, boolean autoInitialize
) { 
  55         super(context
, autoInitialize
); 
  59     public synchronized void onPerformSync(Account account
, Bundle extras
, 
  60             String authority
, ContentProviderClient provider
, 
  61             SyncResult syncResult
) { 
  63         this.setAccount(account
); 
  64         this.setContentProvider(provider
); 
  65         this.setStorageManager(new FileDataStorageManager(account
, 
  66                 getContentProvider())); 
  68         Log
.d(TAG
, "syncing owncloud account " + account
.name
); 
  70         Intent i 
= new Intent(FileSyncService
.SYNC_MESSAGE
); 
  71         i
.putExtra(FileSyncService
.IN_PROGRESS
, true
); 
  72         i
.putExtra(FileSyncService
.ACCOUNT_NAME
, account
.name
); 
  73         getContext().sendStickyBroadcast(i
); 
  77             mCurrentSyncTime 
= System
.currentTimeMillis(); 
  78             query 
= new PropFindMethod(getUri().toString() + "/"); 
  79             getClient().executeMethod(query
); 
  80             MultiStatus resp 
= null
; 
  81             resp 
= query
.getResponseBodyAsMultiStatus(); 
  83             if (resp
.getResponses().length 
> 0) { 
  84                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath()); 
  85                 OCFile file 
= fillOCFile(we
); 
  87                 getStorageManager().saveFile(file
); 
  88                 fetchData(getUri().toString(), syncResult
, file
.getFileId()); 
  90         } catch (OperationCanceledException e
) { 
  92         } catch (AuthenticatorException e
) { 
  93             syncResult
.stats
.numAuthExceptions
++; 
  95         } catch (IOException e
) { 
  96             syncResult
.stats
.numIoExceptions
++; 
  98         } catch (DavException e
) { 
  99             syncResult
.stats
.numIoExceptions
++; 
 102         i
.putExtra(FileSyncService
.IN_PROGRESS
, false
); 
 103         getContext().sendStickyBroadcast(i
); 
 106     private void fetchData(String uri
, SyncResult syncResult
, long parentId
) { 
 108             PropFindMethod query 
= new PropFindMethod(uri
); 
 109             getClient().executeMethod(query
); 
 110             MultiStatus resp 
= null
; 
 111             resp 
= query
.getResponseBodyAsMultiStatus(); 
 112             for (int i 
= 1; i 
< resp
.getResponses().length
; ++i
) { 
 113                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath()); 
 114                 OCFile file 
= fillOCFile(we
); 
 115                 file
.setParentId(parentId
); 
 116                 getStorageManager().saveFile(file
); 
 118                     parentId 
= file
.getFileId(); 
 119                 if (we
.contentType().equals("DIR")) 
 120                     fetchData(getUri().toString() + we
.path(), syncResult
, file
.getFileId()); 
 122             Vector
<OCFile
> files 
= getStorageManager().getDirectoryContent( 
 123                     getStorageManager().getFileById(parentId
)); 
 124             for (OCFile file 
: files
) { 
 125                 if (file
.getLastSyncDate() != mCurrentSyncTime 
&& file
.getLastSyncDate() != 0) 
 126                     getStorageManager().removeFile(file
); 
 128         } catch (OperationCanceledException e
) { 
 130         } catch (AuthenticatorException e
) { 
 131             syncResult
.stats
.numAuthExceptions
++; 
 133         } catch (IOException e
) { 
 134             syncResult
.stats
.numIoExceptions
++; 
 136         } catch (DavException e
) { 
 137             syncResult
.stats
.numIoExceptions
++; 
 142     private OCFile 
fillOCFile(WebdavEntry we
) { 
 143         OCFile file 
= new OCFile(URLDecoder
.decode(we
.path())); 
 144         file
.setCreationTimestamp(we
.createTimestamp()); 
 145         file
.setFileLength(we
.contentLength()); 
 146         file
.setMimetype(we
.contentType()); 
 147         file
.setModificationTimestamp(we
.modifiedTimesamp()); 
 148         file
.setLastSyncDate(mCurrentSyncTime
);