1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

Simplified flow of interruption of uploads in cancellations

This commit is contained in:
David A. Velasco 2015-07-22 10:40:50 +02:00
parent 8bf276377c
commit 522ea30da0
2 changed files with 21 additions and 17 deletions

View File

@ -32,7 +32,6 @@ import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Random;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PutMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
@ -85,6 +84,10 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(file.length()));
((ChunkFromFileChannelRequestEntity) mEntity).setOffset(offset);
mPutMethod.setRequestEntity(mEntity);
if (mCancellationRequested.get()) {
mPutMethod.abort();
// next method will throw an exception
}
status = client.executeMethod(mPutMethod);
if (status == 400) {

View File

@ -67,7 +67,7 @@ public class UploadRemoteFileOperation extends RemoteOperation {
protected PutMethod mPutMethod = null;
protected boolean mForbiddenCharsInServer = false;
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
protected RequestEntity mEntity = null;
@ -83,16 +83,14 @@ public class UploadRemoteFileOperation extends RemoteOperation {
RemoteOperationResult result = null;
try {
// / perform the upload
synchronized (mCancellationRequested) {
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
} else {
mPutMethod = new PutMethod(client.getWebdavUri() +
WebdavUtils.encodePath(mRemotePath));
}
}
mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
if (mCancellationRequested.get()) {
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
result = new RemoteOperationResult(new OperationCancelledException());
} else {
// perform the upload
int status = uploadFile(client);
if (mForbiddenCharsInServer){
result = new RemoteOperationResult(
@ -101,9 +99,12 @@ public class UploadRemoteFileOperation extends RemoteOperation {
result = new RemoteOperationResult(isSuccess(status), status,
(mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
}
}
} catch (Exception e) {
if (mCancellationRequested.get() && !(e instanceof OperationCancelledException)) {
if (mPutMethod != null && mPutMethod.isAborted()) {
result = new RemoteOperationResult(new OperationCancelledException());
} else {
result = new RemoteOperationResult(e);
}