-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(android): progress support for uploading
- Loading branch information
1 parent
e776b25
commit 67817ac
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
plugin/platforms/android/java/com/nativescript/https/ProgressRequestWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.nativescript.https; | ||
|
||
import okhttp3.RequestBody; | ||
import okhttp3.MediaType; | ||
|
||
import java.io.IOException; | ||
|
||
import okio.Buffer; | ||
import okio.BufferedSink; | ||
import okio.ForwardingSink; | ||
import okio.Okio; | ||
import okio.Sink; | ||
|
||
public class ProgressRequestWrapper extends RequestBody { | ||
|
||
protected RequestBody delegate; | ||
protected ProgressListener listener; | ||
|
||
protected CountingSink countingSink; | ||
|
||
public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) { | ||
this.delegate = delegate; | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return delegate.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() throws IOException { | ||
return delegate.contentLength(); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
|
||
BufferedSink bufferedSink; | ||
|
||
countingSink = new CountingSink(sink); | ||
bufferedSink = Okio.buffer(countingSink); | ||
|
||
delegate.writeTo(bufferedSink); | ||
|
||
bufferedSink.flush(); | ||
} | ||
|
||
protected final class CountingSink extends ForwardingSink { | ||
|
||
private long bytesWritten = 0; | ||
|
||
public CountingSink(Sink delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) throws IOException { | ||
|
||
super.write(source, byteCount); | ||
|
||
bytesWritten += byteCount; | ||
listener.onRequestProgress(bytesWritten, contentLength()); | ||
} | ||
|
||
} | ||
|
||
public interface ProgressListener { | ||
|
||
void onRequestProgress(long bytesWritten, long contentLength); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters