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
.util
.List
; 
  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
.owncloud
.files
.services
.FileDownloader
; 
  41 import eu
.alefzero
.webdav
.WebdavEntry
; 
  44  * SyncAdapter implementation for syncing sample SyncAdapter contacts to the 
  45  * platform ContactOperations provider. 
  47  * @author Bartek Przybylski 
  49 public class FileSyncAdapter 
extends AbstractOwnCloudSyncAdapter 
{ 
  51     private final static String TAG 
= "FileSyncAdapter";  
  53     /*  Commented code for ugly performance tests 
  54     private final static int MAX_DELAYS = 100; 
  55     private static long[] mResponseDelays = new long[MAX_DELAYS];  
  56     private static long[] mSaveDelays = new long[MAX_DELAYS]; 
  57     private int mDelaysIndex = 0; 
  58     private int mDelaysCount = 0; 
  61     private long mCurrentSyncTime
; 
  62     private boolean mCancellation
; 
  63     private Account mAccount
; 
  65     public FileSyncAdapter(Context context
, boolean autoInitialize
) { 
  66         super(context
, autoInitialize
); 
  70     public synchronized void onPerformSync(Account account
, Bundle extras
, 
  71             String authority
, ContentProviderClient provider
, 
  72             SyncResult syncResult
) { 
  74         mCancellation 
= false
; 
  77         this.setAccount(mAccount
); 
  78         this.setContentProvider(provider
); 
  79         this.setStorageManager(new FileDataStorageManager(mAccount
, 
  80                 getContentProvider())); 
  82         /*  Commented code for ugly performance tests 
  88         Log
.d(TAG
, "syncing owncloud account " + mAccount
.name
); 
  90         sendStickyBroadcast(true
, null
);  // message to signal the start to the UI 
  94             mCurrentSyncTime 
= System
.currentTimeMillis(); 
  95             query 
= new PropFindMethod(getUri().toString() + "/"); 
  96             getClient().executeMethod(query
); 
  97             MultiStatus resp 
= null
; 
  98             resp 
= query
.getResponseBodyAsMultiStatus(); 
 100             if (resp
.getResponses().length 
> 0) { 
 101                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath()); 
 102                 OCFile file 
= fillOCFile(we
); 
 104                 getStorageManager().saveFile(file
); 
 105                 if (!mCancellation
) { 
 106                     fetchData(getUri().toString(), syncResult
, file
.getFileId()); 
 109         } catch (OperationCanceledException e
) { 
 111         } catch (AuthenticatorException e
) { 
 112             syncResult
.stats
.numAuthExceptions
++; 
 114         } catch (IOException e
) { 
 115             syncResult
.stats
.numIoExceptions
++; 
 117         } catch (DavException e
) { 
 118             syncResult
.stats
.numIoExceptions
++; 
 120         } catch (Throwable t
) { 
 121             // TODO update syncResult 
 122             Log
.e(TAG
, "problem while synchronizing owncloud account " + account
.name
, t
); 
 126         /*  Commented code for ugly performance tests 
 127         long sum = 0, mean = 0, max = 0, min = Long.MAX_VALUE; 
 128         for (int i=0; i<MAX_DELAYS && i<mDelaysCount; i++) { 
 129             sum += mResponseDelays[i]; 
 130             max = Math.max(max, mResponseDelays[i]); 
 131             min = Math.min(min, mResponseDelays[i]); 
 133         mean = sum / mDelaysCount; 
 134         Log.e(TAG, "SYNC STATS - response: mean time = " + mean + " ; max time = " + max + " ; min time = " + min); 
 136         sum = 0; max = 0; min = Long.MAX_VALUE; 
 137         for (int i=0; i<MAX_DELAYS && i<mDelaysCount; i++) { 
 138             sum += mSaveDelays[i]; 
 139             max = Math.max(max, mSaveDelays[i]); 
 140             min = Math.min(min, mSaveDelays[i]); 
 142         mean = sum / mDelaysCount; 
 143         Log.e(TAG, "SYNC STATS - save:     mean time = " + mean + " ; max time = " + max + " ; min time = " + min); 
 144         Log.e(TAG, "SYNC STATS - folders measured: " + mDelaysCount); 
 147         sendStickyBroadcast(false
, null
);         
 150     private void fetchData(String uri
, SyncResult syncResult
, long parentId
) { 
 152             //Log.v(TAG, "syncing: fetching " + uri); 
 155             PropFindMethod query 
= new PropFindMethod(uri
); 
 156             /*  Commented code for ugly performance tests 
 157             long responseDelay = System.currentTimeMillis(); 
 159             getClient().executeMethod(query
); 
 160             /*  Commented code for ugly performance tests 
 161             responseDelay = System.currentTimeMillis() - responseDelay; 
 162             Log.e(TAG, "syncing: RESPONSE TIME for " + uri + " contents, " + responseDelay + "ms"); 
 164             MultiStatus resp 
= null
; 
 165             resp 
= query
.getResponseBodyAsMultiStatus(); 
 167             // insertion or update of files 
 168             List
<OCFile
> updatedFiles 
= new Vector
<OCFile
>(resp
.getResponses().length 
- 1); 
 169             for (int i 
= 1; i 
< resp
.getResponses().length
; ++i
) { 
 170                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath()); 
 171                 OCFile file 
= fillOCFile(we
); 
 172                 file
.setParentId(parentId
); 
 173                 if (getStorageManager().getFileByPath(file
.getRemotePath()) != null 
&& 
 174                     getStorageManager().getFileByPath(file
.getRemotePath()).keepInSync() && 
 175                     file
.getModificationTimestamp() > getStorageManager().getFileByPath(file
.getRemotePath()) 
 176                                                                          .getModificationTimestamp()) { 
 177                     Intent intent 
= new Intent(this.getContext(), FileDownloader
.class); 
 178                     intent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount()); 
 179                     intent
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, file
.getURLDecodedRemotePath()); 
 180                     intent
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, file
.getRemotePath()); 
 181                     intent
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, file
.getFileLength()); 
 182                     file
.setKeepInSync(true
); 
 183                     getContext().startService(intent
); 
 185                 if (getStorageManager().getFileByPath(file
.getRemotePath()) != null
) 
 186                     file
.setKeepInSync(getStorageManager().getFileByPath(file
.getRemotePath()).keepInSync()); 
 187                 //getStorageManager().saveFile(file); 
 188                 updatedFiles
.add(file
); 
 190                     parentId 
= file
.getFileId(); 
 192             /*  Commented code for ugly performance tests 
 193             long saveDelay = System.currentTimeMillis(); 
 195             getStorageManager().saveFiles(updatedFiles
);    // all "at once" ; trying to get a best performance in database update 
 196             /*  Commented code for ugly performance tests 
 197             saveDelay = System.currentTimeMillis() - saveDelay; 
 198             Log.e(TAG, "syncing: SAVE TIME for " + uri + " contents, " + mSaveDelays[mDelaysIndex] + "ms"); 
 201             // removal of obsolete files 
 202             Vector
<OCFile
> files 
= getStorageManager().getDirectoryContent( 
 203                     getStorageManager().getFileById(parentId
)); 
 205             for (int i
=0; i 
< files
.size(); ) { 
 207                 if (file
.getLastSyncDate() != mCurrentSyncTime 
&& file
.getLastSyncDate() != 0) { 
 208                     getStorageManager().removeFile(file
); 
 215             // synchronized folder -> notice to UI 
 216             sendStickyBroadcast(true
, getStorageManager().getFileById(parentId
).getRemotePath()); 
 219             for (int i
=0; i 
< files
.size() && !mCancellation
; i
++) { 
 220                 OCFile newFile 
= files
.get(i
); 
 221                 if (newFile
.getMimetype().equals("DIR")) { 
 222                     fetchData(getUri().toString() + newFile
.getRemotePath(), syncResult
, newFile
.getFileId()); 
 225             if (mCancellation
) Log
.d(TAG
, "Leaving " + uri 
+ " because cancellation request"); 
 227             /*  Commented code for ugly performance tests 
 228             mResponseDelays[mDelaysIndex] = responseDelay; 
 229             mSaveDelays[mDelaysIndex] = saveDelay; 
 232             if (mDelaysIndex >= MAX_DELAYS) 
 238         } catch (OperationCanceledException e
) { 
 240         } catch (AuthenticatorException e
) { 
 241             syncResult
.stats
.numAuthExceptions
++; 
 243         } catch (IOException e
) { 
 244             syncResult
.stats
.numIoExceptions
++; 
 246         } catch (DavException e
) { 
 247             syncResult
.stats
.numIoExceptions
++; 
 249         } catch (Throwable t
) { 
 250             // TODO update syncResult 
 251             Log
.e(TAG
, "problem while synchronizing owncloud account " + mAccount
.name
, t
); 
 256     private OCFile 
fillOCFile(WebdavEntry we
) { 
 257         OCFile file 
= new OCFile(we
.path()); 
 258         file
.setCreationTimestamp(we
.createTimestamp()); 
 259         file
.setFileLength(we
.contentLength()); 
 260         file
.setMimetype(we
.contentType()); 
 261         file
.setModificationTimestamp(we
.modifiedTimesamp()); 
 262         file
.setLastSyncDate(mCurrentSyncTime
); 
 267     private void sendStickyBroadcast(boolean inProgress
, String dirRemotePath
) { 
 268         Intent i 
= new Intent(FileSyncService
.SYNC_MESSAGE
); 
 269         i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
); 
 270         i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
); 
 271         if (dirRemotePath 
!= null
) { 
 272             i
.putExtra(FileSyncService
.SYNC_FOLDER_REMOTE_PATH
, dirRemotePath
); 
 274         getContext().sendStickyBroadcast(i
); 
 278      * Called by system SyncManager when a synchronization is required to be cancelled. 
 280      * Sets the mCancellation flag to 'true'. THe synchronization will be stopped when before a new folder is fetched. Data of the last folder 
 281      * fetched will be still saved in the database. See onPerformSync implementation. 
 284     public void onSyncCanceled() { 
 285         Log
.d(TAG
, "Synchronization of " + mAccount
.name 
+ " has been requested to cancell"); 
 286         mCancellation 
= true
; 
 287         super.onSyncCanceled();