mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-30 18:07:38 +00:00 
			
		
		
		
	Abort upload [WIP]
This commit is contained in:
		
							parent
							
								
									f110992489
								
							
						
					
					
						commit
						e261b1df2f
					
				| @ -1 +1 @@ | |||||||
| Subproject commit aec2f98c69e4e86d148226c7ff00fe5dd2be49e4 | Subproject commit fab24c4727d4a201cbb0bb1e7f2f45bc5b5e0756 | ||||||
| @ -30,6 +30,7 @@ import java.io.IOException; | |||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
|  | import okhttp3.Call; | ||||||
| import okhttp3.Headers; | import okhttp3.Headers; | ||||||
| import okhttp3.HttpUrl; | import okhttp3.HttpUrl; | ||||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||||
| @ -50,6 +51,7 @@ public abstract class HttpBaseMethod { | |||||||
|     protected Request mRequest; |     protected Request mRequest; | ||||||
|     protected RequestBody mRequestBody; |     protected RequestBody mRequestBody; | ||||||
|     protected Response mResponse; |     protected Response mResponse; | ||||||
|  |     protected Call mCall; | ||||||
| 
 | 
 | ||||||
|     protected HttpBaseMethod (HttpUrl httpUrl) { |     protected HttpBaseMethod (HttpUrl httpUrl) { | ||||||
|         mOkHttpClient = HttpClient.getOkHttpClient(); |         mOkHttpClient = HttpClient.getOkHttpClient(); | ||||||
| @ -125,4 +127,12 @@ public abstract class HttpBaseMethod { | |||||||
|     public String getResponseHeader(String headerName) { |     public String getResponseHeader(String headerName) { | ||||||
|         return mResponse.header(headerName); |         return mResponse.header(headerName); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public void abort() { | ||||||
|  |         mCall.cancel(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isAborted() { | ||||||
|  |         return mCall.isCanceled(); | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -46,5 +46,4 @@ public class GetMethod extends HttpMethod { | |||||||
| 
 | 
 | ||||||
|         return super.execute(); |         return super.execute(); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -38,8 +38,6 @@ import okhttp3.HttpUrl; | |||||||
|  */ |  */ | ||||||
| public abstract class HttpMethod extends HttpBaseMethod { | public abstract class HttpMethod extends HttpBaseMethod { | ||||||
| 
 | 
 | ||||||
|     private Call mCall; |  | ||||||
| 
 |  | ||||||
|     public HttpMethod(HttpUrl httpUrl) { |     public HttpMethod(HttpUrl httpUrl) { | ||||||
|         super(httpUrl); |         super(httpUrl); | ||||||
|     } |     } | ||||||
| @ -50,8 +48,4 @@ public abstract class HttpMethod extends HttpBaseMethod { | |||||||
|         mResponse = mCall.execute(); |         mResponse = mCall.execute(); | ||||||
|         return super.getStatusCode(); |         return super.getStatusCode(); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     public void abort() { |  | ||||||
|         mCall.cancel(); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @ -49,4 +49,14 @@ public abstract class DavMethod extends HttpBaseMethod { | |||||||
|     public DavResource getDavResource() { |     public DavResource getDavResource() { | ||||||
|         return mDavResource; |         return mDavResource; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void abort() { | ||||||
|  |         mDavResource.cancelCall(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public boolean isAborted() { | ||||||
|  |         return mDavResource.isCallAborted(); | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -67,4 +67,4 @@ public class PutMethod extends DavMethod { | |||||||
| 
 | 
 | ||||||
|         return super.getStatusCode(); |         return super.getStatusCode(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -24,16 +24,6 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.resources.files; | package com.owncloud.android.lib.resources.files; | ||||||
| 
 | 
 | ||||||
| import java.io.BufferedInputStream; |  | ||||||
| import java.io.File; |  | ||||||
| import java.io.FileOutputStream; |  | ||||||
| import java.io.IOException; |  | ||||||
| import java.util.Date; |  | ||||||
| import java.util.HashSet; |  | ||||||
| import java.util.Iterator; |  | ||||||
| import java.util.Set; |  | ||||||
| import java.util.concurrent.atomic.AtomicBoolean; |  | ||||||
| 
 |  | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod; | import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod; | ||||||
| @ -44,6 +34,16 @@ import com.owncloud.android.lib.common.operations.RemoteOperation; | |||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| 
 | 
 | ||||||
|  | import java.io.BufferedInputStream; | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.FileOutputStream; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.util.Date; | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.Iterator; | ||||||
|  | import java.util.Set; | ||||||
|  | import java.util.concurrent.atomic.AtomicBoolean; | ||||||
|  | 
 | ||||||
| import okhttp3.HttpUrl; | import okhttp3.HttpUrl; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -98,7 +98,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     private RemoteOperationResult downloadFile(OwnCloudClient client, File targetFile) throws |     private RemoteOperationResult downloadFile(OwnCloudClient client, File targetFile) throws | ||||||
|         IOException, OperationCancelledException { |             Exception { | ||||||
| 
 | 
 | ||||||
|         RemoteOperationResult result; |         RemoteOperationResult result; | ||||||
|         int status; |         int status; | ||||||
| @ -128,7 +128,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { | |||||||
|                 while ((readResult = bis.read(bytes)) != -1) { |                 while ((readResult = bis.read(bytes)) != -1) { | ||||||
|                     synchronized (mCancellationRequested) { |                     synchronized (mCancellationRequested) { | ||||||
|                         if (mCancellationRequested.get()) { |                         if (mCancellationRequested.get()) { | ||||||
|                              mGet.abort(); |                             mGet.abort(); | ||||||
|                             throw new OperationCancelledException(); |                             throw new OperationCancelledException(); | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
| @ -174,9 +174,6 @@ public class DownloadRemoteFileOperation extends RemoteOperation { | |||||||
|             result = isSuccess(status) |             result = isSuccess(status) | ||||||
|                     ? new RemoteOperationResult(RemoteOperationResult.ResultCode.OK) |                     ? new RemoteOperationResult(RemoteOperationResult.ResultCode.OK) | ||||||
|                     : new RemoteOperationResult(mGet); |                     : new RemoteOperationResult(mGet); | ||||||
| 
 |  | ||||||
|         } catch (Exception e) { |  | ||||||
|             return new RemoteOperationResult(e); |  | ||||||
|         } finally { |         } finally { | ||||||
|             if (fos != null) fos.close(); |             if (fos != null) fos.close(); | ||||||
|             if (bis != null) bis.close(); |             if (bis != null) bis.close(); | ||||||
| @ -218,4 +215,4 @@ public class DownloadRemoteFileOperation extends RemoteOperation { | |||||||
|     public String getEtag() { |     public String getEtag() { | ||||||
|         return mEtag; |         return mEtag; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -113,13 +113,13 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
| //            if (mPutMethod != null && mPutMethod.isAborted()) { |             if (mPutMethod != null && mPutMethod.isAborted()) { | ||||||
| //                result = new RemoteOperationResult(new OperationCancelledException()); |                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||||
| // |             } else { | ||||||
| //            } else { |                 result = new RemoteOperationResult(e); | ||||||
| //                result = new RemoteOperationResult(e); |             } | ||||||
| //            } |  | ||||||
|         } finally { |         } finally { | ||||||
|  |             // TODO | ||||||
|             // reset previous retry handler |             // reset previous retry handler | ||||||
| //            client.getParams().setParameter( | //            client.getParams().setParameter( | ||||||
| //                HttpMethodParams.RETRY_HANDLER, | //                HttpMethodParams.RETRY_HANDLER, | ||||||
| @ -129,47 +129,41 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|         return result; |         return result; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { |     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws Exception { | ||||||
|         RemoteOperationResult result; |  | ||||||
|         try { |  | ||||||
|             File fileToUpload = new File(mLocalPath); |  | ||||||
| 
 | 
 | ||||||
|             MediaType mediaType = MediaType.parse(mMimeType); |         File fileToUpload = new File(mLocalPath); | ||||||
| 
 | 
 | ||||||
|             mFileRequestBody = new FileRequestBody(fileToUpload, mediaType); |         MediaType mediaType = MediaType.parse(mMimeType); | ||||||
| 
 | 
 | ||||||
|             synchronized (mDataTransferListeners) { |         mFileRequestBody = new FileRequestBody(fileToUpload, mediaType); | ||||||
|                 mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |         synchronized (mDataTransferListeners) { | ||||||
|                 mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); |             mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); | ||||||
|             } |         } | ||||||
| 
 | 
 | ||||||
|             mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); |         if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||||
| 
 |             mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||||
|             mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); |         } | ||||||
| 
 | 
 | ||||||
|             RequestBody requestBody = new MultipartBody.Builder() |         mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); | ||||||
|                     .setType(MultipartBody.FORM) | 
 | ||||||
|                     .addPart(mFileRequestBody) |         mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
|                     .build(); | 
 | ||||||
| 
 |         RequestBody requestBody = new MultipartBody.Builder() | ||||||
|             mPutMethod.setRequestBody(requestBody); |                 .setType(MultipartBody.FORM) | ||||||
| 
 |                 .addPart(mFileRequestBody) | ||||||
|             int status = client.executeHttpMethod(mPutMethod); |                 .build(); | ||||||
| 
 | 
 | ||||||
|             if (isSuccess(status)) { |         mPutMethod.setRequestBody(requestBody); | ||||||
|                 result = new RemoteOperationResult(OK); | 
 | ||||||
| 
 |         int status = client.executeHttpMethod(mPutMethod); | ||||||
|             } else { // synchronization failed | 
 | ||||||
|                 result = new RemoteOperationResult(mPutMethod); |         if (isSuccess(status)) { | ||||||
|             } |             return new RemoteOperationResult(OK); | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |         } else { // synchronization failed | ||||||
|             result = new RemoteOperationResult(e); |             return new RemoteOperationResult(mPutMethod); | ||||||
|         } |         } | ||||||
|         return result; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Set<OnDatatransferProgressListener> getDataTransferListeners() { |     public Set<OnDatatransferProgressListener> getDataTransferListeners() { | ||||||
| @ -195,11 +189,11 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|     } |     } | ||||||
|      |      | ||||||
|     public void cancel() { |     public void cancel() { | ||||||
| //        synchronized (mCancellationRequested) { |         synchronized (mCancellationRequested) { | ||||||
| //            mCancellationRequested.set(true); |             mCancellationRequested.set(true); | ||||||
| //            if (mPutMethod != null) |             if (mPutMethod != null) | ||||||
| //                mPutMethod.abort(); |                 mPutMethod.abort(); | ||||||
| //        } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public boolean isSuccess(int status) { |     public boolean isSuccess(int status) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user