use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.
the class UploadFileOperation method saveUploadedFile.
/**
* Saves a OC File after a successful upload.
* <p/>
* A PROPFIND is necessary to keep the props in the local database
* synchronized with the server, specially the modification time and Etag
* (where available)
* <p/>
*/
private void saveUploadedFile(OwnCloudClient client) {
OCFile file = mFile;
if (file.fileExists()) {
file = getStorageManager().getFileById(file.getFileId());
}
long syncDate = System.currentTimeMillis();
file.setLastSyncDateForData(syncDate);
// new PROPFIND to keep data consistent with server
// in theory, should return the same we already have
// TODO from the appropriate OC server version, get data from last PUT response headers, instead
// TODO of a new PROPFIND; the latter may fail, specially for chunked uploads
ReadRemoteFileOperation operation = new ReadRemoteFileOperation(getRemotePath());
RemoteOperationResult result = operation.execute(client);
if (result.isSuccess()) {
updateOCFile(file, (RemoteFile) result.getData().get(0));
file.setLastSyncDateForProperties(syncDate);
} else {
Log_OC.e(TAG, "Error reading properties of file after successful upload; this is gonna hurt...");
}
if (mWasRenamed) {
OCFile oldFile = getStorageManager().getFileByPath(mOldFile.getRemotePath());
if (oldFile != null) {
oldFile.setStoragePath(null);
getStorageManager().saveFile(oldFile);
getStorageManager().saveConflict(oldFile, null);
}
// else: it was just an automatic renaming due to a name
// coincidence; nothing else is needed, the storagePath is right
// in the instance returned by mCurrentUpload.getFile()
}
file.setNeedsUpdateThumbnail(true);
getStorageManager().saveFile(file);
getStorageManager().saveConflict(file, null);
FileDataStorageManager.triggerMediaScan(file.getStoragePath());
}
use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.
the class UploadFileOperation method grantFolderExistence.
/**
* Checks the existence of the folder where the current file will be uploaded both
* in the remote server and in the local database.
* <p/>
* If the upload is set to enforce the creation of the folder, the method tries to
* create it both remote and locally.
*
* @param pathToGrant Full remote path whose existence will be granted.
* @return An {@link OCFile} instance corresponding to the folder where the file
* will be uploaded.
*/
private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, mContext, false);
RemoteOperationResult result = operation.execute(client);
if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
SyncOperation syncOp = new CreateFolderOperation(pathToGrant, true);
result = syncOp.execute(client, getStorageManager());
}
if (result.isSuccess()) {
OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
if (parentDir == null) {
parentDir = createLocalFolder(pathToGrant);
}
if (parentDir != null) {
result = new RemoteOperationResult(ResultCode.OK);
} else {
result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
}
}
return result;
}
use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.
the class SyncFolderHandler method doOperation.
/**
* Performs the next operation in the queue
*/
private void doOperation(Account account, String remotePath) {
mCurrentSyncOperation = mPendingOperations.get(account.name, remotePath);
if (mCurrentSyncOperation != null) {
RemoteOperationResult result = null;
try {
if (mCurrentAccount == null || !mCurrentAccount.equals(account)) {
mCurrentAccount = account;
mStorageManager = new FileDataStorageManager(account, mService.getContentResolver());
}
// else, reuse storage manager from previous operation
// always get client from client manager, to get fresh credentials in case of update
OwnCloudAccount ocAccount = new OwnCloudAccount(account, mService);
mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, mService);
result = mCurrentSyncOperation.execute(mOwnCloudClient, mStorageManager);
} catch (AccountsException e) {
Log_OC.e(TAG, "Error while trying to get authorization", e);
} catch (IOException e) {
Log_OC.e(TAG, "Error while trying to get authorization", e);
} finally {
mPendingOperations.removePayload(account.name, remotePath);
mService.dispatchResultToOperationListeners(mCurrentSyncOperation, result);
sendBroadcastFinishedSyncFolder(account, remotePath, result.isSuccess());
}
}
}
use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.
the class AvailableOfflineObserver method startSyncOperation.
/**
* Triggers an operation to synchronize the contents of a file inside the observed folder with
* its remote counterpart in the associated ownCloud account.
*
* @param fileName Name of a file inside the watched folder.
*/
private void startSyncOperation(String fileName) {
FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, mContext.getContentResolver());
// a fresh object is needed; many things could have occurred to the file
// since it was registered to observe again, assuming that local files
// are linked to a remote file AT MOST, SOMETHING TO BE DONE;
OCFile file = storageManager.getFileByLocalPath(mPath + File.separator + fileName);
if (file == null) {
Log_OC.w(TAG, "Could not find OC file for observed " + mPath + File.separator + fileName);
} else {
SynchronizeFileOperation sfo = new SynchronizeFileOperation(file, null, mAccount, false, mContext);
RemoteOperationResult result = sfo.execute(storageManager, mContext);
if (result.getCode() == ResultCode.SYNC_CONFLICT) {
// ISSUE 5: if the user is not running the app (this is a service!),
// this can be very intrusive; a notification should be preferred
Intent i = new Intent(mContext, ConflictsResolveActivity.class);
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);
mContext.startActivity(i);
}
}
}
use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.
the class FileSyncAdapter method synchronizeFolder.
/**
* Synchronizes the list of files contained in a folder identified with its remote path.
*
* Fetches the list and properties of the files contained in the given folder, including their
* properties, and updates the local database with them.
*
* Enters in the child folders to synchronize their contents also, following a recursive
* depth first strategy.
*
* @param folder Folder to synchronize.
* @param pushOnly When 'true', it's assumed that the folder did not change in the
* server, so data will not be fetched. Only local changes of
* available offline files will be pushed.
*/
private void synchronizeFolder(OCFile folder, boolean pushOnly) {
if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
return;
// folder synchronization
SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation(getContext(), folder.getRemotePath(), getAccount(), mCurrentSyncTime, pushOnly, // sync full account
true, // only sync contents of available offline files
false);
RemoteOperationResult result = synchFolderOp.execute(getClient(), getStorageManager());
// synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result);
// check the result of synchronizing the folder
if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
if (result.getCode() == ResultCode.SYNC_CONFLICT) {
mConflictsFound += synchFolderOp.getConflictsFound();
mFailsInFavouritesFound += synchFolderOp.getFailsInFileSyncsFound();
}
if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
}
if (result.isSuccess()) {
// synchronize children folders
List<Pair<OCFile, Boolean>> children = synchFolderOp.getFoldersToVisit();
// beware of the 'hidden' recursion here!
syncSubfolders(children);
}
} else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
// in failures, the statistics for the global result are updated
if (RemoteOperationResult.ResultCode.UNAUTHORIZED.equals(result.getCode())) {
mSyncResult.stats.numAuthExceptions++;
} else if (result.getException() instanceof DavException) {
mSyncResult.stats.numParseExceptions++;
} else if (result.getException() instanceof IOException) {
mSyncResult.stats.numIoExceptions++;
}
mFailedResultsCounter++;
mLastFailedResult = result;
}
// else, ResultCode.FILE_NOT_FOUND is ignored, remote folder was
// removed from other thread or other client during the synchronization,
// before this thread fetched its contents
}
Aggregations