From: David A. Velasco Date: Thu, 25 Oct 2012 08:41:24 +0000 (+0200) Subject: Merge two-way synch changes with synch-service refactoring for SSL warning X-Git-Tag: oc-android-1.4.3~132 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/d5a327c342b97ad4a7234fdbb533682f171c8716?ds=inline;hp=--cc Merge two-way synch changes with synch-service refactoring for SSL warning --- d5a327c342b97ad4a7234fdbb533682f171c8716 diff --cc src/com/owncloud/android/operations/SynchronizeFolderOperation.java index ad7df511,00000000..91de06ec mode 100644,000000..100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@@ -1,218 -1,0 +1,234 @@@ +/* ownCloud Android client application + * Copyright (C) 2012 Bartek Przybylski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.operations; + +import java.util.List; +import java.util.Vector; + +import org.apache.http.HttpStatus; +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.DataStorageManager; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.services.FileDownloader; ++import com.owncloud.android.files.services.FileObserverService; + +import eu.alefzero.webdav.WebdavClient; +import eu.alefzero.webdav.WebdavEntry; +import eu.alefzero.webdav.WebdavUtils; + + +/** + * Remote operation performing the synchronization a the contents of a remote folder with the local database + * + * @author David A. Velasco + */ +public class SynchronizeFolderOperation extends RemoteOperation { + + private static final String TAG = SynchronizeFolderOperation.class.getCanonicalName(); + + /** Remote folder to synchronize */ + private String mRemotePath; + + /** Timestamp for the synchronization in progress */ + private long mCurrentSyncTime; + + /** Id of the folder to synchronize in the local database */ + private long mParentId; + + /** Access to the local database */ + 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; + + /** 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 = 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; + } + + + @Override + protected RemoteOperationResult run(WebdavClient client) { + RemoteOperationResult result = null; + + // code before in FileSyncAdapter.fetchData + PropFindMethod query = null; + try { + 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 - /// TODO take into account all the possible status per child-resource + if (isMultiStatus(status)) { + 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) { + WebdavEntry we = new WebdavEntry(resp.getResponses()[i], client.getBaseUri().getPath()); + OCFile file = fillOCFile(we); + file.setParentId(mParentId); + OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath()); + if (oldFile != null) { + if (oldFile.keepInSync() && file.getModificationTimestamp() > oldFile.getModificationTimestamp()) { ++ disableObservance(file); // first disable observer so we won't get file upload right after download + requestContentDownload(file); + } + file.setKeepInSync(oldFile.keepInSync()); + } + + updatedFiles.add(file); + } - - ++ + // save updated contents in local database; all at once, trying to get a best performance in database update (not a big deal, indeed) + mStorageManager.saveFiles(updatedFiles); + + + // removal of obsolete files + mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId)); + OCFile file; + String currentSavePath = FileDownloader.getSavePath(mAccount.name); + 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))); + mChildren.remove(i); + } else { + i++; + } + } + + } else { + client.exhaustResponse(query.getResponseBodyAsStream()); + } + + // prepare result object + result = new RemoteOperationResult(isMultiStatus(status), status); + Log.i(TAG, "Synchronizing " + mAccount.name + ", folder " + mRemotePath + ": " + result.getLogMessage()); + + + } catch (Exception e) { + result = new RemoteOperationResult(e); + Log.e(TAG, "Synchronizing " + mAccount.name + ", folder " + mRemotePath + ": " + result.getLogMessage(), result.getException()); + + } finally { + if (query != null) + query.releaseConnection(); // let the connection available for other methods + } + + return result; + } + - ++ + public boolean isMultiStatus(int status) { + return (status == HttpStatus.SC_MULTI_STATUS); + } + + + /** + * Creates and populates a new {@link OCFile} object with the data read from the server. + * + * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder). + * @return New OCFile instance representing the remote resource described by we. + */ + 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; + } + + ++ /** ++ * Request to stop the observance of local updates for a file. ++ * ++ * @param file OCFile representing the remote file to stop to monitor for local updates ++ */ ++ private void disableObservance(OCFile file) { ++ Log.d(TAG, "Disabling observation of remote file" + file.getRemotePath()); ++ Intent intent = new Intent(mContext, FileObserverService.class); ++ intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_ADD_DOWNLOADING_FILE); ++ intent.putExtra(FileObserverService.KEY_CMD_ARG, file.getRemotePath()); ++ mContext.startService(intent); ++ ++ } ++ ++ + /** + * 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(mContext, FileDownloader.class); + intent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); + intent.putExtra(FileDownloader.EXTRA_FILE, file); + mContext.startService(intent); + } + + +} diff --cc src/com/owncloud/android/syncadapter/FileSyncAdapter.java index 3763908a,8c9a1535..b78b127b --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@@ -19,18 -19,23 +19,24 @@@ package com.owncloud.android.syncadapter; import java.io.IOException; +import java.net.UnknownHostException; 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; ++//<<<<<<< HEAD +import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.operations.SynchronizeFolderOperation; +import com.owncloud.android.operations.UpdateOCVersionOperation; ++/*======= + import com.owncloud.android.files.services.FileDownloader; + import com.owncloud.android.files.services.FileObserverService; + import com.owncloud.android.utils.OwnCloudVersion; ++>>>>>>> origin/master*/ import android.accounts.Account; import android.app.Notification; @@@ -87,137 -90,209 +93,164 @@@ public class FileSyncAdapter extends Ab this.setAccount(account); this.setContentProvider(provider); this.setStorageManager(new FileDataStorageManager(account, getContentProvider())); + try { + this.initClientForCurrentAccount(); + } catch (UnknownHostException e) { + /// the account is unknown for the Synchronization Manager, or unreachable for this context; don't try this again + mSyncResult.tooManyRetries = true; + notifyFailedSynchronization(); + return; + } - /* 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 + Log.d(TAG, "Synchronization of ownCloud account " + account.name + " starting"); + sendStickyBroadcast(true, null, null); // message to signal the start of the synchronization 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, 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 - mRightSync &= (syncResult.stats.numIoExceptions == 0 && syncResult.stats.numAuthExceptions == 0 && syncResult.stats.numParseExceptions == 0); - if (!mRightSync && mIsManualSync) { + // it's important making this although very unexpected errors occur; that's the reason for the finally + + if (mFailedResultsCounter > 0 && mIsManualSync) { /// don't let the system synchronization manager retries MANUAL synchronizations // (be careful: "MANUAL" currently includes the synchronization requested when a new account is created and when the user changes the current account) - syncResult.tooManyRetries = true; + mSyncResult.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 + sendStickyBroadcast(false, null, mLastFailedResult); // 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(); + + + /** + * 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. + */ + private void fetchData(String remotePath, long parentId) { + + if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult)) + return; + + // perform folder synchronization + SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( remotePath, + mCurrentSyncTime, + parentId, + getStorageManager(), + getAccount(), + getContext() + ); + RemoteOperationResult result = synchFolderOp.execute(getClient()); + + + // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess + sendStickyBroadcast(true, remotePath, null); + + if (result.isSuccess()) { + // synchronize children folders + List children = synchFolderOp.getChildren(); + fetchChildren(children); // beware of the 'hidden' recursion here! ++//<<<<<<< HEAD + } else { + if (result.getCode() == RemoteOperationResult.ResultCode.UNAUTHORIZED) { + mSyncResult.stats.numAuthExceptions++; + + } else if (result.getException() instanceof DavException) { + mSyncResult.stats.numParseExceptions++; + + } else if (result.getException() instanceof IOException) { + mSyncResult.stats.numIoExceptions++; ++/*======= + // 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()) { + // first disable observer so we won't get file upload right after download + Log.d(TAG, "Disabling observation of remote file" + file.getRemotePath()); + Intent intent = new Intent(getContext(), FileObserverService.class); + intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_ADD_DOWNLOADING_FILE); + intent.putExtra(FileObserverService.KEY_CMD_ARG, file.getRemotePath()); + getContext().startService(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()); ++>>>>>>> origin/master*/ - // 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); + mFailedResultsCounter++; + mLastFailedResult = result; + } - } 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()); + /** + * Checks if a failed result should terminate the synchronization process immediately, according to + * OUR OWN POLICY + * + * @param failedResult Remote operation result to check. + * @return 'True' if the result should immediately finish the synchronization + */ + private boolean isFinisher(RemoteOperationResult failedResult) { + if (failedResult != null) { + RemoteOperationResult.ResultCode code = failedResult.getCode(); + return (code.equals(RemoteOperationResult.ResultCode.SSL_ERROR) || + code.equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) || + code.equals(RemoteOperationResult.ResultCode.BAD_OC_VERSION) || + code.equals(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED)); } - - - 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; - */ - + return false; } /**