diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 9bc6a9661cee104d0fcbbacfafa3dade3ac91c8f..f2549c6da683cf2bfd287d4bbe1b2bd425397bc7 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -12,7 +12,9 @@ package foundation.e.drive.operations; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.FILE_NOT_FOUND; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.FORBIDDEN; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.INVALID_OVERWRITE; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; +import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.WRONG_CONNECTION; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; @@ -93,6 +95,9 @@ public class DownloadFileOperation extends RemoteOperation { mustRestart = false; } + } else if (isNetworkDisconnected(downloadResult)) { + mustRestart = false; + resultCode = downloadResult.getCode(); } else { Timber.d("Download failed: %s, %s", downloadResult.getCode(), downloadResult.getLogMessage()); resultCode = RemoteOperationResult.ResultCode.UNKNOWN_ERROR; @@ -116,6 +121,10 @@ public class DownloadFileOperation extends RemoteOperation { return new RemoteOperationResult(resultCode); } + private boolean isNetworkDisconnected(@NonNull final RemoteOperationResult result) { + RemoteOperationResult.ResultCode resultCode = result.getCode(); + return resultCode == NO_NETWORK_CONNECTION || resultCode == WRONG_CONNECTION; + } private RemoteOperationResult.ResultCode onDownloadSuccess(String tmpTargetFolderPath) { final String tmpFilePath = tmpTargetFolderPath + remoteFile.getRemotePath(); diff --git a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java index 321df21a1afc1994eec641939d0cd671ab5eaaee..35484f2fec8befb4903dc35660eb229af26d3300 100644 --- a/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/UploadFileOperation.java @@ -31,6 +31,8 @@ import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import java.io.File; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFileState; @@ -43,8 +45,10 @@ import timber.log.Timber; * High level Operation which upload a local file to a remote cloud storage */ public class UploadFileOperation extends RemoteOperation { - private final static String TAG = UploadFileOperation.class.getSimpleName(); public final static int FILE_SIZE_FLOOR_FOR_CHUNKED = 3072000; //3MB + + private static final Set handledFailureCodes = getHandledFailureCodes(); + private final Context context; private final SyncedFileState syncedState; private final Account account; // TODO Remove as soon as nextcloud library move all Operation to NextcloudClient instead of OwncloudClient @@ -109,14 +113,24 @@ public class UploadFileOperation extends RemoteOperation { * @return */ private ResultCode onUploadFailure(final ResultCode uploadResult, final String fileName) { - if (uploadResult != ResultCode.CONFLICT - && uploadResult != ResultCode.QUOTA_EXCEEDED) { + if (!handledFailureCodes.contains(uploadResult)) { Timber.d("Upload for %s failed : %s", fileName, uploadResult); return ResultCode.UNKNOWN_ERROR; } return uploadResult; } + @NonNull + private static Set getHandledFailureCodes() { + final Set handledResultCodes = new HashSet<>(); + handledResultCodes.add(ResultCode.CONFLICT); + handledResultCodes.add(ResultCode.QUOTA_EXCEEDED); + handledResultCodes.add(ResultCode.WRONG_CONNECTION); + handledResultCodes.add(ResultCode.NO_NETWORK_CONNECTION); + + return handledResultCodes; + } + /** * Check condition required to upload the file: * - the local file exist 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 8218f5b26fe6f95c1c988a668f9495ce120e10a4..1c2bc934fada761d5c1554169a7469a6b1c363e5 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -224,7 +224,12 @@ public class SynchronizationService extends Service implements OnRemoteOperation } } - private void startWorker(Integer threadIndex){ + private void startWorker(int threadIndex) { + if (!isNetworkAvailable()) { + syncRequestQueue.clear(); + return; + } + if (!canStart(threadIndex) || isPaused()) return; final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty @@ -267,16 +272,21 @@ public class SynchronizationService extends Service implements OnRemoteOperation * @return false if nogo */ private boolean canStart(int threadIndex) { - final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); final SyncWrapper syncWrapper = startedSync.get(threadIndex); + return (syncWrapper == null || !syncWrapper.isRunning()); + } - return (syncWrapper == null || !syncWrapper.isRunning()) - && CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed); + private boolean isNetworkAvailable() { + final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); + return CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed); } @Override public void onRemoteOperationFinish(@NonNull RemoteOperation callerOperation, @NonNull RemoteOperationResult result) { Timber.i("onRemoteOperationFinish()"); + + boolean isNetworkDisconnected = false; + if (callerOperation instanceof RemoveFileOperation && result.isSuccess()) { DbHelper .manageSyncedFileStateDB( @@ -326,6 +336,11 @@ public class SynchronizationService extends Service implements OnRemoteOperation //Case specific to DownloadFileOperation Timber.d("%s : Sync_conflict: File is already up to date", operationClassName); break; + case NO_NETWORK_CONNECTION: + case WRONG_CONNECTION: + isNetworkDisconnected = true; + Timber.d("%s : network issue: %s", operationClassName, result.getCode()); + break; } } @@ -339,6 +354,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation break; } } + + if (isNetworkDisconnected) { + syncRequestQueue.clear(); + } } private void updateFailureCounter(SyncRequest request, boolean success) {