diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index ed0f2ab9ae1199e1d0ab5c8f22e2ff99eb1fb940..6abbf1f310fc6d8f8da2df1a8d0b06e374a17197 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -7,6 +7,9 @@ */ package foundation.e.drive.services; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.FORBIDDEN; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.UNKNOWN_ERROR; import static foundation.e.drive.operations.UploadFileOperation.FILE_SIZE_FLOOR_FOR_CHUNKED; import android.accounts.Account; @@ -37,7 +40,6 @@ import foundation.e.drive.database.DbHelper; import foundation.e.drive.database.FailedSyncPrefsManager; import foundation.e.drive.models.SyncRequest; import foundation.e.drive.models.SyncWrapper; -import foundation.e.drive.operations.DownloadFileOperation; import foundation.e.drive.operations.RemoveFileOperation; import foundation.e.drive.operations.UploadFileOperation; import foundation.e.drive.utils.AppConstants; @@ -198,8 +200,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation } private void startWorker(Integer threadIndex){ + Timber.v("startWorker: remaining request: %s", syncRequestQueue.size()); if (!canStart(threadIndex)) return; + final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null || request.getSyncedFileState() == null @@ -245,72 +249,79 @@ public class SynchronizationService extends Service implements OnRemoteOperation } @Override - public void onRemoteOperationFinish(RemoteOperation callerOperation, RemoteOperationResult result) { - Timber.i("onRemoteOperationFinish()"); - SyncWrapper callerWrapper = null; - for (Map.Entry keyValue : startedSync.entrySet()) { - if (keyValue.getValue().getRemoteOperation().equals(callerOperation)) { - callerWrapper = keyValue.getValue(); - callerWrapper.setRunning(false); - startWorker(keyValue.getKey()); - break; + public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + final boolean succeed = result.isSuccess() || result.getCode() == OK; + final String operationClassName = operation.getClass().getSimpleName(); + + String statusLogMsg = ""; + if (succeed) { + if (operation instanceof RemoveFileOperation) { + DbHelper.manageSyncedFileStateDB( ((RemoveFileOperation) operation).getSyncedFileState(), + "DELETE", this); + } + statusLogMsg = "succeed"; + } else { + statusLogMsg = createRemoteOperationErrorLogMsg(result.getCode()); + if( operation instanceof UploadFileOperation && + (result.getCode().equals(UNKNOWN_ERROR) || result.getCode().equals(FORBIDDEN))) { + final int rowAffected = DbHelper.forceFoldertoBeRescan(((UploadFileOperation) operation).getSyncedState().getId(), getApplicationContext()); + Timber.d("Force folder to be rescan next time (row affected) : %s", rowAffected); } } - if (callerWrapper != null) { - updateFailureCounter(callerWrapper.getRequest(), result.isSuccess()); - } + for (Map.Entry keyValue : startedSync.entrySet()) { + if (keyValue.getValue().getRemoteOperation().equals(operation)) { + final SyncWrapper syncWrapper = keyValue.getValue(); + if (syncWrapper == null) return; - if (callerOperation instanceof RemoveFileOperation) { - if ( result.isSuccess() ) { - DbHelper.manageSyncedFileStateDB( ( ( RemoveFileOperation ) callerOperation ).getSyncedFileState(), - "DELETE", this); - } - } else { - final String operationClassName = callerOperation.getClass().getSimpleName(); - switch (result.getCode()) { - case OK: - Timber.d("%s Succeed", operationClassName); - break; - case SYNC_CONFLICT: - //Case specific to UploadFileOperation - Timber.e("%s : Sync_conflict : File is already up to date", operationClassName); - break; - case INVALID_OVERWRITE: - Timber.e("%s => invalid_overwrite :\n remote file and local file doesn't have the same size", operationClassName); - break; - case UNKNOWN_ERROR: - if (callerOperation instanceof UploadFileOperation) { - final int rowAffected = DbHelper.forceFoldertoBeRescan(((UploadFileOperation) callerOperation).getSyncedState().getId(), getApplicationContext()); - Timber.e("Upload failed for unknown reason.\n Force folder to be rescan next time (row affected) : %s", rowAffected); - } else if (callerOperation instanceof DownloadFileOperation) { - Timber.e("Download: Unknown_error : failed"); - } - break; - case FORBIDDEN: - if (callerOperation instanceof UploadFileOperation) { - final int rowAffected = DbHelper.forceFoldertoBeRescan(((UploadFileOperation) callerOperation).getSyncedState().getId(), getApplicationContext()); - Timber.e("Upload: Forbidden : Can't get syncedFileState, no remote path defined. Force folder to be rescan next time (row affected) : %s", rowAffected); - } else if (callerOperation instanceof DownloadFileOperation) { - Timber.e("Download : Forbidden: Can't get syncedFileState, no local path defined"); - } - break; - case QUOTA_EXCEEDED: - //Case specific to UploadFileOperation - Timber.w("Quota_EXCEEDED"); - break; - case FILE_NOT_FOUND: - //Case specific to DownloadFileOperation - Timber.e("%s : File_not_found: File not found after download", operationClassName); - break; - case ETAG_UNCHANGED: - //Case specific to DownloadFileOperation - Timber.e("%s : Sync_conflict: File is already up to date", operationClassName); - break; + updateFailureCounter(syncWrapper.getRequest(), succeed); + syncWrapper.setRunning(false); + + Timber.w("%s of %s %s", operationClassName, + syncWrapper.getRequest().getSyncedFileState().getLocalPath(), + statusLogMsg); + + startWorker(keyValue.getKey()); + return; } } } + private String createRemoteOperationErrorLogMsg(RemoteOperationResult.ResultCode resultCode) { + final String result; + switch (resultCode) { + case SYNC_CONFLICT: + //Case specific to UploadFileOperation + result = "failed: File is already up to date (Sync_conflict)"; + break; + case INVALID_OVERWRITE: + result = "failed: invalid_overwrite, remote and local size are different"; + break; + case UNKNOWN_ERROR: + result = "failed : unknown error"; + break; + case FORBIDDEN: + result = "failed: Forbidden, Can't get syncedFileState, no local path defined"; + break; + case QUOTA_EXCEEDED: + //Case specific to UploadFileOperation + result = "failed: Quota_EXCEEDED"; + break; + case FILE_NOT_FOUND: + //Case specific to DownloadFileOperation + result = "failed: file_not_found after download"; + break; + case ETAG_UNCHANGED: + //Case specific to DownloadFileOperation + result = "failed: Etag unchanged, File is already up to date (Sync_conflict)"; + break; + default: + result = "Invalid result code"; + break; + } + return result; + } + private void updateFailureCounter(SyncRequest request, boolean success) { final FailedSyncPrefsManager failedPref = FailedSyncPrefsManager.getInstance(getApplicationContext()); final int fileStateId = request.getSyncedFileState().getId();