From: David A. Velasco Date: Tue, 23 Oct 2012 10:47:29 +0000 (+0200) Subject: Updating synchronization for providing SSL warning when necessary; step 1: refactorin... X-Git-Tag: oc-android-1.4.3~135 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/6e469559c558d87d7e3cc81fc024b597e00be313?ds=inline;hp=--cc Updating synchronization for providing SSL warning when necessary; step 1: refactoring based in RemoteOperation (DONE) --- 6e469559c558d87d7e3cc81fc024b597e00be313 diff --git a/src/com/owncloud/android/datamodel/DataStorageManager.java b/src/com/owncloud/android/datamodel/DataStorageManager.java index e824bdea..49a3cd73 100644 --- a/src/com/owncloud/android/datamodel/DataStorageManager.java +++ b/src/com/owncloud/android/datamodel/DataStorageManager.java @@ -23,6 +23,8 @@ import java.util.Vector; public interface DataStorageManager { + public static final int ROOT_PARENT_ID = 0; + public OCFile getFileByPath(String path); public OCFile getFileById(long id); diff --git a/src/com/owncloud/android/operations/RemoteOperationResult.java b/src/com/owncloud/android/operations/RemoteOperationResult.java index 1415228f..d1d39f63 100644 --- a/src/com/owncloud/android/operations/RemoteOperationResult.java +++ b/src/com/owncloud/android/operations/RemoteOperationResult.java @@ -30,6 +30,7 @@ import javax.net.ssl.SSLException; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; +import org.apache.jackrabbit.webdav.DavException; import com.owncloud.android.network.CertificateCombinedException; @@ -211,6 +212,9 @@ public class RemoteOperationResult implements Serializable { else return "SSL exception"; + } else if (mException instanceof DavException) { + return "Unexpected WebDAV exception"; + } else if (mException instanceof HttpException) { return "HTTP violation"; @@ -236,7 +240,7 @@ public class RemoteOperationResult implements Serializable { } return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")"; - + } } diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 04fb4dbc..ad7df511 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -18,20 +18,19 @@ package com.owncloud.android.operations; -import java.io.IOException; import java.util.List; import java.util.Vector; import org.apache.http.HttpStatus; -import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.MultiStatus; import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; import android.accounts.Account; +import android.content.Context; import android.content.Intent; import android.util.Log; -import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.datamodel.DataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; @@ -59,18 +58,40 @@ public class SynchronizeFolderOperation extends RemoteOperation { private long mParentId; /** Access to the local database */ - private FileDataStorageManager mStorageManager; + private DataStorageManager mStorageManager; /** Account where the file to synchronize belongs */ private Account mAccount; + /** Android context; necessary to send requests to the download service; maybe something to refactor */ + private Context mContext; - SynchronizeFolderOperation(String remotePath, long currentSyncTime, long parentId, FileDataStorageManager storageManager, Account account) { + /** Files and folders contained in the synchronized folder */ + private List mChildren; + + + public SynchronizeFolderOperation( String remotePath, + long currentSyncTime, + long parentId, + DataStorageManager dataStorageManager, + Account account, + Context context ) { mRemotePath = remotePath; mCurrentSyncTime = currentSyncTime; mParentId = parentId; - mStorageManager = storageManager; + mStorageManager = dataStorageManager; mAccount = account; + mContext = context; + } + + + /** + * Returns the list of files and folders contained in the synchronized folder, if called after synchronization is complete. + * + * @return List of files and folders contained in the synchronized folder. + */ + public List getChildren() { + return mChildren; } @@ -80,20 +101,26 @@ public class SynchronizeFolderOperation extends RemoteOperation { // code before in FileSyncAdapter.fetchData PropFindMethod query = null; - Vector children = null; try { - Log.d(TAG, "Fetching files in " + mRemotePath); + Log.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath); // remote request query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); int status = client.executeMethod(query); - // check and process response + // check and process response - /// TODO take into account all the possible status per child-resource if (isMultiStatus(status)) { - /// TODO take into account all the possible status per child-resource - MultiStatus resp = query.getResponseBodyAsMultiStatus(); + // synchronize properties of the parent folder, if necessary + if (mParentId == DataStorageManager.ROOT_PARENT_ID) { + WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath()); + OCFile parent = fillOCFile(we); + parent.setParentId(mParentId); + mStorageManager.saveFile(parent); + mParentId = parent.getFileId(); + } + // read contents in folder List updatedFiles = new Vector(resp.getResponses().length - 1); for (int i = 1; i < resp.getResponses().length; ++i) { @@ -117,15 +144,15 @@ public class SynchronizeFolderOperation extends RemoteOperation { // removal of obsolete files - children = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId)); + mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId)); OCFile file; String currentSavePath = FileDownloader.getSavePath(mAccount.name); - for (int i=0; i < children.size(); ) { - file = children.get(i); + for (int i=0; i < mChildren.size(); ) { + file = mChildren.get(i); if (file.getLastSyncDate() != mCurrentSyncTime) { Log.d(TAG, "removing file: " + file); mStorageManager.removeFile(file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath))); - children.remove(i); + mChildren.remove(i); } else { i++; } @@ -137,13 +164,12 @@ public class SynchronizeFolderOperation extends RemoteOperation { // prepare result object result = new RemoteOperationResult(isMultiStatus(status), status); - Log.i(TAG, "Synchronization of " + mRemotePath + ": " + result.getLogMessage()); + Log.i(TAG, "Synchronizing " + mAccount.name + ", folder " + mRemotePath + ": " + result.getLogMessage()); } catch (Exception e) { - // TODO - distinguish IOException and DavException in RemoteOperationResult constructor result = new RemoteOperationResult(e); - Log.e(TAG, "Synchronization of " + mRemotePath + ": " + result.getLogMessage(), result.getException()); + Log.e(TAG, "Synchronizing " + mAccount.name + ", folder " + mRemotePath + ": " + result.getLogMessage(), result.getException()); } finally { if (query != null) @@ -177,13 +203,15 @@ public class SynchronizeFolderOperation extends RemoteOperation { /** - * Probably this code would be better in other place + * Requests a download to the file download service + * + * @param file OCFile representing the remote file to download */ private void requestContentDownload(OCFile file) { - Intent intent = new Intent(this.getContext(), FileDownloader.class); + Intent intent = new Intent(mContext, FileDownloader.class); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); intent.putExtra(FileDownloader.EXTRA_FILE, file); - getContext().startService(intent); + mContext.startService(intent); } diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index ec620911..c5776de1 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -20,21 +20,18 @@ package com.owncloud.android.syncadapter; import java.io.IOException; import java.util.List; -import java.util.Vector; -import org.apache.http.HttpStatus; import org.apache.jackrabbit.webdav.DavException; -import org.apache.jackrabbit.webdav.MultiStatus; -import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; import org.json.JSONObject; import com.owncloud.android.AccountUtils; import com.owncloud.android.R; import com.owncloud.android.authenticator.AccountAuthenticator; +import com.owncloud.android.datamodel.DataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.utils.OwnCloudVersion; import android.accounts.Account; @@ -48,8 +45,7 @@ import android.content.Intent; import android.content.SyncResult; import android.os.Bundle; import android.util.Log; -import eu.alefzero.webdav.WebdavEntry; -import eu.alefzero.webdav.WebdavUtils; +import eu.alefzero.webdav.WebdavClient; /** * SyncAdapter implementation for syncing sample SyncAdapter contacts to the @@ -61,14 +57,6 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { private final static String TAG = "FileSyncAdapter"; - /* Commented code for ugly performance tests - private final static int MAX_DELAYS = 100; - private static long[] mResponseDelays = new long[MAX_DELAYS]; - private static long[] mSaveDelays = new long[MAX_DELAYS]; - private int mDelaysIndex = 0; - private int mDelaysCount = 0; - */ - private long mCurrentSyncTime; private boolean mCancellation; private boolean mIsManualSync; @@ -91,55 +79,23 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { this.setContentProvider(provider); this.setStorageManager(new FileDataStorageManager(account, getContentProvider())); - /* Commented code for ugly performance tests - mDelaysIndex = 0; - mDelaysCount = 0; - */ - Log.d(TAG, "syncing owncloud account " + account.name); sendStickyBroadcast(true, null); // message to signal the start to the UI - updateOCVersion(); - - String uri = getUri().toString(); - PropFindMethod query = null; try { + updateOCVersion(); mCurrentSyncTime = System.currentTimeMillis(); - query = new PropFindMethod(uri + "/"); - int status = getClient().executeMethod(query); - if (status != HttpStatus.SC_UNAUTHORIZED) { - MultiStatus resp = query.getResponseBodyAsMultiStatus(); - - if (resp.getResponses().length > 0) { - WebdavEntry we = new WebdavEntry(resp.getResponses()[0], getUri().getPath()); - OCFile file = fillOCFile(we); - file.setParentId(0); - getStorageManager().saveFile(file); - if (!mCancellation) { - fetchData(uri, syncResult, file.getFileId()); - } - } - + if (!mCancellation) { + fetchData(OCFile.PATH_SEPARATOR, syncResult, DataStorageManager.ROOT_PARENT_ID); + } else { - syncResult.stats.numAuthExceptions++; + Log.d(TAG, "Leaving synchronization before any remote request due to cancellation was requested"); } - } catch (IOException e) { - syncResult.stats.numIoExceptions++; - logException(e, uri + "/"); - - } catch (DavException e) { - syncResult.stats.numParseExceptions++; - logException(e, uri + "/"); - - } catch (Exception e) { - // TODO something smart with syncresult - logException(e, uri + "/"); - mRightSync = false; } finally { - if (query != null) - query.releaseConnection(); // let the connection available for other methods + // it's important making this although very unexpected errors occur; that's the reason for the finally + mRightSync &= (syncResult.stats.numIoExceptions == 0 && syncResult.stats.numAuthExceptions == 0 && syncResult.stats.numParseExceptions == 0); if (!mRightSync && mIsManualSync) { /// don't let the system synchronization manager retries MANUAL synchronizations @@ -147,188 +103,15 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { syncResult.tooManyRetries = true; /// notify the user about the failure of MANUAL synchronization - Notification notification = new Notification(R.drawable.icon, getContext().getString(R.string.sync_fail_ticker), System.currentTimeMillis()); - notification.flags |= Notification.FLAG_AUTO_CANCEL; - // TODO put something smart in the contentIntent below - notification.contentIntent = PendingIntent.getActivity(getContext().getApplicationContext(), (int)System.currentTimeMillis(), new Intent(), 0); - notification.setLatestEventInfo(getContext().getApplicationContext(), - getContext().getString(R.string.sync_fail_ticker), - String.format(getContext().getString(R.string.sync_fail_content), account.name), - notification.contentIntent); - ((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify(R.string.sync_fail_ticker, notification); + notifyFailedSynchronization(); } sendStickyBroadcast(false, null); // message to signal the end to the UI } - /* Commented code for ugly performance tests - long sum = 0, mean = 0, max = 0, min = Long.MAX_VALUE; - for (int i=0; i children = null; - try { - Log.d(TAG, "fetching " + uri); - - // remote request - query = new PropFindMethod(uri); - /* Commented code for ugly performance tests - long responseDelay = System.currentTimeMillis(); - */ - int status = getClient().executeMethod(query); - /* Commented code for ugly performance tests - responseDelay = System.currentTimeMillis() - responseDelay; - Log.e(TAG, "syncing: RESPONSE TIME for " + uri + " contents, " + responseDelay + "ms"); - */ - if (status != HttpStatus.SC_UNAUTHORIZED) { - MultiStatus resp = query.getResponseBodyAsMultiStatus(); - - // insertion or update of files - List updatedFiles = new Vector(resp.getResponses().length - 1); - for (int i = 1; i < resp.getResponses().length; ++i) { - WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath()); - OCFile file = fillOCFile(we); - file.setParentId(parentId); - if (getStorageManager().getFileByPath(file.getRemotePath()) != null && - getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() && - file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath()) - .getModificationTimestamp()) { - Intent intent = new Intent(this.getContext(), FileDownloader.class); - intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); - intent.putExtra(FileDownloader.EXTRA_FILE, file); - file.setKeepInSync(true); - getContext().startService(intent); - } - if (getStorageManager().getFileByPath(file.getRemotePath()) != null) - file.setKeepInSync(getStorageManager().getFileByPath(file.getRemotePath()).keepInSync()); - - // Log.v(TAG, "adding file: " + file); - updatedFiles.add(file); - } - /* Commented code for ugly performance tests - long saveDelay = System.currentTimeMillis(); - */ - getStorageManager().saveFiles(updatedFiles); // all "at once" ; trying to get a best performance in database update - /* Commented code for ugly performance tests - saveDelay = System.currentTimeMillis() - saveDelay; - Log.e(TAG, "syncing: SAVE TIME for " + uri + " contents, " + mSaveDelays[mDelaysIndex] + "ms"); - */ - - // removal of obsolete files - children = getStorageManager().getDirectoryContent( - getStorageManager().getFileById(parentId)); - OCFile file; - String currentSavePath = FileDownloader.getSavePath(getAccount().name); - for (int i=0; i < children.size(); ) { - file = children.get(i); - if (file.getLastSyncDate() != mCurrentSyncTime) { - Log.v(TAG, "removing file: " + file); - getStorageManager().removeFile(file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath))); - children.remove(i); - } else { - i++; - } - } - - } else { - syncResult.stats.numAuthExceptions++; - } - } catch (IOException e) { - syncResult.stats.numIoExceptions++; - logException(e, uri); - - } catch (DavException e) { - syncResult.stats.numParseExceptions++; - logException(e, uri); - - } catch (Exception e) { - // TODO something smart with syncresult - mRightSync = false; - logException(e, uri); - - } finally { - if (query != null) - query.releaseConnection(); // let the connection available for other methods - - // synchronized folder -> notice to UI - sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath()); - } - - - fetchChildren(children, syncResult); - if (mCancellation) Log.d(TAG, "Leaving " + uri + " because cancelation request"); - - - /* Commented code for ugly performance tests - mResponseDelays[mDelaysIndex] = responseDelay; - mSaveDelays[mDelaysIndex] = saveDelay; - mDelaysCount++; - mDelaysIndex++; - if (mDelaysIndex >= MAX_DELAYS) - mDelaysIndex = 0; - */ - - } - - /** - * Synchronize data of folders in the list of received files - * - * @param files Files to recursively fetch - * @param syncResult Updated object to provide results to the Synchronization Manager - */ - private void fetchChildren(Vector files, SyncResult syncResult) { - for (int i=0; i < files.size() && !mCancellation; i++) { - OCFile newFile = files.get(i); - if (newFile.getMimetype().equals("DIR")) { - fetchData(getUri().toString() + WebdavUtils.encodePath(newFile.getRemotePath()), syncResult, newFile.getFileId()); - } - } - } - - private OCFile fillOCFile(WebdavEntry we) { - OCFile file = new OCFile(we.decodedPath()); - file.setCreationTimestamp(we.createTimestamp()); - file.setFileLength(we.contentLength()); - file.setMimetype(we.contentType()); - file.setModificationTimestamp(we.modifiedTimesamp()); - file.setLastSyncDate(mCurrentSyncTime); - return file; - } - private void sendStickyBroadcast(boolean inProgress, String dirRemotePath/*, RemoteOperationResult result*/) { - Intent i = new Intent(FileSyncService.SYNC_MESSAGE); - i.putExtra(FileSyncService.IN_PROGRESS, inProgress); - i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name); - if (dirRemotePath != null) { - i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath); - } - /*if (result != null) { - i.putExtra(FileSyncService.SYNC_RESULT, result); - }*/ - getContext().sendStickyBroadcast(i); - } - /** * Called by system SyncManager when a synchronization is required to be cancelled. * @@ -341,26 +124,11 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { mCancellation = true; super.onSyncCanceled(); } - + /** - * Logs an exception triggered in a synchronization request. - * - * @param e Caught exception. - * @param uri Uri to the remote directory that was fetched when the synchronization failed + * Updates the locally stored version value of the ownCloud server */ - private void logException(Exception e, String uri) { - if (e instanceof IOException) { - Log.e(TAG, "Unrecovered transport exception while synchronizing " + uri + " at " + getAccount().name, e); - - } else if (e instanceof DavException) { - Log.e(TAG, "Unexpected WebDAV exception while synchronizing " + uri + " at " + getAccount().name, e); - - } else { - Log.e(TAG, "Unexpected exception while synchronizing " + uri + " at " + getAccount().name, e); - } - } - private void updateOCVersion() { String statUrl = getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_BASE_URL); statUrl += AccountUtils.STATUS_PATH; @@ -389,4 +157,119 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { Log.e(TAG, "Problem getting response from server", e); } } + + + + /** + * Synchronize the properties of files and folders contained in a remote folder given by remotePath. + * + * @param remotePath Remote path to the folder to synchronize. + * @param parentId Database Id of the folder to synchronize. + * @param syncResult Object to update for communicate results to the system's synchronization manager. + */ + private void fetchData(String remotePath, SyncResult syncResult, long parentId) { + + // get client object to connect to the remote ownCloud server + WebdavClient client = null; + try { + client = getClient(); + } catch (IOException e) { + syncResult.stats.numIoExceptions++; + Log.d(TAG, "Could not get client object while trying to synchronize - impossible to continue"); + return; + } + + // perform folder synchronization + SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( remotePath, + mCurrentSyncTime, + parentId, + getStorageManager(), + getAccount(), + getContext() + ); + RemoteOperationResult result = synchFolderOp.execute(client); + + + // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess + sendStickyBroadcast(true, remotePath); + + if (result.isSuccess()) { + // synchronize children folders + List children = synchFolderOp.getChildren(); + fetchChildren(children, syncResult); // beware of the 'hidden' recursion here! + + } else { + if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED) { + syncResult.stats.numAuthExceptions++; + + } else if (result.getException() instanceof DavException) { + syncResult.stats.numParseExceptions++; + + } else if (result.getException() instanceof IOException) { + syncResult.stats.numIoExceptions++; + + } else if (result.getException() != null) { + // TODO maybe something smarter with syncResult + mRightSync = false; + } + } + + } + + /** + * Synchronize data of folders in the list of received files + * + * @param files Files to recursively fetch + * @param syncResult Updated object to provide results to the Synchronization Manager + */ + private void fetchChildren(List files, SyncResult syncResult) { + int i; + for (i=0; i < files.size() && !mCancellation; i++) { + OCFile newFile = files.get(i); + if (newFile.isDirectory()) { + fetchData(newFile.getRemotePath(), syncResult, newFile.getFileId()); + } + } + if (mCancellation && i