-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/trubitpro/uploadapkplugin/help/ProgressListener.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,5 @@ | ||
package com.trubitpro.uploadapkplugin.help; | ||
|
||
public interface ProgressListener { | ||
void onProgress(long currentBytes, long totalBytes); | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/trubitpro/uploadapkplugin/help/ProgressRequestBody.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,54 @@ | ||
package com.trubitpro.uploadapkplugin.help; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.Buffer; | ||
import okio.BufferedSink; | ||
import okio.ForwardingSink; | ||
import okio.Okio; | ||
|
||
public class ProgressRequestBody extends RequestBody { | ||
private RequestBody requestBody; | ||
private ProgressListener progressListener; | ||
|
||
public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) { | ||
this.requestBody = requestBody; | ||
this.progressListener = progressListener; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public MediaType contentType() { | ||
return requestBody.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() throws IOException { | ||
return requestBody.contentLength(); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
BufferedSink bufferedSink = Okio.buffer(new ForwardingSink(sink) { | ||
private long bytesWritten = 0L; | ||
private long contentLength = 0L; | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) throws IOException { | ||
super.write(source, byteCount); | ||
if (contentLength == 0) { | ||
contentLength = contentLength(); | ||
} | ||
bytesWritten += byteCount; | ||
progressListener.onProgress(bytesWritten, contentLength); | ||
} | ||
}); | ||
|
||
requestBody.writeTo(bufferedSink); | ||
bufferedSink.flush(); | ||
} | ||
} |
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