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
.ArrayList
; 
  23 import java
.util
.Iterator
; 
  24 import java
.util
.LinkedList
; 
  25 import java
.util
.List
; 
  26 import java
.util
.Queue
; 
  27 import java
.util
.Vector
; 
  29 import org
.apache
.jackrabbit
.webdav
.DavException
; 
  30 import org
.apache
.jackrabbit
.webdav
.MultiStatus
; 
  31 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
; 
  33 import android
.accounts
.Account
; 
  34 import android
.accounts
.AuthenticatorException
; 
  35 import android
.accounts
.OperationCanceledException
; 
  36 import android
.content
.ContentProviderClient
; 
  37 import android
.content
.Context
; 
  38 import android
.content
.Intent
; 
  39 import android
.content
.SyncResult
; 
  40 import android
.os
.Bundle
; 
  41 import android
.util
.Log
; 
  42 import android
.webkit
.MimeTypeMap
; 
  43 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
; 
  44 import eu
.alefzero
.owncloud
.datamodel
.OCFile
; 
  45 import eu
.alefzero
.webdav
.WebdavEntry
; 
  48  * SyncAdapter implementation for syncing sample SyncAdapter contacts to the 
  49  * platform ContactOperations provider. 
  51  * @author Bartek Przybylski 
  53 public class FileSyncAdapter 
extends AbstractOwnCloudSyncAdapter 
{ 
  55     private final static String TAG 
= "FileSyncAdapter";  
  57     private long mCurrentSyncTime
; 
  59     public FileSyncAdapter(Context context
, boolean autoInitialize
) { 
  60         super(context
, autoInitialize
); 
  64     public synchronized void onPerformSync(Account account
, Bundle extras
, 
  65             String authority
, ContentProviderClient provider
, 
  66             SyncResult syncResult
) { 
  68         this.setAccount(account
); 
  69         this.setContentProvider(provider
); 
  70         this.setStorageManager(new FileDataStorageManager(account
, 
  71                 getContentProvider())); 
  73         Log
.d(TAG
, "syncing owncloud account " + account
.name
); 
  75         sendStickyBroadcast(true
, -1);  // message to signal the start to the UI 
  79             mCurrentSyncTime 
= System
.currentTimeMillis(); 
  80             query 
= new PropFindMethod(getUri().toString() + "/"); 
  81             getClient().executeMethod(query
); 
  82             MultiStatus resp 
= null
; 
  83             resp 
= query
.getResponseBodyAsMultiStatus(); 
  85             if (resp
.getResponses().length 
> 0) { 
  86                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[0], getUri().getPath()); 
  87                 OCFile file 
= fillOCFile(we
); 
  89                 getStorageManager().saveFile(file
); 
  90                 fetchData(getUri().toString(), syncResult
, file
.getFileId()); 
  92         } catch (OperationCanceledException e
) { 
  94         } catch (AuthenticatorException e
) { 
  95             syncResult
.stats
.numAuthExceptions
++; 
  97         } catch (IOException e
) { 
  98             syncResult
.stats
.numIoExceptions
++; 
 100         } catch (DavException e
) { 
 101             syncResult
.stats
.numIoExceptions
++; 
 103         } catch (Throwable t
) { 
 104             //TODO count ; any type of exception should be treated, and the progress indicator finished; 
 105             //             reporting the user about bad synchronizations should be discussed 
 108         sendStickyBroadcast(false
, -1);         
 111     private void fetchData(String uri
, SyncResult syncResult
, long parentId
) { 
 113             PropFindMethod query 
= new PropFindMethod(uri
); 
 114             getClient().executeMethod(query
); 
 115             MultiStatus resp 
= null
; 
 116             resp 
= query
.getResponseBodyAsMultiStatus(); 
 117             Queue
<String
> paths 
= new LinkedList
<String
>(); 
 118             Queue
<Long
> fileIds 
= new LinkedList
<Long
>();  
 119             for (int i 
= 1; i 
< resp
.getResponses().length
; ++i
) { 
 120                 WebdavEntry we 
= new WebdavEntry(resp
.getResponses()[i
], getUri().getPath()); 
 121                 OCFile file 
= fillOCFile(we
); 
 122                 file
.setParentId(parentId
); 
 123                 getStorageManager().saveFile(file
); 
 125                     parentId 
= file
.getFileId(); 
 126                 if (we
.contentType().equals("DIR")) { 
 127                     // for recursive fetch later 
 128                     paths
.add(we
.path()); 
 129                     fileIds
.add(file
.getFileId()); 
 133             Vector
<OCFile
> files 
= getStorageManager().getDirectoryContent( 
 134                     getStorageManager().getFileById(parentId
)); 
 135             for (OCFile file 
: files
) { 
 136                 if (file
.getLastSyncDate() != mCurrentSyncTime 
&& file
.getLastSyncDate() != 0) 
 137                     getStorageManager().removeFile(file
); 
 140             // synched folder -> notice to IU 
 141             sendStickyBroadcast(true
, parentId
); 
 144             while(!paths
.isEmpty()) { 
 145                 fetchData(getUri().toString() + paths
.remove(), syncResult
, fileIds
.remove()); 
 151         } catch (OperationCanceledException e
) { 
 153         } catch (AuthenticatorException e
) { 
 154             syncResult
.stats
.numAuthExceptions
++; 
 156         } catch (IOException e
) { 
 157             syncResult
.stats
.numIoExceptions
++; 
 159         } catch (DavException e
) { 
 160             syncResult
.stats
.numIoExceptions
++; 
 165     private OCFile 
fillOCFile(WebdavEntry we
) { 
 166         OCFile file 
= new OCFile(we
.path()); 
 167         file
.setCreationTimestamp(we
.createTimestamp()); 
 168         file
.setFileLength(we
.contentLength()); 
 169         file
.setMimetype(we
.contentType()); 
 170         file
.setModificationTimestamp(we
.modifiedTimesamp()); 
 171         file
.setLastSyncDate(mCurrentSyncTime
); 
 176     private void sendStickyBroadcast(boolean inProgress
, long OCDirId
) { 
 177         Intent i 
= new Intent(FileSyncService
.SYNC_MESSAGE
); 
 178         i
.putExtra(FileSyncService
.IN_PROGRESS
, inProgress
); 
 179         i
.putExtra(FileSyncService
.ACCOUNT_NAME
, getAccount().name
); 
 181             i
.putExtra(FileSyncService
.SYNC_FOLDER
, OCDirId
); 
 183         getContext().sendStickyBroadcast(i
);