mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Implement upload progress
This commit is contained in:
		
							parent
							
								
									9caa79becc
								
							
						
					
					
						commit
						b1f2c0cbef
					
				| @ -1,26 +1,55 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  *   Copyright (C) 2018 ownCloud GmbH. | ||||||
|  |  * | ||||||
|  |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  *   of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  *   in the Software without restriction, including without limitation the rights | ||||||
|  |  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  *   copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  *   furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  *   The above copyright notice and this permission notice shall be included in | ||||||
|  |  *   all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  |  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  |  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||
|  |  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||||
|  |  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||||
|  |  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||||
|  |  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  *   THE SOFTWARE. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
| package com.owncloud.android.lib.common.network; | package com.owncloud.android.lib.common.network; | ||||||
| 
 | 
 | ||||||
| import android.util.Log; |  | ||||||
| 
 |  | ||||||
| import java.io.File; | import java.io.File; | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.HashSet; | ||||||
|  | import java.util.Iterator; | ||||||
|  | import java.util.Set; | ||||||
| 
 | 
 | ||||||
| import okhttp3.MediaType; | import okhttp3.MediaType; | ||||||
| import okhttp3.RequestBody; | import okhttp3.RequestBody; | ||||||
| import okio.Buffer; |  | ||||||
| import okio.BufferedSink; | import okio.BufferedSink; | ||||||
| import okio.Okio; | import okio.Okio; | ||||||
| import okio.Source; | import okio.Source; | ||||||
| 
 | 
 | ||||||
| import static android.content.ContentValues.TAG; | /** | ||||||
|  |  * A Request body that represents a file and include information about the progress when uploading it | ||||||
|  |  * | ||||||
|  |  * @author David González Verdugo | ||||||
|  |  */ | ||||||
|  | public class FileRequestBody extends RequestBody implements ProgressiveDataTransferer { | ||||||
| 
 | 
 | ||||||
| public class FileRequestBody extends RequestBody { |     private File mFile; | ||||||
|  |     private MediaType mContentType; | ||||||
|  |     Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>(); | ||||||
| 
 | 
 | ||||||
|     MediaType mContentType; |     public FileRequestBody(File file, MediaType contentType) { | ||||||
|     File mFile; |  | ||||||
| 
 |  | ||||||
|     FileRequestBody(MediaType contentType, File file) { |  | ||||||
|         mContentType = contentType; |  | ||||||
|         mFile = file; |         mFile = file; | ||||||
|  |         mContentType = contentType; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -31,18 +60,46 @@ public class FileRequestBody extends RequestBody { | |||||||
|     @Override |     @Override | ||||||
|     public void writeTo(BufferedSink sink) { |     public void writeTo(BufferedSink sink) { | ||||||
|         Source source; |         Source source; | ||||||
|  |         Iterator<OnDatatransferProgressListener> it; | ||||||
|         try { |         try { | ||||||
|             source = Okio.source(mFile); |             source = Okio.source(mFile); | ||||||
|             //sink.writeAll(source); |             long transferred = 0; | ||||||
|             Buffer buffer = new Buffer(); |             long read; | ||||||
|             Long remaining = contentLength(); |  | ||||||
| 
 | 
 | ||||||
|             for (long readCount; (readCount = source.read(buffer, 2048)) != -1;) { |             while ((read = source.read(sink.buffer(), 2048)) != -1) { | ||||||
|                 sink.write(buffer, readCount); |                 transferred += read; | ||||||
|                 Log.d(TAG, "source size: " + contentLength() + " remaining bytes: " + (remaining -= readCount)); |                 sink.flush(); | ||||||
|  |                 synchronized (mDataTransferListeners) { | ||||||
|  |                     it = mDataTransferListeners.iterator(); | ||||||
|  |                     while (it.hasNext()) { | ||||||
|  |                         it.next().onTransferProgress(read, transferred, mFile.length(), mFile.getAbsolutePath()); | ||||||
|                     } |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void addDatatransferProgressListener(OnDatatransferProgressListener listener) { | ||||||
|  |         synchronized (mDataTransferListeners) { | ||||||
|  |             mDataTransferListeners.add(listener); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) { | ||||||
|  |         synchronized (mDataTransferListeners) { | ||||||
|  |             mDataTransferListeners.addAll(listeners); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) { | ||||||
|  |         synchronized (mDataTransferListeners) { | ||||||
|  |             mDataTransferListeners.remove(listener); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -25,6 +25,8 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.common.network; | package com.owncloud.android.lib.common.network; | ||||||
| 
 | 
 | ||||||
|  | import org.apache.commons.httpclient.methods.RequestEntity; | ||||||
|  | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| @ -37,10 +39,6 @@ import java.util.HashSet; | |||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
| import java.util.Set; | import java.util.Set; | ||||||
| 
 | 
 | ||||||
| import org.apache.commons.httpclient.methods.RequestEntity; |  | ||||||
| 
 |  | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -51,7 +49,7 @@ public class FileRequestEntity implements RequestEntity, ProgressiveDataTransfer | |||||||
| 
 | 
 | ||||||
|     final File mFile; |     final File mFile; | ||||||
|     final String mContentType; |     final String mContentType; | ||||||
|     Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); |     Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>(); | ||||||
| 
 | 
 | ||||||
|     public FileRequestEntity(final File file, final String contentType) { |     public FileRequestEntity(final File file, final String contentType) { | ||||||
|         super(); |         super(); | ||||||
| @ -162,5 +160,4 @@ public class FileRequestEntity implements RequestEntity, ProgressiveDataTransfer | |||||||
|             return mWrapped; |             return mWrapped; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -26,5 +26,5 @@ | |||||||
| package com.owncloud.android.lib.common.network; | package com.owncloud.android.lib.common.network; | ||||||
| 
 | 
 | ||||||
| public interface OnDatatransferProgressListener { | public interface OnDatatransferProgressListener { | ||||||
|     public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileAbsoluteName); |     void onTransferProgress(long read, long transferred, long percent, String absolutePath); | ||||||
| } | } | ||||||
| @ -27,7 +27,6 @@ package com.owncloud.android.lib.common.network; | |||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| public interface ProgressiveDataTransferer { | public interface ProgressiveDataTransferer { | ||||||
| 
 | 
 | ||||||
|     public void addDatatransferProgressListener (OnDatatransferProgressListener listener); |     public void addDatatransferProgressListener (OnDatatransferProgressListener listener); | ||||||
|  | |||||||
| @ -27,20 +27,13 @@ package com.owncloud.android.lib.resources.files; | |||||||
| 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.HttpUtils; | import com.owncloud.android.lib.common.http.HttpUtils; | ||||||
|  | import com.owncloud.android.lib.common.http.methods.webdav.PutMethod; | ||||||
| import com.owncloud.android.lib.common.network.FileRequestBody; | import com.owncloud.android.lib.common.network.FileRequestBody; | ||||||
| import com.owncloud.android.lib.common.network.FileRequestEntity; |  | ||||||
| import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; | import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; | ||||||
| import com.owncloud.android.lib.common.network.ProgressiveDataTransferer; |  | ||||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | import com.owncloud.android.lib.common.network.WebdavUtils; | ||||||
| import com.owncloud.android.lib.common.operations.OperationCancelledException; | import com.owncloud.android.lib.common.operations.OperationCancelledException; | ||||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | 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.http.methods.webdav.PutMethod; |  | ||||||
| 
 |  | ||||||
| import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; |  | ||||||
| import org.apache.commons.httpclient.HttpStatus; |  | ||||||
| import org.apache.commons.httpclient.methods.RequestEntity; |  | ||||||
| import org.apache.commons.httpclient.params.HttpMethodParams; |  | ||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| @ -49,6 +42,7 @@ import java.util.Set; | |||||||
| import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | ||||||
| 
 | 
 | ||||||
| import okhttp3.MediaType; | import okhttp3.MediaType; | ||||||
|  | import okhttp3.MultipartBody; | ||||||
| import okhttp3.RequestBody; | import okhttp3.RequestBody; | ||||||
| 
 | 
 | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
| @ -75,7 +69,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|     protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); |     protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); | ||||||
|     protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); |     protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); | ||||||
| 
 | 
 | ||||||
|     protected RequestEntity mEntity = null; |     protected FileRequestBody mFileRequestBody = null; | ||||||
| 
 | 
 | ||||||
|     public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, |     public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType, | ||||||
|                                      String fileLastModifTimestamp) { |                                      String fileLastModifTimestamp) { | ||||||
| @ -138,13 +132,15 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { |     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { | ||||||
|         RemoteOperationResult result; |         RemoteOperationResult result; | ||||||
|         try { |         try { | ||||||
|             // Headers |  | ||||||
|             File fileToUpload = new File(mLocalPath); |             File fileToUpload = new File(mLocalPath); | ||||||
|             // TODO | 
 | ||||||
| //            synchronized (mDataTransferListeners) { |             MediaType mediaType = MediaType.parse(mMimeType); | ||||||
| //                ((ProgressiveDataTransferer)mEntity) | 
 | ||||||
| //                        .addDatatransferProgressListeners(mDataTransferListeners); |             mFileRequestBody = new FileRequestBody(fileToUpload, mediaType); | ||||||
| //            } | 
 | ||||||
|  |             synchronized (mDataTransferListeners) { | ||||||
|  |                 mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||||
|                 mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); |                 mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||||
| @ -154,12 +150,12 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|             mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); |             mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
| 
 | 
 | ||||||
|             // Request body |             RequestBody requestBody = new MultipartBody.Builder() | ||||||
|             MediaType mediaType = MediaType.parse(mMimeType); |                     .setType(MultipartBody.FORM) | ||||||
|  |                     .addPart(mFileRequestBody) | ||||||
|  |                     .build(); | ||||||
| 
 | 
 | ||||||
|             mPutMethod.setRequestBody( |             mPutMethod.setRequestBody(requestBody); | ||||||
|                     FileRequestBody.create(mediaType, fileToUpload) |  | ||||||
|             ); |  | ||||||
| 
 | 
 | ||||||
|             int status = client.executeHttpMethod(mPutMethod); |             int status = client.executeHttpMethod(mPutMethod); | ||||||
| 
 | 
 | ||||||
| @ -170,8 +166,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|                 result = new RemoteOperationResult(mPutMethod); |                 result = new RemoteOperationResult(mPutMethod); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             client.exhaustResponse(mPutMethod.getResponseAsStream()); |  | ||||||
| 
 |  | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
|         } |         } | ||||||
| @ -186,8 +180,8 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|         synchronized (mDataTransferListeners) { |         synchronized (mDataTransferListeners) { | ||||||
|             mDataTransferListeners.add(listener); |             mDataTransferListeners.add(listener); | ||||||
|         } |         } | ||||||
|         if (mEntity != null) { |         if (mFileRequestBody != null) { | ||||||
|             ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListener(listener); |             mFileRequestBody.addDatatransferProgressListener(listener); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
| @ -195,8 +189,8 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|         synchronized (mDataTransferListeners) { |         synchronized (mDataTransferListeners) { | ||||||
|             mDataTransferListeners.remove(listener); |             mDataTransferListeners.remove(listener); | ||||||
|         } |         } | ||||||
|         if (mEntity != null) { |         if (mFileRequestBody != null) { | ||||||
|             ((ProgressiveDataTransferer)mEntity).removeDatatransferProgressListener(listener); |             mFileRequestBody.removeDatatransferProgressListener(listener); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user